From 4a15d54e98e00b219253168b3b432e6a920ea07b Mon Sep 17 00:00:00 2001 From: ncou Date: Sat, 21 Nov 2020 14:20:18 +0100 Subject: [PATCH] update --- composer.json | 3 +- tests/Decorator/CallableMiddlewareTest.php | 47 ----- tests/Decorator/LazyLoadingMiddlewareTest.php | 122 ------------- .../RequestHandlerMiddlewareTest.php | 30 ---- tests/Fixtures/EmptyMiddleware.php | 2 +- tests/PipelineBuilderTest.php | 161 ------------------ tests/PipelineTest.php | 35 ++-- 7 files changed, 19 insertions(+), 381 deletions(-) delete mode 100644 tests/Decorator/CallableMiddlewareTest.php delete mode 100644 tests/Decorator/LazyLoadingMiddlewareTest.php delete mode 100644 tests/Decorator/RequestHandlerMiddlewareTest.php delete mode 100644 tests/PipelineBuilderTest.php diff --git a/composer.json b/composer.json index d5cec06..1c90ffc 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,8 @@ "phpstan/phpstan": "^0.12.0", "phpstan/phpstan-strict-rules": "^0.12", "overtrue/phplint": "^2.0", - "chiron/coding-standard": "^3.0" + "chiron/coding-standard": "^3.0", + "nyholm/psr7": "^1.3" }, "autoload": { "psr-4": { diff --git a/tests/Decorator/CallableMiddlewareTest.php b/tests/Decorator/CallableMiddlewareTest.php deleted file mode 100644 index b286f2c..0000000 --- a/tests/Decorator/CallableMiddlewareTest.php +++ /dev/null @@ -1,47 +0,0 @@ -createMock(ServerRequestInterface::class); - $handlerMock = $this->createMock(RequestHandlerInterface::class); - - $callable = function ($request, $handler) use ($requestMock, $handlerMock) { - $this->assertSame($requestMock, $request); - $this->assertSame($handlerMock, $handler); - - return $this->createMock(ResponseInterface::class); - }; - - $middleware = new CallableMiddleware($callable); - $middleware->process($requestMock, $handlerMock); - } - - /** - * @expectedException \LogicException - * @expectedExceptionMessage Decorated callable middleware of type Closure failed to produce a response. - */ - public function testGivenCallback_ThrowException() - { - $requestMock = $this->createMock(ServerRequestInterface::class); - $handlerMock = $this->createMock(RequestHandlerInterface::class); - - $callable = function () { - return "Bad response - it's not an ResponseInterface object !!!"; - }; - - $middleware = new CallableMiddleware($callable); - $middleware->process($requestMock, $handlerMock); - } -} diff --git a/tests/Decorator/LazyLoadingMiddlewareTest.php b/tests/Decorator/LazyLoadingMiddlewareTest.php deleted file mode 100644 index 396c4f8..0000000 --- a/tests/Decorator/LazyLoadingMiddlewareTest.php +++ /dev/null @@ -1,122 +0,0 @@ -createMock(ServerRequestInterface::class); - $responseMock = $this->createMock(ResponseInterface::class); - $handlerMock = $this->createMock(RequestHandlerInterface::class); - $middlewareMock = $this->createMock(MiddlewareInterface::class); - $middlewareMock - ->expects($this->once()) - ->method('process') - ->with($requestMock, $handlerMock) - ->willReturn($responseMock); - - $containerMock = $this->createMock(ContainerInterface::class); - - $containerMock - ->method('has') - ->with('testMiddleware') - ->willReturn(true); - - $containerMock - ->expects($this->once()) - ->method('get') - ->with('testMiddleware') - ->willReturn($middlewareMock); - $middleware = new LazyLoadingMiddleware($containerMock, 'testMiddleware'); - $response = $middleware->process($requestMock, $handlerMock); - $this->assertSame($responseMock, $response); - } - - public function testMiddlewareNotFoundInTheContainerButItsAutoloadable() - { - $containerMock = $this->createMock(ContainerInterface::class); - $containerMock - ->method('has') - ->with(FoobarClass::class) - ->willReturn(false); - - $middleware = new LazyLoadingMiddleware($containerMock, FoobarClass::class); - $this->assertInstanceOf(MiddlewareInterface::class, $middleware); - } - - /** - * @expectedException \InvalidArgumentException - * @expectedExceptionMessage Cannot fetch middleware service "foobar"; service not registered - */ - public function testExceptionMiddlewareNotFoundInTheContainerAndNotAutoloadable() - { - $requestMock = $this->createMock(ServerRequestInterface::class); - $handlerMock = $this->createMock(RequestHandlerInterface::class); - - $containerMock = $this->createMock(ContainerInterface::class); - $containerMock - ->method('has') - ->with('foobar') - ->willReturn(false); - - $middleware = new LazyLoadingMiddleware($containerMock, 'foobar'); - $response = $middleware->process($requestMock, $handlerMock); - } - - /** - * @expectedException \InvalidArgumentException - */ - public function testResolvedMiddlewareIsNot_A_ValidType() - { - $requestMock = $this->createMock(ServerRequestInterface::class); - $handlerMock = $this->createMock(RequestHandlerInterface::class); - - $containerMock = $this->createMock(ContainerInterface::class); - - $containerMock - ->method('has') - ->with('foo') - ->willReturn(true); - - $containerMock - ->expects($this->once()) - ->method('get') - ->with('foo') - ->willReturn('bar'); - - $middleware = new LazyLoadingMiddleware($containerMock, 'foo'); - $response = $middleware->process($requestMock, $handlerMock); - } - - /** - * @expectedException \InvalidArgumentException - * @expectedExceptionMessage Cannot fetch middleware service "Chiron\Tests\Pipe\Fixtures\FoobarBadClass"; service not registered - */ - public function testExceptionMiddlewareNotFoundInTheContainerButItsAutoloadableWith_Not_A_ValidType() - { - $requestMock = $this->createMock(ServerRequestInterface::class); - $handlerMock = $this->createMock(RequestHandlerInterface::class); - - $containerMock = $this->createMock(ContainerInterface::class); - $containerMock - ->method('has') - ->with(FoobarBadClass::class) - ->willReturn(false); - - $middleware = new LazyLoadingMiddleware($containerMock, FoobarBadClass::class); - $response = $middleware->process($requestMock, $handlerMock); - } -} diff --git a/tests/Decorator/RequestHandlerMiddlewareTest.php b/tests/Decorator/RequestHandlerMiddlewareTest.php deleted file mode 100644 index f744ebe..0000000 --- a/tests/Decorator/RequestHandlerMiddlewareTest.php +++ /dev/null @@ -1,30 +0,0 @@ -createMock(ServerRequestInterface::class); - $responseMock = $this->createMock(ResponseInterface::class); - $handlerMock = $this->createMock(RequestHandlerInterface::class); - - $handlerMock - ->expects($this->once()) - ->method('handle') - ->with($requestMock) - ->willReturn($responseMock); - - $middleware = new RequestHandlerMiddleware($handlerMock); - $middleware->process($requestMock, $handlerMock); - } -} diff --git a/tests/Fixtures/EmptyMiddleware.php b/tests/Fixtures/EmptyMiddleware.php index 9230e09..2e1c0e3 100644 --- a/tests/Fixtures/EmptyMiddleware.php +++ b/tests/Fixtures/EmptyMiddleware.php @@ -16,7 +16,7 @@ class EmptyMiddleware implements MiddlewareInterface public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { // do nothing simple use the previous handle to get the response. - // this class is used to test an pipeline with no returned response (because prévious $handler is the empty pipeline class). + // this class is used to test an pipeline with no returned response (because previous $handler is the empty pipeline class). return $handler->handle($request); } } diff --git a/tests/PipelineBuilderTest.php b/tests/PipelineBuilderTest.php deleted file mode 100644 index a1479e3..0000000 --- a/tests/PipelineBuilderTest.php +++ /dev/null @@ -1,161 +0,0 @@ -request = new ServerRequest('GET', new Uri('/')); - } - - public function testEmptyMiddlewareStackAfterFirstInstanciation() - { - $builder = new PipelineBuilder(); - - $middlewaresArray = $this->readAttribute($builder, 'stack'); - $this->assertSame([], $middlewaresArray); - } - - public function testPipeOnTopMiddleware() - { - $response = new Response(); - $response->getBody()->write('FIRST MIDDLEWARE'); - $middleware_1 = new FixedResponseMiddleware($response); - - $response = new Response(); - $response->getBody()->write('MOVE ON TOP'); - $middleware_2 = new FixedResponseMiddleware($response); - - $builder = new PipelineBuilder(); - $builder->add($middleware_1); - $builder->addOnTop($middleware_2); - - $middlewaresArray = $this->readAttribute($builder, 'stack'); - - $this->assertSame($middleware_2, $middlewaresArray[0]); - } - - public function testPipeOnTopMiddlewareArray() - { - $response = new Response(); - $response->getBody()->write('FIRST MIDDLEWARE'); - $middleware_1 = new FixedResponseMiddleware($response); - - $response = new Response(); - $response->getBody()->write('MOVE ON TOP_1'); - $middleware_2 = new FixedResponseMiddleware($response); - - $response = new Response(); - $response->getBody()->write('MOVE ON TOP_2'); - $middleware_3 = new FixedResponseMiddleware($response); - - $response = new Response(); - $response->getBody()->write('MOVE ON TOP_3'); - $middleware_4 = new FixedResponseMiddleware($response); - - $builder = new PipelineBuilder(); - $builder->add($middleware_1); - $builder->addOnTop([$middleware_2, $middleware_3, $middleware_4]); - - $middlewaresArray = $this->readAttribute($builder, 'stack'); - - $this->assertSame($middleware_2, $middlewaresArray[0]); - $this->assertSame($middleware_3, $middlewaresArray[1]); - $this->assertSame($middleware_4, $middlewaresArray[2]); - $this->assertSame($middleware_1, $middlewaresArray[3]); - } - - public function testPipeAtBottomByDefaultMiddleware() - { - $response = new Response(); - $response->getBody()->write('FIRST MIDDLEWARE'); - $middleware_1 = new FixedResponseMiddleware($response); - - $response = new Response(); - $response->getBody()->write('MOVE AT BOTTOM'); - $middleware_2 = new FixedResponseMiddleware($response); - - $builder = new PipelineBuilder(); - $builder->add($middleware_1); - $builder->add($middleware_2); - - $middlewaresArray = $this->readAttribute($builder, 'stack'); - - $this->assertSame($middleware_2, $middlewaresArray[1]); - } - - public function testFlushPipe() - { - $response = new Response(); - $response->getBody()->write('MIDDLEWARE'); - $middleware = new FixedResponseMiddleware($response); - - $builder = new PipelineBuilder(); - $builder->add($middleware); - $builder->flush(); - - $middlewaresArray = $this->readAttribute($builder, 'stack'); - - $this->assertSame([], $middlewaresArray); - } - - public function testBuildEmptyPipeline() - { - $builder = new PipelineBuilder(); - $handler = $builder->build(); - - $this->assertInstanceOf(Pipeline::class, $handler); - - $middlewaresArray = $this->readAttribute($handler, 'queue'); - $this->assertSame([], $middlewaresArray); - } - - public function testBuildPipelineWithOneMiddleware() - { - $builder = new PipelineBuilder(); - - $response = new Response(); - $response->getBody()->write('MIDDLEWARE'); - $middleware = new FixedResponseMiddleware($response); - - $builder->add($middleware); - - $handler = $builder->build(); - - $this->assertInstanceOf(Pipeline::class, $handler); - - $middlewaresArray = $this->readAttribute($handler, 'queue'); - $this->assertSame([$middleware], $middlewaresArray); - } - - /** - * @expectedException \InvalidArgumentException - */ - public function testExceptionWhenIncompatibleTypeIsUsed() - { - $builder = new PipelineBuilder(); - $builder->add(123456); - } - - /** - * @expectedException \InvalidArgumentException - */ - public function testExceptionWhenStringEmptyIsUsed() - { - $builder = new PipelineBuilder(); - $builder->add(''); - } -} diff --git a/tests/PipelineTest.php b/tests/PipelineTest.php index 3e35df6..63874e2 100644 --- a/tests/PipelineTest.php +++ b/tests/PipelineTest.php @@ -4,39 +4,38 @@ namespace Chiron\Pipeline\Tests; -use Chiron\Http\Psr\Response; -use Chiron\Http\Psr\ServerRequest; -use Chiron\Http\Psr\Uri; -use Chiron\Pipe\Decorator\CallableMiddleware; -use Chiron\Pipe\Decorator\FixedResponseMiddleware; -use Chiron\Pipe\Pipeline; +use Chiron\Pipeline\Decorator\CallableMiddleware; +use Chiron\Pipeline\Decorator\FixedResponseMiddleware; +use Chiron\Pipeline\Pipeline; use PHPUnit\Framework\TestCase; use Psr\Http\Server\RequestHandlerInterface; -use Chiron\Tests\Pipe\Fixtures\EmptyMiddleware; +use Chiron\Tests\Pipeline\Fixtures\EmptyMiddleware; +use Nyholm\Psr7\ServerRequest; +use Chiron\Container\Container; //https://github.com/zendframework/zend-expressive/blob/master/test/MiddlewareFactoryTest.php#L49 class PipelineTest extends TestCase { - public $request; + private $request; - protected function setUp() + protected function setUp(): void { - $this->request = new ServerRequest('GET', new Uri('/')); + $this->request = new ServerRequest('GET', '/'); } public function testPipelineInstanceOfRequestHandler() { - $handler = new Pipeline(); + $handler = new Pipeline(new Container()); $this->assertInstanceOf(RequestHandlerInterface::class, $handler); } public function testEmptyMiddlewareQueueAfterFirstInstanciation() { - $handler = new Pipeline(); + $handler = new Pipeline(new Container()); - $middlewaresArray = $this->readAttribute($handler, 'queue'); + $middlewaresArray = $this->readAttribute($handler, 'middlewares'); $this->assertSame([], $middlewaresArray); } @@ -46,7 +45,7 @@ public function testEmptyMiddlewareQueueAfterFirstInstanciation() */ public function testPipelineThrowExceptionIfQueueIsEmpty() { - $handler = new Pipeline(); + $handler = new Pipeline(new Container()); $handler->handle($this->request); } @@ -57,7 +56,7 @@ public function testPipelineThrowExceptionIfQueueIsEmpty() */ public function testPipelineThrowExceptionIfMiddlewareDoesntReturnAResponse() { - $handler = new Pipeline(); + $handler = new Pipeline(new Container()); $handler->pipe(new EmptyMiddleware()); @@ -82,13 +81,13 @@ public function testPipeMiddlewares() $middleware_3 = new FixedResponseMiddleware(new Response(202)); - $handler = new Pipeline(); + $handler = new Pipeline(new Container()); $handler->pipe($middleware_1); $handler->pipe($middleware_2); $handler->pipe($middleware_3); - $middlewaresArray = $this->readAttribute($handler, 'queue'); + $middlewaresArray = $this->readAttribute($handler, 'middlewares'); $this->assertSame([$middleware_1, $middleware_2, $middleware_3], $middlewaresArray); @@ -97,6 +96,4 @@ public function testPipeMiddlewares() $this->assertEquals(202, $response->getStatusCode()); $this->assertEquals('foobar', (string) $response->getBody()); } - - }