summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/Unit/Controllers/CreditsControllerTest.php28
-rw-r--r--tests/Unit/Middleware/RequestHandlerTest.php77
2 files changed, 98 insertions, 7 deletions
diff --git a/tests/Unit/Controllers/CreditsControllerTest.php b/tests/Unit/Controllers/CreditsControllerTest.php
new file mode 100644
index 00000000..6f0200f2
--- /dev/null
+++ b/tests/Unit/Controllers/CreditsControllerTest.php
@@ -0,0 +1,28 @@
+<?php
+
+namespace Unit\Controllers;
+
+use Engelsystem\Controllers\CreditsController;
+use Engelsystem\Http\Response;
+use PHPUnit\Framework\MockObject\MockObject;
+use PHPUnit\Framework\TestCase;
+
+class CreditsControllerTest extends TestCase
+{
+ /**
+ * @covers \Engelsystem\Controllers\CreditsController::__construct
+ * @covers \Engelsystem\Controllers\CreditsController::index
+ */
+ public function testIndex()
+ {
+ /** @var Response|MockObject $response */
+ $response = $this->createMock(Response::class);
+
+ $response->expects($this->once())
+ ->method('withView')
+ ->with('pages/credits.twig');
+
+ $controller = new CreditsController($response);
+ $controller->index();
+ }
+}
diff --git a/tests/Unit/Middleware/RequestHandlerTest.php b/tests/Unit/Middleware/RequestHandlerTest.php
index 896b55c3..b1ffbd33 100644
--- a/tests/Unit/Middleware/RequestHandlerTest.php
+++ b/tests/Unit/Middleware/RequestHandlerTest.php
@@ -38,15 +38,12 @@ class RequestHandlerTest extends TestCase
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);
+ /** @var MiddlewareInterface|MockObject $middlewareInterface */
+ list($container, $request, $handler, $response, $middlewareInterface) = $this->getMocks();
- $middlewareInterface = $this->getMockForAbstractClass(MiddlewareInterface::class);
$requestHandlerInterface = $this->getMockForAbstractClass(RequestHandlerInterface::class);
$request->expects($this->exactly(3))
@@ -57,10 +54,10 @@ class RequestHandlerTest extends TestCase
/** @var RequestHandler|MockObject $middleware */
$middleware = $this->getMockBuilder(RequestHandler::class)
->setConstructorArgs([$container])
- ->setMethods(['resolveMiddleware'])
+ ->setMethods(['resolveRequestHandler'])
->getMock();
$middleware->expects($this->exactly(3))
- ->method('resolveMiddleware')
+ ->method('resolveRequestHandler')
->with('FooBarClass')
->willReturnOnConsecutiveCalls(
$middlewareInterface,
@@ -86,4 +83,70 @@ class RequestHandlerTest extends TestCase
$this->expectException(InvalidArgumentException::class);
$middleware->process($request, $handler);
}
+
+ /**
+ * @covers \Engelsystem\Middleware\RequestHandler::resolveRequestHandler
+ */
+ public function testResolveRequestHandler()
+ {
+ /** @var Application|MockObject $container */
+ /** @var ServerRequestInterface|MockObject $request */
+ /** @var RequestHandlerInterface|MockObject $handler */
+ /** @var ResponseInterface|MockObject $response */
+ /** @var MiddlewareInterface|MockObject $middlewareInterface */
+ list($container, $request, $handler, $response, $middlewareInterface) = $this->getMocks();
+
+ $className = 'Engelsystem\\Controllers\\FooBarTestController';
+
+ $request->expects($this->exactly(1))
+ ->method('getAttribute')
+ ->with('route-request-handler')
+ ->willReturn('FooBarTestController@showStuff');
+
+ /** @var RequestHandler|MockObject $middleware */
+ $middleware = $this->getMockBuilder(RequestHandler::class)
+ ->setConstructorArgs([$container])
+ ->setMethods(['resolveMiddleware'])
+ ->getMock();
+ $middleware->expects($this->once())
+ ->method('resolveMiddleware')
+ ->with([$middlewareInterface, 'showStuff'])
+ ->willReturn($middlewareInterface);
+
+ $middlewareInterface->expects($this->once())
+ ->method('process')
+ ->with($request, $handler)
+ ->willReturn($response);
+
+ $container->expects($this->exactly(2))
+ ->method('has')
+ ->withConsecutive(['FooBarTestController'], [$className])
+ ->willReturnOnConsecutiveCalls(false, true);
+ $container->expects($this->once())
+ ->method('make')
+ ->with($className)
+ ->willReturn($middlewareInterface);
+
+ $return = $middleware->process($request, $handler);
+ $this->assertEquals($return, $response);
+ }
+
+ /**
+ * @return array
+ */
+ protected function getMocks(): array
+ {
+ /** @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);
+ /** @var MiddlewareInterface $middlewareInterface */
+ $middlewareInterface = $this->getMockForAbstractClass(MiddlewareInterface::class);
+
+ return array($container, $request, $handler, $response, $middlewareInterface);
+ }
}