[Messenger] Add method HandlerFailedException::getNestedExceptionOfClass

To know if specific exception are the origin of messenger failure
This commit is contained in:
Timothée BARRAY 2019-11-29 10:29:20 +01:00 committed by Timothée Barray
parent 1de42a5f08
commit e0dd84b426
4 changed files with 73 additions and 0 deletions

View File

@ -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);
}
)
);
}
}

View File

@ -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));
}
}

View File

@ -0,0 +1,16 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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
{
}

View File

@ -0,0 +1,16 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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
{
}