diff --git a/pkg/enqueue-bundle/Tests/Functional/App/CustomAppKernel.php b/pkg/enqueue-bundle/Tests/Functional/App/CustomAppKernel.php index b5043aa26..574dc6b39 100644 --- a/pkg/enqueue-bundle/Tests/Functional/App/CustomAppKernel.php +++ b/pkg/enqueue-bundle/Tests/Functional/App/CustomAppKernel.php @@ -13,6 +13,8 @@ class CustomAppKernel extends Kernel { use MicroKernelTrait; + private $enqueueConfigId; + private $enqueueConfig = [ 'client' => [ 'prefix' => 'enqueue', @@ -25,11 +27,13 @@ class CustomAppKernel extends Kernel public function setEnqueueConfig(array $config) { - $fs = new Filesystem(); - $fs->remove(sys_get_temp_dir().'/EnqueueBundleCustom/cache'); - $fs->mkdir(sys_get_temp_dir().'/EnqueueBundleCustom/cache'); - $this->enqueueConfig = array_replace_recursive($this->enqueueConfig, $config); + $this->enqueueConfig['client']['app_name'] = str_replace('.', '', uniqid(true)); + $this->enqueueConfigId = md5(json_encode($this->enqueueConfig)); + + $fs = new Filesystem(); + $fs->remove(sys_get_temp_dir().'/EnqueueBundleCustom/cache/'.$this->enqueueConfigId); + $fs->mkdir(sys_get_temp_dir().'/EnqueueBundleCustom/cache/'.$this->enqueueConfigId); } /** @@ -52,7 +56,7 @@ public function registerBundles() */ public function getCacheDir() { - return sys_get_temp_dir().'/EnqueueBundleCustom/cache'; + return sys_get_temp_dir().'/EnqueueBundleCustom/cache/'.$this->enqueueConfigId; } /** @@ -60,12 +64,12 @@ public function getCacheDir() */ public function getLogDir() { - return sys_get_temp_dir().'/EnqueueBundleCustom/cache/logs'; + return sys_get_temp_dir().'/EnqueueBundleCustom/cache/logs/'.$this->enqueueConfigId; } protected function getContainerClass() { - return parent::getContainerClass().'Custom'; + return parent::getContainerClass().'Custom'.$this->enqueueConfigId; } /** diff --git a/pkg/enqueue-bundle/Tests/Functional/UseCasesTest.php b/pkg/enqueue-bundle/Tests/Functional/UseCasesTest.php index 9c633b3d1..3541badc0 100644 --- a/pkg/enqueue-bundle/Tests/Functional/UseCasesTest.php +++ b/pkg/enqueue-bundle/Tests/Functional/UseCasesTest.php @@ -6,11 +6,14 @@ use Enqueue\Client\DriverInterface; use Enqueue\Client\Producer; use Enqueue\Client\ProducerInterface; +use Enqueue\Stomp\StompDestination; use Enqueue\Symfony\Client\ConsumeMessagesCommand; use Enqueue\Symfony\Consumption\ContainerAwareConsumeMessagesCommand; use Interop\Queue\PsrContext; use Interop\Queue\PsrMessage; +use Interop\Queue\PsrQueue; use Symfony\Component\Console\Tester\CommandTester; +use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\HttpKernel\Kernel; /** @@ -24,6 +27,17 @@ public function setUp() // parent::setUp(); } + public function tearDown() + { + if (static::$kernel) { + $fs = new Filesystem(); + $fs->remove(static::$kernel->getLogDir()); + $fs->remove(static::$kernel->getCacheDir()); + } + + parent::tearDown(); + } + public function provideEnqueueConfigs() { $baseDir = realpath(__DIR__.'/../../../../'); @@ -44,7 +58,8 @@ public function provideEnqueueConfigs() 'user' => getenv('SYMFONY__RABBITMQ__USER'), 'pass' => getenv('SYMFONY__RABBITMQ__PASSWORD'), 'vhost' => getenv('SYMFONY__RABBITMQ__VHOST'), - 'lazy' => false, + 'lazy' => true, + 'persisted' => false, ], ], ]]; @@ -90,15 +105,16 @@ public function provideEnqueueConfigs() ], ]]; - yield 'stomp' => [[ + yield 'rabbitmq_stomp' => [[ 'transport' => [ - 'default' => 'stomp', - 'stomp' => [ + 'default' => 'rabbitmq_stomp', + 'rabbitmq_stomp' => [ 'host' => getenv('SYMFONY__RABBITMQ__HOST'), 'port' => getenv('SYMFONY__RABBITMQ__STOMP__PORT'), 'login' => getenv('SYMFONY__RABBITMQ__USER'), 'password' => getenv('SYMFONY__RABBITMQ__PASSWORD'), 'vhost' => getenv('SYMFONY__RABBITMQ__VHOST'), + 'management_plugin_installed' => true, 'lazy' => false, ], ], @@ -184,12 +200,12 @@ public function provideEnqueueConfigs() ], ]]; - yield 'gps' => [[ - 'transport' => [ - 'default' => 'gps', - 'gps' => [], - ], - ]]; +// yield 'gps' => [[ +// 'transport' => [ +// 'default' => 'gps', +// 'gps' => [], +// ], +// ]]; } /** @@ -199,16 +215,17 @@ public function testProducerSendsMessage(array $enqueueConfig) { $this->customSetUp($enqueueConfig); - $this->getMessageProducer()->sendEvent(TestProcessor::TOPIC, 'test message body'); + $expectedBody = __METHOD__.time(); - $queue = $this->getPsrContext()->createQueue('enqueue.test'); + $this->getMessageProducer()->sendEvent(TestProcessor::TOPIC, $expectedBody); - $consumer = $this->getPsrContext()->createConsumer($queue); + $consumer = $this->getPsrContext()->createConsumer($this->getTestQueue()); $message = $consumer->receive(100); - $this->assertInstanceOf(PsrMessage::class, $message); - $this->assertSame('test message body', $message->getBody()); + $consumer->acknowledge($message); + + $this->assertSame($expectedBody, $message->getBody()); } /** @@ -222,11 +239,10 @@ public function testProducerSendsCommandMessage(array $enqueueConfig) $this->getMessageProducer()->sendCommand(TestCommandProcessor::COMMAND, $expectedBody); - $queue = $this->getPsrContext()->createQueue('enqueue.test'); - - $consumer = $this->getPsrContext()->createConsumer($queue); + $consumer = $this->getPsrContext()->createConsumer($this->getTestQueue()); $message = $consumer->receive(100); + $this->assertInstanceOf(PsrMessage::class, $message); $consumer->acknowledge($message); $this->assertInstanceOf(PsrMessage::class, $message); @@ -265,10 +281,12 @@ public function testClientConsumeMessagesFromExplicitlySetQueue(array $enqueueCo { $this->customSetUp($enqueueConfig); + $expectedBody = __METHOD__.time(); + $command = $this->container->get(ConsumeMessagesCommand::class); $processor = $this->container->get('test.message.processor'); - $this->getMessageProducer()->sendEvent(TestProcessor::TOPIC, 'test message body'); + $this->getMessageProducer()->sendEvent(TestProcessor::TOPIC, $expectedBody); $tester = new CommandTester($command); $tester->execute([ @@ -278,7 +296,7 @@ public function testClientConsumeMessagesFromExplicitlySetQueue(array $enqueueCo ]); $this->assertInstanceOf(PsrMessage::class, $processor->message); - $this->assertEquals('test message body', $processor->message->getBody()); + $this->assertEquals($expectedBody, $processor->message->getBody()); } /** @@ -288,22 +306,31 @@ public function testTransportConsumeMessagesCommandShouldConsumeMessage(array $e { $this->customSetUp($enqueueConfig); + if ($this->getTestQueue() instanceof StompDestination) { + $this->markTestSkipped('The test fails with the exception Stomp\Exception\ErrorFrameException: Error "precondition_failed". '. + 'It happens because of the destination options are different from the one used while creating the dest. Nothing to do about it' + ); + } + + $expectedBody = __METHOD__.time(); + $command = $this->container->get(ContainerAwareConsumeMessagesCommand::class); $command->setContainer($this->container); $processor = $this->container->get('test.message.processor'); - $this->getMessageProducer()->sendEvent(TestProcessor::TOPIC, 'test message body'); + $this->getMessageProducer()->sendEvent(TestProcessor::TOPIC, $expectedBody); $tester = new CommandTester($command); $tester->execute([ '--message-limit' => 1, '--time-limit' => '+10sec', - '--queue' => ['enqueue.test'], + '--receive-timeout' => 1000, + '--queue' => [$this->getTestQueue()->getQueueName()], 'processor-service' => 'test.message.processor', ]); $this->assertInstanceOf(PsrMessage::class, $processor->message); - $this->assertEquals('test message body', $processor->message->getBody()); + $this->assertEquals($expectedBody, $processor->message->getBody()); } /** @@ -329,16 +356,26 @@ protected function customSetUp(array $enqueueConfig) $driver = $this->container->get('enqueue.client.driver'); $context = $this->getPsrContext(); - $queue = $driver->createQueue('test'); - - //guard - $this->assertEquals('enqueue.test', $queue->getQueueName()); + $driver->setupBroker(); - if (method_exists($context, 'deleteQueue')) { - $context->deleteQueue($queue); + try { + if (method_exists($context, 'purgeQueue')) { + $queue = $this->getTestQueue(); + $context->purgeQueue($queue); + } + } catch (\Exception $e) { } + } - $driver->setupBroker(); + /** + * @return PsrQueue + */ + protected function getTestQueue() + { + /** @var DriverInterface $driver */ + $driver = $this->container->get('enqueue.client.driver'); + + return $driver->createQueue('test'); } /** diff --git a/pkg/sqs/SqsContext.php b/pkg/sqs/SqsContext.php index 93bb2834d..57384daaf 100644 --- a/pkg/sqs/SqsContext.php +++ b/pkg/sqs/SqsContext.php @@ -186,12 +186,22 @@ public function deleteQueue(SqsDestination $dest) } /** + * @deprecated since 0.8 will be removed 0.9 use self::purgeQueue() + * * @param SqsDestination $dest */ public function purge(SqsDestination $dest) + { + $this->purgeQueue($dest); + } + + /** + * @param SqsDestination $destination + */ + public function purgeQueue(SqsDestination $destination) { $this->getClient()->purgeQueue([ - 'QueueUrl' => $this->getQueueUrl($dest), + 'QueueUrl' => $this->getQueueUrl($destination), ]); } } diff --git a/pkg/stomp/StompConnectionFactory.php b/pkg/stomp/StompConnectionFactory.php index ce2aae733..1f82a7b8a 100644 --- a/pkg/stomp/StompConnectionFactory.php +++ b/pkg/stomp/StompConnectionFactory.php @@ -64,7 +64,7 @@ public function createContext() }); } - return new StompContext($this->stomp); + return new StompContext($this->establishConnection()); } /**