Skip to content

[Messenger][DoctrineBridge] Throws UnrecoverableMessageHandlingException when passed invalid entity manager name #31482

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
namespace Symfony\Bridge\Doctrine\Messenger;

use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
use Symfony\Component\Messenger\Middleware\StackInterface;

Expand All @@ -40,10 +40,10 @@ public function __construct(ManagerRegistry $managerRegistry, string $entityMana
*/
public function handle(Envelope $envelope, StackInterface $stack): Envelope
{
$entityManager = $this->managerRegistry->getManager($this->entityManagerName);

if (!$entityManager instanceof EntityManagerInterface) {
throw new \InvalidArgumentException(sprintf('The ObjectManager with name "%s" must be an instance of EntityManagerInterface', $this->entityManagerName));
try {
$entityManager = $this->managerRegistry->getManager($this->entityManagerName);
} catch (\InvalidArgumentException $e) {
throw new UnrecoverableMessageHandlingException($e->getMessage(), 0, $e);
}

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
namespace Symfony\Bridge\Doctrine\Messenger;

use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
use Symfony\Component\Messenger\Middleware\StackInterface;

Expand All @@ -40,10 +40,10 @@ public function __construct(ManagerRegistry $managerRegistry, string $entityMana
*/
public function handle(Envelope $envelope, StackInterface $stack): Envelope
{
$entityManager = $this->managerRegistry->getManager($this->entityManagerName);

if (!$entityManager instanceof EntityManagerInterface) {
throw new \InvalidArgumentException(sprintf('The ObjectManager with name "%s" must be an instance of EntityManagerInterface', $this->entityManagerName));
try {
$entityManager = $this->managerRegistry->getManager($this->entityManagerName);
} catch (\InvalidArgumentException $e) {
throw new UnrecoverableMessageHandlingException($e->getMessage(), 0, $e);
}

$connection = $entityManager->getConnection();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
namespace Symfony\Bridge\Doctrine\Messenger;

use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
use Symfony\Component\Messenger\Middleware\StackInterface;

Expand All @@ -40,10 +40,10 @@ public function __construct(ManagerRegistry $managerRegistry, string $entityMana
*/
public function handle(Envelope $envelope, StackInterface $stack): Envelope
{
$entityManager = $this->managerRegistry->getManager($this->entityManagerName);

if (!$entityManager instanceof EntityManagerInterface) {
throw new \InvalidArgumentException(sprintf('The ObjectManager with name "%s" must be an instance of EntityManagerInterface', $this->entityManagerName));
try {
$entityManager = $this->managerRegistry->getManager($this->entityManagerName);
} catch (\InvalidArgumentException $e) {
throw new UnrecoverableMessageHandlingException($e->getMessage(), 0, $e);
}

$entityManager->getConnection()->beginTransaction();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bridge\Doctrine\Messenger\DoctrineCloseConnectionMiddleware;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;
use Symfony\Component\Messenger\Test\Middleware\MiddlewareTestCase;

class DoctrineCloseConnectionMiddlewareTest extends MiddlewareTestCase
Expand Down Expand Up @@ -50,4 +51,19 @@ public function testMiddlewareCloseConnection()

$this->middleware->handle(new Envelope(new \stdClass()), $this->getStackMock());
}

public function testInvalidEntityManagerThrowsException()
{
$managerRegistry = $this->createMock(ManagerRegistry::class);
$managerRegistry
->method('getManager')
->with('unknown_manager')
->will($this->throwException(new \InvalidArgumentException()));

$middleware = new DoctrineCloseConnectionMiddleware($managerRegistry, 'unknown_manager');

$this->expectException(UnrecoverableMessageHandlingException::class);

$middleware->handle(new Envelope(new \stdClass()), $this->getStackMock(false));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bridge\Doctrine\Messenger\DoctrinePingConnectionMiddleware;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;
use Symfony\Component\Messenger\Test\Middleware\MiddlewareTestCase;

class DoctrinePingConnectionMiddlewareTest extends MiddlewareTestCase
Expand Down Expand Up @@ -71,4 +72,19 @@ public function testMiddlewarePingResetEntityManager()

$this->middleware->handle(new Envelope(new \stdClass()), $this->getStackMock());
}

public function testInvalidEntityManagerThrowsException()
{
$managerRegistry = $this->createMock(ManagerRegistry::class);
$managerRegistry
->method('getManager')
->with('unknown_manager')
->will($this->throwException(new \InvalidArgumentException()));

$middleware = new DoctrinePingConnectionMiddleware($managerRegistry, 'unknown_manager');

$this->expectException(UnrecoverableMessageHandlingException::class);

$middleware->handle(new Envelope(new \stdClass()), $this->getStackMock(false));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bridge\Doctrine\Messenger\DoctrineTransactionMiddleware;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;
use Symfony\Component\Messenger\Test\Middleware\MiddlewareTestCase;

class DoctrineTransactionMiddlewareTest extends MiddlewareTestCase
Expand Down Expand Up @@ -67,4 +68,19 @@ public function testTransactionIsRolledBackOnException()

$this->middleware->handle(new Envelope(new \stdClass()), $this->getThrowingStackMock());
}

public function testInvalidEntityManagerThrowsException()
{
$managerRegistry = $this->createMock(ManagerRegistry::class);
$managerRegistry
->method('getManager')
->with('unknown_manager')
->will($this->throwException(new \InvalidArgumentException()));

$middleware = new DoctrineTransactionMiddleware($managerRegistry, 'unknown_manager');

$this->expectException(UnrecoverableMessageHandlingException::class);

$middleware->handle(new Envelope(new \stdClass()), $this->getStackMock(false));
}
}
4 changes: 2 additions & 2 deletions src/Symfony/Bridge/Doctrine/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"symfony/dependency-injection": "~3.4|~4.0",
"symfony/form": "~4.3",
"symfony/http-kernel": "~3.4|~4.0",
"symfony/messenger": "~4.2",
"symfony/messenger": "~4.3",
"symfony/property-access": "~3.4|~4.0",
"symfony/property-info": "~3.4|~4.0",
"symfony/proxy-manager-bridge": "~3.4|~4.0",
Expand All @@ -49,7 +49,7 @@
"phpunit/phpunit": "<4.8.35|<5.4.3,>=5.0",
"symfony/dependency-injection": "<3.4",
"symfony/form": "<4.3",
"symfony/messenger": "<4.2"
"symfony/messenger": "<4.3"
},
"suggest": {
"symfony/form": "",
Expand Down