b2pweb /
bdf-queue-bundle
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Bdf\QueueBundle\Tests; |
||
| 4 | |||
| 5 | require_once __DIR__.'/TestKernel.php'; |
||
| 6 | |||
| 7 | use Bdf\Queue\Connection\Prime\PrimeConnection; |
||
| 8 | use Bdf\Queue\Console\Command\BindCommand; |
||
| 9 | use Bdf\Queue\Console\Command\ConsumeCommand; |
||
| 10 | use Bdf\Queue\Console\Command\Failer\AbstractFailerCommand; |
||
| 11 | use Bdf\Queue\Console\Command\Failer\DeleteCommand; |
||
| 12 | use Bdf\Queue\Console\Command\Failer\RetryCommand; |
||
| 13 | use Bdf\Queue\Console\Command\Failer\ShowCommand; |
||
| 14 | use Bdf\Queue\Console\Command\InfoCommand; |
||
| 15 | use Bdf\Queue\Console\Command\ProduceCommand; |
||
| 16 | use Bdf\Queue\Console\Command\SetupCommand; |
||
| 17 | use Bdf\Queue\Consumer\Receiver\Builder\ReceiverLoaderInterface; |
||
| 18 | use Bdf\Queue\Destination\CachedDestinationFactory; |
||
| 19 | use Bdf\Queue\Destination\ConfigurationDestinationFactory; |
||
| 20 | use Bdf\Queue\Destination\DestinationFactory; |
||
| 21 | use Bdf\Queue\Destination\DestinationInterface; |
||
| 22 | use Bdf\Queue\Destination\DestinationManager; |
||
| 23 | use Bdf\Queue\Destination\DsnDestinationFactory; |
||
| 24 | use Bdf\Queue\Failer\FailedJobStorageInterface; |
||
| 25 | use Bdf\Queue\Failer\MemoryFailedJobRepository; |
||
| 26 | use Bdf\Queue\Message\Message; |
||
| 27 | use Bdf\Queue\Message\QueuedMessage; |
||
| 28 | use Bdf\Queue\Testing\QueueHelper; |
||
| 29 | use Bdf\QueueBundle\BdfQueueBundle; |
||
| 30 | use Bdf\QueueBundle\Consumption\ReceiverLoader; |
||
| 31 | use Bdf\QueueBundle\Tests\Fixtures\GetDestinationFactory; |
||
| 32 | use Bdf\QueueBundle\Tests\Fixtures\TestHandler; |
||
| 33 | use PHPUnit\Framework\TestCase; |
||
| 34 | use Symfony\Bundle\FrameworkBundle\Console\Application; |
||
| 35 | use Symfony\Component\Console\Command\Command; |
||
| 36 | use Symfony\Component\Console\Command\LazyCommand; |
||
| 37 | use Symfony\Component\DependencyInjection\ContainerBuilder; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * BdfSerializerBundleTest. |
||
| 41 | */ |
||
| 42 | class BdfQueueBundleTest extends TestCase |
||
| 43 | { |
||
| 44 | public function testDefaultConfig() |
||
| 45 | { |
||
| 46 | $builder = $this->createMock(ContainerBuilder::class); |
||
| 47 | |||
| 48 | $bundle = new BdfQueueBundle(); |
||
| 49 | |||
| 50 | $this->assertNull($bundle->build($builder)); |
||
| 51 | } |
||
| 52 | |||
| 53 | public function testKernel() |
||
| 54 | { |
||
| 55 | $kernel = new \TestKernel(); |
||
| 56 | $kernel->boot(); |
||
| 57 | |||
| 58 | $this->assertInstanceOf(DestinationManager::class, $kernel->getContainer()->get('bdf_queue.destination_manager')); |
||
| 59 | $this->assertSame($kernel->getContainer()->get('bdf_queue.destination_manager'), $kernel->getContainer()->get(DestinationManager::class)); |
||
| 60 | $this->assertInstanceOf(ReceiverLoader::class, $kernel->getContainer()->get('bdf_queue.receiver.loader')); |
||
| 61 | $this->assertInstanceOf(ReceiverLoader::class, $kernel->getContainer()->get(ReceiverLoaderInterface::class)); |
||
| 62 | } |
||
| 63 | |||
| 64 | public function testFunctional() |
||
| 65 | { |
||
| 66 | $kernel = new \TestKernel(__DIR__.'/Fixtures/conf_functional.yaml'); |
||
| 67 | $kernel->boot(); |
||
| 68 | |||
| 69 | /** @var DestinationManager $destinations */ |
||
| 70 | $destinations = $kernel->getContainer()->get('bdf_queue.destination_manager'); |
||
| 71 | $destination = $destinations->create('test'); |
||
| 72 | |||
| 73 | $this->assertInstanceOf(DestinationInterface::class, $destination); |
||
| 74 | |||
| 75 | /** @var TestHandler $handler */ |
||
| 76 | $handler = $kernel->getContainer()->get(TestHandler::class); |
||
| 77 | |||
| 78 | $helper = new QueueHelper($kernel->getContainer()); |
||
| 79 | $destination->send(new Message(['foo' => 'bar'])); |
||
| 80 | |||
| 81 | $helper->consume(1, 'test'); |
||
| 82 | $this->assertEquals([['foo' => 'bar']], $handler->messages); |
||
| 83 | } |
||
| 84 | |||
| 85 | public function testWithJsonSerializer() |
||
| 86 | { |
||
| 87 | $kernel = new \TestKernel(__DIR__.'/Fixtures/conf_with_json_serializer.yaml'); |
||
| 88 | $kernel->boot(); |
||
| 89 | |||
| 90 | /** @var DestinationManager $destinations */ |
||
| 91 | $destinations = $kernel->getContainer()->get('bdf_queue.destination_manager'); |
||
| 92 | $destination = $destinations->create('test'); |
||
| 93 | |||
| 94 | $this->assertInstanceOf(DestinationInterface::class, $destination); |
||
| 95 | |||
| 96 | /** @var TestHandler $handler */ |
||
| 97 | $handler = $kernel->getContainer()->get(TestHandler::class); |
||
|
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||
| 98 | |||
| 99 | $helper = new QueueHelper($kernel->getContainer()); |
||
| 100 | $destination->send(new Message(new class() implements \JsonSerializable { |
||
| 101 | #[\ReturnTypeWillChange] |
||
| 102 | public function jsonSerialize() |
||
| 103 | { |
||
| 104 | return ['foo' => 'bar']; |
||
| 105 | } |
||
| 106 | })); |
||
| 107 | |||
| 108 | /** @var QueuedMessage $message */ |
||
| 109 | $message = $helper->peek(1, 'test')[0]; |
||
| 110 | |||
| 111 | $this->assertEquals( |
||
| 112 | '{"data":{"foo":"bar"},"queuedAt":'.json_encode($message->queuedAt()).'}', |
||
| 113 | $message->raw() |
||
| 114 | ); |
||
| 115 | } |
||
| 116 | |||
| 117 | public function testConnectionOptions() |
||
| 118 | { |
||
| 119 | $kernel = new \TestKernel(); |
||
| 120 | $kernel->boot(); |
||
| 121 | |||
| 122 | $configs = $kernel->getContainer()->getParameter('bdf_queue.connections'); |
||
| 123 | |||
| 124 | $this->assertEquals([ |
||
| 125 | 'gearman' => [ |
||
| 126 | 'driver' => null, |
||
| 127 | 'host' => null, |
||
| 128 | 'serializer' => ['id' => 'native'], |
||
| 129 | 'queue' => null, |
||
| 130 | 'url' => 'gearman://127.0.0.1', |
||
| 131 | 'options' => ['client_timeout' => 1], |
||
| 132 | 'vendor' => null, |
||
| 133 | 'port' => null, |
||
| 134 | 'user' => null, |
||
| 135 | 'password' => null, |
||
| 136 | 'connection_factory' => null, |
||
| 137 | ], |
||
| 138 | ], $configs); |
||
| 139 | } |
||
| 140 | |||
| 141 | public function testDestinationOptions() |
||
| 142 | { |
||
| 143 | $kernel = new \TestKernel(); |
||
| 144 | $kernel->boot(); |
||
| 145 | |||
| 146 | $configs = $kernel->getContainer()->getParameter('bdf_queue.destinations'); |
||
| 147 | |||
| 148 | $this->assertEquals([ |
||
| 149 | 'b2p_bus' => 'queue://gearman/b2p_bus', |
||
| 150 | 'custom_bus' => 'queue://gearman/custom_bus', |
||
| 151 | ], $configs); |
||
| 152 | } |
||
| 153 | |||
| 154 | public function testCommands() |
||
| 155 | { |
||
| 156 | $kernel = new \TestKernel(); |
||
| 157 | $kernel->boot(); |
||
| 158 | $console = new Application($kernel); |
||
| 159 | |||
| 160 | $this->assertInstanceOf(BindCommand::class, $this->getCommand($console, 'queue:bind')); |
||
| 161 | $this->assertInstanceOf(ConsumeCommand::class, $this->getCommand($console, 'queue:consume')); |
||
| 162 | $this->assertInstanceOf(InfoCommand::class, $this->getCommand($console, 'queue:info')); |
||
| 163 | $this->assertInstanceOf(ProduceCommand::class, $this->getCommand($console, 'queue:produce')); |
||
| 164 | $this->assertInstanceOf(SetupCommand::class, $this->getCommand($console, 'queue:setup')); |
||
| 165 | |||
| 166 | $this->assertInstanceOf(DeleteCommand::class, $this->getCommand($console, 'queue:failer:delete')); |
||
| 167 | $this->assertInstanceOf(RetryCommand::class, $this->getCommand($console, 'queue:failer:retry')); |
||
| 168 | $this->assertInstanceOf(ShowCommand::class, $this->getCommand($console, 'queue:failer:show')); |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @return void |
||
| 173 | */ |
||
| 174 | public function testFailer() |
||
| 175 | { |
||
| 176 | $kernel = new \TestKernel(); |
||
| 177 | $kernel->boot(); |
||
| 178 | $console = new Application($kernel); |
||
| 179 | |||
| 180 | $command = $this->getCommand($console, 'queue:failer:delete'); |
||
| 181 | |||
| 182 | $r = new \ReflectionProperty(AbstractFailerCommand::class, 'repository'); |
||
| 183 | $r->setAccessible(true); |
||
| 184 | |||
| 185 | /** @var FailedJobStorageInterface $failer */ |
||
| 186 | $failer = $r->getValue($command); |
||
| 187 | |||
| 188 | $this->assertInstanceOf(MemoryFailedJobRepository::class, $failer); |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @return void |
||
| 193 | */ |
||
| 194 | public function testCustomFailer() |
||
| 195 | { |
||
| 196 | $kernel = new \TestKernel(__DIR__.'/Fixtures/conf_with_custom_failer.yaml'); |
||
| 197 | $kernel->boot(); |
||
| 198 | $console = new Application($kernel); |
||
| 199 | |||
| 200 | $command = $this->getCommand($console, 'queue:failer:delete'); |
||
| 201 | |||
| 202 | $r = new \ReflectionProperty(AbstractFailerCommand::class, 'repository'); |
||
| 203 | $r->setAccessible(true); |
||
| 204 | |||
| 205 | /** @var FailedJobStorageInterface $failer */ |
||
| 206 | $failer = $r->getValue($command); |
||
| 207 | |||
| 208 | $this->assertInstanceOf(MemoryFailedJobRepository::class, $failer); |
||
| 209 | $this->assertSame($failer, $kernel->getContainer()->get('foo')); |
||
| 210 | } |
||
| 211 | |||
| 212 | public function testPrimeConnection() |
||
| 213 | { |
||
| 214 | if (class_exists(PrimeConnection::class)) { |
||
| 215 | $this->markTestSkipped('b2pweb/bdf-queue-prime-adapter installed'); |
||
| 216 | } |
||
| 217 | |||
| 218 | $this->expectException(\LogicException::class); |
||
| 219 | |||
| 220 | $kernel = new \TestKernel(__DIR__.'/Fixtures/conf_with_prime_connection.yaml'); |
||
| 221 | $kernel->boot(); |
||
| 222 | |||
| 223 | /** @var DestinationManager $destinations */ |
||
| 224 | $destinations = $kernel->getContainer()->get('bdf_queue.destination_manager'); |
||
| 225 | $destinations->queue('prime', 'queue_name'); |
||
| 226 | } |
||
| 227 | |||
| 228 | public function testDestinationFactoryInstance() |
||
| 229 | { |
||
| 230 | $kernel = new \TestKernel(); |
||
| 231 | $kernel->boot(); |
||
| 232 | |||
| 233 | /** @var GetDestinationFactory $service */ |
||
| 234 | $service = $kernel->getContainer()->get(GetDestinationFactory::class); |
||
| 235 | |||
| 236 | if (class_exists(DestinationFactory::class)) { |
||
| 237 | $this->assertInstanceOf(DestinationFactory::class, $service->destinationFactory); |
||
| 238 | } else { |
||
| 239 | $this->assertInstanceOf(CachedDestinationFactory::class, $service->destinationFactory); |
||
| 240 | } |
||
| 241 | |||
| 242 | $this->assertInstanceOf(DsnDestinationFactory::class, $service->legacyDsnDestinationFactory); |
||
| 243 | $this->assertInstanceOf(ConfigurationDestinationFactory::class, $service->legacyConfigurationDestinationFactory); |
||
| 244 | $this->assertEquals(['custom_bus', 'b2p_bus'], $service->legacyConfigurationDestinationFactory->destinationNames()); |
||
| 245 | $this->assertEquals(['custom_bus', 'b2p_bus'], $service->destinationFactory->destinationNames()); |
||
| 246 | } |
||
| 247 | |||
| 248 | private function getCommand(Application $application, string $name): Command |
||
| 249 | { |
||
| 250 | $command = $application->get($name); |
||
| 251 | |||
| 252 | return $command instanceof LazyCommand ? $command->getCommand() : $command; |
||
| 253 | } |
||
| 254 | } |
||
| 255 |