Skip to content

fix compat with Symfony 5 #3246

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ parameters:
path: %currentWorkingDirectory%/src/Bridge/Symfony/Bundle/Test/ApiTestAssertionsTrait.php
-
message: '#Service "api_platform.iri_converter" is private\.#'
path: %currentWorkingDirectory%/src/Bridge/Symfony/Bundle/Test/ApiTestCase.php
path: %currentWorkingDirectory%/src/Bridge/Symfony/Bundle/Test/ApiTestCase.php

# Expected, due to optional interfaces
- '#Method ApiPlatform\\Core\\Bridge\\Doctrine\\Orm\\Extension\\QueryCollectionExtensionInterface::applyToCollection\(\) invoked with 5 parameters, 3-4 required\.#'
Expand All @@ -113,11 +113,22 @@ parameters:
-
message: '#Class Symfony\\Component\\ErrorHandler\\ErrorRenderer\\ErrorRendererInterface not found\.#'
path: %currentWorkingDirectory%/tests/Fixtures/app/AppKernel.php
- '#Class Symfony\\Component\\ErrorHandler\\Exception\\FlattenException not found\.#'
-
message: '#Class Symfony\\Component\\HttpKernel\\EventListener\\ErrorListener not found\.#'
path: %currentWorkingDirectory%/src/EventListener/ExceptionListener.php
-
message: '#Instantiated class Symfony\\Component\\HttpKernel\\EventListener\\ErrorListener not found\.#'
path: %currentWorkingDirectory%/src/EventListener/ExceptionListener.php
-
message: '#Parameter \$exception of method ApiPlatform\\Core\\Action\\ExceptionAction::__invoke\(\) has invalid typehint type Symfony\\Component\\ErrorHandler\\Exception\\FlattenException\.#'
path: %currentWorkingDirectory%/src/Action/ExceptionAction.php
- '#Call to method get(Class|Headers|StatusCode)\(\) on an unknown class Symfony\\Component\\ErrorHandler\\Exception\\FlattenException\.#'
- '#Class Symfony\\Component\\ErrorHandler\\Exception\\FlattenException not found\.#'
-
message: "#Call to function method_exists\\(\\) with 'Symfony\\\\\\\\Component.+' and 'getThrowable' will always evaluate to false\\.#"
paths:
- %currentWorkingDirectory%/tests/Bridge/Symfony/Validator/EventListener/ValidationExceptionListenerTest.php
- %currentWorkingDirectory%/tests/EventListener/ExceptionListenerTest.php
-
message: '#Instanceof between bool\|float\|int|null and ArrayObject will always evaluate to false\.#'
paths:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function __construct(SerializerInterface $serializer, array $errorFormats
*/
public function onKernelException(ExceptionEvent $event): void
{
$exception = $event->getException();
$exception = method_exists($event, 'getThrowable') ? $event->getThrowable() : $event->getException();
if (!$exception instanceof ValidationException) {
return;
}
Expand Down
7 changes: 6 additions & 1 deletion src/EventListener/ExceptionListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use ApiPlatform\Core\Util\RequestAttributesExtractor;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\EventListener\ErrorListener;
use Symfony\Component\HttpKernel\EventListener\ExceptionListener as BaseExceptionListener;

/**
Expand All @@ -30,7 +31,11 @@ final class ExceptionListener

public function __construct($controller, LoggerInterface $logger = null, $debug = false)
{
$this->exceptionListener = new BaseExceptionListener($controller, $logger, $debug);
if (class_exists(ErrorListener::class)) {
$this->exceptionListener = new ErrorListener($controller, $logger, $debug);
} else {
$this->exceptionListener = new BaseExceptionListener($controller, $logger, $debug);
}
}

public function onKernelException(ExceptionEvent $event): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ class ValidationExceptionListenerTest extends TestCase
public function testNotValidationException()
{
$eventProphecy = $this->prophesize(ExceptionEvent::class);
$eventProphecy->getException()->willReturn(new \Exception())->shouldBeCalled();
if (method_exists(ExceptionEvent::class, 'getThrowable')) {
$eventProphecy->getThrowable()->willReturn(new \Exception())->shouldBeCalled();
} else {
$eventProphecy->getException()->willReturn(new \Exception())->shouldBeCalled();
}
$eventProphecy->setResponse()->shouldNotBeCalled();

$serializerProphecy = $this->prophesize(SerializerInterface::class);
Expand All @@ -46,7 +50,11 @@ public function testValidationException()
$list = new ConstraintViolationList([]);

$eventProphecy = $this->prophesize(ExceptionEvent::class);
$eventProphecy->getException()->willReturn(new ValidationException($list))->shouldBeCalled();
if (method_exists(ExceptionEvent::class, 'getThrowable')) {
$eventProphecy->getThrowable()->willReturn(new ValidationException($list))->shouldBeCalled();
} else {
$eventProphecy->getException()->willReturn(new ValidationException($list))->shouldBeCalled();
}
$eventProphecy->getRequest()->willReturn(new Request())->shouldBeCalled();
$eventProphecy->setResponse(Argument::allOf(
Argument::type(Response::class),
Expand Down
6 changes: 5 additions & 1 deletion tests/EventListener/ExceptionListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ public function testOnKernelException(Request $request)

$eventProphecy = $this->prophesize(ExceptionEvent::class);
$eventProphecy->getRequest()->willReturn($request);
$eventProphecy->getException()->willReturn(new \Exception());
if (method_exists(ExceptionEvent::class, 'getThrowable')) {
$eventProphecy->getThrowable()->willReturn(new \Exception());
} else {
$eventProphecy->getException()->willReturn(new \Exception());
}
$eventProphecy->getKernel()->willReturn($kernel);
$eventProphecy->setResponse(Argument::type(Response::class))->shouldBeCalled();

Expand Down