8000 [Messenger] Fix DoctrineTransactionMiddleware after MiddlewareInterface bc-break by skalpa · Pull Request #28999 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Messenger] Fix DoctrineTransactionMiddleware after MiddlewareInterface bc-break #28999

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
Oct 27, 2018
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 @@ -36,7 +36,7 @@ public function __construct(ManagerRegistry $managerRegistry, ?string $entityMan
/**
* {@inheritdoc}
*/
public function handle(Envelope $envelope, StackInterface $stack): void
public function handle(Envelope $envelope, StackInterface $stack): Envelope
{
$entityManager = $this->managerRegistry->getManager($this->entityManagerName);

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

$entityManager->getConnection()->beginTransaction();
try {
$stack->next()->handle($envelope, $stack);
$envelope = $stack->next()->handle($envelope, $stack);
$entityManager->flush();
$entityManager->getConnection()->commit();

return $envelope;
} catch (\Throwable $exception) {
$entityManager->getConnection()->rollBack();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Symfony\Bridge\Doctrine\Tests\Fixtures\Messenger;

use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
use Symfony\Component\Messenger\Middleware\StackInterface;

class DummyMiddleware implements MiddlewareInterface
{
public function handle(Envelope $envelope, StackInterface $stack): Envelope
{
return $envelope;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Symfony\Bridge\Doctrine\Tests\Fixtures\Messenger;

use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
use Symfony\Component\Messenger\Middleware\StackInterface;

class ThrowingMiddleware implements MiddlewareInterface
{
public function handle(Envelope $envelope, StackInterface $stack): Envelope
{
throw new \RuntimeException('Thrown from middleware.');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?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\Bridge\Doctrine\Tests\Messenger;

use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\DBAL\Connection;
use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Doctrine\Messenger\DoctrineTransactionMiddleware;
use Symfony\Bridge\Doctrine\Tests\Fixtures\Messenger\DummyMiddleware;
use Symfony\Bridge\Doctrine\Tests\Fixtures\Messenger\ThrowingMiddleware;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Middleware\StackInterface;

class DoctrineTransactionMiddlewareTest extends TestCase
{
private $connection;
private $entityManager;
private $middleware;
private $stack;

public function setUp()
{
$this->connection = $this->createMock(Connection::class);

$this->entityManager = $this->createMock(EntityManagerInterface::class);
$this->entityManager->method('getConnection')->willReturn($this->connection);

$managerRegistry = $this->createMock(ManagerRegistry::class);
$managerRegistry->method('getManager')->willReturn($this->entityManager);

$this->middleware = new DoctrineTransactionMiddleware($managerRegistry, null);

$this->stack = $this->createMock(StackInterface::class);
}

public function testMiddlewareWrapsInTransactionAndFlushes()
{
$this->connection->expects($this->once())
->method('beginTransaction')
;
$this->connection->expects($this->once())
->method('commit')
;
$this->entityManager->expects($this->once())
->method('flush')
;
$this->stack
->expects($this->once())
->method('next')
->willReturn(new DummyMiddleware())
;

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

public function testTransactionIsRolledBackOnException()
{
$this->connection->expects($this->once())
->method('beginTransaction')
;
$this->connection->expects($this->once())
->method('rollBack')
;
$this->stack
->expects($this->once())
->method('next')
->willReturn(new ThrowingMiddleware())
;
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('Thrown from middleware.');

$this->middleware->handle(new Envelope(new \stdClass()), $this->stack);
}
}
1 change: 1 addition & 0 deletions src/Symfony/Bridge/Doctrine/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"symfony/dependency-injection": "~3.4|~4.0",
"symfony/form": "~3.4|~4.0",
"symfony/http-kernel": "~3.4|~4.0",
"symfony/messenger": "~4.2",
"symfony/property-access": "~3.4|~4.0",
"symfony/property-info": "~3.4|~4.0",
"symfony/proxy-manager-bridge": "~3.4|~4.0",
Expand Down
0