Skip to content

Commit 187ffc6

Browse files
authored
Merge pull request #2930 from teohhanhui/fix/messenger-handler-failed-exception
Unwrap exception thrown in handler for Symfony Messenger 4.3
2 parents 8d0bd66 + 10a5b59 commit 187ffc6

30 files changed

+109
-58
lines changed

.circleci/config.yml

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ commands:
3939
command: |
4040
sudo pecl install mongodb-1.5.4
4141
sudo docker-php-ext-enable mongodb
42+
install-pcov-php-extension:
43+
steps:
44+
- run:
45+
name: Install pcov PHP extension
46+
command: |
47+
sudo pecl install pcov-1.0.6
48+
sudo docker-php-ext-enable pcov
4249
merge-code-coverage-reports:
4350
parameters:
4451
dir:
@@ -51,14 +58,16 @@ commands:
5158
- run:
5259
name: Download phpcov
5360
command: |
54-
if [ ! -e phpcov.phar ]; then
61+
if ! command -v phpcov; then
5562
wget https://phar.phpunit.de/phpcov.phar
63+
chmod +x phpcov.phar
64+
sudo mv phpcov.phar /usr/local/bin/phpcov
5665
fi
5766
- run:
5867
name: Merge code coverage reports
5968
command: |
6069
mkdir -p "$(dirname -- "<< parameters.out >>")"
61-
phpdbg -qrr phpcov.phar merge --clover "<< parameters.out >>" "<< parameters.dir >>"
70+
phpcov merge --clover "<< parameters.out >>" "<< parameters.dir >>"
6271
merge-test-reports:
6372
parameters:
6473
dir:
@@ -263,6 +272,7 @@ jobs:
263272
steps:
264273
- checkout
265274
- install-mongodb-php-extension
275+
- install-pcov-php-extension
266276
- disable-xdebug-php-extension
267277
- disable-php-memory-limit
268278
- update-composer
@@ -276,7 +286,7 @@ jobs:
276286
mkdir -p build/logs/phpunit build/coverage
277287
split_tests=$(find tests -name '*Test.php' | circleci tests split --split-by=timings)
278288
export PARALLEL='-j10% --joblog build/logs/parallel.log --rpl {_}\ s/\\//_/g;'
279-
phpunit_cmd='phpdbg -qrr vendor/bin/phpunit --coverage-php build/coverage/coverage-{_}.cov --log-junit build/logs/phpunit/{_}.xml --exclude-group mongodb,resource-hog --colors=always {}'
289+
phpunit_cmd='vendor/bin/phpunit --coverage-php build/coverage/coverage-{_}.cov --log-junit build/logs/phpunit/{_}.xml --exclude-group mongodb,resource-hog --colors=always {}'
280290
echo "$split_tests" | parallel "$phpunit_cmd" || echo "$split_tests" | parallel --retry-failed "$phpunit_cmd"
281291
- restore-npm-cache
282292
- merge-test-reports:
@@ -310,6 +320,7 @@ jobs:
310320
steps:
311321
- checkout
312322
- install-mongodb-php-extension
323+
- install-pcov-php-extension
313324
- disable-xdebug-php-extension
314325
- disable-php-memory-limit
315326
- update-composer
@@ -323,7 +334,7 @@ jobs:
323334
mkdir -p build/logs/behat build/coverage
324335
for f in $(find features -name '*.feature' -not -path 'features/main/exposed_state.feature' -not -path 'features/elasticsearch/*' -not -path 'features/mongodb/*' | circleci tests split --split-by=timings); do
325336
_f=$(echo "$f" | tr / _)
326-
FEATURE="${_f}" phpdbg -qrr vendor/bin/behat --format=progress --out=std --format=junit --out=build/logs/behat/"${_f}" --profile=default-coverage --no-interaction "$f"
337+
FEATURE="${_f}" vendor/bin/behat --format=progress --out=std --format=junit --out=build/logs/behat/"${_f}" --profile=default-coverage --no-interaction "$f"
327338
done
328339
- restore-npm-cache
329340
- merge-test-reports:
@@ -355,6 +366,7 @@ jobs:
355366
steps:
356367
- checkout
357368
- install-mongodb-php-extension
369+
- install-pcov-php-extension
358370
- disable-xdebug-php-extension
359371
- disable-php-memory-limit
360372
- update-composer
@@ -367,7 +379,7 @@ jobs:
367379
name: Run PHPUnit tests
368380
command: |
369381
mkdir -p build/logs/phpunit
370-
phpdbg -qrr vendor/bin/phpunit --coverage-clover build/logs/clover.xml --log-junit build/logs/phpunit/junit.xml --exclude-group resource-hog --colors=always --configuration phpunit_mongodb.xml
382+
vendor/bin/phpunit --coverage-clover build/logs/clover.xml --log-junit build/logs/phpunit/junit.xml --exclude-group resource-hog --colors=always --configuration phpunit_mongodb.xml
371383
- store_test_results:
372384
path: build/logs
373385
- store_artifacts:
@@ -392,6 +404,7 @@ jobs:
392404
steps:
393405
- checkout
394406
- install-mongodb-php-extension
407+
- install-pcov-php-extension
395408
- disable-xdebug-php-extension
396409
- disable-php-memory-limit
397410
- update-composer
@@ -404,7 +417,7 @@ jobs:
404417
name: Run Behat tests
405418
command: |
406419
mkdir -p build/logs/behat
407-
phpdbg -qrr vendor/bin/behat --format=progress --out=std --format=junit --out=build/logs/behat --profile=mongodb-coverage --no-interaction
420+
vendor/bin/behat --format=progress --out=std --format=junit --out=build/logs/behat --profile=mongodb-coverage --no-interaction
408421
- restore-npm-cache
409422
- merge-test-reports:
410423
dir: build/logs/behat
@@ -435,6 +448,7 @@ jobs:
435448
steps:
436449
- checkout
437450
- install-mongodb-php-extension
451+
- install-pcov-php-extension
438452
- disable-xdebug-php-extension
439453
- disable-php-memory-limit
440454
- update-composer
@@ -447,7 +461,7 @@ jobs:
447461
name: Run Behat tests
448462
command: |
449463
mkdir -p build/logs/behat
450-
phpdbg -qrr vendor/bin/behat --format=progress --out=std --format=junit --out=build/logs/behat --profile=elasticsearch-coverage --no-interaction
464+
vendor/bin/behat --format=progress --out=std --format=junit --out=build/logs/behat --profile=elasticsearch-coverage --no-interaction
451465
- restore-npm-cache
452466
- merge-test-reports:
453467
dir: build/logs/behat

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/.php_cs
22
/.php_cs.cache
3+
/.phpunit.result.cache
34
/build/
45
/composer.lock
56
/composer.phar

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
"phpstan/phpstan-doctrine": "^0.11",
5454
"phpstan/phpstan-phpunit": "^0.11",
5555
"phpstan/phpstan-symfony": "^0.11",
56-
"phpunit/phpunit": "^7.5.2",
56+
"phpunit/phpunit": "^7.5.2 || ^8.0",
5757
"psr/log": "^1.0",
5858
"ramsey/uuid": "^3.7",
5959
"ramsey/uuid-doctrine": "^1.4",

