8000 [Messenger] Add method HandlerFailedException::getNestedExceptionOfClass · symfony/symfony@e0dd84b · GitHub
[go: up one dir, main page]

Skip to content

Commit e0dd84b

Browse files
Timothée BARRAYtyx
authored andcommitted
[Messenger] Add method HandlerFailedException::getNestedExceptionOfClass
To know if specific exception are the origin of messenger failure
1 parent 1de42a5 commit e0dd84b

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed

src/Symfony/Component/Messenger/Exception/HandlerFailedException.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,16 @@ public function getNestedExceptions(): array
4949
{
5050
return $this->exceptions;
5151
}
52+
53+
public function getNestedExceptionOfClass(string $exceptionClassName): array
54+
{
55+
return array_values(
56+
array_filter(
57+
$this->exceptions,
58+
function ($exception) use ($exceptionClassName) {
59+
return is_a($exception, $exceptionClassName);
60+
}
61+
)
62+
);
63+
}
5264
}

src/Symfony/Component/Messenger/Tests/Exception/HandlerFailedExceptionTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use PHPUnit\Framework\TestCase;
66
use Symfony\Component\Messenger\Envelope;
77
use Symfony\Component\Messenger\Exception\HandlerFailedException;
8+
use Symfony\Component\Messenger\Tests\Fixtures\MyOwnChildException;
9+
use Symfony\Component\Messenger\Tests\Fixtures\MyOwnException;
810

911
class HandlerFailedExceptionTest extends TestCase
1012
{
@@ -28,4 +30,31 @@ public function __construct()
2830
$this->assertIsString($originalException->getCode(), 'Original exception code still with original type (string)');
2931
$this->assertSame($exception->getCode(), $originalException->getCode(), 'Original exception code is not modified');
3032
}
33+
34+
public function testThatNestedExceptionClassAreFound()
35+
{
36+
$envelope = new Envelope(new \stdClass());
37+
$exception = new MyOwnException();
38+
39+
$handlerException = new HandlerFailedException($envelope, [new \LogicException(), $exception]);
40+
$this->assertSame([$exception], $handlerException->getNestedExceptionOfClass(MyOwnException::class));
41+
}
42+
43+
public function testThatNestedExceptionClassAreFoundWhenUsingChildException()
44+
{
45+
$envelope = new Envelope(new \stdClass());
46+
$exception = new MyOwnChildException();
47+
48+
$handlerException = new HandlerFailedException($envelope, [$exception]);
49+
$this->assertSame([$exception], $handlerException->getNestedExceptionOfClass(MyOwnException::class));
50+
}
51+
52+
public function testThatNestedExceptionClassAreNotFoundIfNotPresent()
53+
{
54+
$envelope = new Envelope(new \stdClass());
55+
$exception = new \LogicException();
56+
57+
$handlerException = new HandlerFailedException($envelope, [$exception]);
58+
$this->assertCount(0, $handlerException->getNestedExceptionOfClass(MyOwnException::class));
59+
}
3160
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Messenger\Tests\Fixtures;
13+
14+
class MyOwnChildException extends MyOwnException
15+
{
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Messenger\Tests\Fixtures;
13+
14+
class MyOwnException extends \Exception
15+
{
16+
}

0 commit comments

Comments
 (0)
0