summaryrefslogtreecommitdiff
path: root/tests/Unit/Middleware
diff options
context:
space:
mode:
authorIgor Scheller <igor.scheller@igorshp.de>2018-09-08 12:48:08 +0200
committerIgor Scheller <igor.scheller@igorshp.de>2018-09-09 12:11:12 +0200
commit222c9fed7d4ca2b3b44c05907fbb7080c1efd342 (patch)
tree62b1bfa9233000985331b9dea703d51dc24515a2 /tests/Unit/Middleware
parent2d6bca1357faff28bc1f86a56b432cc463ff7574 (diff)
parent8257864829ffdfb410f05e0dd0a9c781f48b741a (diff)
Merge remote-tracking branch 'MyIgel/templating'
Diffstat (limited to 'tests/Unit/Middleware')
-rw-r--r--tests/Unit/Middleware/ErrorHandlerTest.php88
-rw-r--r--tests/Unit/Middleware/SetLocaleTest.php71
-rw-r--r--tests/Unit/Middleware/Stub/ReturnResponseMiddlewareHandler.php10
3 files changed, 169 insertions, 0 deletions
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/SetLocaleTest.php b/tests/Unit/Middleware/SetLocaleTest.php
new file mode 100644
index 00000000..c4e9d2a4
--- /dev/null
+++ b/tests/Unit/Middleware/SetLocaleTest.php
@@ -0,0 +1,71 @@
+<?php
+
+namespace Engelsystem\Test\Unit\Middleware;
+
+use Engelsystem\Helpers\Translator;
+use Engelsystem\Middleware\SetLocale;
+use PHPUnit\Framework\TestCase;
+use PHPUnit_Framework_MockObject_MockObject as MockObject;
+use Psr\Http\Message\ResponseInterface;
+use Psr\Http\Message\ServerRequestInterface;
+use Psr\Http\Server\RequestHandlerInterface;
+use Symfony\Component\HttpFoundation\Session\Session;
+
+class SetLocaleTest extends TestCase
+{
+ /**
+ * @covers \Engelsystem\Middleware\SetLocale::__construct
+ * @covers \Engelsystem\Middleware\SetLocale::process
+ */
+ public function testRegister()
+ {
+ /** @var Translator|MockObject $translator */
+ $translator = $this->createMock(Translator::class);
+ /** @var Session|MockObject $session */
+ $session = $this->createMock(Session::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);
+
+ $locale = 'te_ST.UTF8';
+
+ $request->expects($this->exactly(3))
+ ->method('getQueryParams')
+ ->willReturnOnConsecutiveCalls(
+ [],
+ ['set-locale' => 'en_US.UTF8'],
+ ['set-locale' => $locale]
+ );
+
+ $translator->expects($this->exactly(2))
+ ->method('hasLocale')
+ ->withConsecutive(
+ ['en_US.UTF8'],
+ [$locale]
+ )
+ ->willReturnOnConsecutiveCalls(
+ false,
+ true
+ );
+ $translator->expects($this->once())
+ ->method('setLocale')
+ ->with($locale);
+
+ $session->expects($this->once())
+ ->method('set')
+ ->with('locale', $locale);
+
+ $handler->expects($this->exactly(3))
+ ->method('handle')
+ ->with($request)
+ ->willReturn($response);
+
+ $middleware = new SetLocale($translator, $session);
+ $middleware->process($request, $handler);
+ $middleware->process($request, $handler);
+ $middleware->process($request, $handler);
+ }
+}
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;
+ }
}