features/json/input_output.feature

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Feature: JSON DTO input and output
77
Given I add "Accept" header equal to "application/json"
88
And I add "Content-Type" header equal to "application/json"
99

10-
Scenario: Messenger handler returning output object
10+
Scenario: Request a password reset
1111
And I send a "POST" request to "/users/password_reset_request" with body:
1212
"""
1313
{
@@ -23,3 +23,15 @@ Feature: JSON DTO input and output
2323
"emailSentAt": "2019-07-05T15:44:00+00:00"
2424
}
2525
"""
26+
27+
Scenario: Request a password reset for a non-existent user
28+
And I send a "POST" request to "/users/password_reset_request" with body:
29+
"""
30+
{
31+
"email": "[email protected]"
32+
}
33+
"""
34+
Then the response status code should be 404
35+
And the response should be in JSON
36+
And the header "Content-Type" should be equal to "application/problem+json; charset=utf-8"
37+
And the JSON node "detail" should be equal to "User does not exist."

src/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
4545
use Symfony\Component\Finder\Finder;
4646
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
47-
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;
47+
use Symfony\Component\Messenger\Exception\HandlerFailedException;
4848
use Symfony\Component\Messenger\MessageBusInterface;
4949
use Symfony\Component\Validator\Validator\ValidatorInterface;
5050
use Symfony\Component\Yaml\Yaml;
@@ -526,7 +526,7 @@ private function registerMercureConfiguration(ContainerBuilder $container, array
526526
$loader->load('doctrine_orm_mercure_publisher.xml');
527527

528528
// BC for Symfony Messenger 4.2
529-
if (interface_exists(MessageBusInterface::class) && !class_exists(UnrecoverableMessageHandlingException::class)) {
529+
if (interface_exists(MessageBusInterface::class) && !class_exists(HandlerFailedException::class)) {
530530
$container->getDefinition('api_platform.doctrine.listener.mercure.publish')->replaceArgument(5, new Reference('message_bus', ContainerInterface::IGNORE_ON_INVALID_REFERENCE));
531531
}
532532
}
@@ -541,7 +541,7 @@ private function registerMessengerConfiguration(ContainerBuilder $container, arr
541541
$loader->load('messenger.xml');
542542

543543
// BC for Symfony Messenger 4.2
544-
if (interface_exists(MessageBusInterface::class) && !class_exists(UnrecoverableMessageHandlingException::class)) {
544+
if (interface_exists(MessageBusInterface::class) && !class_exists(HandlerFailedException::class)) {
545545
$container->setAlias('api_platform.message_bus', 'message_bus');
546546
}
547547
}

src/EventListener/ExceptionListener.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Psr\Log\LoggerInterface;
1818
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
1919
use Symfony\Component\HttpKernel\EventListener\ExceptionListener as BaseExceptionListener;
20+
use Symfony\Component\Messenger\Exception\HandlerFailedException;
2021

2122
/**
2223
* Handles requests errors.
@@ -44,6 +45,18 @@ public function onKernelException(GetResponseForExceptionEvent $event): void
4445
return;
4546
}
4647

48+
// unwrap the exception thrown in handler for Symfony Messenger >= 4.3
49+
$exception = $event->getException();
50+
if ($exception instanceof HandlerFailedException) {
51+
/** @var \Throwable $previousException */
52+
$previousException = $exception->getPrevious();
53+
if (!$previousException instanceof \Exception) {
54+
throw $previousException;
55+
}
56+
57+
$event->setException($previousException);
58+
}
59+
4760
$this->exceptionListener->onKernelException($event);
4861
}
4962
}

