8000 bug #31482 [Messenger][DoctrineBridge] Throws UnrecoverableMessageHan… · symfony/symfony@987e1cf · GitHub
[go: up one dir, main page]

Skip to content

Commit 987e1cf

Browse files
committed
bug #31482 [Messenger][DoctrineBridge] Throws UnrecoverableMessageHandlingException when passed invalid entity manager name (Koc)
This PR was merged into the 4.3 branch. Discussion ---------- [Messenger][DoctrineBridge] Throws UnrecoverableMessageHandlingException when passed invalid entity manager name | Q | A | ------------- | --- | Branch? | 4.3 | Bug fix? | yes | New feature? | no | BC breaks? | not sure | Deprecations? | no | Tests pass? | Waiting for Travis | Fixed tickets | - | License | MIT | Doc PR | - 1. Throws `UnrecoverableMessageHandlingException` and do not retry messages if middlewares missconfigured 2. `getManager()` doesn't return null. Actually [it throws](https://github.com/doctrine/persistence/blob/master/lib/Doctrine/Persistence/AbstractManagerRegistry.php#L144-L160) `\InvalidArgumentException` in requested entity manager not exists. Not sure, should this changes considered as BC-break. Also I can extract abstract Doctrine middleware but not sure what branch should I use for it? Master or 4.3? Commits ------- c4eca27 Throws UnrecoverableMessageHandlingException when passed invalid entity manager name for Doctrine middlewares
2 parents 6545805 + c4eca27 commit 987e1cf

7 files changed

+65
-17
lines changed

src/Symfony/Bridge/Doctrine/Messenger/DoctrineCloseConnectionMiddleware.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
namespace Symfony\Bridge\Doctrine\Messenger;
1313

1414
use Doctrine\Common\Persistence\ManagerRegistry;
15-
use Doctrine\ORM\EntityManagerInterface;
1615
use Symfony\Component\Messenger\Envelope;
16+
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;
1717
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
1818
use Symfony\Component\Messenger\Middleware\StackInterface;
1919

