From eeb9a5c3d91de2bc76c4112d0b568177d6c18d9f Mon Sep 17 00:00:00 2001 From: Alexander Kozienko Date: Thu, 4 May 2017 15:45:04 +0300 Subject: [PATCH 1/2] fix issue 67 --- .../DependencyInjection/EnqueueExtension.php | 42 ++++++- pkg/enqueue-bundle/Entity/Job.php | 108 ------------------ pkg/enqueue-bundle/Entity/JobUnique.php | 18 --- pkg/enqueue-bundle/Resources/config/job.yml | 4 +- .../Tests/Functional/Job/JobStorageTest.php | 2 +- .../EnqueueExtensionTest.php | 43 +++++++ .../CalculateRootJobStatusProcessor.php | 1 + .../CalculateRootJobStatusService.php | 1 + pkg/job-queue/DependentJobProcessor.php | 1 + pkg/job-queue/DependentJobService.php | 2 + pkg/job-queue/Doctrine/Entity/Job.php | 15 +++ pkg/job-queue/Doctrine/Entity/JobUnique.php | 7 ++ pkg/job-queue/{ => Doctrine}/JobStorage.php | 4 +- pkg/job-queue/Doctrine/mapping/Job.orm.xml | 21 ++++ .../Doctrine/mapping/JobUnique.orm.xml | 8 ++ pkg/job-queue/JobProcessor.php | 1 + .../CalculateRootJobStatusProcessorTest.php | 2 +- .../CalculateRootJobStatusServiceTest.php | 4 +- .../Tests/DependentJobProcessorTest.php | 4 +- .../Tests/DependentJobServiceTest.php | 4 +- .../Tests/{ => Doctrine}/JobStorageTest.php | 4 +- .../Tests/Functional/Job/JobStorageTest.php | 4 +- pkg/job-queue/Tests/JobProcessorTest.php | 2 +- 23 files changed, 159 insertions(+), 143 deletions(-) delete mode 100644 pkg/enqueue-bundle/Entity/Job.php delete mode 100644 pkg/enqueue-bundle/Entity/JobUnique.php create mode 100644 pkg/job-queue/Doctrine/Entity/Job.php create mode 100644 pkg/job-queue/Doctrine/Entity/JobUnique.php rename pkg/job-queue/{ => Doctrine}/JobStorage.php (98%) create mode 100644 pkg/job-queue/Doctrine/mapping/Job.orm.xml create mode 100644 pkg/job-queue/Doctrine/mapping/JobUnique.orm.xml rename pkg/job-queue/Tests/{ => Doctrine}/JobStorageTest.php (99%) diff --git a/pkg/enqueue-bundle/DependencyInjection/EnqueueExtension.php b/pkg/enqueue-bundle/DependencyInjection/EnqueueExtension.php index 6907c6412..0ba9dcf2e 100644 --- a/pkg/enqueue-bundle/DependencyInjection/EnqueueExtension.php +++ b/pkg/enqueue-bundle/DependencyInjection/EnqueueExtension.php @@ -8,11 +8,12 @@ use Symfony\Component\Config\FileLocator; use Symfony\Component\Config\Resource\FileResource; use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface; use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\HttpKernel\DependencyInjection\Extension; -class EnqueueExtension extends Extension +class EnqueueExtension extends Extension implements PrependExtensionInterface { /** * @var TransportFactoryInterface[] @@ -132,4 +133,43 @@ public function getConfiguration(array $config, ContainerBuilder $container) return new Configuration($this->factories); } + + public function prepend(ContainerBuilder $container) + { + $this->registerJobQueueDoctrineEntityMapping($container); + } + + private function registerJobQueueDoctrineEntityMapping(ContainerBuilder $container) + { + if (false == class_exists(Job::class)) { + return; + } + + $bundles = $container->getParameter('kernel.bundles'); + + if (false == isset($bundles['DoctrineBundle'])) { + return; + } + + foreach ($container->getExtensionConfig('doctrine') as $config) { + // do not register mappings if dbal not configured. + if (false == empty($config['dbal'])) { + $rc = new \ReflectionClass(Job::class); + $jobQueueRootDir = dirname($rc->getFileName()); + $container->prependExtensionConfig('doctrine', [ + 'orm' => [ + 'mappings' => [ + 'enqueue_job_queue' => [ + 'is_bundle' => false, + 'type' => 'xml', + 'dir' => $jobQueueRootDir.'/Doctrine/mapping', + 'prefix' => 'Enqueue\JobQueue\Doctrine\Entity', + ], + ], + ], + ]); + break; + } + } + } } diff --git a/pkg/enqueue-bundle/Entity/Job.php b/pkg/enqueue-bundle/Entity/Job.php deleted file mode 100644 index 38abf755e..000000000 --- a/pkg/enqueue-bundle/Entity/Job.php +++ /dev/null @@ -1,108 +0,0 @@ -childJobs = new ArrayCollection(); - } -} diff --git a/pkg/enqueue-bundle/Entity/JobUnique.php b/pkg/enqueue-bundle/Entity/JobUnique.php deleted file mode 100644 index 3c7426bb9..000000000 --- a/pkg/enqueue-bundle/Entity/JobUnique.php +++ /dev/null @@ -1,18 +0,0 @@ -hasDefinition('enqueue.consumption.signal_extension')); } + + public function testShouldAddJobQueueEntityMapping() + { + $container = new ContainerBuilder(); + $container->setParameter('kernel.bundles', ['DoctrineBundle' => true]); + $container->prependExtensionConfig('doctrine', ['dbal' => true]); + + $extension = new EnqueueExtension(); + + $extension->prepend($container); + + $expectedConfig = [ + [ + 'orm' => [ + 'mappings' => [ + 'enqueue_job_queue' => [ + 'is_bundle' => false, + 'type' => 'xml', + 'dir' => '/mqdev/pkg/job-queue/Doctrine/mapping', + 'prefix' => 'Enqueue\JobQueue\Doctrine\Entity', + ], + ], + ], + ], + [ + 'dbal' => true, + ], + ]; + + $this->assertSame($expectedConfig, $container->getExtensionConfig('doctrine')); + } + + public function testShouldNotAddJobQueueEntityMappingIfDoctrineBundleIsNotRegistered() + { + $container = new ContainerBuilder(); + $container->setParameter('kernel.bundles', []); + + $extension = new EnqueueExtension(); + + $extension->prepend($container); + + $this->assertSame([], $container->getExtensionConfig('doctrine')); + } } diff --git a/pkg/job-queue/CalculateRootJobStatusProcessor.php b/pkg/job-queue/CalculateRootJobStatusProcessor.php index b2a8aab3a..cdc27a44d 100644 --- a/pkg/job-queue/CalculateRootJobStatusProcessor.php +++ b/pkg/job-queue/CalculateRootJobStatusProcessor.php @@ -5,6 +5,7 @@ use Enqueue\Client\ProducerInterface; use Enqueue\Client\TopicSubscriberInterface; use Enqueue\Consumption\Result; +use Enqueue\JobQueue\Doctrine\JobStorage; use Enqueue\Psr\PsrContext; use Enqueue\Psr\PsrMessage; use Enqueue\Psr\PsrProcessor; diff --git a/pkg/job-queue/CalculateRootJobStatusService.php b/pkg/job-queue/CalculateRootJobStatusService.php index ae54315da..af4631e2a 100644 --- a/pkg/job-queue/CalculateRootJobStatusService.php +++ b/pkg/job-queue/CalculateRootJobStatusService.php @@ -3,6 +3,7 @@ namespace Enqueue\JobQueue; use Doctrine\Common\Collections\Collection; +use Enqueue\JobQueue\Doctrine\JobStorage; class CalculateRootJobStatusService { diff --git a/pkg/job-queue/DependentJobProcessor.php b/pkg/job-queue/DependentJobProcessor.php index c968e4271..e87918718 100644 --- a/pkg/job-queue/DependentJobProcessor.php +++ b/pkg/job-queue/DependentJobProcessor.php @@ -6,6 +6,7 @@ use Enqueue\Client\ProducerInterface; use Enqueue\Client\TopicSubscriberInterface; use Enqueue\Consumption\Result; +use Enqueue\JobQueue\Doctrine\JobStorage; use Enqueue\Psr\PsrContext; use Enqueue\Psr\PsrMessage; use Enqueue\Psr\PsrProcessor; diff --git a/pkg/job-queue/DependentJobService.php b/pkg/job-queue/DependentJobService.php index e0d42b961..b6066d669 100644 --- a/pkg/job-queue/DependentJobService.php +++ b/pkg/job-queue/DependentJobService.php @@ -2,6 +2,8 @@ namespace Enqueue\JobQueue; +use Enqueue\JobQueue\Doctrine\JobStorage; + class DependentJobService { /** diff --git a/pkg/job-queue/Doctrine/Entity/Job.php b/pkg/job-queue/Doctrine/Entity/Job.php new file mode 100644 index 000000000..7dcfcb477 --- /dev/null +++ b/pkg/job-queue/Doctrine/Entity/Job.php @@ -0,0 +1,15 @@ +childJobs = new ArrayCollection(); + } +} diff --git a/pkg/job-queue/Doctrine/Entity/JobUnique.php b/pkg/job-queue/Doctrine/Entity/JobUnique.php new file mode 100644 index 000000000..dfa622432 --- /dev/null +++ b/pkg/job-queue/Doctrine/Entity/JobUnique.php @@ -0,0 +1,7 @@ + + + + + + + + + + + + + + + + + + + + + diff --git a/pkg/job-queue/Doctrine/mapping/JobUnique.orm.xml b/pkg/job-queue/Doctrine/mapping/JobUnique.orm.xml new file mode 100644 index 000000000..0b5496a58 --- /dev/null +++ b/pkg/job-queue/Doctrine/mapping/JobUnique.orm.xml @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/pkg/job-queue/JobProcessor.php b/pkg/job-queue/JobProcessor.php index 053f6aac2..f2272d7d6 100644 --- a/pkg/job-queue/JobProcessor.php +++ b/pkg/job-queue/JobProcessor.php @@ -3,6 +3,7 @@ namespace Enqueue\JobQueue; use Enqueue\Client\ProducerInterface; +use Enqueue\JobQueue\Doctrine\JobStorage; class JobProcessor { diff --git a/pkg/job-queue/Tests/CalculateRootJobStatusProcessorTest.php b/pkg/job-queue/Tests/CalculateRootJobStatusProcessorTest.php index 63f0956c4..5cd162713 100644 --- a/pkg/job-queue/Tests/CalculateRootJobStatusProcessorTest.php +++ b/pkg/job-queue/Tests/CalculateRootJobStatusProcessorTest.php @@ -7,7 +7,7 @@ use Enqueue\JobQueue\CalculateRootJobStatusProcessor; use Enqueue\JobQueue\CalculateRootJobStatusService; use Enqueue\JobQueue\Job; -use Enqueue\JobQueue\JobStorage; +use Enqueue\JobQueue\Doctrine\JobStorage; use Enqueue\JobQueue\Topics; use Enqueue\Psr\PsrContext; use Enqueue\Null\NullMessage; diff --git a/pkg/job-queue/Tests/CalculateRootJobStatusServiceTest.php b/pkg/job-queue/Tests/CalculateRootJobStatusServiceTest.php index 47e2c6e21..d6229da36 100644 --- a/pkg/job-queue/Tests/CalculateRootJobStatusServiceTest.php +++ b/pkg/job-queue/Tests/CalculateRootJobStatusServiceTest.php @@ -4,7 +4,7 @@ use Enqueue\JobQueue\CalculateRootJobStatusService; use Enqueue\JobQueue\Job; -use Enqueue\JobQueue\JobStorage; +use Enqueue\JobQueue\Doctrine\JobStorage; class CalculateRootJobStatusServiceTest extends \PHPUnit\Framework\TestCase { @@ -356,7 +356,7 @@ public function testShouldSetStatusSuccessIfAllAreSuccess() } /** - * @return \PHPUnit_Framework_MockObject_MockObject|JobStorage + * @return \PHPUnit_Framework_MockObject_MockObject|\Enqueue\JobQueue\Doctrine\JobStorage */ private function createJobStorageMock() { diff --git a/pkg/job-queue/Tests/DependentJobProcessorTest.php b/pkg/job-queue/Tests/DependentJobProcessorTest.php index fc2b0b80d..94c51f967 100644 --- a/pkg/job-queue/Tests/DependentJobProcessorTest.php +++ b/pkg/job-queue/Tests/DependentJobProcessorTest.php @@ -7,7 +7,7 @@ use Enqueue\Consumption\Result; use Enqueue\JobQueue\DependentJobProcessor; use Enqueue\JobQueue\Job; -use Enqueue\JobQueue\JobStorage; +use Enqueue\JobQueue\Doctrine\JobStorage; use Enqueue\JobQueue\Topics; use Enqueue\Psr\PsrContext; use Enqueue\Null\NullMessage; @@ -325,7 +325,7 @@ private function createContextMock() } /** - * @return \PHPUnit_Framework_MockObject_MockObject|JobStorage + * @return \PHPUnit_Framework_MockObject_MockObject|\Enqueue\JobQueue\Doctrine\JobStorage */ private function createJobStorageMock() { diff --git a/pkg/job-queue/Tests/DependentJobServiceTest.php b/pkg/job-queue/Tests/DependentJobServiceTest.php index 1f6f5796d..4b7156b19 100644 --- a/pkg/job-queue/Tests/DependentJobServiceTest.php +++ b/pkg/job-queue/Tests/DependentJobServiceTest.php @@ -5,7 +5,7 @@ use Enqueue\JobQueue\DependentJobContext; use Enqueue\JobQueue\DependentJobService; use Enqueue\JobQueue\Job; -use Enqueue\JobQueue\JobStorage; +use Enqueue\JobQueue\Doctrine\JobStorage; class DependentJobServiceTest extends \PHPUnit\Framework\TestCase { @@ -66,7 +66,7 @@ public function testShouldSaveDependentJobs() } /** - * @return \PHPUnit_Framework_MockObject_MockObject|JobStorage + * @return \PHPUnit_Framework_MockObject_MockObject|\Enqueue\JobQueue\Doctrine\JobStorage */ private function createJobStorageMock() { diff --git a/pkg/job-queue/Tests/JobStorageTest.php b/pkg/job-queue/Tests/Doctrine/JobStorageTest.php similarity index 99% rename from pkg/job-queue/Tests/JobStorageTest.php rename to pkg/job-queue/Tests/Doctrine/JobStorageTest.php index ec1fc25ef..7302cc3ce 100644 --- a/pkg/job-queue/Tests/JobStorageTest.php +++ b/pkg/job-queue/Tests/Doctrine/JobStorageTest.php @@ -1,6 +1,6 @@ Date: Thu, 4 May 2017 16:22:00 +0300 Subject: [PATCH 2/2] fix test --- .../EnqueueExtensionTest.php | 21 +++---------------- 1 file changed, 3 insertions(+), 18 deletions(-) diff --git a/pkg/enqueue-bundle/Tests/Unit/DependencyInjection/EnqueueExtensionTest.php b/pkg/enqueue-bundle/Tests/Unit/DependencyInjection/EnqueueExtensionTest.php index 086a55ecb..84210f390 100644 --- a/pkg/enqueue-bundle/Tests/Unit/DependencyInjection/EnqueueExtensionTest.php +++ b/pkg/enqueue-bundle/Tests/Unit/DependencyInjection/EnqueueExtensionTest.php @@ -445,25 +445,10 @@ public function testShouldAddJobQueueEntityMapping() $extension->prepend($container); - $expectedConfig = [ - [ - 'orm' => [ - 'mappings' => [ - 'enqueue_job_queue' => [ - 'is_bundle' => false, - 'type' => 'xml', - 'dir' => '/mqdev/pkg/job-queue/Doctrine/mapping', - 'prefix' => 'Enqueue\JobQueue\Doctrine\Entity', - ], - ], - ], - ], - [ - 'dbal' => true, - ], - ]; + $config = $container->getExtensionConfig('doctrine'); - $this->assertSame($expectedConfig, $container->getExtensionConfig('doctrine')); + $this->assertSame(['dbal' => true], $config[1]); + $this->assertNotEmpty($config[0]['orm']['mappings']['enqueue_job_queue']); } public function testShouldNotAddJobQueueEntityMappingIfDoctrineBundleIsNotRegistered()