From a1bc763a16ee8be109de5c9053fbc5eded53824e Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Sat, 25 Aug 2018 21:16:20 +0200 Subject: Added nikic/fast-route as routing dispatcher --- tests/Unit/Middleware/RouteDispatcherTest.php | 148 ++++++++++++++++++++++++++ 1 file changed, 148 insertions(+) create mode 100644 tests/Unit/Middleware/RouteDispatcherTest.php (limited to 'tests/Unit/Middleware/RouteDispatcherTest.php') diff --git a/tests/Unit/Middleware/RouteDispatcherTest.php b/tests/Unit/Middleware/RouteDispatcherTest.php new file mode 100644 index 00000000..edb2f158 --- /dev/null +++ b/tests/Unit/Middleware/RouteDispatcherTest.php @@ -0,0 +1,148 @@ +getMocks(); + + $dispatcher->expects($this->once()) + ->method('dispatch') + ->with('HEAD', '/foo!bar') + ->willReturn([FastRouteDispatcher::FOUND, $handler, ['foo' => 'bar', 'lorem' => 'ipsum']]); + + $request->expects($this->exactly(3)) + ->method('withAttribute') + ->withConsecutive( + ['route-request-handler', $handler], + ['foo', 'bar'], + ['lorem', 'ipsum'] + ) + ->willReturn($request); + + $handler->expects($this->once()) + ->method('handle') + ->with($request) + ->willReturn($response); + + $middleware = new RouteDispatcher($dispatcher, $response); + $return = $middleware->process($request, $handler); + $this->assertEquals($response, $return); + } + + /** + * @covers \Engelsystem\Middleware\RouteDispatcher::process + */ + public function testProcessNotFound() + { + /** @var FastRouteDispatcher|MockObject $dispatcher */ + /** @var ResponseInterface|MockObject $response */ + /** @var ServerRequestInterface|MockObject $request */ + /** @var RequestHandlerInterface|MockObject $handler */ + list($dispatcher, $response, $request, $handler) = $this->getMocks(); + /** @var MiddlewareInterface|MockObject $notFound */ + $notFound = $this->createMock(MiddlewareInterface::class); + + $dispatcher->expects($this->exactly(2)) + ->method('dispatch') + ->with('HEAD', '/foo!bar') + ->willReturn([FastRouteDispatcher::NOT_FOUND]); + + $response->expects($this->once()) + ->method('withStatus') + ->with(404) + ->willReturn($response); + + $notFound->expects($this->once()) + ->method('process') + ->with($request, $handler) + ->willReturn($response); + + $middleware = new RouteDispatcher($dispatcher, $response, $notFound); + $return = $middleware->process($request, $handler); + $this->assertEquals($response, $return); + + $middleware = new RouteDispatcher($dispatcher, $response); + $return = $middleware->process($request, $handler); + $this->assertEquals($response, $return); + } + + /** + * @covers \Engelsystem\Middleware\RouteDispatcher::process + */ + public function testProcessNotAllowed() + { + /** @var FastRouteDispatcher|MockObject $dispatcher */ + /** @var ResponseInterface|MockObject $response */ + /** @var ServerRequestInterface|MockObject $request */ + /** @var RequestHandlerInterface|MockObject $handler */ + list($dispatcher, $response, $request, $handler) = $this->getMocks(); + + $dispatcher->expects($this->once()) + ->method('dispatch') + ->with('HEAD', '/foo!bar') + ->willReturn([FastRouteDispatcher::METHOD_NOT_ALLOWED, ['POST', 'TEST']]); + + $response->expects($this->once()) + ->method('withStatus') + ->with(405) + ->willReturn($response); + $response->expects($this->once()) + ->method('withHeader') + ->with('Allow', 'POST, TEST') + ->willReturn($response); + + $middleware = new RouteDispatcher($dispatcher, $response); + $return = $middleware->process($request, $handler); + $this->assertEquals($response, $return); + } + + /** + * @return array + */ + protected function getMocks(): array + { + /** @var FastRouteDispatcher|MockObject $dispatcher */ + $dispatcher = $this->getMockForAbstractClass(FastRouteDispatcher::class); + /** @var ResponseInterface|MockObject $response */ + $response = $this->getMockForAbstractClass(ResponseInterface::class); + /** @var ServerRequestInterface|MockObject $request */ + $request = $this->getMockForAbstractClass(ServerRequestInterface::class); + /** @var RequestHandlerInterface|MockObject $handler */ + $handler = $this->getMockForAbstractClass(RequestHandlerInterface::class); + /** @var UriInterface|MockObject $uriInterface */ + $uriInterface = $this->getMockForAbstractClass(UriInterface::class); + + $request->expects($this->atLeastOnce()) + ->method('getMethod') + ->willReturn('HEAD'); + $request->expects($this->atLeastOnce()) + ->method('getUri') + ->willReturn($uriInterface); + $uriInterface->expects($this->atLeastOnce()) + ->method('getPath') + ->willReturn('/foo%21bar'); + + return array($dispatcher, $response, $request, $handler); + } +} -- cgit v1.2.3-54-g00ecf From ce6d0fd13b54ac79a955b85d50860736a520d333 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Tue, 4 Sep 2018 21:44:34 +0200 Subject: tests: fixed array() return --- tests/Unit/Middleware/CallableHandlerTest.php | 2 +- tests/Unit/Middleware/RouteDispatcherTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'tests/Unit/Middleware/RouteDispatcherTest.php') diff --git a/tests/Unit/Middleware/CallableHandlerTest.php b/tests/Unit/Middleware/CallableHandlerTest.php index 6e6dab58..29424480 100644 --- a/tests/Unit/Middleware/CallableHandlerTest.php +++ b/tests/Unit/Middleware/CallableHandlerTest.php @@ -136,6 +136,6 @@ class CallableHandlerTest extends TestCase $callable = $this->getMockBuilder(stdClass::class) ->setMethods(['__invoke']) ->getMock(); - return array($request, $response, $callable, $handler); + return [$request, $response, $callable, $handler]; } } diff --git a/tests/Unit/Middleware/RouteDispatcherTest.php b/tests/Unit/Middleware/RouteDispatcherTest.php index edb2f158..1e920f06 100644 --- a/tests/Unit/Middleware/RouteDispatcherTest.php +++ b/tests/Unit/Middleware/RouteDispatcherTest.php @@ -143,6 +143,6 @@ class RouteDispatcherTest extends TestCase ->method('getPath') ->willReturn('/foo%21bar'); - return array($dispatcher, $response, $request, $handler); + return [$dispatcher, $response, $request, $handler]; } } -- cgit v1.2.3-54-g00ecf