PipelineTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. namespace Illuminate\Tests\Pipeline;
  3. use Illuminate\Container\Container;
  4. use Illuminate\Pipeline\Pipeline;
  5. use PHPUnit\Framework\TestCase;
  6. use RuntimeException;
  7. class PipelineTest extends TestCase
  8. {
  9. public function testPipelineBasicUsage()
  10. {
  11. $pipeTwo = function ($piped, $next) {
  12. $_SERVER['__test.pipe.two'] = $piped;
  13. return $next($piped);
  14. };
  15. $result = (new Pipeline(new Container))
  16. ->send('foo')
  17. ->through([PipelineTestPipeOne::class, $pipeTwo])
  18. ->then(function ($piped) {
  19. return $piped;
  20. });
  21. $this->assertSame('foo', $result);
  22. $this->assertSame('foo', $_SERVER['__test.pipe.one']);
  23. $this->assertSame('foo', $_SERVER['__test.pipe.two']);
  24. unset($_SERVER['__test.pipe.one'], $_SERVER['__test.pipe.two']);
  25. }
  26. public function testPipelineUsageWithObjects()
  27. {
  28. $result = (new Pipeline(new Container))
  29. ->send('foo')
  30. ->through([new PipelineTestPipeOne])
  31. ->then(function ($piped) {
  32. return $piped;
  33. });
  34. $this->assertSame('foo', $result);
  35. $this->assertSame('foo', $_SERVER['__test.pipe.one']);
  36. unset($_SERVER['__test.pipe.one']);
  37. }
  38. public function testPipelineUsageWithInvokableObjects()
  39. {
  40. $result = (new Pipeline(new Container))
  41. ->send('foo')
  42. ->through([new PipelineTestPipeTwo])
  43. ->then(
  44. function ($piped) {
  45. return $piped;
  46. }
  47. );
  48. $this->assertSame('foo', $result);
  49. $this->assertSame('foo', $_SERVER['__test.pipe.one']);
  50. unset($_SERVER['__test.pipe.one']);
  51. }
  52. public function testPipelineUsageWithCallable()
  53. {
  54. $function = function ($piped, $next) {
  55. $_SERVER['__test.pipe.one'] = 'foo';
  56. return $next($piped);
  57. };
  58. $result = (new Pipeline(new Container))
  59. ->send('foo')
  60. ->through([$function])
  61. ->then(
  62. function ($piped) {
  63. return $piped;
  64. }
  65. );
  66. $this->assertSame('foo', $result);
  67. $this->assertSame('foo', $_SERVER['__test.pipe.one']);
  68. unset($_SERVER['__test.pipe.one']);
  69. $result = (new Pipeline(new Container))
  70. ->send('bar')
  71. ->through($function)
  72. ->thenReturn();
  73. $this->assertSame('bar', $result);
  74. $this->assertSame('foo', $_SERVER['__test.pipe.one']);
  75. unset($_SERVER['__test.pipe.one']);
  76. }
  77. public function testPipelineUsageWithInvokableClass()
  78. {
  79. $result = (new Pipeline(new Container))
  80. ->send('foo')
  81. ->through([PipelineTestPipeTwo::class])
  82. ->then(
  83. function ($piped) {
  84. return $piped;
  85. }
  86. );
  87. $this->assertSame('foo', $result);
  88. $this->assertSame('foo', $_SERVER['__test.pipe.one']);
  89. unset($_SERVER['__test.pipe.one']);
  90. }
  91. public function testPipelineUsageWithParameters()
  92. {
  93. $parameters = ['one', 'two'];
  94. $result = (new Pipeline(new Container))
  95. ->send('foo')
  96. ->through(PipelineTestParameterPipe::class.':'.implode(',', $parameters))
  97. ->then(function ($piped) {
  98. return $piped;
  99. });
  100. $this->assertSame('foo', $result);
  101. $this->assertEquals($parameters, $_SERVER['__test.pipe.parameters']);
  102. unset($_SERVER['__test.pipe.parameters']);
  103. }
  104. public function testPipelineViaChangesTheMethodBeingCalledOnThePipes()
  105. {
  106. $pipelineInstance = new Pipeline(new Container);
  107. $result = $pipelineInstance->send('data')
  108. ->through(PipelineTestPipeOne::class)
  109. ->via('differentMethod')
  110. ->then(function ($piped) {
  111. return $piped;
  112. });
  113. $this->assertSame('data', $result);
  114. }
  115. public function testPipelineThrowsExceptionOnResolveWithoutContainer()
  116. {
  117. $this->expectException(RuntimeException::class);
  118. $this->expectExceptionMessage('A container instance has not been passed to the Pipeline.');
  119. (new Pipeline)->send('data')
  120. ->through(PipelineTestPipeOne::class)
  121. ->then(function ($piped) {
  122. return $piped;
  123. });
  124. }
  125. public function testPipelineThenReturnMethodRunsPipelineThenReturnsPassable()
  126. {
  127. $result = (new Pipeline(new Container))
  128. ->send('foo')
  129. ->through([PipelineTestPipeOne::class])
  130. ->thenReturn();
  131. $this->assertSame('foo', $result);
  132. $this->assertSame('foo', $_SERVER['__test.pipe.one']);
  133. unset($_SERVER['__test.pipe.one']);
  134. }
  135. }
  136. class PipelineTestPipeOne
  137. {
  138. public function handle($piped, $next)
  139. {
  140. $_SERVER['__test.pipe.one'] = $piped;
  141. return $next($piped);
  142. }
  143. public function differentMethod($piped, $next)
  144. {
  145. return $next($piped);
  146. }
  147. }
  148. class PipelineTestPipeTwo
  149. {
  150. public function __invoke($piped, $next)
  151. {
  152. $_SERVER['__test.pipe.one'] = $piped;
  153. return $next($piped);
  154. }
  155. }
  156. class PipelineTestParameterPipe
  157. {
  158. public function handle($piped, $next, $parameter1 = null, $parameter2 = null)
  159. {
  160. $_SERVER['__test.pipe.parameters'] = [$parameter1, $parameter2];
  161. return $next($piped);
  162. }
  163. }