Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add possibility to change used entity manager. #502

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add possibility to change used entity manager.
  • Loading branch information
piotrantosik committed Jan 21, 2020
commit a16315794b269351dde0726773ab23606db63430
8 changes: 7 additions & 1 deletion DependencyInjection/MainConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ public function getConfigTreeBuilder()

$this->addStoragesSection($rootNode);

$rootNode
->children()
->scalarNode('entity_manager')
->defaultValue('default')
->end();

return $tb;
}

Expand Down Expand Up @@ -260,4 +266,4 @@ protected function addDynamicGatewaysSection(ArrayNodeDefinition $dynamicGateway
);
}
}
}
}
42 changes: 30 additions & 12 deletions DependencyInjection/PayumExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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'));

Expand All @@ -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']));
}

/**
Expand Down Expand Up @@ -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) {

}
}
}
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 0 additions & 6 deletions Resources/config/storage/doctrine.orm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,5 @@
<argument type="service" id="payum.entity_manager" />
<argument><!-- should be set in DoctrineStorageFactory --></argument>
</service>

<service
id="payum.entity_manager"
alias="doctrine.orm.default_entity_manager"
public="false"
/>
</services>
</container>
131 changes: 112 additions & 19 deletions Tests/DependencyInjection/PayumExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -105,6 +105,7 @@ public function shouldNotAddPayumMappingIfDoctrineBundleNotRegistered()

$extension = new PayumExtension;

$this->setupDefaultConfig($extension, $container);
$extension->prepend($container);

$this->assertEmpty($container->getExtensionConfig('doctrine'));
Expand All @@ -124,6 +125,7 @@ public function shouldNotAddPayumMappingIfDoctrineBundleRegisteredButDbalNotConf
'orm' => 'not empty',
));

$this->setupDefaultConfig($extension, $container);
$extension->prepend($container);

$this->assertEquals(array(
Expand All @@ -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',
));
Expand Down Expand Up @@ -171,6 +175,7 @@ public function shouldAddPayumMappingIfDoctrineBundleRegisteredWithDbalAndOrmCon
'orm' => 'not empty'
));

$this->setupDefaultConfig($extension, $container);
$extension->prepend($container);

$rc = new \ReflectionClass('Payum\Core\Gateway');
Expand All @@ -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',
Expand All @@ -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');
Expand All @@ -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')
Expand Down Expand Up @@ -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
Expand Down