summaryrefslogtreecommitdiff
path: root/tests/Unit/Middleware/RouteDispatcherTest.php
diff options
context:
space:
mode:
authorIgor Scheller <igor.scheller@igorshp.de>2018-08-25 21:16:20 +0200
committerIgor Scheller <igor.scheller@igorshp.de>2018-08-25 21:48:39 +0200
commita1bc763a16ee8be109de5c9053fbc5eded53824e (patch)
tree7253db0c8ea918e82253bef3926bcba461a7105d /tests/Unit/Middleware/RouteDispatcherTest.php
parent73c9d923e7cc77847cfcbff4b90ad4815699a4fa (diff)
Added nikic/fast-route as routing dispatcher
Diffstat (limited to 'tests/Unit/Middleware/RouteDispatcherTest.php')
-rw-r--r--tests/Unit/Middleware/RouteDispatcherTest.php148
1 files changed, 148 insertions, 0 deletions
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 @@
+<?php
+
+namespace Engelsystem\Test\Unit\Middleware;
+
+use Engelsystem\Middleware\RouteDispatcher;
+use FastRoute\Dispatcher as FastRouteDispatcher;
+use PHPUnit\Framework\MockObject\MockObject;
+use PHPUnit\Framework\TestCase;
+use Psr\Http\Message\ResponseInterface;
+use Psr\Http\Message\ServerRequestInterface;
+use Psr\Http\Message\UriInterface;
+use Psr\Http\Server\MiddlewareInterface;
+use Psr\Http\Server\RequestHandlerInterface;
+
+class RouteDispatcherTest extends TestCase
+{
+ /**
+ * @covers \Engelsystem\Middleware\RouteDispatcher::process
+ * @covers \Engelsystem\Middleware\RouteDispatcher::__construct
+ */
+ public function testProcess()
+ {
+ /** @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::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);
+ }
+}