ErrorHandler: Return 404 on ModelNotFoundException
This commit is contained in:
parent
edf3220824
commit
36da807ca2
|
@ -6,6 +6,7 @@ use Engelsystem\Http\Exceptions\HttpException;
|
|||
use Engelsystem\Http\Exceptions\ValidationException;
|
||||
use Engelsystem\Http\Request;
|
||||
use Engelsystem\Http\Response;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
use Illuminate\Support\Arr;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
|
@ -69,6 +70,8 @@ class ErrorHandler implements MiddlewareInterface
|
|||
if ($request instanceof Request) {
|
||||
$response->withInput(Arr::except($request->request->all(), $this->formIgnore));
|
||||
}
|
||||
} catch (ModelNotFoundException $e) {
|
||||
$response = $this->createResponse('', 404);
|
||||
}
|
||||
|
||||
$statusCode = $response->getStatusCode();
|
||||
|
|
|
@ -13,6 +13,7 @@ use Engelsystem\Http\ResponseServiceProvider;
|
|||
use Engelsystem\Http\Validation\Validator;
|
||||
use Engelsystem\Middleware\ErrorHandler;
|
||||
use Engelsystem\Test\Unit\Middleware\Stub\ReturnResponseMiddlewareHandler;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
@ -219,6 +220,50 @@ class ErrorHandlerTest extends TestCase
|
|||
$this->assertEquals('/foo/batz', $return->getHeaderLine('location'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \Engelsystem\Middleware\ErrorHandler::process
|
||||
*/
|
||||
public function testProcessModelNotFoundException()
|
||||
{
|
||||
/** @var ServerRequestInterface|MockObject $request */
|
||||
$request = $this->createMock(ServerRequestInterface::class);
|
||||
/** @var ResponseInterface|MockObject $psrResponse */
|
||||
$psrResponse = $this->getMockForAbstractClass(ResponseInterface::class);
|
||||
/** @var ReturnResponseMiddlewareHandler|MockObject $returnResponseHandler */
|
||||
$returnResponseHandler = $this->getMockBuilder(ReturnResponseMiddlewareHandler::class)
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
|
||||
$psrResponse->expects($this->once())
|
||||
->method('getStatusCode')
|
||||
->willReturn(404);
|
||||
|
||||
$psrResponse->expects($this->once())
|
||||
->method('getHeader')
|
||||
->with('content-type')
|
||||
->willReturn([]);
|
||||
|
||||
$returnResponseHandler->expects($this->once())
|
||||
->method('handle')
|
||||
->willReturnCallback(function () {
|
||||
throw new ModelNotFoundException('Some model could not be found');
|
||||
});
|
||||
|
||||
/** @var ErrorHandler|MockObject $errorHandler */
|
||||
$errorHandler = $this->getMockBuilder(ErrorHandler::class)
|
||||
->disableOriginalConstructor()
|
||||
->onlyMethods(['createResponse'])
|
||||
->getMock();
|
||||
|
||||
$errorHandler->expects($this->once())
|
||||
->method('createResponse')
|
||||
->with('', 404)
|
||||
->willReturn($psrResponse);
|
||||
|
||||
$return = $errorHandler->process($request, $returnResponseHandler);
|
||||
$this->assertEquals($psrResponse, $return);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \Engelsystem\Middleware\ErrorHandler::process
|
||||
*/
|
||||
|
|
Loading…
Reference in New Issue