PipelineTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. declare(strict_types=1);
  3. namespace Yansongda\Supports\Tests;
  4. use Yansongda\Supports\Pipeline;
  5. use Yansongda\Supports\Tests\Stubs\FooPipeline;
  6. use Mockery;
  7. use PHPUnit\Framework\TestCase;
  8. use Psr\Container\ContainerInterface;
  9. /**
  10. * @internal
  11. * @coversNothing
  12. */
  13. class PipelineTest extends TestCase
  14. {
  15. protected function tearDown(): void
  16. {
  17. Mockery::close();
  18. }
  19. public function testPipelineBasicUsage()
  20. {
  21. $pipeTwo = function ($piped, $next) {
  22. $_SERVER['__test.pipe.two'] = $piped;
  23. return $next($piped);
  24. };
  25. $result = (new Pipeline($this->getContainer()))
  26. ->send('foo')
  27. ->through([PipelineTestPipeOne::class, $pipeTwo])
  28. ->then(function ($piped) {
  29. return $piped;
  30. });
  31. static::assertSame('foo', $result);
  32. static::assertSame('foo', $_SERVER['__test.pipe.one']);
  33. static::assertSame('foo', $_SERVER['__test.pipe.two']);
  34. unset($_SERVER['__test.pipe.one'], $_SERVER['__test.pipe.two']);
  35. }
  36. public function testPipelineUsageWithObjects()
  37. {
  38. $result = (new Pipeline($this->getContainer()))
  39. ->send('foo')
  40. ->through([new PipelineTestPipeOne()])
  41. ->then(function ($piped) {
  42. return $piped;
  43. });
  44. static::assertSame('foo', $result);
  45. static::assertSame('foo', $_SERVER['__test.pipe.one']);
  46. unset($_SERVER['__test.pipe.one']);
  47. }
  48. public function testPipelineUsageWithInvokableObjects()
  49. {
  50. $result = (new Pipeline($this->getContainer()))
  51. ->send('foo')
  52. ->through([new PipelineTestPipeTwo()])
  53. ->then(
  54. function ($piped) {
  55. return $piped;
  56. }
  57. );
  58. static::assertSame('foo', $result);
  59. static::assertSame('foo', $_SERVER['__test.pipe.one']);
  60. unset($_SERVER['__test.pipe.one']);
  61. }
  62. public function testPipelineUsageWithCallable()
  63. {
  64. $function = function ($piped, $next) {
  65. $_SERVER['__test.pipe.one'] = 'foo';
  66. return $next($piped);
  67. };
  68. $result = (new Pipeline($this->getContainer()))
  69. ->send('foo')
  70. ->through([$function])
  71. ->then(
  72. function ($piped) {
  73. return $piped;
  74. }
  75. );
  76. static::assertSame('foo', $result);
  77. static::assertSame('foo', $_SERVER['__test.pipe.one']);
  78. unset($_SERVER['__test.pipe.one']);
  79. $result = (new Pipeline($this->getContainer()))
  80. ->send('bar')
  81. ->through($function)
  82. ->then(static function ($passable) {
  83. return $passable;
  84. });
  85. static::assertSame('bar', $result);
  86. static::assertSame('foo', $_SERVER['__test.pipe.one']);
  87. unset($_SERVER['__test.pipe.one']);
  88. }
  89. public function testPipelineUsageWithInvokableClass()
  90. {
  91. $result = (new Pipeline($this->getContainer()))
  92. ->send('foo')
  93. ->through([PipelineTestPipeTwo::class])
  94. ->then(
  95. function ($piped) {
  96. return $piped;
  97. }
  98. );
  99. static::assertSame('foo', $result);
  100. static::assertSame('foo', $_SERVER['__test.pipe.one']);
  101. unset($_SERVER['__test.pipe.one']);
  102. }
  103. public function testPipelineUsageWithParameters()
  104. {
  105. $parameters = ['one', 'two'];
  106. $result = (new Pipeline($this->getContainer()))
  107. ->send('foo')
  108. ->through(PipelineTestParameterPipe::class . ':' . implode(',', $parameters))
  109. ->then(function ($piped) {
  110. return $piped;
  111. });
  112. static::assertSame('foo', $result);
  113. static::assertEquals($parameters, $_SERVER['__test.pipe.parameters']);
  114. unset($_SERVER['__test.pipe.parameters']);
  115. }
  116. public function testPipelineViaChangesTheMethodBeingCalledOnThePipes()
  117. {
  118. $pipelineInstance = new Pipeline($this->getContainer());
  119. $result = $pipelineInstance->send('data')
  120. ->through(PipelineTestPipeOne::class)
  121. ->via('differentMethod')
  122. ->then(function ($piped) {
  123. return $piped;
  124. });
  125. static::assertSame('data', $result);
  126. }
  127. public function testPipelineThenReturnMethodRunsPipelineThenReturnsPassable()
  128. {
  129. $result = (new Pipeline($this->getContainer()))
  130. ->send('foo')
  131. ->through([PipelineTestPipeOne::class])
  132. ->then(static function ($passable) {
  133. return $passable;
  134. });
  135. static::assertSame('foo', $result);
  136. static::assertSame('foo', $_SERVER['__test.pipe.one']);
  137. unset($_SERVER['__test.pipe.one']);
  138. }
  139. public function testHandleCarry()
  140. {
  141. $result = (new FooPipeline($this->getContainer()))
  142. ->send($id = rand(0, 99))
  143. ->through([PipelineTestPipeOne::class])
  144. ->via('incr')
  145. ->then(static function ($passable) {
  146. if (is_int($passable)) {
  147. $passable += 3;
  148. }
  149. return $passable;
  150. });
  151. static::assertSame($id + 6, $result);
  152. }
  153. protected function getContainer()
  154. {
  155. $container = Mockery::mock(ContainerInterface::class);
  156. $container->shouldReceive('get')->with(PipelineTestPipeOne::class)->andReturn(new PipelineTestPipeOne());
  157. $container->shouldReceive('get')->with(PipelineTestPipeTwo::class)->andReturn(new PipelineTestPipeTwo());
  158. $container->shouldReceive('get')->with(PipelineTestParameterPipe::class)->andReturn(new PipelineTestParameterPipe());
  159. return $container;
  160. }
  161. }
  162. class PipelineTestPipeOne
  163. {
  164. public function handle($piped, $next)
  165. {
  166. $_SERVER['__test.pipe.one'] = $piped;
  167. return $next($piped);
  168. }
  169. public function differentMethod($piped, $next)
  170. {
  171. return $next($piped);
  172. }
  173. public function incr($piped, $next)
  174. {
  175. return $next(++$piped);
  176. }
  177. }
  178. class PipelineTestPipeTwo
  179. {
  180. public function __invoke($piped, $next)
  181. {
  182. $_SERVER['__test.pipe.one'] = $piped;
  183. return $next($piped);
  184. }
  185. }
  186. class PipelineTestParameterPipe
  187. {
  188. public function handle($piped, $next, $parameter1 = null, $parameter2 = null)
  189. {
  190. $_SERVER['__test.pipe.parameters'] = [$parameter1, $parameter2];
  191. return $next($piped);
  192. }
  193. }