summaryrefslogtreecommitdiff
path: root/tests/Unit
diff options
context:
space:
mode:
Diffstat (limited to 'tests/Unit')
-rw-r--r--tests/Unit/Http/ResponseTest.php37
-rw-r--r--tests/Unit/Middleware/ErrorHandlerTest.php88
-rw-r--r--tests/Unit/Middleware/Stub/ReturnResponseMiddlewareHandler.php10
-rw-r--r--tests/Unit/Renderer/RendererServiceProviderTest.php4
4 files changed, 137 insertions, 2 deletions
diff --git a/tests/Unit/Http/ResponseTest.php b/tests/Unit/Http/ResponseTest.php
index f6c24767..d7dc37c0 100644
--- a/tests/Unit/Http/ResponseTest.php
+++ b/tests/Unit/Http/ResponseTest.php
@@ -3,6 +3,8 @@
namespace Engelsystem\Test\Unit\Http;
use Engelsystem\Http\Response;
+use Engelsystem\Renderer\Renderer;
+use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
@@ -46,4 +48,37 @@ class ResponseTest extends TestCase
$this->assertNotEquals($response, $newResponse);
$this->assertEquals('Lorem Ipsum?', $newResponse->getContent());
}
-}
+
+ /**
+ * @covers \Engelsystem\Http\Response::withView
+ */
+ public function testWithView()
+ {
+ /** @var REnderer|MockObject $renderer */
+ $renderer = $this->createMock(Renderer::class);
+
+ $renderer->expects($this->once())
+ ->method('render')
+ ->with('foo', ['lorem' => 'ipsum'])
+ ->willReturn('Foo ipsum!');
+
+ $response = new Response('', 200, [], $renderer);
+ $newResponse = $response->withView('foo', ['lorem' => 'ipsum'], 505, ['test' => 'er']);
+
+ $this->assertNotEquals($response, $newResponse);
+ $this->assertEquals('Foo ipsum!', $newResponse->getContent());
+ $this->assertEquals(505, $newResponse->getStatusCode());
+ $this->assertArraySubset(['test' => ['er']], $newResponse->getHeaders());
+ }
+
+ /**
+ * @covers \Engelsystem\Http\Response::withView
+ */
+ public function testWithViewNoRenderer()
+ {
+ $this->expectException(\InvalidArgumentException::class);
+
+ $response = new Response();
+ $response->withView('foo');
+ }
+} \ No newline at end of file
diff --git a/tests/Unit/Middleware/ErrorHandlerTest.php b/tests/Unit/Middleware/ErrorHandlerTest.php
new file mode 100644
index 00000000..abf9c52f
--- /dev/null
+++ b/tests/Unit/Middleware/ErrorHandlerTest.php
@@ -0,0 +1,88 @@
+<?php
+
+namespace Engelsystem\Test\Unit\Middleware;
+
+use Engelsystem\Http\Response;
+use Engelsystem\Middleware\ErrorHandler;
+use Engelsystem\Test\Unit\Middleware\Stub\ReturnResponseMiddlewareHandler;
+use PHPUnit\Framework\MockObject\MockObject;
+use PHPUnit\Framework\TestCase;
+use Psr\Http\Message\ResponseInterface;
+use Psr\Http\Message\ServerRequestInterface;
+use Twig_LoaderInterface as TwigLoader;
+
+class ErrorHandlerTest extends TestCase
+{
+ /**
+ * @covers \Engelsystem\Middleware\ErrorHandler::__construct
+ * @covers \Engelsystem\Middleware\ErrorHandler::process
+ * @covers \Engelsystem\Middleware\ErrorHandler::selectView
+ */
+ public function testProcess()
+ {
+ /** @var TwigLoader|MockObject $twigLoader */
+ $twigLoader = $this->createMock(TwigLoader::class);
+ /** @var ServerRequestInterface|MockObject $request */
+ $request = $this->createMock(ServerRequestInterface::class);
+ /** @var ResponseInterface|MockObject $psrResponse */
+ $psrResponse = $this->getMockForAbstractClass(ResponseInterface::class);
+ $returnResponseHandler = new ReturnResponseMiddlewareHandler($psrResponse);
+
+ $psrResponse->expects($this->once())
+ ->method('getStatusCode')
+ ->willReturn(505);
+
+ $errorHandler = new ErrorHandler($twigLoader);
+
+ $return = $errorHandler->process($request, $returnResponseHandler);
+ $this->assertEquals($psrResponse, $return, 'Plain PSR-7 Response should be passed directly');
+
+ /** @var Response|MockObject $response */
+ $response = $this->createMock(Response::class);
+
+ $response->expects($this->exactly(3))
+ ->method('getStatusCode')
+ ->willReturnOnConsecutiveCalls(
+ 200,
+ 418,
+ 505
+ );
+
+ $returnResponseHandler->setResponse($response);
+ $return = $errorHandler->process($request, $returnResponseHandler);
+ $this->assertEquals($response, $return, 'Only Responses >= 400 should be processed');
+
+ $twigLoader->expects($this->exactly(4))
+ ->method('exists')
+ ->withConsecutive(
+ ['errors/418'],
+ ['errors/4'],
+ ['errors/400'],
+ ['errors/505']
+ )
+ ->willReturnOnConsecutiveCalls(
+ false,
+ false,
+ false,
+ true
+ );
+
+ $response->expects($this->exactly(2))
+ ->method('getContent')
+ ->willReturnOnConsecutiveCalls(
+ 'Teapot',
+ 'Internal Error!'
+ );
+
+ $response->expects($this->exactly(2))
+ ->method('withView')
+ ->withConsecutive(
+ ['errors/default', ['status' => 418, 'content' => 'Teapot'], 418],
+ ['errors/505', ['status' => 505, 'content' => 'Internal Error!'], 505]
+ )
+ ->willReturn($response);
+
+ $errorHandler->process($request, $returnResponseHandler);
+ $errorHandler->process($request, $returnResponseHandler);
+ }
+}
diff --git a/tests/Unit/Middleware/Stub/ReturnResponseMiddlewareHandler.php b/tests/Unit/Middleware/Stub/ReturnResponseMiddlewareHandler.php
index 323e07b4..370187dd 100644
--- a/tests/Unit/Middleware/Stub/ReturnResponseMiddlewareHandler.php
+++ b/tests/Unit/Middleware/Stub/ReturnResponseMiddlewareHandler.php
@@ -27,4 +27,14 @@ class ReturnResponseMiddlewareHandler implements RequestHandlerInterface
{
return $this->response;
}
+
+ /**
+ * Set the response
+ *
+ * @param ResponseInterface $response
+ */
+ public function setResponse(ResponseInterface $response)
+ {
+ $this->response = $response;
+ }
}
diff --git a/tests/Unit/Renderer/RendererServiceProviderTest.php b/tests/Unit/Renderer/RendererServiceProviderTest.php
index 3826da7e..6cdf4363 100644
--- a/tests/Unit/Renderer/RendererServiceProviderTest.php
+++ b/tests/Unit/Renderer/RendererServiceProviderTest.php
@@ -37,10 +37,12 @@ class RendererServiceProviderTest extends ServiceProviderTest
$htmlEngine
);
- $app->expects($this->exactly(2))
+ $app->expects($this->exactly(4))
->method('instance')
->withConsecutive(
+ [Renderer::class, $renderer],
['renderer', $renderer],
+ [HtmlEngine::class, $htmlEngine],
['renderer.htmlEngine', $htmlEngine]
);