123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- <?php
- declare(strict_types=1);
- namespace Yansongda\Supports\Tests;
- use Yansongda\Supports\Pipeline;
- use Yansongda\Supports\Tests\Stubs\FooPipeline;
- use Mockery;
- use PHPUnit\Framework\TestCase;
- use Psr\Container\ContainerInterface;
- /**
- * @internal
- * @coversNothing
- */
- class PipelineTest extends TestCase
- {
- protected function tearDown(): void
- {
- Mockery::close();
- }
- public function testPipelineBasicUsage()
- {
- $pipeTwo = function ($piped, $next) {
- $_SERVER['__test.pipe.two'] = $piped;
- return $next($piped);
- };
- $result = (new Pipeline($this->getContainer()))
- ->send('foo')
- ->through([PipelineTestPipeOne::class, $pipeTwo])
- ->then(function ($piped) {
- return $piped;
- });
- static::assertSame('foo', $result);
- static::assertSame('foo', $_SERVER['__test.pipe.one']);
- static::assertSame('foo', $_SERVER['__test.pipe.two']);
- unset($_SERVER['__test.pipe.one'], $_SERVER['__test.pipe.two']);
- }
- public function testPipelineUsageWithObjects()
- {
- $result = (new Pipeline($this->getContainer()))
- ->send('foo')
- ->through([new PipelineTestPipeOne()])
- ->then(function ($piped) {
- return $piped;
- });
- static::assertSame('foo', $result);
- static::assertSame('foo', $_SERVER['__test.pipe.one']);
- unset($_SERVER['__test.pipe.one']);
- }
- public function testPipelineUsageWithInvokableObjects()
- {
- $result = (new Pipeline($this->getContainer()))
- ->send('foo')
- ->through([new PipelineTestPipeTwo()])
- ->then(
- function ($piped) {
- return $piped;
- }
- );
- static::assertSame('foo', $result);
- static::assertSame('foo', $_SERVER['__test.pipe.one']);
- unset($_SERVER['__test.pipe.one']);
- }
- public function testPipelineUsageWithCallable()
- {
- $function = function ($piped, $next) {
- $_SERVER['__test.pipe.one'] = 'foo';
- return $next($piped);
- };
- $result = (new Pipeline($this->getContainer()))
- ->send('foo')
- ->through([$function])
- ->then(
- function ($piped) {
- return $piped;
- }
- );
- static::assertSame('foo', $result);
- static::assertSame('foo', $_SERVER['__test.pipe.one']);
- unset($_SERVER['__test.pipe.one']);
- $result = (new Pipeline($this->getContainer()))
- ->send('bar')
- ->through($function)
- ->then(static function ($passable) {
- return $passable;
- });
- static::assertSame('bar', $result);
- static::assertSame('foo', $_SERVER['__test.pipe.one']);
- unset($_SERVER['__test.pipe.one']);
- }
- public function testPipelineUsageWithInvokableClass()
- {
- $result = (new Pipeline($this->getContainer()))
- ->send('foo')
- ->through([PipelineTestPipeTwo::class])
- ->then(
- function ($piped) {
- return $piped;
- }
- );
- static::assertSame('foo', $result);
- static::assertSame('foo', $_SERVER['__test.pipe.one']);
- unset($_SERVER['__test.pipe.one']);
- }
- public function testPipelineUsageWithParameters()
- {
- $parameters = ['one', 'two'];
- $result = (new Pipeline($this->getContainer()))
- ->send('foo')
- ->through(PipelineTestParameterPipe::class . ':' . implode(',', $parameters))
- ->then(function ($piped) {
- return $piped;
- });
- static::assertSame('foo', $result);
- static::assertEquals($parameters, $_SERVER['__test.pipe.parameters']);
- unset($_SERVER['__test.pipe.parameters']);
- }
- public function testPipelineViaChangesTheMethodBeingCalledOnThePipes()
- {
- $pipelineInstance = new Pipeline($this->getContainer());
- $result = $pipelineInstance->send('data')
- ->through(PipelineTestPipeOne::class)
- ->via('differentMethod')
- ->then(function ($piped) {
- return $piped;
- });
- static::assertSame('data', $result);
- }
- public function testPipelineThenReturnMethodRunsPipelineThenReturnsPassable()
- {
- $result = (new Pipeline($this->getContainer()))
- ->send('foo')
- ->through([PipelineTestPipeOne::class])
- ->then(static function ($passable) {
- return $passable;
- });
- static::assertSame('foo', $result);
- static::assertSame('foo', $_SERVER['__test.pipe.one']);
- unset($_SERVER['__test.pipe.one']);
- }
- public function testHandleCarry()
- {
- $result = (new FooPipeline($this->getContainer()))
- ->send($id = rand(0, 99))
- ->through([PipelineTestPipeOne::class])
- ->via('incr')
- ->then(static function ($passable) {
- if (is_int($passable)) {
- $passable += 3;
- }
- return $passable;
- });
- static::assertSame($id + 6, $result);
- }
- protected function getContainer()
- {
- $container = Mockery::mock(ContainerInterface::class);
- $container->shouldReceive('get')->with(PipelineTestPipeOne::class)->andReturn(new PipelineTestPipeOne());
- $container->shouldReceive('get')->with(PipelineTestPipeTwo::class)->andReturn(new PipelineTestPipeTwo());
- $container->shouldReceive('get')->with(PipelineTestParameterPipe::class)->andReturn(new PipelineTestParameterPipe());
- return $container;
- }
- }
- class PipelineTestPipeOne
- {
- public function handle($piped, $next)
- {
- $_SERVER['__test.pipe.one'] = $piped;
- return $next($piped);
- }
- public function differentMethod($piped, $next)
- {
- return $next($piped);
- }
- public function incr($piped, $next)
- {
- return $next(++$piped);
- }
- }
- class PipelineTestPipeTwo
- {
- public function __invoke($piped, $next)
- {
- $_SERVER['__test.pipe.one'] = $piped;
- return $next($piped);
- }
- }
- class PipelineTestParameterPipe
- {
- public function handle($piped, $next, $parameter1 = null, $parameter2 = null)
- {
- $_SERVER['__test.pipe.parameters'] = [$parameter1, $parameter2];
- return $next($piped);
- }
- }
|