diff --git a/CHANGELOG.md b/CHANGELOG.md index 8231b95d1..221d4aabf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,19 @@ # Change Log +## [0.9.13](https://github.com/php-enqueue/enqueue-dev/tree/0.9.13) (2019-09-03) +[Full Changelog](https://github.com/php-enqueue/enqueue-dev/compare/0.9.12...0.9.13) + +**Merged pull requests:** + +- docs: describe drawbacks of using amqp extension [\#942](https://github.com/php-enqueue/enqueue-dev/pull/942) ([gnumoksha](https://github.com/gnumoksha)) +- Add a service to reset doctrine/odm identity maps [\#933](https://github.com/php-enqueue/enqueue-dev/pull/933) ([Lctrs](https://github.com/Lctrs)) +- Add an extension to stop consumption on closed entity manager [\#932](https://github.com/php-enqueue/enqueue-dev/pull/932) ([Lctrs](https://github.com/Lctrs)) +- Add an extension to reset services [\#929](https://github.com/php-enqueue/enqueue-dev/pull/929) ([Lctrs](https://github.com/Lctrs)) +- \[DoctrineClearIdentityMapExtension\] allow instances of ManagerRegistry [\#927](https://github.com/php-enqueue/enqueue-dev/pull/927) ([Lctrs](https://github.com/Lctrs)) +- Link to documentation from logo [\#926](https://github.com/php-enqueue/enqueue-dev/pull/926) ([Steveb-p](https://github.com/Steveb-p)) +- DBAL Change ParameterType class to Type class [\#916](https://github.com/php-enqueue/enqueue-dev/pull/916) ([Nevoss](https://github.com/Nevoss)) +- async\_commands: extended configuration proposal [\#914](https://github.com/php-enqueue/enqueue-dev/pull/914) ([uro](https://github.com/uro)) + ## [0.9.12](https://github.com/php-enqueue/enqueue-dev/tree/0.9.12) (2019-06-25) [Full Changelog](https://github.com/php-enqueue/enqueue-dev/compare/0.9.11...0.9.12) diff --git a/README.md b/README.md index e73c0623f..1d1cccb5f 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -

Enqueue logo

+

Enqueue logo

