8000 Merge pull request #3246 from nicolas-grekas/sf5 · api-platform/core@7035e9a · GitHub
[go: up one dir, main page]

Skip to content

Commit 7035e9a

Browse files
authored
Merge pull request #3246 from nicolas-grekas/sf5
fix compat with Symfony 5
2 parents 208964b + 980db8a commit 7035e9a

File tree

5 files changed

+35
-7
lines changed

5 files changed

+35
-7
lines changed

phpstan.neon.dist

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ parameters:
8989
path: %currentWorkingDirectory%/src/Bridge/Symfony/Bundle/Test/ApiTestAssertionsTrait.php
9090
-
9191
message: '#Service "api_platform.iri_converter" is private\.#'
92-
path: %currentWorkingDirectory%/src/Bridge/Symfony/Bundle/Test/ApiTestCase.php
92+
path: %currentWorkingDirectory%/src/Bridge/Symfony/Bundle/Test/ApiTestCase.php
9393

9494
# Expected, due to optional interfaces
9595
- '#Method ApiPlatform\\Core\\Bridge\\Doctrine\\Orm\\Extension\\QueryCollectionExtensionInterface::applyToCollection\(\) invoked with 5 parameters, 3-4 required\.#'
@@ -113,11 +113,22 @@ parameters:
113113
-
114114
message: '#Class Symfony\\Component\\ErrorHandler\\ErrorRenderer\\ErrorRendererInterface not found\.#'
115115
path: %currentWorkingDirectory%/tests/Fixtures/app/AppKernel.php
116+
- '#Class Symfony\\Component\\ErrorHandler\\Exception\\FlattenException not found\.#'
117+
-
118+
message: '#Class Symfony\\Component\\HttpKernel\\EventListener\\ErrorListener not found\.#'
119+
path: %currentWorkingDirectory%/src/EventListener/ExceptionListener.php
120+
-
121+
message: '#Instantiated class Symfony\\Component\\HttpKernel\\EventListener\\ErrorListener not found\.#'
122+
path: %currentWorkingDirectory%/src/EventListener/ExceptionListener.php
116123
-
117124
message: '#Parameter \$exception of method ApiPlatform\\Core\\Action\\ExceptionAction::__invoke\(\) has invalid typehint type Symfony\\Component\\ErrorHandler\\Exception\\FlattenException\.#'
118125
path: %currentWorkingDirectory%/src/Action/ExceptionAction.php
119126
- '#Call to method get(Class|Headers|StatusCode)\(\) on an unknown class Symfony\\Component\\ErrorHandler\\Exception\\FlattenException\.#'
120-
- '#Class Symfony\\Component\\ErrorHandler\\Exception\\FlattenException not found\.#'
127+
-
128+
message: "#Call to function method_exists\\(\\) with 'Symfony\\\\\\\\Component.+' and 'getThrowable' will always evaluate to false\\.#"
129+
paths:
130+
- %currentWorkingDirectory%/tests/Bridge/Symfony/Validator/EventListener/ValidationExceptionListenerTest.php
131+
- %currentWorkingDirectory%/tests/EventListener/ExceptionListenerTest.php
121132
-
122133
message: '#Instanceof between bool\|float\|int|null and ArrayObject will always evaluate to false\.#'
123134
paths:

src/Bridge/Symfony/Validator/EventListener/ValidationExceptionListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function __construct(SerializerInterface $serializer, array $errorFormats
4040
*/
4141
public function onKernelException(ExceptionEvent $event): void
4242
{
43-
$exception = $event->getException();
43+
$exception = method_exists($event, 'getThrowable') ? $event->getThrowable() : $event->getException();
4444
if (!$exception instanceof ValidationException) {
4545
return;
4646
}

src/EventListener/ExceptionListener.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use ApiPlatform\Core\Util\RequestAttributesExtractor;
1717
use Psr\Log\LoggerInterface;
1818
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
19+
use Symfony\Component\HttpKernel\EventListener\ErrorListener;
1920
use Symfony\Component\HttpKernel\EventListener\ExceptionListener as BaseExceptionListener;
2021

2122
/**
@@ -30,7 +31,11 @@ final class ExceptionListener
3031

3132
public function __construct($controller, LoggerInterface $logger = null, $debug = false)
3233
{
33-
$this->exceptionListener = new BaseExceptionListener($controller, $logger, $debug);
34+
if (class_exists(ErrorListener::class)) {
35+
$this->exceptionListener = new ErrorListener($controller, $logger, $debug);
36+
} else {
37+
$this->exceptionListener = new BaseExceptionListener($controller, $logger, $debug);
38+
}
3439
}
3540

3641
public function onKernelException(ExceptionEvent $event): void

tests/Bridge/Symfony/Validator/EventListener/ValidationExceptionListenerTest.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ class ValidationExceptionListenerTest extends TestCase
3131
public function testNotValidationException()
3232
{
3333
$eventProphecy = $this->prophesize(ExceptionEvent::class);
34-
$eventProphecy->getException()->willReturn(new \Exception())->shouldBeCalled();
34+
if (method_exists(ExceptionEvent::class, 'getThrowable')) {
35+
$eventProphecy->getThrowable()->willReturn(new \Exception())->shouldBeCalled();
36+
} else {
37+
$eventProphecy->getException()->willReturn(new \Exception())->shouldBeCalled();
38+
}
3539
$eventProphecy->setResponse()->shouldNotBeCalled();
3640

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

4852
$eventProphecy = $this->prophesize(ExceptionEvent::class);
49-
$eventProphecy->getException()->willReturn(new ValidationException($list))->shouldBeCalled();
53+
if (method_exists(ExceptionEvent::class, 'getThrowable')) {
54+
$eventProphecy->getThrowable()->willReturn(new ValidationException($list))->shouldBeCalled();
55+
} else {
56+
$eventProphecy->getException()->willReturn(new ValidationException($list))->shouldBeCalled();
57+
}
5058
$eventProphecy->getRequest()->willReturn(new Request())->shouldBeCalled();
5159
$eventProphecy->setResponse(Argument::allOf(
5260
Argument::type(Response::class),

tests/EventListener/ExceptionListenerTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@ public function testOnKernelException(Request $request)
3636

3737
$eventProphecy = $this->prophesize(ExceptionEvent::class);
3838
$eventProphecy->getRequest()->willReturn($request);
39-
$eventProphecy->getException()->willReturn(new \Exception());
39+
if (method_exists(ExceptionEvent::class, 'getThrowable')) {
40+
$eventProphecy->getThrowable()->willReturn(new \Exception());
41+
} else {
42+
$eventProphecy->getException()->willReturn(new \Exception());
43+
}
4044
$eventProphecy->getKernel()->willReturn($kernel);
4145
$eventProphecy->setResponse(Argument::type(Response::class))->shouldBeCalled();
4246

0 commit comments

Comments
 (0)
0