From a16315794b269351dde0726773ab23606db63430 Mon Sep 17 00:00:00 2001 From: Piotr Antosik Date: Mon, 20 Jan 2020 16:08:57 +0100 Subject: [PATCH 1/3] Add possibility to change used entity manager. --- DependencyInjection/MainConfiguration.php | 8 +- DependencyInjection/PayumExtension.php | 42 ++++-- README.md | 2 + Resources/config/storage/doctrine.orm.xml | 6 - .../PayumExtensionTest.php | 131 +++++++++++++++--- 5 files changed, 151 insertions(+), 38 deletions(-) diff --git a/DependencyInjection/MainConfiguration.php b/DependencyInjection/MainConfiguration.php index 5640160e..243050b4 100644 --- a/DependencyInjection/MainConfiguration.php +++ b/DependencyInjection/MainConfiguration.php @@ -58,6 +58,12 @@ public function getConfigTreeBuilder() $this->addStoragesSection($rootNode); + $rootNode + ->children() + ->scalarNode('entity_manager') + ->defaultValue('default') + ->end(); + return $tb; } @@ -260,4 +266,4 @@ protected function addDynamicGatewaysSection(ArrayNodeDefinition $dynamicGateway ); } } -} \ No newline at end of file +} diff --git a/DependencyInjection/PayumExtension.php b/DependencyInjection/PayumExtension.php index 7e33367d..6aebe28f 100644 --- a/DependencyInjection/PayumExtension.php +++ b/DependencyInjection/PayumExtension.php @@ -37,12 +37,7 @@ class PayumExtension extends Extension implements PrependExtensionInterface */ public function load(array $configs, ContainerBuilder $container) { - $this->addStorageFactory(new FilesystemStorageFactory); - $this->addStorageFactory(new DoctrineStorageFactory); - $this->addStorageFactory(new CustomStorageFactory); - $this->addStorageFactory(new Propel1StorageFactory); - $this->addStorageFactory(new Propel2StorageFactory); - + $this->addDefaultStorageFactories(); $mainConfig = $this->getConfiguration($configs, $container); $config = $this->processConfiguration($mainConfig, $configs); @@ -75,8 +70,12 @@ public function load(array $configs, ContainerBuilder $container) */ public function prepend(ContainerBuilder $container) { + $this->addDefaultStorageFactories(); $bundles = $container->getParameter('kernel.bundles'); + $configs = $container->getExtensionConfig($this->getAlias()); + $payumConfig = $this->processConfiguration(new MainConfiguration($this->storagesFactories), $configs); + if (isset($bundles['DoctrineBundle'])) { $config = array_merge(...$container->getExtensionConfig('doctrine')); @@ -87,18 +86,24 @@ public function prepend(ContainerBuilder $container) $container->prependExtensionConfig('doctrine', array( 'orm' => array( - 'mappings' => array( - 'payum' => array( - 'is_bundle' => false, - 'type' => 'xml', - 'dir' => $payumRootDir.'/Bridge/Doctrine/Resources/mapping', - 'prefix' => 'Payum\Core\Model', + 'entity_managers' => array( + $payumConfig['entity_manager'] => array( + 'mappings' => array( + 'payum' => array( + 'is_bundle' => false, + 'type' => 'xml', + 'dir' => $payumRootDir.'/Bridge/Doctrine/Resources/mapping', + 'prefix' => 'Payum\Core\Model', + ), + ), ), ), ), )); } } + + $container->setAlias('payum.entity_manager', \sprintf('doctrine.orm.%s_entity_manager', $payumConfig['entity_manager'])); } /** @@ -312,4 +317,17 @@ protected function findSelectedStorageFactoryNameInStorageConfig($storageConfig) } } } + + private function addDefaultStorageFactories() + { + try { + $this->addStorageFactory(new FilesystemStorageFactory); + $this->addStorageFactory(new DoctrineStorageFactory); + $this->addStorageFactory(new CustomStorageFactory); + $this->addStorageFactory(new Propel1StorageFactory); + $this->addStorageFactory(new Propel2StorageFactory); + } catch (\InvalidArgumentException $e) { + + } + } } diff --git a/README.md b/README.md index 1c56a273..2e7f2c53 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,8 @@ payum: gateways: offline: factory: offline + + entity_manager: default ``` _note_ if you're using Symfony 4+ then create `config/packages/payum.yaml` file with contents described above. diff --git a/Resources/config/storage/doctrine.orm.xml b/Resources/config/storage/doctrine.orm.xml index 8163d672..e02794bd 100644 --- a/Resources/config/storage/doctrine.orm.xml +++ b/Resources/config/storage/doctrine.orm.xml @@ -13,11 +13,5 @@ - - diff --git a/Tests/DependencyInjection/PayumExtensionTest.php b/Tests/DependencyInjection/PayumExtensionTest.php index c362e9ce..693e7004 100644 --- a/Tests/DependencyInjection/PayumExtensionTest.php +++ b/Tests/DependencyInjection/PayumExtensionTest.php @@ -8,7 +8,7 @@ use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface; use Payum\Bundle\PayumBundle\DependencyInjection\PayumExtension; -class PayumExtensionTest extends \PHPUnit_Framework_TestCase +class PayumExtensionTest extends \PHPUnit_Framework_TestCase { /** * @test @@ -105,6 +105,7 @@ public function shouldNotAddPayumMappingIfDoctrineBundleNotRegistered() $extension = new PayumExtension; + $this->setupDefaultConfig($extension, $container); $extension->prepend($container); $this->assertEmpty($container->getExtensionConfig('doctrine')); @@ -124,6 +125,7 @@ public function shouldNotAddPayumMappingIfDoctrineBundleRegisteredButDbalNotConf 'orm' => 'not empty', )); + $this->setupDefaultConfig($extension, $container); $extension->prepend($container); $this->assertEquals(array( @@ -143,6 +145,8 @@ public function shouldNotAddPayumMappingIfDoctrineBundleRegisteredButOrmNotConfi $container = new ContainerBuilder; $container->setParameter('kernel.bundles', array('DoctrineBundle' => 'DoctrineBundle')); + $this->setupDefaultConfig($extension, $container); + $container->prependExtensionConfig('doctrine', array( 'dbal' => 'not empty', )); @@ -171,6 +175,7 @@ public function shouldAddPayumMappingIfDoctrineBundleRegisteredWithDbalAndOrmCon 'orm' => 'not empty' )); + $this->setupDefaultConfig($extension, $container); $extension->prepend($container); $rc = new \ReflectionClass('Payum\Core\Gateway'); @@ -179,14 +184,20 @@ public function shouldAddPayumMappingIfDoctrineBundleRegisteredWithDbalAndOrmCon $this->assertEquals( array( array( - 'orm' => array('mappings' => array( - 'payum' => array( - 'is_bundle' => false, - 'type' => 'xml', - 'dir' => $payumRootDir.'/Bridge/Doctrine/Resources/mapping', - 'prefix' => 'Payum\Core\Model', + 'orm' => array( + 'entity_managers' => array( + 'default' => array( + 'mappings' => array( + 'payum' => array( + 'is_bundle' => false, + 'type' => 'xml', + 'dir' => $payumRootDir.'/Bridge/Doctrine/Resources/mapping', + 'prefix' => 'Payum\Core\Model', + ) + ) + ), ) - )), + ) ), array( 'dbal' => 'not empty', @@ -206,14 +217,13 @@ public function shouldAddPayumMappingIfDoctrineBundleRegisteredWithDbalAndOrmCon $container = new ContainerBuilder; $container->setParameter('kernel.bundles', array('DoctrineBundle' => 'DoctrineBundle')); - + $container->setParameter('kernel.debug', true); $container->prependExtensionConfig('doctrine', array( 'dbal' => 'not empty', - )); - $container->prependExtensionConfig('doctrine', array( - 'orm' => 'not empty', + 'orm' => 'not empty' )); + $this->setupDefaultConfig($extension, $container); $extension->prepend($container); $rc = new \ReflectionClass('Payum\Core\Gateway'); @@ -222,20 +232,78 @@ public function shouldAddPayumMappingIfDoctrineBundleRegisteredWithDbalAndOrmCon $this->assertEquals( array( array( - 'orm' => array('mappings' => array( - 'payum' => array( - 'is_bundle' => false, - 'type' => 'xml', - 'dir' => $payumRootDir.'/Bridge/Doctrine/Resources/mapping', - 'prefix' => 'Payum\Core\Model', + 'orm' => array( + 'entity_managers' => array( + 'default' => array( + 'mappings' => array( + 'payum' => array( + 'is_bundle' => false, + 'type' => 'xml', + 'dir' => $payumRootDir.'/Bridge/Doctrine/Resources/mapping', + 'prefix' => 'Payum\Core\Model', + ) + ) + ), ) - )), + ) ), array( + 'dbal' => 'not empty', 'orm' => 'not empty' ), + ), + $container->getExtensionConfig('doctrine') + ); + } + + /** + * @test + */ + public function shouldAddPayumMappingIfDoctrineBundleRegisteredWithDbalAndOrmConfiguredWithCustomEntityManager() + { + $extension = new PayumExtension; + + $container = new ContainerBuilder; + $container->setParameter('kernel.bundles', array('DoctrineBundle' => 'DoctrineBundle')); + $container->setParameter('kernel.debug', true); + $container->prependExtensionConfig('doctrine', array( + 'dbal' => 'not empty', + 'orm' => 'not empty' + )); + $this->setupDefaultConfig($extension, $container); + + $container->prependExtensionConfig($extension->getAlias(), + [ + 'entity_manager' => 'custom_em' + ] + ); + + $extension->prepend($container); + + $rc = new \ReflectionClass('Payum\Core\Gateway'); + $payumRootDir = dirname($rc->getFileName()); + + $this->assertEquals( + array( + array( + 'orm' => array( + 'entity_managers' => array( + 'custom_em' => array( + 'mappings' => array( + 'payum' => array( + 'is_bundle' => false, + 'type' => 'xml', + 'dir' => $payumRootDir.'/Bridge/Doctrine/Resources/mapping', + 'prefix' => 'Payum\Core\Model', + ) + ) + ), + ) + ) + ), array( 'dbal' => 'not empty', + 'orm' => 'not empty' ), ), $container->getExtensionConfig('doctrine') @@ -290,6 +358,31 @@ public function shouldAddGatewaysToBuilder() $this->assertEquals('another_gateway', $calls[8][1][0]); $this->assertEquals(['bar' => 'barVal'], $calls[8][1][1]); } + + private function setupDefaultConfig(PayumExtension $extension, ContainerBuilder $container) + { + $extension->addStorageFactory(new FeeStorageFactory()); + + $container->prependExtensionConfig($extension->getAlias(), + [ + 'security' => array( + 'token_storage' => array( + 'Payum\Core\Model\Token' => array( + 'bar_storage' => ['bar_opt' => 'val'] + ) + ) + ), + 'gateways' => array( + 'a_gateway' => array( + 'foo' => 'fooVal', + ), + 'another_gateway' => array( + 'bar' => 'barVal', + ) + ) + ] + ); + } } class FeeStorageFactory implements StorageFactoryInterface From e12d8d32ca1f39bbede35fb96145399a5d38ebca Mon Sep 17 00:00:00 2001 From: Piotr Antosik Date: Tue, 21 Jan 2020 10:13:44 +0100 Subject: [PATCH 2/3] Fix test on Symfony 3.x Add alias in compiler pass --- .../Compiler/BuildEntityManagerPass.php | 23 +++++++++ DependencyInjection/MainConfiguration.php | 14 +++--- DependencyInjection/PayumExtension.php | 2 - PayumBundle.php | 2 + .../Compiler/BuildEntityManagerPassTest.php | 50 +++++++++++++++++++ Tests/Functional/app/AppKernel.php | 2 +- Tests/Functional/app/config/config.yml | 3 +- 7 files changed, 85 insertions(+), 11 deletions(-) create mode 100644 DependencyInjection/Compiler/BuildEntityManagerPass.php create mode 100644 Tests/DependencyInjection/Compiler/BuildEntityManagerPassTest.php diff --git a/DependencyInjection/Compiler/BuildEntityManagerPass.php b/DependencyInjection/Compiler/BuildEntityManagerPass.php new file mode 100644 index 00000000..805798da --- /dev/null +++ b/DependencyInjection/Compiler/BuildEntityManagerPass.php @@ -0,0 +1,23 @@ +getExtensionConfig('payum')[0]; + + $serviceId = \sprintf('doctrine.orm.%s_entity_manager', $payumConfig['entity_manager']); + + if ($container->has($serviceId)) { + $container->setAlias('payum.entity_manager', $serviceId); + } + } +} diff --git a/DependencyInjection/MainConfiguration.php b/DependencyInjection/MainConfiguration.php index 243050b4..72b0b8d2 100644 --- a/DependencyInjection/MainConfiguration.php +++ b/DependencyInjection/MainConfiguration.php @@ -38,6 +38,12 @@ public function getConfigTreeBuilder() $rootNode = $tb->root('payum'); } + $rootNode + ->children() + ->scalarNode('entity_manager') + ->defaultValue('default') + ->end(); + $securityNode = $rootNode->children() ->arrayNode('security')->isRequired() ; @@ -55,14 +61,8 @@ public function getConfigTreeBuilder() ->prototype('variable') ->treatNullLike([]) ; - - $this->addStoragesSection($rootNode); - $rootNode - ->children() - ->scalarNode('entity_manager') - ->defaultValue('default') - ->end(); + $this->addStoragesSection($rootNode); return $tb; } diff --git a/DependencyInjection/PayumExtension.php b/DependencyInjection/PayumExtension.php index 6aebe28f..45c1846d 100644 --- a/DependencyInjection/PayumExtension.php +++ b/DependencyInjection/PayumExtension.php @@ -102,8 +102,6 @@ public function prepend(ContainerBuilder $container) )); } } - - $container->setAlias('payum.entity_manager', \sprintf('doctrine.orm.%s_entity_manager', $payumConfig['entity_manager'])); } /** diff --git a/PayumBundle.php b/PayumBundle.php index 6552e8b7..636e0ee1 100644 --- a/PayumBundle.php +++ b/PayumBundle.php @@ -2,6 +2,7 @@ namespace Payum\Bundle\PayumBundle; use Payum\Bundle\PayumBundle\DependencyInjection\Compiler\BuildConfigsPass; +use Payum\Bundle\PayumBundle\DependencyInjection\Compiler\BuildEntityManagerPass; use Payum\Bundle\PayumBundle\DependencyInjection\Compiler\BuildGatewayFactoriesBuilderPass; use Payum\Bundle\PayumBundle\DependencyInjection\Compiler\BuildGatewayFactoriesPass; use Payum\Bundle\PayumBundle\DependencyInjection\Compiler\BuildGatewaysPass; @@ -20,5 +21,6 @@ public function build(ContainerBuilder $container) $container->addCompilerPass(new BuildStoragesPass); $container->addCompilerPass(new BuildGatewayFactoriesPass); $container->addCompilerPass(new BuildGatewayFactoriesBuilderPass()); + $container->addCompilerPass(new BuildEntityManagerPass()); } } diff --git a/Tests/DependencyInjection/Compiler/BuildEntityManagerPassTest.php b/Tests/DependencyInjection/Compiler/BuildEntityManagerPassTest.php new file mode 100644 index 00000000..00f47eae --- /dev/null +++ b/Tests/DependencyInjection/Compiler/BuildEntityManagerPassTest.php @@ -0,0 +1,50 @@ +assertTrue($rc->implementsInterface(CompilerPassInterface::class)); + } + + public function testShouldAddAliasToContainer() + { + $service = new Definition(); + + $container = new ContainerBuilder(); + $container->setDefinition('doctrine.orm.custom_entity_manager', $service); + $container->prependExtensionConfig('payum', [ + 'entity_manager' => 'custom', + ]); + + $pass = new BuildEntityManagerPass(); + + $pass->process($container); + + $this->assertTrue($container->hasAlias('payum.entity_manager')); + } + + public function testShouldNotAddAliasToContainer() + { + $container = new ContainerBuilder(); + $container->prependExtensionConfig('payum', [ + 'entity_manager' => 'default', + ]); + + $pass = new BuildEntityManagerPass(); + + $pass->process($container); + + $this->assertFalse($container->hasAlias('payum.entity_manager')); + } +} diff --git a/Tests/Functional/app/AppKernel.php b/Tests/Functional/app/AppKernel.php index f9f38f85..2d3bf8a8 100644 --- a/Tests/Functional/app/AppKernel.php +++ b/Tests/Functional/app/AppKernel.php @@ -58,4 +58,4 @@ public function registerContainerConfiguration(LoaderInterface $loader) { $loader->load(__DIR__ . '/config/config.yml'); } -} \ No newline at end of file +} diff --git a/Tests/Functional/app/config/config.yml b/Tests/Functional/app/config/config.yml index 574ae1ac..c00cd7a5 100644 --- a/Tests/Functional/app/config/config.yml +++ b/Tests/Functional/app/config/config.yml @@ -41,4 +41,5 @@ payum: username: 'aUsername' password: 'aPassword' signature: 'aSignature' - sandbox: true \ No newline at end of file + sandbox: true + entity_manager: default From 5d06fcdf83bd4a6e464a663cabcd6a184312dff8 Mon Sep 17 00:00:00 2001 From: Piotr Antosik Date: Tue, 21 Jan 2020 14:58:56 +0100 Subject: [PATCH 3/3] Add abstract service definition --- Resources/config/storage/doctrine.orm.xml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Resources/config/storage/doctrine.orm.xml b/Resources/config/storage/doctrine.orm.xml index e02794bd..a9f557e8 100644 --- a/Resources/config/storage/doctrine.orm.xml +++ b/Resources/config/storage/doctrine.orm.xml @@ -13,5 +13,7 @@ + +