Skip to content

Commit

Permalink
Avoid duplication of AttachEntityListeners (#560)
Browse files Browse the repository at this point in the history
* Avoid duplication of AttachEntityListeners

* Test AttachEntityListeners are filtered by connections
  • Loading branch information
aminin authored and mikeSimonson committed Jan 16, 2017
1 parent 4418423 commit a01d99b
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 1 deletion.
6 changes: 5 additions & 1 deletion DependencyInjection/DoctrineExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,11 @@ protected function loadOrmEntityManager(array $entityManager, ContainerBuilder $
if (version_compare(Version::VERSION, "2.5.0-DEV") >= 0) {
$listenerId = sprintf('doctrine.orm.%s_listeners.attach_entity_listeners', $entityManager['name']);
$listenerDef = $container->setDefinition($listenerId, new Definition('%doctrine.orm.listeners.attach_entity_listeners.class%'));
$listenerDef->addTag('doctrine.event_listener', array('event' => 'loadClassMetadata'));
$listenerTagParams = array('event' => 'loadClassMetadata');
if (isset($entityManager['connection'])) {
$listenerTagParams['connection'] = $entityManager['connection'];
}
$listenerDef->addTag('doctrine.event_listener', $listenerTagParams);
}

if (isset($entityManager['second_level_cache'])) {
Expand Down
46 changes: 46 additions & 0 deletions Tests/DependencyInjection/AbstractDoctrineExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\EntityListenerPass;
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\DoctrineExtension;
use Doctrine\ORM\Version;
use Symfony\Bridge\Doctrine\DependencyInjection\CompilerPass\RegisterEventListenersAndSubscribersPass;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
Expand Down Expand Up @@ -808,6 +809,30 @@ public function testAttachEntityListenerTag()
$this->assertDICDefinitionMethodCallOnce($attachListener, 'addEntityListener', array('My/Entity2', 'EntityListener2', 'preFlush', 'preFlushHandler'));
}

public function testAttachEntityListenersTwoConnections()
{
if (version_compare(Version::VERSION, '2.5.0-DEV') < 0) {
$this->markTestSkipped('Attaching entity listeners by tag requires doctrine-orm 2.5.0 or newer');
}

$container = $this->getContainer(['YamlBundle']);
$loader = new DoctrineExtension();
$container->registerExtension($loader);
$container->addCompilerPass(new RegisterEventListenersAndSubscribersPass('doctrine.connections', 'doctrine.dbal.%s_connection.event_manager', 'doctrine'));

$this->loadFromFile($container, 'orm_attach_entity_listeners_two_connections');

$this->compileContainer($container);

$defaultEventManager = $container->getDefinition('doctrine.dbal.default_connection.event_manager');
$this->assertDICDefinitionNoMethodCall($defaultEventManager, 'addEventListener', [['loadClassMetadata'], new Reference('doctrine.orm.em2_listeners.attach_entity_listeners')]);
$this->assertDICDefinitionMethodCallOnce($defaultEventManager, 'addEventListener', [['loadClassMetadata'], new Reference('doctrine.orm.em1_listeners.attach_entity_listeners')]);

$foobarEventManager = $container->getDefinition('doctrine.dbal.foobar_connection.event_manager');
$this->assertDICDefinitionNoMethodCall($foobarEventManager, 'addEventListener', [['loadClassMetadata'], new Reference('doctrine.orm.em1_listeners.attach_entity_listeners')]);
$this->assertDICDefinitionMethodCallOnce($foobarEventManager, 'addEventListener', [['loadClassMetadata'], new Reference('doctrine.orm.em2_listeners.attach_entity_listeners')]);
}

public function testAttachLazyEntityListener()
{
if (version_compare(Version::VERSION, '2.5.0-DEV') < 0) {
Expand Down Expand Up @@ -1008,6 +1033,27 @@ private function assertDICDefinitionMethodCallCount(Definition $definition, $met
$this->assertEquals($nbCalls, $called, sprintf('The method "%s" should be called %d times', $methodName, $nbCalls));
}

/**
* Assertion for the DI Container, check if the given definition does not contain a method call with the given parameters.
*
* @param Definition $definition
* @param string $methodName
* @param array $params
*/
private function assertDICDefinitionNoMethodCall(Definition $definition, $methodName, array $params = null)
{
$calls = $definition->getMethodCalls();
foreach ($calls as $call) {
if ($call[0] == $methodName) {
if ($params !== null) {
$this->assertNotEquals($params, $call[1], "Method '" . $methodName . "' is not expected to be called with the given parameters.");
} else {
$this->fail("Method '" . $methodName . "' is not expected to be called");
}
}
}
}

private function compileContainer(ContainerBuilder $container)
{
$container->getCompilerPassConfig()->setOptimizationPasses(array(new ResolveDefinitionTemplatesPass()));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:doctrine="http://symfony.com/schema/dic/doctrine"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/doctrine http://symfony.com/schema/dic/doctrine/doctrine-1.0.xsd">

<doctrine:config>
<doctrine:dbal default-connection="default">
<doctrine:connection name="default" dbname="db" />
<doctrine:connection name="foobar" dbname="foobar" />
</doctrine:dbal>

<doctrine:orm default-entity-manager="em1">
<doctrine:entity-manager name="em1" connection="default" />
<doctrine:entity-manager name="em2" connection="foobar" />
</doctrine:orm>
</doctrine:config>
</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
doctrine:
dbal:
default_connection: default
connections:
default:
dbname: db
foobar:
dbname: foobar

orm:
default_entity_manager: em1
entity_managers:
em1:
connection: default
em2:
connection: foobar

0 comments on commit a01d99b

Please sign in to comment.