Skip to content

Commit

Permalink
Fix not found message for new attribute (#40)
Browse files Browse the repository at this point in the history
* Add preg_match for new message

* Add test

---------

Co-authored-by: Vitalii Kondratiuk <[email protected]>
  • Loading branch information
devvitalii and vitaliikstfalcon authored Feb 14, 2023
1 parent 51c140a commit db2802d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
3 changes: 2 additions & 1 deletion EventListener/Kernel/ApiExceptionFormatterListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ public function __invoke(ExceptionEvent $event): void
$statusCode = $e->getStatusCode();
$errorName = BaseErrorNames::RESOURCE_NOT_FOUND;

if (preg_match('/^(.+) object not found by the @(.+) annotation\.$/', $message)) {
if (preg_match('/^(.+) object not found by the @(.+) annotation\.$/', $message)
|| preg_match('/^(.+) object not found by (.+). The expression (.+) returned null\.$/', $message)) {
$message = 'resource_not_found_exception_message';
}
break;
Expand Down
50 changes: 50 additions & 0 deletions Tests/EventListener/Kernel/ApiExceptionFormatterListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,56 @@ public function testOnKernelExceptionOnPaymentRequired(): void
self::assertInstanceOf(HttpException::class, $exceptionEvent->getThrowable());
}

public function testOnKernelExceptionWhenResourceNotFoundCausedByMapEntityAttribute(): void
{
$exceptionMessage = '"App\\Entity\\Event\\Event\" object not found by \"Symfony\\Bridge\\Doctrine\\ArgumentResolver\\EntityValueResolver\". The expression \"repository.findClosedEventById(id)\" returned null.';
$httpException = new NotFoundHttpException($exceptionMessage);
$resourceNotFoundMessage = 'Resource not found';

$exceptionEvent = new ExceptionEvent(
$this->kernel,
$this->request,
HttpKernelInterface::MAIN_REQUEST,
$httpException
);

$this->request
->expects(self::once())
->method('getHost')
->willReturn(self::API_HOST)
;

$this->translator
->expects(self::once())
->method('trans')
->with('resource_not_found_exception_message')
->willReturn($resourceNotFoundMessage)
;

$this->serializer
->expects(self::once())
->method('serialize')
->willReturn(sprintf('{"error":"resource_not_found", "error_description":"%s"}', $resourceNotFoundMessage))
;

$this->exceptionResponseProcessor
->expects(self::never())
->method('processResponseForException')
;

$json = '{"error":"resource_not_found", "error_description":"Resource not found"}';
$this->exceptionResponseFactory
->expects(self::once())
->method('createJsonResponse')
->with($json, Response::HTTP_NOT_FOUND)
->willReturn($this->response)
;

$this->exceptionFormatterListener->__invoke($exceptionEvent);

self::assertInstanceOf(NotFoundHttpException::class, $exceptionEvent->getThrowable());
}

public function testOnKernelExceptionWhenGenericNotFoundHttpException(): void
{
$exceptionMessage = 'Exception test message';
Expand Down

0 comments on commit db2802d

Please sign in to comment.