summaryrefslogtreecommitdiff
path: root/tests/Unit/Middleware/RequestHandlerTest.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/RequestHandlerTest.php
parent73c9d923e7cc77847cfcbff4b90ad4815699a4fa (diff)
Added nikic/fast-route as routing dispatcher
Diffstat (limited to 'tests/Unit/Middleware/RequestHandlerTest.php')
-rw-r--r--tests/Unit/Middleware/RequestHandlerTest.php89
1 files changed, 89 insertions, 0 deletions
diff --git a/tests/Unit/Middleware/RequestHandlerTest.php b/tests/Unit/Middleware/RequestHandlerTest.php
new file mode 100644
index 00000000..896b55c3
--- /dev/null
+++ b/tests/Unit/Middleware/RequestHandlerTest.php
@@ -0,0 +1,89 @@
+<?php
+
+namespace Engelsystem\Test\Unit\Middleware;
+
+use Engelsystem\Application;
+use Engelsystem\Middleware\RequestHandler;
+use InvalidArgumentException;
+use PHPUnit\Framework\MockObject\MockObject;
+use PHPUnit\Framework\TestCase;
+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 RequestHandlerTest extends TestCase
+{
+ /**
+ * @covers \Engelsystem\Middleware\RequestHandler::__construct
+ */
+ public function testInit()
+ {
+ /** @var Application|MockObject $container */
+ $container = $this->createMock(Application::class);
+
+ $handler = new RequestHandler($container);
+
+ $reflection = new Reflection(get_class($handler));
+ $property = $reflection->getProperty('container');
+ $property->setAccessible(true);
+
+ $this->assertEquals($container, $property->getValue($handler));
+ }
+
+ /**
+ * @covers \Engelsystem\Middleware\RequestHandler::process
+ */
+ public function testProcess()
+ {
+ /** @var Application|MockObject $container */
+ $container = $this->createMock(Application::class);
+ /** @var ServerRequestInterface|MockObject $request */
+ $request = $this->getMockForAbstractClass(ServerRequestInterface::class);
+ /** @var RequestHandlerInterface|MockObject $handler */
+ $handler = $this->getMockForAbstractClass(RequestHandlerInterface::class);
+ /** @var ResponseInterface|MockObject $response */
+ $response = $this->getMockForAbstractClass(ResponseInterface::class);
+
+ $middlewareInterface = $this->getMockForAbstractClass(MiddlewareInterface::class);
+ $requestHandlerInterface = $this->getMockForAbstractClass(RequestHandlerInterface::class);
+
+ $request->expects($this->exactly(3))
+ ->method('getAttribute')
+ ->with('route-request-handler')
+ ->willReturn('FooBarClass');
+
+ /** @var RequestHandler|MockObject $middleware */
+ $middleware = $this->getMockBuilder(RequestHandler::class)
+ ->setConstructorArgs([$container])
+ ->setMethods(['resolveMiddleware'])
+ ->getMock();
+ $middleware->expects($this->exactly(3))
+ ->method('resolveMiddleware')
+ ->with('FooBarClass')
+ ->willReturnOnConsecutiveCalls(
+ $middlewareInterface,
+ $requestHandlerInterface,
+ null
+ );
+
+ $middlewareInterface->expects($this->once())
+ ->method('process')
+ ->with($request, $handler)
+ ->willReturn($response);
+ $requestHandlerInterface->expects($this->once())
+ ->method('handle')
+ ->with($request)
+ ->willReturn($response);
+
+ $return = $middleware->process($request, $handler);
+ $this->assertEquals($return, $response);
+
+ $middleware->process($request, $handler);
+ $this->assertEquals($return, $response);
+
+ $this->expectException(InvalidArgumentException::class);
+ $middleware->process($request, $handler);
+ }
+}