Enqueue Chat diff --git a/composer.json b/composer.json index c238f3148..6a115bf02 100644 --- a/composer.json +++ b/composer.json @@ -56,6 +56,8 @@ "symfony/framework-bundle": "^3.4|^4", "empi89/php-amqp-stubs": "*@dev", "doctrine/doctrine-bundle": "~1.2", + "doctrine/mongodb-odm-bundle": "^3.5", + "alcaeus/mongo-php-adapter": "^1.0", "kwn/php-rdkafka-stubs": "^1.0.2", "friendsofphp/php-cs-fixer": "^2" }, @@ -112,7 +114,8 @@ "ext-rdkafka": "3.3", "ext-mongodb": "1.3", "ext-bcmath": "1", - "ext-mbstring": "1" + "ext-mbstring": "1", + "ext-mongo": "1.6.14" }, "prefer-stable": true } diff --git a/docs/bundle/async_commands.md b/docs/bundle/async_commands.md index c82024110..8c83d2b4d 100644 --- a/docs/bundle/async_commands.md +++ b/docs/bundle/async_commands.md @@ -21,7 +21,11 @@ $ composer require enqueue/async-command:0.9.x-dev enqueue: default: - async_commands: true + async_commands: + enabled: true + timeout: 60 + command_name: ~ + queue_name: ~ ``` ## Usage diff --git a/docs/bundle/config_reference.md b/docs/bundle/config_reference.md index edbd917f2..d27877765 100644 --- a/docs/bundle/config_reference.md +++ b/docs/bundle/config_reference.md @@ -62,6 +62,9 @@ enqueue: storage_factory_class: ~ async_commands: enabled: false + timeout: 60 + command_name: ~ + queue_name: ~ job: enabled: false async_events: @@ -69,6 +72,9 @@ enqueue: extensions: doctrine_ping_connection_extension: false doctrine_clear_identity_map_extension: false + doctrine_odm_clear_identity_map_extension: false + doctrine_closed_entity_manager_extension: false + reset_services_extension: false signal_extension: true reply_extension: true ``` diff --git a/docs/consumption/extensions.md b/docs/consumption/extensions.md index 58ea0402a..8afd61e8b 100644 --- a/docs/consumption/extensions.md +++ b/docs/consumption/extensions.md @@ -22,6 +22,16 @@ It clears Doctrine's identity map after a message is processed. It reduce memory It test a database connection and if it is lost it does reconnect. Fixes "MySQL has gone away" errors. +## [DoctrineClosedEntityManagerExtension](https://github.com/php-enqueue/enqueue-dev/blob/master/pkg/enqueue-bundle/Consumption/Extension/DoctrineClosedEntityManagerExtension.php) + +The extension interrupts consumption if an entity manager has been closed. + +## [ResetServicesExtension](https://github.com/php-enqueue/enqueue-dev/blob/master/pkg/enqueue-bundle/Consumption/Extension/ResetServicesExtension.php) + +It resets all services with tag "kernel.reset". +For example, this includes all monolog loggers if installed and will flush/clean all buffers, +reset internal state, and get them back to a state in which they can receive log records again. + ## [ReplyExtension](https://github.com/php-enqueue/enqueue-dev/blob/master/pkg/enqueue/Consumption/Extension/ReplyExtension.php) It comes with RPC code and simplifies reply logic. diff --git a/docs/transport/amqp.md b/docs/transport/amqp.md index ab22ed044..f398f0043 100644 --- a/docs/transport/amqp.md +++ b/docs/transport/amqp.md @@ -11,6 +11,11 @@ nav_order: 3 Implements [AMQP specifications](https://www.rabbitmq.com/specification.html) and implements [amqp interop](https://github.com/queue-interop/amqp-interop) interfaces. Build on top of [php amqp extension](https://github.com/pdezwart/php-amqp). +Drawbacks: +* [heartbeats will not work properly](https://github.com/pdezwart/php-amqp#persistent-connection) +* [signals will not be properly handled](https://github.com/pdezwart/php-amqp#keeping-track-of-the-workers) + +Parts: * [Installation](#installation) * [Create context](#create-context) * [Declare topic](#declare-topic) diff --git a/pkg/async-command/DependencyInjection/AsyncCommandExtension.php b/pkg/async-command/DependencyInjection/AsyncCommandExtension.php index 2cfdf261c..d1f4157a2 100644 --- a/pkg/async-command/DependencyInjection/AsyncCommandExtension.php +++ b/pkg/async-command/DependencyInjection/AsyncCommandExtension.php @@ -12,13 +12,23 @@ class AsyncCommandExtension extends Extension public function load(array $configs, ContainerBuilder $container) { foreach ($configs['clients'] as $client) { - $id = sprintf('enqueue.async_command.%s.run_command_processor', $client); + // BC compatibility + if (!is_array($client)) { + $client = [ + 'name' => $client, + 'command_name' => Commands::RUN_COMMAND, + 'queue_name' => Commands::RUN_COMMAND, + 'timeout' => 60, + ]; + } + + $id = sprintf('enqueue.async_command.%s.run_command_processor', $client['name']); $container->register($id, RunCommandProcessor::class) - ->addArgument('%kernel.project_dir%') + ->addArgument('%kernel.project_dir%', $client['timeout']) ->addTag('enqueue.processor', [ - 'client' => $client, - 'command' => Commands::RUN_COMMAND, - 'queue' => Commands::RUN_COMMAND, + 'client' => $client['name'], + 'command' => $client['command_name'] ?? Commands::RUN_COMMAND, + 'queue' => $client['queue_name'] ?? Commands::RUN_COMMAND, 'prefix_queue' => false, 'exclusive' => true, ]) diff --git a/pkg/async-command/RunCommandProcessor.php b/pkg/async-command/RunCommandProcessor.php index 7925d658c..f1495f399 100644 --- a/pkg/async-command/RunCommandProcessor.php +++ b/pkg/async-command/RunCommandProcessor.php @@ -11,14 +11,20 @@ final class RunCommandProcessor implements Processor { + /** + * @var int + */ + private $timeout; + /** * @var string */ private $projectDir; - public function __construct(string $projectDir) + public function __construct(string $projectDir, int $timeout = 60) { $this->projectDir = $projectDir; + $this->timeout = $timeout; } public function process(Message $message, Context $context): Result @@ -29,7 +35,7 @@ public function process(Message $message, Context $context): Result $consoleBin = file_exists($this->projectDir.'/bin/console') ? './bin/console' : './app/console'; $process = new Process($phpBin.' '.$consoleBin.' '.$this->getCommandLine($command), $this->projectDir); - + $process->setTimeout($this->timeout); $process->run(); if ($message->getReplyTo()) { diff --git a/pkg/async-command/Tests/RunCommandProcessorTest.php b/pkg/async-command/Tests/RunCommandProcessorTest.php index 83274e2c0..3065ec315 100644 --- a/pkg/async-command/Tests/RunCommandProcessorTest.php +++ b/pkg/async-command/Tests/RunCommandProcessorTest.php @@ -28,4 +28,11 @@ public function testCouldBeConstructedWithProjectDirAsFirstArgument() $this->assertAttributeSame('aProjectDir', 'projectDir', $processor); } + + public function testCouldBeConstructedWithTimeoutAsSecondArgument() + { + $processor = new RunCommandProcessor('aProjectDir', 60); + + $this->assertAttributeSame(60, 'timeout', $processor); + } } diff --git a/pkg/dbal/DbalConsumerHelperTrait.php b/pkg/dbal/DbalConsumerHelperTrait.php index 87e720dac..31fa52b5c 100644 --- a/pkg/dbal/DbalConsumerHelperTrait.php +++ b/pkg/dbal/DbalConsumerHelperTrait.php @@ -6,7 +6,6 @@ use Doctrine\DBAL\Connection; use Doctrine\DBAL\Exception\RetryableException; -use Doctrine\DBAL\ParameterType; use Doctrine\DBAL\Types\Type; use Ramsey\Uuid\Uuid; @@ -40,7 +39,7 @@ protected function fetchMessage(array $queues, int $redeliveryDelay): ?DbalMessa ->addOrderBy('priority', 'asc') ->addOrderBy('published_at', 'asc') ->setParameter('queues', $queues, Connection::PARAM_STR_ARRAY) - ->setParameter('delayedUntil', $now, ParameterType::INTEGER) + ->setParameter('delayedUntil', $now, Type::INTEGER) ->setMaxResults(1); $update = $this->getConnection()->createQueryBuilder() diff --git a/pkg/enqueue-bundle/Consumption/Extension/DoctrineClearIdentityMapExtension.php b/pkg/enqueue-bundle/Consumption/Extension/DoctrineClearIdentityMapExtension.php index 3a973d6b2..a8f258f21 100644 --- a/pkg/enqueue-bundle/Consumption/Extension/DoctrineClearIdentityMapExtension.php +++ b/pkg/enqueue-bundle/Consumption/Extension/DoctrineClearIdentityMapExtension.php @@ -2,21 +2,21 @@ namespace Enqueue\Bundle\Consumption\Extension; +use Doctrine\Common\Persistence\ManagerRegistry; use Enqueue\Consumption\Context\MessageReceived; use Enqueue\Consumption\MessageReceivedExtensionInterface; -use Symfony\Bridge\Doctrine\RegistryInterface; class DoctrineClearIdentityMapExtension implements MessageReceivedExtensionInterface { /** - * @var RegistryInterface + * @var ManagerRegistry */ protected $registry; /** - * @param RegistryInterface $registry + * @param ManagerRegistry $registry */ - public function __construct(RegistryInterface $registry) + public function __construct(ManagerRegistry $registry) { $this->registry = $registry; } diff --git a/pkg/enqueue-bundle/Consumption/Extension/DoctrineClosedEntityManagerExtension.php b/pkg/enqueue-bundle/Consumption/Extension/DoctrineClosedEntityManagerExtension.php new file mode 100644 index 000000000..d2765231a --- /dev/null +++ b/pkg/enqueue-bundle/Consumption/Extension/DoctrineClosedEntityManagerExtension.php @@ -0,0 +1,65 @@ +registry = $registry; + } + + public function onPreConsume(PreConsume $context): void + { + if ($this->shouldBeStopped($context->getLogger())) { + $context->interruptExecution(); + } + } + + public function onPostConsume(PostConsume $context): void + { + if ($this->shouldBeStopped($context->getLogger())) { + $context->interruptExecution(); + } + } + + public function onPostMessageReceived(PostMessageReceived $context): void + { + if ($this->shouldBeStopped($context->getLogger())) { + $context->interruptExecution(); + } + } + + private function shouldBeStopped(LoggerInterface $logger): bool + { + foreach ($this->registry->getManagers() as $name => $manager) { + if (!$manager instanceof EntityManagerInterface || $manager->isOpen()) { + continue; + } + + $logger->debug(sprintf( + '[DoctrineClosedEntityManagerExtension] Interrupt execution as entity manager "%s" has been closed', + $name + )); + + return true; + } + + return false; + } +} diff --git a/pkg/enqueue-bundle/Consumption/Extension/ResetServicesExtension.php b/pkg/enqueue-bundle/Consumption/Extension/ResetServicesExtension.php new file mode 100644 index 000000000..36eb21782 --- /dev/null +++ b/pkg/enqueue-bundle/Consumption/Extension/ResetServicesExtension.php @@ -0,0 +1,27 @@ +resetter = $resetter; + } + + public function onMessageReceived(MessageReceived $context): void + { + $context->getLogger()->debug('[ResetServicesExtension] Resetting services.'); + + $this->resetter->reset(); + } +} diff --git a/pkg/enqueue-bundle/DependencyInjection/Configuration.php b/pkg/enqueue-bundle/DependencyInjection/Configuration.php index 6257fa4f1..3b6367fda 100644 --- a/pkg/enqueue-bundle/DependencyInjection/Configuration.php +++ b/pkg/enqueue-bundle/DependencyInjection/Configuration.php @@ -47,6 +47,9 @@ public function getConfigTreeBuilder(): TreeBuilder ->arrayNode('extensions')->addDefaultsIfNotSet()->children() ->booleanNode('doctrine_ping_connection_extension')->defaultFalse()->end() ->booleanNode('doctrine_clear_identity_map_extension')->defaultFalse()->end() + ->booleanNode('doctrine_odm_clear_identity_map_extension')->defaultFalse()->end() + ->booleanNode('doctrine_closed_entity_manager_extension')->defaultFalse()->end() + ->booleanNode('reset_services_extension')->defaultFalse()->end() ->booleanNode('signal_extension')->defaultValue(function_exists('pcntl_signal_dispatch'))->end() ->booleanNode('reply_extension')->defaultTrue()->end() ->end()->end() @@ -73,6 +76,12 @@ private function getAsyncCommandsConfiguration(): ArrayNodeDefinition } return (new ArrayNodeDefinition('async_commands')) + ->children() + ->booleanNode('enabled')->defaultFalse()->end() + ->integerNode('timeout')->min(0)->defaultValue(60)->end() + ->scalarNode('command_name')->defaultNull()->end() + ->scalarNode('queue_name')->defaultNull()->end() + ->end() ->addDefaultsIfNotSet() ->canBeEnabled() ; diff --git a/pkg/enqueue-bundle/DependencyInjection/EnqueueExtension.php b/pkg/enqueue-bundle/DependencyInjection/EnqueueExtension.php index b5ae10ab9..10521b3a0 100644 --- a/pkg/enqueue-bundle/DependencyInjection/EnqueueExtension.php +++ b/pkg/enqueue-bundle/DependencyInjection/EnqueueExtension.php @@ -5,7 +5,9 @@ use Enqueue\AsyncCommand\DependencyInjection\AsyncCommandExtension; use Enqueue\AsyncEventDispatcher\DependencyInjection\AsyncEventDispatcherExtension; use Enqueue\Bundle\Consumption\Extension\DoctrineClearIdentityMapExtension; +use Enqueue\Bundle\Consumption\Extension\DoctrineClosedEntityManagerExtension; use Enqueue\Bundle\Consumption\Extension\DoctrinePingConnectionExtension; +use Enqueue\Bundle\Consumption\Extension\ResetServicesExtension; use Enqueue\Bundle\Profiler\MessageQueueCollector; use Enqueue\Client\CommandSubscriberInterface; use Enqueue\Client\TopicSubscriberInterface; @@ -136,6 +138,9 @@ public function load(array $configs, ContainerBuilder $container): void // extensions $this->loadDoctrinePingConnectionExtension($config, $container); $this->loadDoctrineClearIdentityMapExtension($config, $container); + $this->loadDoctrineOdmClearIdentityMapExtension($config, $container); + $this->loadDoctrineClosedEntityManagerExtension($config, $container); + $this->loadResetServicesExtension($config, $container); $this->loadSignalExtension($config, $container); $this->loadReplyExtension($config, $container); } @@ -210,7 +215,7 @@ private function loadDoctrinePingConnectionExtension(array $config, ContainerBui } } - if (false == $configNames) { + if ([] === $configNames) { return; } @@ -233,7 +238,7 @@ private function loadDoctrineClearIdentityMapExtension(array $config, ContainerB } } - if (false == $configNames) { + if ([] === $configNames) { return; } @@ -247,6 +252,73 @@ private function loadDoctrineClearIdentityMapExtension(array $config, ContainerB } } + private function loadDoctrineOdmClearIdentityMapExtension(array $config, ContainerBuilder $container): void + { + $configNames = []; + foreach ($config as $name => $modules) { + if ($modules['extensions']['doctrine_odm_clear_identity_map_extension']) { + $configNames[] = $name; + } + } + + if ([] === $configNames) { + return; + } + + $extension = $container->register('enqueue.consumption.doctrine_odm_clear_identity_map_extension', DoctrineClearIdentityMapExtension::class) + ->addArgument(new Reference('doctrine_mongodb')) + ; + + foreach ($configNames as $name) { + $extension->addTag('enqueue.consumption_extension', ['client' => $name]); + $extension->addTag('enqueue.transport.consumption_extension', ['transport' => $name]); + } + } + + private function loadDoctrineClosedEntityManagerExtension(array $config, ContainerBuilder $container) + { + $configNames = []; + foreach ($config as $name => $modules) { + if ($modules['extensions']['doctrine_closed_entity_manager_extension']) { + $configNames[] = $name; + } + } + + if ([] === $configNames) { + return; + } + + $extension = $container->register('enqueue.consumption.doctrine_closed_entity_manager_extension', DoctrineClosedEntityManagerExtension::class) + ->addArgument(new Reference('doctrine')); + + foreach ($configNames as $name) { + $extension->addTag('enqueue.consumption_extension', ['client' => $name]); + $extension->addTag('enqueue.transport.consumption_extension', ['transport' => $name]); + } + } + + private function loadResetServicesExtension(array $config, ContainerBuilder $container) + { + $configNames = []; + foreach ($config as $name => $modules) { + if ($modules['extensions']['reset_services_extension']) { + $configNames[] = $name; + } + } + + if ([] === $configNames) { + return; + } + + $extension = $container->register('enqueue.consumption.reset_services_extension', ResetServicesExtension::class) + ->addArgument(new Reference('services_resetter')); + + foreach ($configNames as $name) { + $extension->addTag('enqueue.consumption_extension', ['client' => $name]); + $extension->addTag('enqueue.transport.consumption_extension', ['transport' => $name]); + } + } + private function loadSignalExtension(array $config, ContainerBuilder $container): void { $configNames = []; @@ -256,7 +328,7 @@ private function loadSignalExtension(array $config, ContainerBuilder $container) } } - if (false == $configNames) { + if ([] === $configNames) { return; } @@ -277,7 +349,7 @@ private function loadReplyExtension(array $config, ContainerBuilder $container): } } - if (false == $configNames) { + if ([] === $configNames) { return; } @@ -291,14 +363,19 @@ private function loadReplyExtension(array $config, ContainerBuilder $container): private function loadAsyncCommands(array $config, ContainerBuilder $container): void { - $configNames = []; + $configs = []; foreach ($config as $name => $modules) { if (false === empty($modules['async_commands']['enabled'])) { - $configNames[] = $name; + $configs[] = [ + 'name' => $name, + 'timeout' => $modules['async_commands']['timeout'], + 'command_name' => $modules['async_commands']['command_name'], + 'queue_name' => $modules['async_commands']['queue_name'], + ]; } } - if (false == $configNames) { + if (false == $configs) { return; } @@ -307,7 +384,7 @@ private function loadAsyncCommands(array $config, ContainerBuilder $container): } $extension = new AsyncCommandExtension(); - $extension->load(['clients' => $configNames], $container); + $extension->load(['clients' => $configs], $container); } private function loadMessageQueueCollector(array $config, ContainerBuilder $container) diff --git a/pkg/enqueue-bundle/Tests/Functional/App/config/config.yml b/pkg/enqueue-bundle/Tests/Functional/App/config/config.yml index 1198e60d2..562647c31 100644 --- a/pkg/enqueue-bundle/Tests/Functional/App/config/config.yml +++ b/pkg/enqueue-bundle/Tests/Functional/App/config/config.yml @@ -28,7 +28,11 @@ enqueue: traceable_producer: true job: true async_events: true - async_commands: true + async_commands: + enabled: true + timeout: 60 + command_name: ~ + queue_name: ~ services: test_enqueue.client.default.traceable_producer: @@ -122,4 +126,4 @@ services: enqueue.events.async_listener: class: 'Enqueue\Bundle\Tests\Functional\App\AsyncListener' public: true - arguments: ['@enqueue.client.default.producer', '@enqueue.events.registry'] \ No newline at end of file + arguments: ['@enqueue.client.default.producer', '@enqueue.events.registry'] diff --git a/pkg/enqueue-bundle/Tests/Unit/Consumption/Extension/DoctrineClearIdentityMapExtensionTest.php b/pkg/enqueue-bundle/Tests/Unit/Consumption/Extension/DoctrineClearIdentityMapExtensionTest.php index db3f28e00..47dceeb36 100644 --- a/pkg/enqueue-bundle/Tests/Unit/Consumption/Extension/DoctrineClearIdentityMapExtensionTest.php +++ b/pkg/enqueue-bundle/Tests/Unit/Consumption/Extension/DoctrineClearIdentityMapExtensionTest.php @@ -2,6 +2,7 @@ namespace Enqueue\Bundle\Tests\Unit\Consumption\Extension; +use Doctrine\Common\Persistence\ManagerRegistry; use Doctrine\Common\Persistence\ObjectManager; use Enqueue\Bundle\Consumption\Extension\DoctrineClearIdentityMapExtension; use Enqueue\Consumption\Context\MessageReceived; @@ -11,7 +12,6 @@ use Interop\Queue\Processor; use PHPUnit\Framework\TestCase; use Psr\Log\LoggerInterface; -use Symfony\Bridge\Doctrine\RegistryInterface; class DoctrineClearIdentityMapExtensionTest extends TestCase { @@ -59,11 +59,11 @@ protected function createContext(): MessageReceived } /** - * @return \PHPUnit_Framework_MockObject_MockObject|RegistryInterface + * @return \PHPUnit_Framework_MockObject_MockObject|ManagerRegistry */ - protected function createRegistryMock(): RegistryInterface + protected function createRegistryMock(): ManagerRegistry { - return $this->createMock(RegistryInterface::class); + return $this->createMock(ManagerRegistry::class); } /** diff --git a/pkg/enqueue-bundle/Tests/Unit/Consumption/Extension/DoctrineClosedEntityManagerExtensionTest.php b/pkg/enqueue-bundle/Tests/Unit/Consumption/Extension/DoctrineClosedEntityManagerExtensionTest.php new file mode 100644 index 000000000..1cccb0b13 --- /dev/null +++ b/pkg/enqueue-bundle/Tests/Unit/Consumption/Extension/DoctrineClosedEntityManagerExtensionTest.php @@ -0,0 +1,222 @@ +createManagerMock(true); + + $registry = $this->createRegistryMock([ + 'manager' => $manager, + ]); + + $message = new PreConsume( + $this->createMock(InteropContext::class), + $this->createMock(SubscriptionConsumer::class), + $this->createMock(LoggerInterface::class), + 1, + 2, + 3 + ); + + self::assertFalse($message->isExecutionInterrupted()); + + $extension = new DoctrineClosedEntityManagerExtension($registry); + $extension->onPreConsume($message); + + self::assertFalse($message->isExecutionInterrupted()); + } + + public function testOnPreConsumeShouldInterruptExecutionIfAManagerIsClosed() + { + $manager1 = $this->createManagerMock(true); + $manager2 = $this->createManagerMock(false); + + $registry = $this->createRegistryMock([ + 'manager1' => $manager1, + 'manager2' => $manager2, + ]); + + $message = new PreConsume( + $this->createMock(InteropContext::class), + $this->createMock(SubscriptionConsumer::class), + $this->createMock(LoggerInterface::class), + 1, + 2, + 3 + ); + $message->getLogger() + ->expects($this->once()) + ->method('debug') + ->with('[DoctrineClosedEntityManagerExtension] Interrupt execution as entity manager "manager2" has been closed') + ; + + self::assertFalse($message->isExecutionInterrupted()); + + $extension = new DoctrineClosedEntityManagerExtension($registry); + $extension->onPreConsume($message); + + self::assertTrue($message->isExecutionInterrupted()); + } + + public function testOnPostConsumeShouldNotInterruptExecution() + { + $manager = $this->createManagerMock(true); + + $registry = $this->createRegistryMock([ + 'manager' => $manager, + ]); + + $message = new PostConsume( + $this->createMock(InteropContext::class), + $this->createMock(SubscriptionConsumer::class), + 1, + 1, + 1, + $this->createMock(LoggerInterface::class) + ); + + self::assertFalse($message->isExecutionInterrupted()); + + $extension = new DoctrineClosedEntityManagerExtension($registry); + $extension->onPostConsume($message); + + self::assertFalse($message->isExecutionInterrupted()); + } + + public function testOnPostConsumeShouldInterruptExecutionIfAManagerIsClosed() + { + $manager1 = $this->createManagerMock(true); + $manager2 = $this->createManagerMock(false); + + $registry = $this->createRegistryMock([ + 'manager1' => $manager1, + 'manager2' => $manager2, + ]); + + $message = new PostConsume( + $this->createMock(InteropContext::class), + $this->createMock(SubscriptionConsumer::class), + 1, + 1, + 1, + $this->createMock(LoggerInterface::class) + ); + $message->getLogger() + ->expects($this->once()) + ->method('debug') + ->with('[DoctrineClosedEntityManagerExtension] Interrupt execution as entity manager "manager2" has been closed') + ; + + self::assertFalse($message->isExecutionInterrupted()); + + $extension = new DoctrineClosedEntityManagerExtension($registry); + $extension->onPostConsume($message); + + self::assertTrue($message->isExecutionInterrupted()); + } + + public function testOnPostReceivedShouldNotInterruptExecution() + { + $manager = $this->createManagerMock(true); + + $registry = $this->createRegistryMock([ + 'manager' => $manager, + ]); + + $message = new PostMessageReceived( + $this->createMock(InteropContext::class), + $this->createMock(Consumer::class), + $this->createMock(Message::class), + 'aResult', + 1, + $this->createMock(LoggerInterface::class) + ); + + self::assertFalse($message->isExecutionInterrupted()); + + $extension = new DoctrineClosedEntityManagerExtension($registry); + $extension->onPostMessageReceived($message); + + self::assertFalse($message->isExecutionInterrupted()); + } + + public function testOnPostReceivedShouldInterruptExecutionIfAManagerIsClosed() + { + $manager1 = $this->createManagerMock(true); + $manager2 = $this->createManagerMock(false); + + $registry = $this->createRegistryMock([ + 'manager1' => $manager1, + 'manager2' => $manager2, + ]); + + $message = new PostMessageReceived( + $this->createMock(InteropContext::class), + $this->createMock(Consumer::class), + $this->createMock(Message::class), + 'aResult', + 1, + $this->createMock(LoggerInterface::class) + ); + $message->getLogger() + ->expects($this->once()) + ->method('debug') + ->with('[DoctrineClosedEntityManagerExtension] Interrupt execution as entity manager "manager2" has been closed') + ; + + self::assertFalse($message->isExecutionInterrupted()); + + $extension = new DoctrineClosedEntityManagerExtension($registry); + $extension->onPostMessageReceived($message); + + self::assertTrue($message->isExecutionInterrupted()); + } + + /** + * @return \PHPUnit_Framework_MockObject_MockObject|ManagerRegistry + */ + protected function createRegistryMock(array $managers): ManagerRegistry + { + $mock = $this->createMock(ManagerRegistry::class); + + $mock + ->expects($this->once()) + ->method('getManagers') + ->willReturn($managers) + ; + + return $mock; + } + + /** + * @return \PHPUnit_Framework_MockObject_MockObject|EntityManagerInterface + */ + protected function createManagerMock(bool $open): EntityManagerInterface + { + $mock = $this->createMock(EntityManagerInterface::class); + + $mock + ->expects($this->once()) + ->method('isOpen') + ->willReturn($open) + ; + + return $mock; + } +} diff --git a/pkg/enqueue-bundle/Tests/Unit/Consumption/Extension/ResetServicesExtensionTest.php b/pkg/enqueue-bundle/Tests/Unit/Consumption/Extension/ResetServicesExtensionTest.php new file mode 100644 index 000000000..e5f787420 --- /dev/null +++ b/pkg/enqueue-bundle/Tests/Unit/Consumption/Extension/ResetServicesExtensionTest.php @@ -0,0 +1,61 @@ +createResetterMock()); + } + + public function testItShouldResetServices() + { + $resetter = $this->createResetterMock(); + $resetter + ->expects($this->once()) + ->method('reset') + ; + + $context = $this->createContext(); + $context->getLogger() + ->expects($this->once()) + ->method('debug') + ->with('[ResetServicesExtension] Resetting services.') + ; + + $extension = new ResetServicesExtension($resetter); + $extension->onMessageReceived($context); + } + + protected function createContext(): MessageReceived + { + return new MessageReceived( + $this->createMock(InteropContext::class), + $this->createMock(Consumer::class), + $this->createMock(Message::class), + $this->createMock(Processor::class), + 1, + $this->createMock(LoggerInterface::class) + ); + } + + /** + * @return \PHPUnit_Framework_MockObject_MockObject|ManagerRegistry + */ + protected function createResetterMock(): ServicesResetter + { + return $this->createMock(ServicesResetter::class); + } +} diff --git a/pkg/enqueue-bundle/Tests/Unit/DependencyInjection/ConfigurationTest.php b/pkg/enqueue-bundle/Tests/Unit/DependencyInjection/ConfigurationTest.php index d33d9e0e3..3f3aae8ee 100644 --- a/pkg/enqueue-bundle/Tests/Unit/DependencyInjection/ConfigurationTest.php +++ b/pkg/enqueue-bundle/Tests/Unit/DependencyInjection/ConfigurationTest.php @@ -336,6 +336,135 @@ public function testDoctrineClearIdentityMapExtensionCouldBeEnabled() ], $config); } + public function testDoctrineOdmClearIdentityMapExtensionShouldBeDisabledByDefault() + { + $configuration = new Configuration(true); + + $processor = new Processor(); + $config = $processor->processConfiguration($configuration, [[ + 'default' => [ + 'transport' => null, + ], + ]]); + + $this->assertArraySubset([ + 'default' => [ + 'extensions' => [ + 'doctrine_odm_clear_identity_map_extension' => false, + ], + ], + ], $config); + } + + public function testDoctrineOdmClearIdentityMapExtensionCouldBeEnabled() + { + $configuration = new Configuration(true); + + $processor = new Processor(); + $config = $processor->processConfiguration($configuration, [[ + 'default' => [ + 'transport' => [], + 'extensions' => [ + 'doctrine_odm_clear_identity_map_extension' => true, + ], + ], + ]]); + + $this->assertArraySubset([ + 'default' => [ + 'extensions' => [ + 'doctrine_odm_clear_identity_map_extension' => true, + ], + ], + ], $config); + } + + public function testDoctrineClosedEntityManagerExtensionShouldBeDisabledByDefault() + { + $configuration = new Configuration(true); + + $processor = new Processor(); + $config = $processor->processConfiguration($configuration, [[ + 'default' => [ + 'transport' => null, + ], + ]]); + + $this->assertArraySubset([ + 'default' => [ + 'extensions' => [ + 'doctrine_closed_entity_manager_extension' => false, + ], + ], + ], $config); + } + + public function testDoctrineClosedEntityManagerExtensionCouldBeEnabled() + { + $configuration = new Configuration(true); + + $processor = new Processor(); + $config = $processor->processConfiguration($configuration, [[ + 'default' => [ + 'transport' => null, + 'extensions' => [ + 'doctrine_closed_entity_manager_extension' => true, + ], + ], + ]]); + + $this->assertArraySubset([ + 'default' => [ + 'extensions' => [ + 'doctrine_closed_entity_manager_extension' => true, + ], + ], + ], $config); + } + + public function testResetServicesExtensionShouldBeDisabledByDefault() + { + $configuration = new Configuration(true); + + $processor = new Processor(); + $config = $processor->processConfiguration($configuration, [[ + 'default' => [ + 'transport' => null, + ], + ]]); + + $this->assertArraySubset([ + 'default' => [ + 'extensions' => [ + 'reset_services_extension' => false, + ], + ], + ], $config); + } + + public function testResetServicesExtensionCouldBeEnabled() + { + $configuration = new Configuration(true); + + $processor = new Processor(); + $config = $processor->processConfiguration($configuration, [[ + 'default' => [ + 'transport' => [], + 'extensions' => [ + 'reset_services_extension' => true, + ], + ], + ]]); + + $this->assertArraySubset([ + 'default' => [ + 'extensions' => [ + 'reset_services_extension' => true, + ], + ], + ], $config); + } + public function testSignalExtensionShouldBeEnabledIfPcntlExtensionIsLoaded() { $isLoaded = function_exists('pcntl_signal_dispatch'); diff --git a/pkg/enqueue-bundle/Tests/Unit/DependencyInjection/EnqueueExtensionTest.php b/pkg/enqueue-bundle/Tests/Unit/DependencyInjection/EnqueueExtensionTest.php index b17d6a949..51bbaa26d 100644 --- a/pkg/enqueue-bundle/Tests/Unit/DependencyInjection/EnqueueExtensionTest.php +++ b/pkg/enqueue-bundle/Tests/Unit/DependencyInjection/EnqueueExtensionTest.php @@ -384,6 +384,114 @@ public function testShouldNotLoadDoctrineClearIdentityMapExtensionServiceIfDisab self::assertFalse($container->hasDefinition('enqueue.consumption.doctrine_clear_identity_map_extension')); } + public function testShouldLoadDoctrineOdmClearIdentityMapExtensionServiceIfEnabled() + { + $container = $this->getContainerBuilder(true); + + $extension = new EnqueueExtension(); + + $extension->load([[ + 'default' => [ + 'transport' => [], + 'extensions' => [ + 'doctrine_odm_clear_identity_map_extension' => true, + ], + ], + ]], $container); + + self::assertTrue($container->hasDefinition('enqueue.consumption.doctrine_odm_clear_identity_map_extension')); + } + + public function testShouldNotLoadDoctrineOdmClearIdentityMapExtensionServiceIfDisabled() + { + $container = $this->getContainerBuilder(true); + + $extension = new EnqueueExtension(); + + $extension->load([[ + 'default' => [ + 'transport' => [], + 'extensions' => [ + 'doctrine_odm_clear_identity_map_extension' => false, + ], + ], + ]], $container); + + self::assertFalse($container->hasDefinition('enqueue.consumption.doctrine_odm_clear_identity_map_extension')); + } + + public function testShouldLoadDoctrineClosedEntityManagerExtensionServiceIfEnabled() + { + $container = $this->getContainerBuilder(true); + + $extension = new EnqueueExtension(); + + $extension->load([[ + 'default' => [ + 'transport' => [], + 'extensions' => [ + 'doctrine_closed_entity_manager_extension' => true, + ], + ], + ]], $container); + + self::assertTrue($container->hasDefinition('enqueue.consumption.doctrine_closed_entity_manager_extension')); + } + + public function testShouldNotLoadDoctrineClosedEntityManagerExtensionServiceIfDisabled() + { + $container = $this->getContainerBuilder(true); + + $extension = new EnqueueExtension(); + + $extension->load([[ + 'default' => [ + 'transport' => [], + 'extensions' => [ + 'doctrine_closed_entity_manager_extension' => false, + ], + ], + ]], $container); + + self::assertFalse($container->hasDefinition('enqueue.consumption.doctrine_closed_entity_manager_extension')); + } + + public function testShouldLoadResetServicesExtensionServiceIfEnabled() + { + $container = $this->getContainerBuilder(true); + + $extension = new EnqueueExtension(); + + $extension->load([[ + 'default' => [ + 'transport' => [], + 'extensions' => [ + 'reset_services_extension' => true, + ], + ], + ]], $container); + + self::assertTrue($container->hasDefinition('enqueue.consumption.reset_services_extension')); + } + + public function testShouldNotLoadResetServicesExtensionServiceIfDisabled() + { + $container = $this->getContainerBuilder(true); + + $extension = new EnqueueExtension(); + + $extension->load([[ + 'default' => [ + 'transport' => [], + 'extensions' => [ + 'reset_services_extension' => false, + ], + ], + ]], $container); + + self::assertFalse($container->hasDefinition('enqueue.consumption.reset_services_extension')); + } + public function testShouldLoadSignalExtensionServiceIfEnabled() { $container = $this->getContainerBuilder(true); diff --git a/pkg/enqueue-bundle/Tests/fix_composer_json.php b/pkg/enqueue-bundle/Tests/fix_composer_json.php index f025f6081..6cc24aa03 100644 --- a/pkg/enqueue-bundle/Tests/fix_composer_json.php +++ b/pkg/enqueue-bundle/Tests/fix_composer_json.php @@ -5,5 +5,6 @@ $composerJson = json_decode(file_get_contents(__DIR__.'/../composer.json'), true); $composerJson['config']['platform']['ext-amqp'] = '1.9.3'; +$composerJson['config']['platform']['ext-mongo'] = '1.6.14'; file_put_contents(__DIR__.'/../composer.json', json_encode($composerJson, JSON_PRETTY_PRINT)); diff --git a/pkg/enqueue-bundle/composer.json b/pkg/enqueue-bundle/composer.json index 63620a3b5..49a637ab3 100644 --- a/pkg/enqueue-bundle/composer.json +++ b/pkg/enqueue-bundle/composer.json @@ -37,6 +37,8 @@ "enqueue/async-command": "0.9.x-dev", "php-amqplib/php-amqplib": "^2.7", "doctrine/doctrine-bundle": "~1.2", + "doctrine/mongodb-odm-bundle": "^3.5", + "alcaeus/mongo-php-adapter": "^1.0", "symfony/browser-kit": "^3.4|^4", "symfony/expression-language": "^3.4|^4" },