diff --git a/src/Symfony/Component/Messenger/Exception/HandlerFailedException.php b/src/Symfony/Component/Messenger/Exception/HandlerFailedException.php index 50172c38bd..e577acd4bd 100644 --- a/src/Symfony/Component/Messenger/Exception/HandlerFailedException.php +++ b/src/Symfony/Component/Messenger/Exception/HandlerFailedException.php @@ -49,4 +49,16 @@ class HandlerFailedException extends RuntimeException { return $this->exceptions; } + + public function getNestedExceptionOfClass(string $exceptionClassName): array + { + return array_values( + array_filter( + $this->exceptions, + function ($exception) use ($exceptionClassName) { + return is_a($exception, $exceptionClassName); + } + ) + ); + } } diff --git a/src/Symfony/Component/Messenger/Tests/Exception/HandlerFailedExceptionTest.php b/src/Symfony/Component/Messenger/Tests/Exception/HandlerFailedExceptionTest.php index e007c517ee..5aa700d1ae 100644 --- a/src/Symfony/Component/Messenger/Tests/Exception/HandlerFailedExceptionTest.php +++ b/src/Symfony/Component/Messenger/Tests/Exception/HandlerFailedExceptionTest.php @@ -5,6 +5,8 @@ namespace Symfony\Component\Messenger\Tests\Exception; use PHPUnit\Framework\TestCase; use Symfony\Component\Messenger\Envelope; use Symfony\Component\Messenger\Exception\HandlerFailedException; +use Symfony\Component\Messenger\Tests\Fixtures\MyOwnChildException; +use Symfony\Component\Messenger\Tests\Fixtures\MyOwnException; class HandlerFailedExceptionTest extends TestCase { @@ -28,4 +30,31 @@ class HandlerFailedExceptionTest extends TestCase $this->assertIsString($originalException->getCode(), 'Original exception code still with original type (string)'); $this->assertSame($exception->getCode(), $originalException->getCode(), 'Original exception code is not modified'); } + + public function testThatNestedExceptionClassAreFound() + { + $envelope = new Envelope(new \stdClass()); + $exception = new MyOwnException(); + + $handlerException = new HandlerFailedException($envelope, [new \LogicException(), $exception]); + $this->assertSame([$exception], $handlerException->getNestedExceptionOfClass(MyOwnException::class)); + } + + public function testThatNestedExceptionClassAreFoundWhenUsingChildException() + { + $envelope = new Envelope(new \stdClass()); + $exception = new MyOwnChildException(); + + $handlerException = new HandlerFailedException($envelope, [$exception]); + $this->assertSame([$exception], $handlerException->getNestedExceptionOfClass(MyOwnException::class)); + } + + public function testThatNestedExceptionClassAreNotFoundIfNotPresent() + { + $envelope = new Envelope(new \stdClass()); + $exception = new \LogicException(); + + $handlerException = new HandlerFailedException($envelope, [$exception]); + $this->assertCount(0, $handlerException->getNestedExceptionOfClass(MyOwnException::class)); + } } diff --git a/src/Symfony/Component/Messenger/Tests/Fixtures/MyOwnChildException.php b/src/Symfony/Component/Messenger/Tests/Fixtures/MyOwnChildException.php new file mode 100644 index 0000000000..2c361db694 --- /dev/null +++ b/src/Symfony/Component/Messenger/Tests/Fixtures/MyOwnChildException.php @@ -0,0 +1,16 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Messenger\Tests\Fixtures; + +class MyOwnChildException extends MyOwnException +{ +} diff --git a/src/Symfony/Component/Messenger/Tests/Fixtures/MyOwnException.php b/src/Symfony/Component/Messenger/Tests/Fixtures/MyOwnException.php new file mode 100755 index 0000000000..2a1c64c866 --- /dev/null +++ b/src/Symfony/Component/Messenger/Tests/Fixtures/MyOwnException.php @@ -0,0 +1,16 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Messenger\Tests\Fixtures; + +class MyOwnException extends \Exception +{ +}