|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Enqueue\Bundle\Tests\Unit\Consumption\Extension; |
| 4 | + |
| 5 | +use Doctrine\Common\Persistence\ManagerRegistry; |
| 6 | +use Doctrine\ORM\EntityManagerInterface; |
| 7 | +use Enqueue\Bundle\Consumption\Extension\DoctrineClosedEntityManagerExtension; |
| 8 | +use Enqueue\Consumption\Context\PostConsume; |
| 9 | +use Enqueue\Consumption\Context\PostMessageReceived; |
| 10 | +use Enqueue\Consumption\Context\PreConsume; |
| 11 | +use Interop\Queue\Consumer; |
| 12 | +use Interop\Queue\Context as InteropContext; |
| 13 | +use Interop\Queue\Message; |
| 14 | +use Interop\Queue\SubscriptionConsumer; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | +use Psr\Log\LoggerInterface; |
| 17 | + |
| 18 | +class DoctrineClosedEntityManagerExtensionTest extends TestCase |
| 19 | +{ |
| 20 | + public function testOnPreConsumeShouldNotInterruptExecution() |
| 21 | + { |
| 22 | + $manager = $this->createManagerMock(true); |
| 23 | + |
| 24 | + $registry = $this->createRegistryMock([ |
| 25 | + 'manager' => $manager, |
| 26 | + ]); |
| 27 | + |
| 28 | + $message = new PreConsume( |
| 29 | + $this->createMock(InteropContext::class), |
| 30 | + $this->createMock(SubscriptionConsumer::class), |
| 31 | + $this->createMock(LoggerInterface::class), |
| 32 | + 1, |
| 33 | + 2, |
| 34 | + 3 |
| 35 | + ); |
| 36 | + |
| 37 | + self::assertFalse($message->isExecutionInterrupted()); |
| 38 | + |
| 39 | + $extension = new DoctrineClosedEntityManagerExtension($registry); |
| 40 | + $extension->onPreConsume($message); |
| 41 | + |
| 42 | + self::assertFalse($message->isExecutionInterrupted()); |
| 43 | + } |
| 44 | + |
| 45 | + public function testOnPreConsumeShouldInterruptExecutionIfAManagerIsClosed() |
| 46 | + { |
| 47 | + $manager1 = $this->createManagerMock(true); |
| 48 | + $manager2 = $this->createManagerMock(false); |
| 49 | + |
| 50 | + $registry = $this->createRegistryMock([ |
| 51 | + 'manager1' => $manager1, |
| 52 | + 'manager2' => $manager2, |
| 53 | + ]); |
| 54 | + |
| 55 | + $message = new PreConsume( |
| 56 | + $this->createMock(InteropContext::class), |
| 57 | + $this->createMock(SubscriptionConsumer::class), |
| 58 | + $this->createMock(LoggerInterface::class), |
| 59 | + 1, |
| 60 | + 2, |
| 61 | + 3 |
| 62 | + ); |
| 63 | + $message->getLogger() |
| 64 | + ->expects($this->once()) |
| 65 | + ->method('debug') |
| 66 | + ->with('[DoctrineClosedEntityManagerExtension] Interrupt execution as entity manager "manager2" has been closed') |
| 67 | + ; |
| 68 | + |
| 69 | + self::assertFalse($message->isExecutionInterrupted()); |
| 70 | + |
| 71 | + $extension = new DoctrineClosedEntityManagerExtension($registry); |
| 72 | + $extension->onPreConsume($message); |
| 73 | + |
| 74 | + self::assertTrue($message->isExecutionInterrupted()); |
| 75 | + } |
| 76 | + |
| 77 | + public function testOnPostConsumeShouldNotInterruptExecution() |
| 78 | + { |
| 79 | + $manager = $this->createManagerMock(true); |
| 80 | + |
| 81 | + $registry = $this->createRegistryMock([ |
| 82 | + 'manager' => $manager, |
| 83 | + ]); |
| 84 | + |
| 85 | + $message = new PostConsume( |
| 86 | + $this->createMock(InteropContext::class), |
| 87 | + $this->createMock(SubscriptionConsumer::class), |
| 88 | + 1, |
| 89 | + 1, |
| 90 | + 1, |
| 91 | + $this->createMock(LoggerInterface::class) |
| 92 | + ); |
| 93 | + |
| 94 | + self::assertFalse($message->isExecutionInterrupted()); |
| 95 | + |
| 96 | + $extension = new DoctrineClosedEntityManagerExtension($registry); |
| 97 | + $extension->onPostConsume($message); |
| 98 | + |
| 99 | + self::assertFalse($message->isExecutionInterrupted()); |
| 100 | + } |
| 101 | + |
| 102 | + public function testOnPostConsumeShouldInterruptExecutionIfAManagerIsClosed() |
| 103 | + { |
| 104 | + $manager1 = $this->createManagerMock(true); |
| 105 | + $manager2 = $this->createManagerMock(false); |
| 106 | + |
| 107 | + $registry = $this->createRegistryMock([ |
| 108 | + 'manager1' => $manager1, |
| 109 | + 'manager2' => $manager2, |
| 110 | + ]); |
| 111 | + |
| 112 | + $message = new PostConsume( |
| 113 | + $this->createMock(InteropContext::class), |
| 114 | + $this->createMock(SubscriptionConsumer::class), |
| 115 | + 1, |
| 116 | + 1, |
| 117 | + 1, |
| 118 | + $this->createMock(LoggerInterface::class) |
| 119 | + ); |
| 120 | + $message->getLogger() |
| 121 | + ->expects($this->once()) |
| 122 | + ->method('debug') |
| 123 | + ->with('[DoctrineClosedEntityManagerExtension] Interrupt execution as entity manager "manager2" has been closed') |
| 124 | + ; |
| 125 | + |
| 126 | + self::assertFalse($message->isExecutionInterrupted()); |
| 127 | + |
| 128 | + $extension = new DoctrineClosedEntityManagerExtension($registry); |
| 129 | + $extension->onPostConsume($message); |
| 130 | + |
| 131 | + self::assertTrue($message->isExecutionInterrupted()); |
| 132 | + } |
| 133 | + |
| 134 | + public function testOnPostReceivedShouldNotInterruptExecution() |
| 135 | + { |
| 136 | + $manager = $this->createManagerMock(true); |
| 137 | + |
| 138 | + $registry = $this->createRegistryMock([ |
| 139 | + 'manager' => $manager, |
| 140 | + ]); |
| 141 | + |
| 142 | + $message = new PostMessageReceived( |
| 143 | + $this->createMock(InteropContext::class), |
| 144 | + $this->createMock(Consumer::class), |
| 145 | + $this->createMock(Message::class), |
| 146 | + 'aResult', |
| 147 | + 1, |
| 148 | + $this->createMock(LoggerInterface::class) |
| 149 | + ); |
| 150 | + |
| 151 | + self::assertFalse($message->isExecutionInterrupted()); |
| 152 | + |
| 153 | + $extension = new DoctrineClosedEntityManagerExtension($registry); |
| 154 | + $extension->onPostMessageReceived($message); |
| 155 | + |
| 156 | + self::assertFalse($message->isExecutionInterrupted()); |
| 157 | + } |
| 158 | + |
| 159 | + public function testOnPostReceivedShouldInterruptExecutionIfAManagerIsClosed() |
| 160 | + { |
| 161 | + $manager1 = $this->createManagerMock(true); |
| 162 | + $manager2 = $this->createManagerMock(false); |
| 163 | + |
| 164 | + $registry = $this->createRegistryMock([ |
| 165 | + 'manager1' => $manager1, |
| 166 | + 'manager2' => $manager2, |
| 167 | + ]); |
| 168 | + |
| 169 | + $message = new PostMessageReceived( |
| 170 | + $this->createMock(InteropContext::class), |
| 171 | + $this->createMock(Consumer::class), |
| 172 | + $this->createMock(Message::class), |
| 173 | + 'aResult', |
| 174 | + 1, |
| 175 | + $this->createMock(LoggerInterface::class) |
| 176 | + ); |
| 177 | + $message->getLogger() |
| 178 | + ->expects($this->once()) |
| 179 | + ->method('debug') |
| 180 | + ->with('[DoctrineClosedEntityManagerExtension] Interrupt execution as entity manager "manager2" has been closed') |
| 181 | + ; |
| 182 | + |
| 183 | + self::assertFalse($message->isExecutionInterrupted()); |
| 184 | + |
| 185 | + $extension = new DoctrineClosedEntityManagerExtension($registry); |
| 186 | + $extension->onPostMessageReceived($message); |
| 187 | + |
| 188 | + self::assertTrue($message->isExecutionInterrupted()); |
| 189 | + } |
| 190 | + |
| 191 | + /** |
| 192 | + * @return \PHPUnit_Framework_MockObject_MockObject|ManagerRegistry |
| 193 | + */ |
| 194 | + protected function createRegistryMock(array $managers): ManagerRegistry |
| 195 | + { |
| 196 | + $mock = $this->createMock(ManagerRegistry::class); |
| 197 | + |
| 198 | + $mock |
| 199 | + ->expects($this->once()) |
| 200 | + ->method('getManagers') |
| 201 | + ->willReturn($managers) |
| 202 | + ; |
| 203 | + |
| 204 | + return $mock; |
| 205 | + } |
| 206 | + |
| 207 | + /** |
| 208 | + * @return \PHPUnit_Framework_MockObject_MockObject|EntityManagerInterface |
| 209 | + */ |
| 210 | + protected function createManagerMock(bool $open): EntityManagerInterface |
| 211 | + { |
| 212 | + $mock = $this->createMock(EntityManagerInterface::class); |
| 213 | + |
| 214 | + $mock |
| 215 | + ->expects($this->once()) |
| 216 | + ->method('isOpen') |
| 217 | + ->willReturn($open) |
| 218 | + ; |
| 219 | + |
| 220 | + return $mock; |
| 221 | + } |
| 222 | +} |
0 commit comments