src/Test/DoctrineMongoDbOdmFilterTestCase.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
2222

2323
/**
24+
* @internal
25+
*
2426
* @author Alan Poulain <[email protected]>
2527
*/
2628
abstract class DoctrineMongoDbOdmFilterTestCase extends KernelTestCase
@@ -45,7 +47,7 @@ abstract class DoctrineMongoDbOdmFilterTestCase extends KernelTestCase
4547
*/
4648
protected $filterClass;
4749

48-
protected function setUp()
50+
protected function setUp(): void
4951
{
5052
self::bootKernel();
5153

src/Test/DoctrineMongoDbOdmSetup.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
/**
2323
* Convenience class for setting up Doctrine from different installations and configurations.
2424
*
25+
* @internal
26+
*
2527
* @author Benjamin Eberlei <[email protected]>
2628
* @author Alan Poulain <[email protected]>
2729
*/

src/Test/DoctrineOrmFilterTestCase.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
use Symfony\Component\HttpFoundation\RequestStack;
2525

2626
/**
27+
* @internal
28+
*
2729
* @author Kévin Dunglas <[email protected]>
2830
*/
2931
abstract class DoctrineOrmFilterTestCase extends KernelTestCase
@@ -53,7 +55,7 @@ abstract class DoctrineOrmFilterTestCase extends KernelTestCase
5355
*/
5456
protected $filterClass;
5557

56-
protected function setUp()
58+
protected function setUp(): void
5759
{
5860
self::bootKernel();
5961

tests/Bridge/Doctrine/MongoDbOdm/Extension/PaginationExtensionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class PaginationExtensionTest extends TestCase
4343
{
4444
private $managerRegistryProphecy;
4545

46-
protected function setUp()
46+
protected function setUp(): void
4747
{
4848
parent::setUp();
4949

0 commit comments

Comments
 (0)