|
| 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\DBAL\Connection; |
| 15 | +use Doctrine\ORM\EntityManagerInterface; |
| 16 | +use Doctrine\Persistence\ManagerRegistry; |
| 17 | +use Psr\Log\AbstractLogger; |
| 18 | +use Symfony\Bridge\Doctrine\Messenger\DoctrineOpenTransactionLoggerMiddleware; |
| 19 | +use Symfony\Component\Messenger\Envelope; |
| 20 | +use Symfony\Component\Messenger\Test\Middleware\MiddlewareTestCase; |
| 21 | + |
| 22 | +class DoctrineOpenTransactionLoggerMiddlewareTest extends MiddlewareTestCase |
| 23 | +{ |
| 24 | + private $logger; |
| 25 | + private $connection; |
| 26 | + private $entityManager; |
| 27 | + private $middleware; |
| 28 | + |
| 29 | + protected function setUp(): void |
| 30 | + { |
| 31 | + $this->logger = new class() extends AbstractLogger { |
| 32 | + public $logs = []; |
| 33 | + |
| 34 | + public function log($level, $message, $context = []): void |
| 35 | + { |
| 36 | + $this->logs[$level][] = $message; |
| 37 | + } |
| 38 | + }; |
| 39 | + |
| 40 | + $this->connection = $this->createMock(Connection::class); |
| 41 | + |
| 42 | + $this->entityManager = $this->createMock(EntityManagerInterface::class); |
| 43 | + $this->entityManager->method('getConnection')->willReturn($this->connection); |
| 44 | + |
| 45 | + $managerRegistry = $this->createMock(ManagerRegistry::class); |
| 46 | + $managerRegistry->method('getManager')->willReturn($this->entityManager); |
| 47 | + |
| 48 | + $this->middleware = new DoctrineOpenTransactionLoggerMiddleware($managerRegistry, null, $this->logger); |
| 49 | + } |
| 50 | + |
| 51 | + public function testMiddlewareWrapsInTransactionAndFlushes() |
| 52 | + { |
| 53 | + $this->connection->expects($this->exactly(1)) |
| 54 | + ->method('isTransactionActive') |
| 55 | + ->will($this->onConsecutiveCalls(true, true, false)) |
| 56 | + ; |
| 57 | + |
| 58 | + $this->middleware->handle(new Envelope(new \stdClass()), $this->getStackMock()); |
| 59 | + |
| 60 | + $this->assertSame(['error' => ['A handler opened a transaction but did not close it.']], $this->logger->logs); |
| 61 | + } |
| 62 | +} |
0 commit comments