Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c11fe35

Browse files
committed
bug #28999 [Messenger] Fix DoctrineTransactionMiddleware after MiddlewareInterface bc-break (skalpa)
This PR was merged into the 4.2-dev branch. Discussion ---------- [Messenger] Fix DoctrineTransactionMiddleware after MiddlewareInterface bc-break | Q | A | ------------- | --- | Branch? | master | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Fixes `DoctrineTransactionMiddleware` that got broken by #28983, and adds tests. Commits ------- 378ca06 Fix DoctrineTransactionMiddleware after MiddlewareInterface bc-break
2 parents c333d85 + 378ca06 commit c11fe35

File tree

5 files changed

+119
-2
lines changed

5 files changed

+119
-2
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function __construct(ManagerRegistry $managerRegistry, ?string $entityMan
3636
/**
3737
* {@inheritdoc}
3838
*/
39-
public function handle(Envelope $envelope, StackInterface $stack): void
39+
public function handle(Envelope $envelope, StackInterface $stack): Envelope
4040
{
4141
$entityManager = $this->managerRegistry->getManager($this->entityManagerName);
4242

@@ -46,9 +46,11 @@ public function handle(Envelope $envelope, StackInterface $stack): void
4646

4747
$entityManager->getConnection()->beginTransaction();
4848
try {
49-
$stack->next()->handle($envelope, $stack);
49+
$envelope = $stack->next()->handle($envelope, $stack);
5050
$entityManager->flush();
5151
$entityManager->getConnection()->commit();
52+
53+
return $envelope;
5254
} catch (\Throwable $exception) {
5355
$entityManager->getConnection()->rollBack();
5456

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Symfony\Bridge\Doctrine\Tests\Fixtures\Messenger;
4+
5+
use Symfony\Component\Messenger\Envelope;
6+
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
7+
use Symfony\Component\Messenger\Middleware\StackInterface;
8+
9+
class DummyMiddleware implements MiddlewareInterface
10+
{
11+
public function handle(Envelope $envelope, StackInterface $stack): Envelope
12+
{
13+
return $envelope;
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Symfony\Bridge\Doctrine\Tests\Fixtures\Messenger;
4+
5+
use Symfony\Component\Messenger\Envelope;
6+
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
7+
use Symfony\Component\Messenger\Middleware\StackInterface;
8+
9+
class ThrowingMiddleware implements MiddlewareInterface
10+
{
11+
public function handle(Envelope $envelope, StackInterface $stack): Envelope
12+
{
13+
throw new \RuntimeException('Thrown from middleware.');
14+
}
15+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\Bridge\Doctrine\Tests\Messenger;
13+
14+
use Doctrine\Common\Persistence\ManagerRegistry;
15+
use Doctrine\DBAL\Connection;
16+
use Doctrine\ORM\EntityManagerInterface;
17+
use PHPUnit\Framework\TestCase;
18+
use Symfony\Bridge\Doctrine\Messenger\DoctrineTransactionMiddleware;
19+
use Symfony\Bridge\Doctrine\Tests\Fixtures\Messenger\DummyMiddleware;
20+
use Symfony\Bridge\Doctrine\Tests\Fixtures\Messenger\ThrowingMiddleware;
21+
use Symfony\Component\Messenger\Envelope;
22+
use Symfony\Component\Messenger\Middleware\StackInterface;
23+
24+
class DoctrineTransactionMiddlewareTest extends TestCase
25+
{
26+
private $connection;
27+
private $entityManager;
28+
private $middleware;
29+
private $stack;
30+
31+
public function setUp()
32+
{
33+
$this->connection = $this->createMock(Connection::class);
34+
35+
$this->entityManager = $this->createMock(EntityManagerInterface::class);
36+
$this->entityManager->method('getConnection')->willReturn($this->connection);
37+
38+
$managerRegistry = $this->createMock(ManagerRegistry::class);
39+
$managerRegistry->method('getManager')->willReturn($this->entityManager);
40+
41+
$this->middleware = new DoctrineTransactionMiddleware($managerRegistry, null);
42+
43+
$this->stack = $this->createMock(StackInterface::class);
44+
}
45+
46+
public function testMiddlewareWrapsInTransactionAndFlushes()
47+
{
48+
$this->connection->expects($this->once())
49+
->method('beginTransaction')
50+
;
51+
$this->connection->expects($this->once())
52+
->method('commit')
53+
;
54+
$this->entityManager->expects($this->once())
55+
->method('flush')
56+
;
57+
$this->stack
58+
->expects($this->once())
59+
->method('next')
60+
->willReturn(new DummyMiddleware())
61+
;
62+
63+
$this->middleware->handle(new Envelope(new \stdClass()), $this->stack);
64+
}
65+
66+
public function testTransactionIsRolledBackOnException()
67+
{
68+
$this->connection->expects($this->once())
69+
->method('beginTransaction')
70+
;
71+
$this->connection->expects($this->once())
72+
->method('rollBack')
73+
;
74+
$this->stack
75+
->expects($this->once())
76+
->method('next')
77+
->willReturn(new ThrowingMiddleware())
78+
;
79+
$this->expectException(\RuntimeException::class);
80+
$this->expectExceptionMessage('Thrown from middleware.');
81+
82+
$this->middleware->handle(new Envelope(new \stdClass()), $this->stack);
83+
}
84+
}

src/Symfony/Bridge/Doctrine/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"symfony/dependency-injection": "~3.4|~4.0",
3030
"symfony/form": "~3.4|~4.0",
3131
"symfony/http-kernel": "~3.4|~4.0",
32+
"symfony/messenger": "~4.2",
3233
"symfony/property-access": "~3.4|~4.0",
3334
"symfony/property-info": "~3.4|~4.0",
3435
"symfony/proxy-manager-bridge": "~3.4|~4.0",

0 commit comments

Comments
 (0)