@@ -40,10 +40,10 @@ public function __construct(ManagerRegistry $managerRegistry, string $entityMana
4040
*/
4141
public function handle(Envelope $envelope, StackInterface $stack): Envelope
4242
{
43-
$entityManager = $this->managerRegistry->getManager($this->entityManagerName);
44-
45-
if (!$entityManager instanceof EntityManagerInterface) {
46-
throw new \InvalidArgumentException(sprintf('The ObjectManager with name "%s" must be an instance of EntityManagerInterface', $this->entityManagerName));
43+
try {
44+
$entityManager = $this->managerRegistry->getManager($this->entityManagerName);
45+
} catch (\InvalidArgumentException $e) {
46+
throw new UnrecoverableMessageHandlingException($e->getMessage(), 0, $e);
4747
}
4848

4949
try {

src/Symfony/Bridge/Doctrine/Messenger/DoctrinePingConnectionMiddleware.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
namespace Symfony\Bridge\Doctrine\Messenger;
1313

1414
use Doctrine\Common\Persistence\ManagerRegistry;
15-
use Doctrine\ORM\EntityManagerInterface;
1615
use Symfony\Component\Messenger\Envelope;
16+
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;
1717
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
1818
use Symfony\Component\Messenger\Middleware\StackInterface;
1919

@@ -40,10 +40,10 @@ public function __construct(ManagerRegistry $managerRegistry, string $entityMana
4040
*/
4141
public function handle(Envelope $envelope, StackInterface $stack): Envelope
4242
{
43-
$entityManager = $this->managerRegistry->getManager($this->entityManagerName);
44-
45-
if (!$entityManager instanceof EntityManagerInterface) {
46-
throw new \InvalidArgumentException(sprintf('The ObjectManager with name "%s" must be an instance of EntityManagerInterface', $this->entityManagerName));
43+
try {
44+
$entityManager = $this->managerRegistry->getManager($this->entityManagerName);
45+
} catch (\InvalidArgumentException $e) {
46+
throw new UnrecoverableMessageHandlingException($e->getMessage(), 0, $e);
4747
}
4848

4949
$connection = $entityManager->getConnection();

src/Symfony/Bridge/Doctrine/Messenger/DoctrineTransactionMiddleware.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
namespace Symfony\Bridge\Doctrine\Messenger;
1313

1414
use Doctrine\Common\Persistence\ManagerRegistry;
15-
use Doctrine\ORM\EntityManagerInterface;
1615
use Symfony\Component\Messenger\Envelope;
16+
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;
1717
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
1818
use Symfony\Component\Messenger\Middleware\StackInterface;
1919

@@ -40,10 +40,10 @@ public function __construct(ManagerRegistry $managerRegistry, string $entityMana
4040
*/
4141
public function handle(Envelope $envelope, StackInterface $stack): Envelope
4242
{
43-
$entityManager = $this->managerRegistry->getManager($this->entityManagerName);
44-
45-
if (!$entityManager instanceof EntityManagerInterface) {
46-
throw new \InvalidArgumentException(sprintf('The ObjectManager with name "%s" must be an instance of EntityManagerInterface', $this->entityManagerName));
43+
try {
44+
$entityManager = $this->managerRegistry->getManager($this->entityManagerName);
45+
} catch (\InvalidArgumentException $e) {
46+
throw new UnrecoverableMessageHandlingException($e->getMessage(), 0, $e);
4747
}
4848

4949
$entityManager->getConnection()->beginTransaction();

src/Symfony/Bridge/Doctrine/Tests/Messenger/DoctrineCloseConnectionMiddlewareTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Doctrine\ORM\EntityManagerInterface;
1717
use Symfony\Bridge\Doctrine\Messenger\DoctrineCloseConnectionMiddleware;
1818
use Symfony\Component\Messenger\Envelope;
19+
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;
1920
use Symfony\Component\Messenger\Test\Middleware\MiddlewareTestCase;
2021

2122
class DoctrineCloseConnectionMiddlewareTest extends MiddlewareTestCase
@@ -50,4 +51,19 @@ public function testMiddlewareCloseConnection()
5051

5152
$this->middleware->handle(new Envelope(new \stdClass()), $this->getStackMock());
5253
}
54+
55+
public function testInvalidEntityManagerThrowsException()
56+
{
57+
$managerRegistry = $this->createMock(ManagerRegistry::class);
58+
$managerRegistry
59+
->method('getManager')
60+
->with('unknown_manager')
61+
->will($this->throwException(new \InvalidArgumentException()));
62+
63+
$middleware = new DoctrineCloseConnectionMiddleware($managerRegistry, 'unknown_manager');
64+
65+
$this->expectException(UnrecoverableMessageHandlingException::class);
66+
67+
$middleware->handle(new Envelope(new \stdClass()), $this->getStackMock(false));
68+
}
5369
}

src/Symfony/Bridge/Doctrine/Tests/Messenger/DoctrinePingConnectionMiddlewareTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Doctrine\ORM\EntityManagerInterface;
1717
use Symfony\Bridge\Doctrine\Messenger\DoctrinePingConnectionMiddleware;
1818
use Symfony\Component\Messenger\Envelope;
19+
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;
1920
use Symfony\Component\Messenger\Test\Middleware\MiddlewareTestCase;
2021

2122
class DoctrinePingConnectionMiddlewareTest extends MiddlewareTestCase
@@ -71,4 +72,19 @@ public function testMiddlewarePingResetEntityManager()
7172

7273
$this->middleware->handle(new Envelope(new \stdClass()), $this->getStackMock());
7374
}
75+
76+
public function testInvalidEntityManagerThrowsException()
77+
{
78+
$managerRegistry = $this->createMock(ManagerRegistry::class);
79+
$managerRegistry
80+
->method('getManager')
81+
->with('unknown_manager')
82+
->will($this->throwException(new \InvalidArgumentException()));
83+
84+
$middleware = new DoctrinePingConnectionMiddleware($managerRegistry, 'unknown_manager');
85+
86+
$this->expectException(UnrecoverableMessageHandlingException::class);
87+
88+
$middleware->handle(new Envelope(new \stdClass()), $this->getStackMock(false));
89+
}
7490
}

src/Symfony/Bridge/Doctrine/Tests/Messenger/DoctrineTransactionMiddlewareTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Doctrine\ORM\EntityManagerInterface;
1717
use Symfony\Bridge\Doctrine\Messenger\DoctrineTransactionMiddleware;
1818
use Symfony\Component\Messenger\Envelope;
19+
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;
1920
use Symfony\Component\Messenger\Test\Middleware\MiddlewareTestCase;
2021

2122
class DoctrineTransactionMiddlewareTest extends MiddlewareTestCase
@@ -67,4 +68,19 @@ public function testTransactionIsRolledBackOnException()
6768

6869
$this->middleware->handle(new Envelope(new \stdClass()), $this->getThrowingStackMock());
6970
}
71+
72+
public function testInvalidEntityManagerThrowsException()
73+
{
74+
$managerRegistry = $this->createMock(ManagerRegistry::class);
75+
$managerRegistry
76+
->method('getManager')
77+
->with('unknown_manager')
78+
->will($this->throwException(new \InvalidArgumentException()));
79+
80+
$middleware = new DoctrineTransactionMiddleware($managerRegistry, 'unknown_manager');
81+
82+
$this->expectException(UnrecoverableMessageHandlingException::class);
83+
84+
$middleware->handle(new Envelope(new \stdClass()), $this->getStackMock(false));
85+
}
7086
}

src/Symfony/Bridge/Doctrine/composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"symfony/dependency-injection": "~3.4|~4.0",
3030
"symfony/form": "~4.3",
3131
"symfony/http-kernel": "~3.4|~4.0",
32-
"symfony/messenger": "~4.2",
32+
"symfony/messenger": "~4.3",
3333
"symfony/property-access": "~3.4|~4.0",
3434
"symfony/property-info": "~3.4|~4.0",
3535
"symfony/proxy-manager-bridge": "~3.4|~4.0",
@@ -49,7 +49,7 @@
4949
"phpunit/phpunit": "<4.8.35|<5.4.3,>=5.0",
5050
"symfony/dependency-injection": "<3.4",
5151
"symfony/form": "<4.3",
52-
"symfony/messenger": "<4.2"
52+
"symfony/messenger": "<4.3"
5353
},
5454
"suggest": {
5555
"symfony/form": "",

0 commit comments

Comments
 (0)
0