From 8079038b66da332548c51017e9125603a6b88ab2 Mon Sep 17 00:00:00 2001 From: Warnar Boekkooi Date: Wed, 20 May 2015 15:58:33 +0800 Subject: [PATCH 1/5] CommandHandlerCompilerPass renamed to CommandHandlerPass and moved into DependencyInjection/Compiler --- .../CommandHandlerPass.php} | 4 ++-- TacticianBundle.php | 4 ++-- .../CommandHandlerPassTest.php} | 10 +++++----- 3 files changed, 9 insertions(+), 9 deletions(-) rename DependencyInjection/{CommandHandlerCompilerPass.php => Compiler/CommandHandlerPass.php} (90%) rename Tests/DependencyInjection/{CommandHandlerCompilerPassTest.php => Compiler/CommandHandlerPassTest.php} (90%) diff --git a/DependencyInjection/CommandHandlerCompilerPass.php b/DependencyInjection/Compiler/CommandHandlerPass.php similarity index 90% rename from DependencyInjection/CommandHandlerCompilerPass.php rename to DependencyInjection/Compiler/CommandHandlerPass.php index eb63b15..e8a3333 100644 --- a/DependencyInjection/CommandHandlerCompilerPass.php +++ b/DependencyInjection/Compiler/CommandHandlerPass.php @@ -1,5 +1,5 @@ addCompilerPass(new CommandHandlerCompilerPass()); + $container->addCompilerPass(new CommandHandlerPass()); } public function getContainerExtension() diff --git a/Tests/DependencyInjection/CommandHandlerCompilerPassTest.php b/Tests/DependencyInjection/Compiler/CommandHandlerPassTest.php similarity index 90% rename from Tests/DependencyInjection/CommandHandlerCompilerPassTest.php rename to Tests/DependencyInjection/Compiler/CommandHandlerPassTest.php index 32196c2..901b0ac 100644 --- a/Tests/DependencyInjection/CommandHandlerCompilerPassTest.php +++ b/Tests/DependencyInjection/Compiler/CommandHandlerPassTest.php @@ -1,13 +1,13 @@ container = \Mockery::mock(ContainerBuilder::class); - $this->compiler = new CommandHandlerCompilerPass(); + $this->compiler = new CommandHandlerPass(); } public function testProcess() From 7de66f7ef0b4be5851e1b5332645486a42f7ffad Mon Sep 17 00:00:00 2001 From: Warnar Boekkooi Date: Wed, 20 May 2015 15:59:48 +0800 Subject: [PATCH 2/5] Removed tactician-doctrine requirement and added support for multiple EntityManagers --- .../Compiler/DoctrineMiddlewarePass.php | 40 ++++++ Resources/config/services/services.yml | 5 - TacticianBundle.php | 2 + .../Compiler/DoctrineMiddlewarePassTest.php | 133 ++++++++++++++++++ composer.json | 5 +- 5 files changed, 178 insertions(+), 7 deletions(-) create mode 100644 DependencyInjection/Compiler/DoctrineMiddlewarePass.php create mode 100644 Tests/DependencyInjection/Compiler/DoctrineMiddlewarePassTest.php diff --git a/DependencyInjection/Compiler/DoctrineMiddlewarePass.php b/DependencyInjection/Compiler/DoctrineMiddlewarePass.php new file mode 100644 index 0000000..a1bcbd3 --- /dev/null +++ b/DependencyInjection/Compiler/DoctrineMiddlewarePass.php @@ -0,0 +1,40 @@ +hasParameter('doctrine.entity_managers')) { + return; + } + + $entityManagers = $container->getParameter('doctrine.entity_managers'); + if (empty($entityManagers)) { + return; + } + + foreach ($entityManagers as $name => $serviceId) { + $container->setDefinition( + sprintf('tactician.middleware.doctrine.%s', $name), + new Definition(TransactionMiddleware::class, [ new Reference($serviceId) ]) + ); + } + + $defaultEntityManager = $container->getParameter('doctrine.default_entity_manager'); + $container->setAlias('tactician.middleware.doctrine', sprintf('tactician.middleware.doctrine.%s', $defaultEntityManager)); + } +} + diff --git a/Resources/config/services/services.yml b/Resources/config/services/services.yml index 6f041a9..e34e3f2 100644 --- a/Resources/config/services/services.yml +++ b/Resources/config/services/services.yml @@ -23,11 +23,6 @@ services: arguments: - @?validator - tactician.middleware.doctrine: - class: League\Tactician\Doctrine\ORM\TransactionMiddleware - arguments: - - @doctrine.orm.entity_manager - # The standard Handler method name inflectors tactician.handler.method_name_inflector.handle: class: League\Tactician\Handler\MethodNameInflector\HandleInflector diff --git a/TacticianBundle.php b/TacticianBundle.php index e258315..dc98010 100755 --- a/TacticianBundle.php +++ b/TacticianBundle.php @@ -2,6 +2,7 @@ use Symfony\Component\DependencyInjection\ContainerBuilder; use League\Tactician\Bundle\DependencyInjection\Compiler\CommandHandlerPass; +use League\Tactician\Bundle\DependencyInjection\Compiler\DoctrineMiddlewarePass; use League\Tactician\Bundle\DependencyInjection\TacticianExtension; use Symfony\Component\HttpKernel\Bundle\Bundle; @@ -10,6 +11,7 @@ class TacticianBundle extends Bundle public function build(ContainerBuilder $container) { parent::build($container); + $container->addCompilerPass(new DoctrineMiddlewarePass()); $container->addCompilerPass(new CommandHandlerPass()); } diff --git a/Tests/DependencyInjection/Compiler/DoctrineMiddlewarePassTest.php b/Tests/DependencyInjection/Compiler/DoctrineMiddlewarePassTest.php new file mode 100644 index 0000000..990e9b7 --- /dev/null +++ b/Tests/DependencyInjection/Compiler/DoctrineMiddlewarePassTest.php @@ -0,0 +1,133 @@ +container = \Mockery::mock(ContainerBuilder::class); + + $this->compiler = new DoctrineMiddlewarePass(); + } + + public function testProcess() + { + if (!class_exists(TransactionMiddleware::class)) { + $this->markTestSkipped('"league/tactician-doctrine" is not installed'); + } + + $this->container->shouldReceive('hasParameter') + ->with('doctrine.entity_managers') + ->once() + ->andReturn(true); + + $this->container->shouldReceive('getParameter') + ->with('doctrine.entity_managers') + ->once() + ->andReturn([ + 'default' => 'doctrine.orm.default_entity_manager', + 'second' => 'doctrine.orm.second_entity_manager', + ]); + + $this->container->shouldReceive('getParameter') + ->with('doctrine.default_entity_manager') + ->once() + ->andReturn('default'); + + $this->container->shouldReceive('setDefinition') + ->andReturnUsing(function($name, Definition $def) { + \PHPUnit_Framework_Assert::assertEquals('tactician.middleware.doctrine.default', $name); + + \PHPUnit_Framework_Assert::assertEquals(TransactionMiddleware::class, $def->getClass()); + \PHPUnit_Framework_Assert::assertCount(1, $def->getArguments()); + \PHPUnit_Framework_Assert::assertInstanceOf(Reference::class, $def->getArgument(0)); + \PHPUnit_Framework_Assert::assertEquals('doctrine.orm.default_entity_manager', (string)$def->getArgument(0)); + }) + ->once(); + + $this->container->shouldReceive('setDefinition') + ->andReturnUsing(function($name, Definition $def) { + \PHPUnit_Framework_Assert::assertEquals('tactician.middleware.doctrine.second', $name); + + \PHPUnit_Framework_Assert::assertEquals(TransactionMiddleware::class, $def->getClass()); + \PHPUnit_Framework_Assert::assertCount(1, $def->getArguments()); + \PHPUnit_Framework_Assert::assertInstanceOf(Reference::class, $def->getArgument(0)); + \PHPUnit_Framework_Assert::assertEquals('doctrine.orm.second_entity_manager', (string)$def->getArgument(0)); + }) + ->once(); + + $this->container->shouldReceive('setDefinition') + ->with('tactician.middleware.doctrine.second') + ->once(); + + $this->container->shouldReceive('setAlias') + ->once() + ->with('tactician.middleware.doctrine', 'tactician.middleware.doctrine.default'); + + $this->compiler->process($this->container); + } + + public function testDoNotProcessWhenThereAreNoEntityManagers() + { + if (!class_exists(TransactionMiddleware::class)) { + $this->markTestSkipped('"league/tactician-doctrine" is not installed'); + } + + $this->container->shouldReceive('hasParameter') + ->with('doctrine.entity_managers') + ->once() + ->andReturn(false); + + $this->container->shouldNotReceive('getParameter') + ->withAnyArgs(); + + $this->container->shouldNotReceive('setDefinition') + ->withAnyArgs(); + + $this->container->shouldNotReceive('setAlias') + ->withAnyArgs(); + + $this->compiler->process($this->container); + } + + public function testDoNotProcessWhenTacticianDoctrineIsNotInstalled() + { + if (class_exists(TransactionMiddleware::class)) { + $this->markTestSkipped('"league/tactician-doctrine" is installed'); + } + + $this->container->shouldReceive('hasParameter') + ->with('doctrine.entity_managers') + ->andReturn(true); + + $this->container->shouldNotReceive('getParameter') + ->withAnyArgs(); + + $this->container->shouldNotReceive('setDefinition') + ->withAnyArgs(); + + $this->container->shouldNotReceive('setAlias') + ->withAnyArgs(); + + $this->compiler->process($this->container); + } +} diff --git a/composer.json b/composer.json index ea95668..0fadac1 100644 --- a/composer.json +++ b/composer.json @@ -31,7 +31,6 @@ "require" : { "php": ">=5.5", "league/tactician": "^0.6", - "league/tactician-doctrine": "^0.6", "symfony/framework-bundle": "~2.3" }, "autoload" : { @@ -50,6 +49,8 @@ "mockery/mockery": "0.9.*", "scrutinizer/ocular": "~1.1", "matthiasnoback/symfony-config-test": "~1.0", - "matthiasnoback/symfony-dependency-injection-test": "^0.7" + "matthiasnoback/symfony-dependency-injection-test": "^0.7", + + "league/tactician-doctrine": "^0.6" } } From 23e31669a8568f55bbb26ce34a7c8a91216077d8 Mon Sep 17 00:00:00 2001 From: Warnar Boekkooi Date: Thu, 21 May 2015 10:01:27 +0800 Subject: [PATCH 3/5] Added league/tactician-doctrine suggest --- composer.json | 3 +++ 1 file changed, 3 insertions(+) diff --git a/composer.json b/composer.json index 0fadac1..c6ceac7 100644 --- a/composer.json +++ b/composer.json @@ -33,6 +33,9 @@ "league/tactician": "^0.6", "symfony/framework-bundle": "~2.3" }, + "suggest": { + "league/tactician-doctrine": "For doctrine transaction middleware" + }, "autoload" : { "psr-4" : { "League\\Tactician\\Bundle\\" : "" From 1d68ad8b5f2b01032488652a57d6cd56cd123610 Mon Sep 17 00:00:00 2001 From: Warnar Boekkooi Date: Thu, 21 May 2015 10:02:14 +0800 Subject: [PATCH 4/5] Update mockery/mockery min dev dependency to 0.9.4 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index c6ceac7..a0af984 100644 --- a/composer.json +++ b/composer.json @@ -49,7 +49,7 @@ "require-dev": { "symfony/validator": "~2.3", "phpunit/phpunit": "~4.5", - "mockery/mockery": "0.9.*", + "mockery/mockery": "~0.9.4", "scrutinizer/ocular": "~1.1", "matthiasnoback/symfony-config-test": "~1.0", "matthiasnoback/symfony-dependency-injection-test": "^0.7", From 3b4f8bde18a2bd61e31de8e6bdab0c79ccf51578 Mon Sep 17 00:00:00 2001 From: Warnar Boekkooi Date: Mon, 8 Jun 2015 10:53:09 +0800 Subject: [PATCH 5/5] [Travis] Test without doctrine --- .travis.yml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/.travis.yml b/.travis.yml index 68daa9d..5fd04cc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,15 +6,22 @@ php: - 7.0 - hhvm +env: + - '' + - 'DOCTRINE="false"' + matrix: allow_failures: - php: 7.0 include: - php: 5.5 env: 'COMPOSER_FLAGS="--prefer-stable --prefer-lowest"' + - php: 5.5 + env: 'COMPOSER_FLAGS="--prefer-stable --prefer-lowest" DOCTRINE="false"' before_script: - travis_retry composer self-update + - if [ "${DOCTRINE}" == "false" ]; then composer remove league/tactician-doctrine --dev --no-update; fi - travis_retry composer update ${COMPOSER_FLAGS} --no-interaction script: