Skip to content

Commit

Permalink
add tests for multiple error exception handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
l0gicgate committed Jan 5, 2020
1 parent 6a45e0b commit 6537a86
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions tests/Middleware/ErrorMiddlewareTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Slim Framework (https://slimframework.com)
*
Expand Down Expand Up @@ -204,6 +205,45 @@ public function testSuperclassExceptionHandlerDoesNotHandleSubclassException()
$this->expectOutputString('Oops..');
}

public function testHandleMultipleExceptionsAddedAsArray()
{
$responseFactory = $this->getResponseFactory();
$app = new App($responseFactory);
$callableResolver = $app->getCallableResolver();

$mw = new ErrorMiddleware($callableResolver, $this->getResponseFactory(), false, false, false);

$app->add(function ($request, $handler) {
throw new InvalidArgumentException('This is an invalid argument exception...');
});

$handler = (function (ServerRequestInterface $request, $exception) {
$response = $this->createResponse();
$response->getBody()->write($exception->getMessage());
return $response;
});

$mw->setErrorHandler([LogicException::class, InvalidArgumentException::class], $handler->bindTo($this));

$mw->setDefaultErrorHandler((function () {
$response = $this->createResponse();
$response->getBody()->write('Oops..');
return $response;
})->bindTo($this));

$app->add($mw);

$app->get('/foo', function (ServerRequestInterface $request, ResponseInterface $response) {
$response->getBody()->write('...');
return $response;
});

$request = $this->createServerRequest('/foo');
$app->run($request);

$this->expectOutputString('This is an invalid argument exception...');
}

public function testErrorHandlerHandlesThrowables()
{
$responseFactory = $this->getResponseFactory();
Expand Down

0 comments on commit 6537a86

Please sign in to comment.