summaryrefslogtreecommitdiff
path: root/tests/Unit/Middleware/ErrorHandlerTest.php
diff options
context:
space:
mode:
authormsquare <msquare@notrademark.de>2019-07-21 13:32:45 +0200
committerGitHub <noreply@github.com>2019-07-21 13:32:45 +0200
commitd4d4b409b6fd96ca297af323936b8922d45b6eda (patch)
tree6c8efa1a1e429f3965820838796cb4b97cd2df11 /tests/Unit/Middleware/ErrorHandlerTest.php
parentd5bf7fd065a5ea93dea9fd55e6ac225ee062a3db (diff)
parent51a3c6eb44a5dbdf9d7a3cfac678f0d29b0d3eef (diff)
Merge pull request #622 from MyIgel/controllers
AuthController (login, logout), use templating, replaced gettext, input validation
Diffstat (limited to 'tests/Unit/Middleware/ErrorHandlerTest.php')
-rw-r--r--tests/Unit/Middleware/ErrorHandlerTest.php74
1 files changed, 72 insertions, 2 deletions
diff --git a/tests/Unit/Middleware/ErrorHandlerTest.php b/tests/Unit/Middleware/ErrorHandlerTest.php
index 6c37b651..a9fdd71a 100644
--- a/tests/Unit/Middleware/ErrorHandlerTest.php
+++ b/tests/Unit/Middleware/ErrorHandlerTest.php
@@ -2,14 +2,23 @@
namespace Engelsystem\Test\Unit\Middleware;
+use Engelsystem\Application;
use Engelsystem\Http\Exceptions\HttpException;
+use Engelsystem\Http\Exceptions\ValidationException;
+use Engelsystem\Http\Psr7ServiceProvider;
+use Engelsystem\Http\Request;
use Engelsystem\Http\Response;
+use Engelsystem\Http\ResponseServiceProvider;
+use Engelsystem\Http\Validation\Validator;
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 Psr\Http\Server\RequestHandlerInterface;
+use Symfony\Component\HttpFoundation\Session\Session;
+use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
use Twig_LoaderInterface as TwigLoader;
class ErrorHandlerTest extends TestCase
@@ -104,7 +113,7 @@ class ErrorHandlerTest extends TestCase
/**
* @covers \Engelsystem\Middleware\ErrorHandler::process
*/
- public function testProcessException()
+ public function testProcessHttpException()
{
/** @var ServerRequestInterface|MockObject $request */
$request = $this->createMock(ServerRequestInterface::class);
@@ -146,6 +155,67 @@ class ErrorHandlerTest extends TestCase
/**
* @covers \Engelsystem\Middleware\ErrorHandler::process
+ * @covers \Engelsystem\Middleware\ErrorHandler::getPreviousUrl
+ */
+ public function testProcessValidationException()
+ {
+ /** @var TwigLoader|MockObject $twigLoader */
+ $twigLoader = $this->createMock(TwigLoader::class);
+ $handler = $this->getMockForAbstractClass(RequestHandlerInterface::class);
+ $validator = $this->createMock(Validator::class);
+
+ $handler->expects($this->exactly(2))
+ ->method('handle')
+ ->willReturnCallback(function () use ($validator) {
+ throw new ValidationException($validator);
+ });
+
+ $validator->expects($this->exactly(2))
+ ->method('getErrors')
+ ->willReturn(['foo' => ['validation.foo.numeric']]);
+
+ $session = new Session(new MockArraySessionStorage());
+ $session->set('errors', ['validation' => ['foo' => ['validation.foo.required']]]);
+ $request = Request::create(
+ '/foo/bar',
+ 'POST',
+ ['foo' => 'bar', 'password' => 'Test123', 'password_confirmation' => 'Test1234']
+ );
+ $request->setSession($session);
+
+ /** @var Application $app */
+ $app = app();
+ (new ResponseServiceProvider($app))->register();
+ (new Psr7ServiceProvider($app))->register();
+
+ $errorHandler = new ErrorHandler($twigLoader);
+
+ $return = $errorHandler->process($request, $handler);
+
+ $this->assertEquals(302, $return->getStatusCode());
+ $this->assertEquals('/', $return->getHeaderLine('location'));
+ $this->assertEquals([
+ 'errors' => [
+ 'validation' => [
+ 'foo' => [
+ 'validation.foo.required',
+ 'validation.foo.numeric',
+ ],
+ ],
+ ],
+ 'form-data' => [
+ 'foo' => 'bar',
+ ],
+ ], $session->all());
+
+ $request = $request->withAddedHeader('referer', '/foo/batz');
+ $return = $errorHandler->process($request, $handler);
+
+ $this->assertEquals('/foo/batz', $return->getHeaderLine('location'));
+ }
+
+ /**
+ * @covers \Engelsystem\Middleware\ErrorHandler::process
*/
public function testProcessContentTypeSniffer()
{
@@ -153,7 +223,7 @@ class ErrorHandlerTest extends TestCase
$request = $this->createMock(ServerRequestInterface::class);
/** @var TwigLoader|MockObject $twigLoader */
$twigLoader = $this->createMock(TwigLoader::class);
- $response = new Response('<!DOCTYPE html><html><body><h1>Hi!</h1></body></html>', 500);
+ $response = new Response('<!DOCTYPE html><html lang="en"><body><h1>Hi!</h1></body></html>', 500);
$returnResponseHandler = new ReturnResponseMiddlewareHandler($response);
/** @var ErrorHandler|MockObject $errorHandler */