diff options
author | Igor Scheller <igor.scheller@igorshp.de> | 2018-08-11 23:46:28 +0200 |
---|---|---|
committer | Igor Scheller <igor.scheller@igorshp.de> | 2018-08-19 13:59:39 +0200 |
commit | f3b3b6683ca90b70ec4d4daae002dc0caac9ebdd (patch) | |
tree | c550252995a46763d8c540c9bd7f41e4b1c4f246 /tests | |
parent | f3f05f6cc4bef3338dbfb6eb340da4fb1c5ba1e1 (diff) |
Added middleware tests
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Unit/Middleware/DispatcherTest.php | 230 | ||||
-rw-r--r-- | tests/Unit/Middleware/ExceptionHandlerTest.php | 58 | ||||
-rw-r--r-- | tests/Unit/Middleware/LegacyMiddlewareTest.php | 85 | ||||
-rw-r--r-- | tests/Unit/Middleware/NotFoundResponseTest.php | 39 | ||||
-rw-r--r-- | tests/Unit/Middleware/SendResponseHandlerTest.php | 71 | ||||
-rw-r--r-- | tests/Unit/Middleware/Stub/ExceptionMiddlewareHandler.php | 23 | ||||
-rw-r--r-- | tests/Unit/Middleware/Stub/NotARealMiddleware.php | 8 | ||||
-rw-r--r-- | tests/Unit/Middleware/Stub/ReturnResponseMiddleware.php | 36 | ||||
-rw-r--r-- | tests/Unit/Middleware/Stub/ReturnResponseMiddlewareHandler.php | 30 |
9 files changed, 580 insertions, 0 deletions
diff --git a/tests/Unit/Middleware/DispatcherTest.php b/tests/Unit/Middleware/DispatcherTest.php new file mode 100644 index 00000000..c01c5029 --- /dev/null +++ b/tests/Unit/Middleware/DispatcherTest.php @@ -0,0 +1,230 @@ +<?php + +namespace Engelsystem\Test\Unit\Middleware; + +use Engelsystem\Application; +use Engelsystem\Middleware\Dispatcher; +use Engelsystem\Test\Unit\Middleware\Stub\NotARealMiddleware; +use Engelsystem\Test\Unit\Middleware\Stub\ReturnResponseMiddleware; +use InvalidArgumentException; +use LogicException; +use PHPUnit\Framework\TestCase; +use PHPUnit_Framework_MockObject_MockObject as MockObject; +use Psr\Http\Message\ResponseInterface; +use Psr\Http\Message\ServerRequestInterface; +use Psr\Http\Server\MiddlewareInterface; +use Psr\Http\Server\RequestHandlerInterface; +use ReflectionClass as Reflection; + +class DispatcherTest extends TestCase +{ + /** + * @covers \Engelsystem\Middleware\Dispatcher::__construct + */ + public function testInit() + { + /** @var Application|MockObject $container */ + $container = $this->createMock(Application::class); + + $dispatcher = new Dispatcher([], $container); + $this->assertInstanceOf(MiddlewareInterface::class, $dispatcher); + $this->assertInstanceOf(RequestHandlerInterface::class, $dispatcher); + + $reflection = new Reflection(get_class($dispatcher)); + $property = $reflection->getProperty('container'); + $property->setAccessible(true); + $this->assertEquals($container, $property->getValue($dispatcher)); + } + + /** + * @covers \Engelsystem\Middleware\Dispatcher::process + */ + public function testProcess() + { + /** @var ServerRequestInterface|MockObject $request */ + $request = $this->createMock(ServerRequestInterface::class); + /** @var ResponseInterface|MockObject $response */ + $response = $this->createMock(ResponseInterface::class); + /** @var RequestHandlerInterface|MockObject $handler */ + $handler = $this->createMock(RequestHandlerInterface::class); + + /** @var Dispatcher|MockObject $dispatcher */ + $dispatcher = $this->getMockBuilder(Dispatcher::class) + ->setMethods(['handle']) + ->getMock(); + + $dispatcher->expects($this->once()) + ->method('handle') + ->willReturn($response); + + $return = $dispatcher->process($request, $handler); + $this->assertEquals($response, $return); + + $reflection = new Reflection(get_class($dispatcher)); + $property = $reflection->getProperty('next'); + $property->setAccessible(true); + + $this->assertEquals($handler, $property->getValue($dispatcher)); + } + + /** + * @covers \Engelsystem\Middleware\Dispatcher::handle + */ + public function testHandle() + { + /** @var ServerRequestInterface|MockObject $request */ + $request = $this->createMock(ServerRequestInterface::class); + /** @var ResponseInterface|MockObject $response */ + $response = $this->createMock(ResponseInterface::class); + /** @var MiddlewareInterface|MockObject $middleware */ + $middleware = $this->createMock(MiddlewareInterface::class); + + $dispatcher = new Dispatcher([$middleware]); + $middleware->expects($this->once()) + ->method('process') + ->with($request, $dispatcher) + ->willReturn($response); + + $return = $dispatcher->handle($request); + $this->assertEquals($response, $return); + } + + /** + * @covers \Engelsystem\Middleware\Dispatcher::handle + */ + public function testHandleNext() + { + /** @var ServerRequestInterface|MockObject $request */ + $request = $this->createMock(ServerRequestInterface::class); + /** @var ResponseInterface|MockObject $response */ + $response = $this->createMock(ResponseInterface::class); + /** @var RequestHandlerInterface|MockObject $handler */ + $handler = $this->createMock(RequestHandlerInterface::class); + + $dispatcher = new Dispatcher(); + $handler->expects($this->once()) + ->method('handle') + ->with($request) + ->willReturn($response); + + $reflection = new Reflection(get_class($dispatcher)); + $property = $reflection->getProperty('next'); + $property->setAccessible(true); + $property->setValue($dispatcher, $handler); + + $return = $dispatcher->handle($request); + $this->assertEquals($response, $return); + } + + /** + * @covers \Engelsystem\Middleware\Dispatcher::handle + */ + public function testHandleNoMiddleware() + { + /** @var ServerRequestInterface|MockObject $request */ + $request = $this->createMock(ServerRequestInterface::class); + + $this->expectException(LogicException::class); + + $dispatcher = new Dispatcher(); + $dispatcher->handle($request); + } + + /** + * @covers \Engelsystem\Middleware\Dispatcher::handle + */ + public function testHandleNoRealMiddleware() + { + /** @var ServerRequestInterface|MockObject $request */ + $request = $this->createMock(ServerRequestInterface::class); + + $this->expectException(InvalidArgumentException::class); + + $dispatcher = new Dispatcher([new NotARealMiddleware()]); + $dispatcher->handle($request); + } + + /** + * @covers \Engelsystem\Middleware\Dispatcher::handle + */ + public function testHandleCallResolve() + { + /** @var ServerRequestInterface|MockObject $request */ + $request = $this->createMock(ServerRequestInterface::class); + /** @var ResponseInterface|MockObject $response */ + $response = $this->createMock(ResponseInterface::class); + /** @var MiddlewareInterface|MockObject $middleware */ + $middleware = $this->createMock(MiddlewareInterface::class); + + /** @var Dispatcher|MockObject $dispatcher */ + $dispatcher = $this->getMockBuilder(Dispatcher::class) + ->setConstructorArgs([[MiddlewareInterface::class]]) + ->setMethods(['resolveMiddleware']) + ->getMock(); + + $dispatcher->expects($this->once()) + ->method('resolveMiddleware') + ->with(MiddlewareInterface::class) + ->willReturn($middleware); + + $middleware->expects($this->once()) + ->method('process') + ->with($request, $dispatcher) + ->willReturn($response); + + $return = $dispatcher->handle($request); + $this->assertEquals($response, $return); + } + + /** + * @covers \Engelsystem\Middleware\Dispatcher::resolveMiddleware + * @covers \Engelsystem\Middleware\Dispatcher::setContainer + */ + public function testResolveMiddleware() + { + /** @var Application|MockObject $container */ + $container = $this->createMock(Application::class); + /** @var ServerRequestInterface|MockObject $request */ + $request = $this->createMock(ServerRequestInterface::class); + /** @var ResponseInterface|MockObject $response */ + $response = $this->createMock(ResponseInterface::class); + + $returnResponseMiddleware = new ReturnResponseMiddleware($response); + + $container->expects($this->exactly(2)) + ->method('has') + ->withConsecutive([ReturnResponseMiddleware::class], ['middleware']) + ->willReturnOnConsecutiveCalls(false, true); + + $container->expects($this->once()) + ->method('make') + ->with(ReturnResponseMiddleware::class) + ->willReturn($returnResponseMiddleware); + + $container->expects($this->once()) + ->method('get') + ->with('middleware') + ->willReturn($returnResponseMiddleware); + + $dispatcher = new Dispatcher([ReturnResponseMiddleware::class]); + $dispatcher->setContainer($container); + $dispatcher->handle($request); + + $dispatcher = new Dispatcher(['middleware'], $container); + $dispatcher->handle($request); + } + + /** + * @covers \Engelsystem\Middleware\Dispatcher::resolveMiddleware + */ + public function testResolveMiddlewareNoContainer() + { + /** @var ServerRequestInterface|MockObject $request */ + $request = $this->createMock(ServerRequestInterface::class); + + $this->expectException(InvalidArgumentException::class); + + $dispatcher = new Dispatcher([ReturnResponseMiddleware::class]); + $dispatcher->handle($request); + } +} diff --git a/tests/Unit/Middleware/ExceptionHandlerTest.php b/tests/Unit/Middleware/ExceptionHandlerTest.php new file mode 100644 index 00000000..6d2a20e6 --- /dev/null +++ b/tests/Unit/Middleware/ExceptionHandlerTest.php @@ -0,0 +1,58 @@ +<?php + +namespace Engelsystem\Test\Unit\Middleware; + +use Engelsystem\Application; +use Engelsystem\Exceptions\Handler; +use Engelsystem\Http\Response; +use Engelsystem\Middleware\ExceptionHandler; +use Engelsystem\Test\Unit\Middleware\Stub\ExceptionMiddlewareHandler; +use Engelsystem\Test\Unit\Middleware\Stub\ReturnResponseMiddlewareHandler; +use Illuminate\Contracts\Container\Container as ContainerInterface; +use PHPUnit\Framework\TestCase; +use PHPUnit_Framework_MockObject_MockObject as MockObject; +use Psr\Http\Message\ResponseInterface; +use Psr\Http\Message\ServerRequestInterface; + +class ExceptionHandlerTest extends TestCase +{ + /** + * @covers \Engelsystem\Middleware\ExceptionHandler::__construct + * @covers \Engelsystem\Middleware\ExceptionHandler::process + */ + public function testRegister() + { + /** @var MockObject|ContainerInterface $container */ + $container = $this->getMockForAbstractClass(ContainerInterface::class); + /** @var MockObject|ServerRequestInterface $request */ + $request = $this->getMockBuilder(ServerRequestInterface::class)->getMock(); + /** @var MockObject|ResponseInterface $response */ + $response = $this->getMockBuilder(Response::class)->getMock(); + /** @var MockObject|Handler $errorHandler */ + $errorHandler = $this->getMockBuilder(Handler::class)->getMock(); + $returnResponseHandler = new ReturnResponseMiddlewareHandler($response); + $throwExceptionHandler = new ExceptionMiddlewareHandler(); + + Application::setInstance($container); + + $container->expects($this->exactly(2)) + ->method('get') + ->withConsecutive(['error.handler'], ['psr7.response']) + ->willReturnOnConsecutiveCalls($errorHandler, $response); + + $response->expects($this->once()) + ->method('withContent') + ->willReturn($response); + $response->expects($this->once()) + ->method('withStatus') + ->with(500) + ->willReturn($response); + + $handler = new ExceptionHandler($container); + $return = $handler->process($request, $returnResponseHandler); + $this->assertEquals($response, $return); + + $return = $handler->process($request, $throwExceptionHandler); + $this->assertEquals($response, $return); + } +} diff --git a/tests/Unit/Middleware/LegacyMiddlewareTest.php b/tests/Unit/Middleware/LegacyMiddlewareTest.php new file mode 100644 index 00000000..34e60b60 --- /dev/null +++ b/tests/Unit/Middleware/LegacyMiddlewareTest.php @@ -0,0 +1,85 @@ +<?php + +namespace Engelsystem\Test\Unit\Middleware; + +use Engelsystem\Http\Request; +use Engelsystem\Middleware\LegacyMiddleware; +use PHPUnit\Framework\TestCase; +use PHPUnit_Framework_MockObject_MockObject as MockObject; +use Psr\Container\ContainerInterface; +use Psr\Http\Message\ResponseInterface; +use Psr\Http\Message\ServerRequestInterface; +use Psr\Http\Server\RequestHandlerInterface; +use Symfony\Component\HttpFoundation\ParameterBag; + +class LegacyMiddlewareTest extends TestCase +{ + /** + * @covers \Engelsystem\Middleware\LegacyMiddleware::__construct + * @covers \Engelsystem\Middleware\LegacyMiddleware::process + */ + public function testRegister() + { + /** @var ContainerInterface|MockObject $container */ + $container = $this->getMockForAbstractClass(ContainerInterface::class); + /** @var LegacyMiddleware|MockObject $middleware */ + $middleware = $this->getMockBuilder(LegacyMiddleware::class) + ->setConstructorArgs([$container]) + ->setMethods(['loadPage', 'renderPage']) + ->getMock(); + /** @var Request|MockObject $defaultRequest */ + $defaultRequest = $this->createMock(Request::class); + /** @var ParameterBag|MockObject $parameters */ + $parameters = $this->createMock(ParameterBag::class); + /** @var ResponseInterface|MockObject $response */ + $response = $this->getMockForAbstractClass(ResponseInterface::class); + /** @var RequestHandlerInterface|MockObject $handler */ + $handler = $this->getMockForAbstractClass(RequestHandlerInterface::class); + /** @var ServerRequestInterface|MockObject $request */ + $request = $this->getMockForAbstractClass(ServerRequestInterface::class); + + $middleware->expects($this->exactly(2)) + ->method('loadPage') + ->withConsecutive(['user_worklog'], ['login']) + ->willReturnOnConsecutiveCalls( + ['title', 'content'], + ['title2', 'content2'] + ); + + $middleware->expects($this->exactly(2)) + ->method('renderPage') + ->withConsecutive( + ['user_worklog', 'title', 'content'], + ['login', 'title2', 'content2'] + ) + ->willReturn($response); + + $container->expects($this->atLeastOnce()) + ->method('get') + ->with('request') + ->willReturn($defaultRequest); + + $defaultRequest->query = $parameters; + $defaultRequest->expects($this->once()) + ->method('path') + ->willReturn('user-worklog'); + + $parameters->expects($this->exactly(3)) + ->method('get') + ->with('p') + ->willReturnOnConsecutiveCalls( + null, + 'foo', + '/' + ); + + $handler->expects($this->once()) + ->method('handle') + ->with($request) + ->willReturn($response); + + $middleware->process($request, $handler); + $middleware->process($request, $handler); + $middleware->process($request, $handler); + } +} diff --git a/tests/Unit/Middleware/NotFoundResponseTest.php b/tests/Unit/Middleware/NotFoundResponseTest.php new file mode 100644 index 00000000..9279e81d --- /dev/null +++ b/tests/Unit/Middleware/NotFoundResponseTest.php @@ -0,0 +1,39 @@ +<?php + +namespace Engelsystem\Test\Unit\Middleware; + +use Engelsystem\Middleware\NotFoundResponse; +use PHPUnit\Framework\TestCase; +use PHPUnit_Framework_MockObject_MockObject as MockObject; +use Psr\Http\Message\ResponseInterface; +use Psr\Http\Message\ServerRequestInterface; +use Psr\Http\Server\RequestHandlerInterface; + +class NotFoundResponseTest extends TestCase +{ + /** + * @covers \Engelsystem\Middleware\NotFoundResponse::process + */ + public function testRegister() + { + /** @var NotFoundResponse|MockObject $middleware */ + $middleware = $this->getMockBuilder(NotFoundResponse::class) + ->setMethods(['renderPage']) + ->getMock(); + /** @var ResponseInterface|MockObject $response */ + $response = $this->getMockForAbstractClass(ResponseInterface::class); + /** @var RequestHandlerInterface|MockObject $handler */ + $handler = $this->getMockForAbstractClass(RequestHandlerInterface::class); + /** @var ServerRequestInterface|MockObject $request */ + $request = $this->getMockForAbstractClass(ServerRequestInterface::class); + + $middleware->expects($this->once()) + ->method('renderPage') + ->willReturn($response); + + $handler->expects($this->never()) + ->method('handle'); + + $middleware->process($request, $handler); + } +} diff --git a/tests/Unit/Middleware/SendResponseHandlerTest.php b/tests/Unit/Middleware/SendResponseHandlerTest.php new file mode 100644 index 00000000..7431299e --- /dev/null +++ b/tests/Unit/Middleware/SendResponseHandlerTest.php @@ -0,0 +1,71 @@ +<?php + +namespace Engelsystem\Test\Unit\Middleware; + +use Engelsystem\Middleware\SendResponseHandler; +use PHPUnit\Framework\TestCase; +use PHPUnit_Framework_MockObject_MockObject as MockObject; +use Psr\Http\Message\ResponseInterface; +use Psr\Http\Message\ServerRequestInterface; +use Psr\Http\Server\RequestHandlerInterface; + +class SendResponseHandlerTest extends TestCase +{ + /** + * @covers \Engelsystem\Middleware\SendResponseHandler::process + */ + public function testRegister() + { + /** @var SendResponseHandler|MockObject $middleware */ + $middleware = $this->getMockBuilder(SendResponseHandler::class) + ->setMethods(['headersSent', 'sendHeader']) + ->getMock(); + /** @var ServerRequestInterface|MockObject $request */ + $request = $this->getMockForAbstractClass(ServerRequestInterface::class); + /** @var ResponseInterface|MockObject $response */ + $response = $this->getMockForAbstractClass(ResponseInterface::class); + /** @var RequestHandlerInterface|MockObject $handler */ + $handler = $this->getMockForAbstractClass(RequestHandlerInterface::class); + + $middleware->expects($this->atLeastOnce()) + ->method('headersSent') + ->willReturnOnConsecutiveCalls(true, false); + + $middleware->expects($this->exactly(4)) + ->method('sendHeader') + ->withConsecutive( + ['HTTP/0.7 505 Something went wrong!', true, 505], + ['Foo: bar', false], + ['lorem: ipsum', false], + ['lorem: dolor', false] + ); + + $handler->expects($this->exactly(2)) + ->method('handle') + ->with($request) + ->willReturn($response); + + $response->expects($this->exactly(2)) + ->method('getBody') + ->willReturn('Lorem Ipsum!'); + + $response->expects($this->atLeastOnce()) + ->method('getProtocolVersion') + ->willReturn('0.7'); + + $response->expects($this->atLeastOnce()) + ->method('getStatusCode') + ->willReturn(505); + + $response->expects($this->once()) + ->method('getReasonPhrase') + ->willReturn('Something went wrong!'); + $response->expects($this->once()) + ->method('getHeaders') + ->willReturn(['Foo' => ['bar'], 'lorem' => ['ipsum', 'dolor']]); + + $this->expectOutputString('Lorem Ipsum!Lorem Ipsum!'); + $middleware->process($request, $handler); + $middleware->process($request, $handler); + } +} diff --git a/tests/Unit/Middleware/Stub/ExceptionMiddlewareHandler.php b/tests/Unit/Middleware/Stub/ExceptionMiddlewareHandler.php new file mode 100644 index 00000000..5e374bea --- /dev/null +++ b/tests/Unit/Middleware/Stub/ExceptionMiddlewareHandler.php @@ -0,0 +1,23 @@ +<?php + +namespace Engelsystem\Test\Unit\Middleware\Stub; + +use Exception; +use Psr\Http\Message\ResponseInterface; +use Psr\Http\Message\ServerRequestInterface; +use Psr\Http\Server\RequestHandlerInterface; + +class ExceptionMiddlewareHandler implements RequestHandlerInterface +{ + /** + * Throws an exception + * + * @param ServerRequestInterface $request + * @return ResponseInterface + * @throws Exception + */ + public function handle(ServerRequestInterface $request): ResponseInterface + { + throw new Exception('Boooom!'); + } +} diff --git a/tests/Unit/Middleware/Stub/NotARealMiddleware.php b/tests/Unit/Middleware/Stub/NotARealMiddleware.php new file mode 100644 index 00000000..017389da --- /dev/null +++ b/tests/Unit/Middleware/Stub/NotARealMiddleware.php @@ -0,0 +1,8 @@ +<?php + +namespace Engelsystem\Test\Unit\Middleware\Stub; + +class NotARealMiddleware +{ + // I'm not a middleware! +} diff --git a/tests/Unit/Middleware/Stub/ReturnResponseMiddleware.php b/tests/Unit/Middleware/Stub/ReturnResponseMiddleware.php new file mode 100644 index 00000000..7c241353 --- /dev/null +++ b/tests/Unit/Middleware/Stub/ReturnResponseMiddleware.php @@ -0,0 +1,36 @@ +<?php + +namespace Engelsystem\Test\Unit\Middleware\Stub; + +use Psr\Http\Message\ResponseInterface; +use Psr\Http\Message\ServerRequestInterface; +use Psr\Http\Server\MiddlewareInterface; +use Psr\Http\Server\RequestHandlerInterface; + +class ReturnResponseMiddleware implements MiddlewareInterface +{ + /** @var ResponseInterface */ + protected $response; + + public function __construct(ResponseInterface $response) + { + $this->response = $response; + } + + /** + * Process an incoming server request and return a response, optionally delegating + * response creation to a handler. + * + * Could be used to group middleware + * + * @param ServerRequestInterface $request + * @param RequestHandlerInterface $handler + * @return ResponseInterface + */ + public function process( + ServerRequestInterface $request, + RequestHandlerInterface $handler + ): ResponseInterface { + return $this->response; + } +} diff --git a/tests/Unit/Middleware/Stub/ReturnResponseMiddlewareHandler.php b/tests/Unit/Middleware/Stub/ReturnResponseMiddlewareHandler.php new file mode 100644 index 00000000..323e07b4 --- /dev/null +++ b/tests/Unit/Middleware/Stub/ReturnResponseMiddlewareHandler.php @@ -0,0 +1,30 @@ +<?php + +namespace Engelsystem\Test\Unit\Middleware\Stub; + +use Psr\Http\Message\ResponseInterface; +use Psr\Http\Message\ServerRequestInterface; +use Psr\Http\Server\RequestHandlerInterface; + +class ReturnResponseMiddlewareHandler implements RequestHandlerInterface +{ + /** @var ResponseInterface */ + protected $response; + + public function __construct(ResponseInterface $response) + { + $this->response = $response; + } + + /** + * Returns a given response + * + * @param ServerRequestInterface $request + * @return ResponseInterface + * @throws \Exception + */ + public function handle(ServerRequestInterface $request): ResponseInterface + { + return $this->response; + } +} |