diff --git a/lib/Doctrine/ORM/Configuration.php b/lib/Doctrine/ORM/Configuration.php index 5e19d29bd..b0a09cbb5 100644 --- a/lib/Doctrine/ORM/Configuration.php +++ b/lib/Doctrine/ORM/Configuration.php @@ -29,6 +29,8 @@ use Doctrine\ORM\Mapping\QuoteStrategy; use Doctrine\ORM\Mapping\DefaultQuoteStrategy; use Doctrine\ORM\Mapping\NamingStrategy; use Doctrine\ORM\Mapping\DefaultNamingStrategy; +use Doctrine\ORM\Mapping\EntityListenerResolver; +use Doctrine\ORM\Mapping\DefaultEntityListenerResolver; use Doctrine\Common\Annotations\SimpleAnnotationReader; use Doctrine\Common\Annotations\CachedReader; @@ -763,4 +765,30 @@ class Configuration extends \Doctrine\DBAL\Configuration return $this->_attributes['quoteStrategy']; } + + /** + * Set the entity listener resolver. + * + * @since 2.4 + * @param \Doctrine\ORM\Mapping\EntityListenerResolver $resolver + */ + public function setEntityListenerResolver(EntityListenerResolver $resolver) + { + $this->_attributes['entityListenerResolver'] = $resolver; + } + + /** + * Get the entity listener resolver. + * + * @since 2.4 + * @return \Doctrine\ORM\Mapping\EntityListenerResolver + */ + public function getEntityListenerResolver() + { + if ( ! isset($this->_attributes['entityListenerResolver'])) { + $this->_attributes['entityListenerResolver'] = new DefaultEntityListenerResolver(); + } + + return $this->_attributes['entityListenerResolver']; + } } diff --git a/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php b/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php index bae46d34a..0ec517d20 100644 --- a/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php +++ b/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php @@ -27,6 +27,7 @@ use ReflectionClass; use Doctrine\Common\Persistence\Mapping\ClassMetadata; use Doctrine\Common\ClassLoader; use Doctrine\Common\EventArgs; +use Doctrine\ORM\Mapping\EntityListenerResolver; /** * A ClassMetadata instance holds all the object-relational mapping metadata @@ -444,11 +445,6 @@ class ClassMetadataInfo implements ClassMetadata */ public $entityListeners = array(); - /** - * @var array entity listeners instances. - */ - static private $entityListenerInstances = array(); - /** * READ-ONLY: The association mappings of this class. * @@ -2520,21 +2516,19 @@ class ClassMetadataInfo implements ClassMetadata /** * Call the entity listeners. * + * @param \Doctrine\ORM\Mapping\EntityListenerResolver $resolver The Entity listener resolver. * @param string $eventName The event name. * @param object $entity An instance of the mapped entity * @param \Doctrine\Common\EventArgs $arg The Event args */ - public function dispatchEntityListeners($eventName, $entity, EventArgs $arg) + public function dispatchEntityListeners(EntityListenerResolver $resolver, $eventName, $entity, EventArgs $arg) { foreach ($this->entityListeners[$eventName] as $listener) { - $class = $listener['class']; - $method = $listener['method']; + $class = $listener['class']; + $method = $listener['method']; + $instance = $resolver->resolve($class); - if ( ! isset(self::$entityListenerInstances[$class])) { - self::$entityListenerInstances[$class] = new $class(); - } - - self::$entityListenerInstances[$class]->{$method}($entity, $arg); + $instance->{$method}($entity, $arg); } } diff --git a/lib/Doctrine/ORM/Mapping/DefaultEntityListenerResolver.php b/lib/Doctrine/ORM/Mapping/DefaultEntityListenerResolver.php new file mode 100644 index 000000000..75657e237 --- /dev/null +++ b/lib/Doctrine/ORM/Mapping/DefaultEntityListenerResolver.php @@ -0,0 +1,76 @@ +. + */ + +namespace Doctrine\ORM\Mapping; + +/** + * The default DefaultEntityListene + * + * @since 2.4 + * @author Fabio B. Silva + */ +class DefaultEntityListenerResolver implements EntityListenerResolver +{ + /** + * @var array Map to store entity listener instances. + */ + private $instances = array(); + + /** + * {@inheritdoc} + */ + public function clear($className = null) + { + if ($className !== null) { + + if (isset($this->instances[$className = trim($className, '\\')])) { + unset($this->instances[$className]); + } + + return; + } + + $this->instances = array(); + } + + /** + * {@inheritdoc}" + */ + public function register($object) + { + if ( ! is_object($object)) { + throw new \InvalidArgumentException(sprintf('An object was expected, but got "%s.', gettype($object))); + } + + $this->instances[get_class($object)] = $object; + } + + /** + * {@inheritdoc} + */ + public function resolve($className) + { + if (isset($this->instances[$className = trim($className, '\\')])) { + return $this->instances[$className]; + } + + return $this->instances[$className] = new $className(); + } +} \ No newline at end of file diff --git a/lib/Doctrine/ORM/Mapping/EntityListenerResolver.php b/lib/Doctrine/ORM/Mapping/EntityListenerResolver.php new file mode 100644 index 000000000..bbf498dc3 --- /dev/null +++ b/lib/Doctrine/ORM/Mapping/EntityListenerResolver.php @@ -0,0 +1,55 @@ +. + */ + +namespace Doctrine\ORM\Mapping; + +/** + * A resolver is used to instantiate an entity listener. + * + * @since 2.4 + * @author Fabio B. Silva + */ +interface EntityListenerResolver +{ + /** + * Clear all instances from the set, or a specific class when given. + * + * @param string $className The fully-qualified class name + * + * @return void + */ + function clear($className = null); + + /** + * Returns a entity listener instance for the given class name. + * + * @param string $className The fully-qualified class name + * + * @return object An entity listener + */ + function resolve($className); + + /** + * Register a entity listener instance. + * + * @return object An entity listener + */ + function register($object); +} \ No newline at end of file diff --git a/lib/Doctrine/ORM/UnitOfWork.php b/lib/Doctrine/ORM/UnitOfWork.php index 4969a31d6..b3904c981 100644 --- a/lib/Doctrine/ORM/UnitOfWork.php +++ b/lib/Doctrine/ORM/UnitOfWork.php @@ -218,6 +218,13 @@ class UnitOfWork implements PropertyChangedListener */ private $evm; + /** + * The EntityListenerResolver used for dispatching events. + * + * @var \Doctrine\ORM\Mapping\EntityListenerResolver + */ + private $entityListenerResolver; + /** * Orphaned entities that are scheduled for removal. * @@ -246,8 +253,9 @@ class UnitOfWork implements PropertyChangedListener */ public function __construct(EntityManager $em) { - $this->em = $em; - $this->evm = $em->getEventManager(); + $this->em = $em; + $this->evm = $em->getEventManager(); + $this->entityListenerResolver = $em->getConfiguration()->getEntityListenerResolver(); } /** @@ -525,7 +533,7 @@ class UnitOfWork implements PropertyChangedListener // Fire PreFlush entity listeners if ($hasEntityListeners) { - $class->dispatchEntityListeners(Events::preFlush, $entity, $event); + $class->dispatchEntityListeners($this->entityListenerResolver, Events::preFlush, $entity, $event); } $actualData = array(); @@ -836,7 +844,7 @@ class UnitOfWork implements PropertyChangedListener } if ($hasEntityListeners) { - $class->dispatchEntityListeners(Events::prePersist, $entity, $event); + $class->dispatchEntityListeners($this->entityListenerResolver, Events::prePersist, $entity, $event); } if ($hasListeners) { @@ -986,7 +994,7 @@ class UnitOfWork implements PropertyChangedListener } if ($hasEntityListeners) { - $class->dispatchEntityListeners(Events::postPersist, $entity, $event); + $class->dispatchEntityListeners($this->entityListenerResolver, Events::postPersist, $entity, $event); } if ($hasListeners) { @@ -1035,7 +1043,7 @@ class UnitOfWork implements PropertyChangedListener } if ($hasPreUpdateEntityListeners) { - $class->dispatchEntityListeners(Events::preUpdate, $entity, $preEvent); + $class->dispatchEntityListeners($this->entityListenerResolver, Events::preUpdate, $entity, $preEvent); } if ($hasPreUpdateListeners) { @@ -1053,7 +1061,7 @@ class UnitOfWork implements PropertyChangedListener } if ($hasPostUpdateEntityListeners) { - $class->dispatchEntityListeners(Events::postUpdate, $entity, $postEvent); + $class->dispatchEntityListeners($this->entityListenerResolver, Events::postUpdate, $entity, $postEvent); } if ($hasPostUpdateListeners) { @@ -1108,7 +1116,7 @@ class UnitOfWork implements PropertyChangedListener } if ($hasEntityListeners) { - $class->dispatchEntityListeners(Events::postRemove, $entity, $event); + $class->dispatchEntityListeners($this->entityListenerResolver, Events::postRemove, $entity, $event); } if ($hasListeners) { @@ -1765,7 +1773,7 @@ class UnitOfWork implements PropertyChangedListener } if ($hasEntityListeners) { - $class->dispatchEntityListeners(Events::preRemove, $entity, $event); + $class->dispatchEntityListeners($this->entityListenerResolver, Events::preRemove, $entity, $event); } if ($hasListeners) { @@ -2781,7 +2789,7 @@ class UnitOfWork implements PropertyChangedListener } if ($hasEntityListeners) { - $class->dispatchEntityListeners(Events::postLoad, $entity, $event); + $class->dispatchEntityListeners($this->entityListenerResolver, Events::postLoad, $entity, $event); } if ($hasListeners) { diff --git a/tests/Doctrine/Tests/Models/Company/CompanyContractListener.php b/tests/Doctrine/Tests/Models/Company/CompanyContractListener.php index 9382660bc..61c1d4719 100644 --- a/tests/Doctrine/Tests/Models/Company/CompanyContractListener.php +++ b/tests/Doctrine/Tests/Models/Company/CompanyContractListener.php @@ -4,32 +4,25 @@ namespace Doctrine\Tests\Models\Company; class CompanyContractListener { - static public $postPersistCalls; - static public $prePersistCalls; + public $postPersistCalls; + public $prePersistCalls; - static public $postUpdateCalls; - static public $preUpdateCalls; + public $postUpdateCalls; + public $preUpdateCalls; - static public $postRemoveCalls; - static public $preRemoveCalls; + public $postRemoveCalls; + public $preRemoveCalls; - static public $preFlushCalls; + public $preFlushCalls; - static public $postLoadCalls; + public $postLoadCalls; - static public $instances; - - public function __construct() - { - self::$instances[] = $this; - } - /** * @PostPersist */ public function postPersistHandler(CompanyContract $contract) { - self::$postPersistCalls[] = func_get_args(); + $this->postPersistCalls[] = func_get_args(); } /** @@ -37,7 +30,7 @@ class CompanyContractListener */ public function prePersistHandler(CompanyContract $contract) { - self::$prePersistCalls[] = func_get_args(); + $this->prePersistCalls[] = func_get_args(); } /** @@ -45,7 +38,7 @@ class CompanyContractListener */ public function postUpdateHandler(CompanyContract $contract) { - self::$postUpdateCalls[] = func_get_args(); + $this->postUpdateCalls[] = func_get_args(); } /** @@ -53,7 +46,7 @@ class CompanyContractListener */ public function preUpdateHandler(CompanyContract $contract) { - self::$preUpdateCalls[] = func_get_args(); + $this->preUpdateCalls[] = func_get_args(); } /** @@ -61,7 +54,7 @@ class CompanyContractListener */ public function postRemoveHandler(CompanyContract $contract) { - self::$postRemoveCalls[] = func_get_args(); + $this->postRemoveCalls[] = func_get_args(); } /** @@ -69,7 +62,7 @@ class CompanyContractListener */ public function preRemoveHandler(CompanyContract $contract) { - self::$preRemoveCalls[] = func_get_args(); + $this->preRemoveCalls[] = func_get_args(); } /** @@ -77,7 +70,7 @@ class CompanyContractListener */ public function preFlushHandler(CompanyContract $contract) { - self::$preFlushCalls[] = func_get_args(); + $this->preFlushCalls[] = func_get_args(); } /** @@ -85,7 +78,7 @@ class CompanyContractListener */ public function postLoadHandler(CompanyContract $contract) { - self::$postLoadCalls[] = func_get_args(); + $this->postLoadCalls[] = func_get_args(); } } diff --git a/tests/Doctrine/Tests/Models/Company/CompanyFlexUltraContractListener.php b/tests/Doctrine/Tests/Models/Company/CompanyFlexUltraContractListener.php index 04f2dd2ea..2f028e35f 100644 --- a/tests/Doctrine/Tests/Models/Company/CompanyFlexUltraContractListener.php +++ b/tests/Doctrine/Tests/Models/Company/CompanyFlexUltraContractListener.php @@ -6,20 +6,14 @@ use Doctrine\ORM\Event\LifecycleEventArgs; class CompanyFlexUltraContractListener { - static public $prePersistCalls; - static public $instances; - - public function __construct() - { - self::$instances[] = $this; - } + public $prePersistCalls; /** * @PrePersist */ public function prePersistHandler1(CompanyContract $contract, LifecycleEventArgs $args) { - self::$prePersistCalls[] = func_get_args(); + $this->prePersistCalls[] = func_get_args(); } /** @@ -27,6 +21,6 @@ class CompanyFlexUltraContractListener */ public function prePersistHandler2(CompanyContract $contract, LifecycleEventArgs $args) { - self::$prePersistCalls[] = func_get_args(); + $this->prePersistCalls[] = func_get_args(); } } diff --git a/tests/Doctrine/Tests/ORM/ConfigurationTest.php b/tests/Doctrine/Tests/ORM/ConfigurationTest.php index 1caaf5701..57ec1ce1c 100644 --- a/tests/Doctrine/Tests/ORM/ConfigurationTest.php +++ b/tests/Doctrine/Tests/ORM/ConfigurationTest.php @@ -260,6 +260,18 @@ class ConfigurationTest extends PHPUnit_Framework_TestCase $this->configuration->setQuoteStrategy($quoteStrategy); $this->assertSame($quoteStrategy, $this->configuration->getQuoteStrategy()); } + + /** + * @group DDC-1955 + */ + public function testSetGetEntityListenerResolver() + { + $this->assertInstanceOf('Doctrine\ORM\Mapping\EntityListenerResolver', $this->configuration->getEntityListenerResolver()); + $this->assertInstanceOf('Doctrine\ORM\Mapping\DefaultEntityListenerResolver', $this->configuration->getEntityListenerResolver()); + $quoteStrategy = $this->getMock('Doctrine\ORM\Mapping\EntityListenerResolver'); + $this->configuration->setEntityListenerResolver($quoteStrategy); + $this->assertSame($quoteStrategy, $this->configuration->getEntityListenerResolver()); + } } class ConfigurationTestAnnotationReaderChecker diff --git a/tests/Doctrine/Tests/ORM/Functional/EntityListenersTest.php b/tests/Doctrine/Tests/ORM/Functional/EntityListenersTest.php index dcc8393e0..fc6048e60 100644 --- a/tests/Doctrine/Tests/ORM/Functional/EntityListenersTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/EntityListenersTest.php @@ -3,7 +3,6 @@ namespace Doctrine\Tests\ORM\Functional; use Doctrine\Tests\Models\Company\CompanyFixContract; -use Doctrine\Tests\Models\Company\CompanyContractListener; require_once __DIR__ . '/../../TestInit.php'; @@ -13,10 +12,19 @@ require_once __DIR__ . '/../../TestInit.php'; class EntityListenersTest extends \Doctrine\Tests\OrmFunctionalTestCase { + /** + * @var \Doctrine\Tests\Models\Company\CompanyContractListener + */ + private $listener; + protected function setUp() { $this->useModelSet('company'); parent::setUp(); + + $this->listener = $this->_em->getConfiguration() + ->getEntityListenerResolver() + ->resolve('Doctrine\Tests\Models\Company\CompanyContractListener'); } public function testPreFlushListeners() @@ -24,24 +32,23 @@ class EntityListenersTest extends \Doctrine\Tests\OrmFunctionalTestCase $fix = new CompanyFixContract(); $fix->setFixPrice(2000); - CompanyContractListener::$preFlushCalls = array(); + $this->listener->preFlushCalls = array(); $this->_em->persist($fix); $this->_em->flush(); - $this->assertCount(1,CompanyContractListener::$instances); - $this->assertCount(1,CompanyContractListener::$preFlushCalls); + $this->assertCount(1,$this->listener->preFlushCalls); - $this->assertSame($fix, CompanyContractListener::$preFlushCalls[0][0]); + $this->assertSame($fix, $this->listener->preFlushCalls[0][0]); $this->assertInstanceOf( 'Doctrine\Tests\Models\Company\CompanyFixContract', - CompanyContractListener::$preFlushCalls[0][0] + $this->listener->preFlushCalls[0][0] ); $this->assertInstanceOf( 'Doctrine\ORM\Event\PreFlushEventArgs', - CompanyContractListener::$preFlushCalls[0][1] + $this->listener->preFlushCalls[0][1] ); } @@ -54,24 +61,23 @@ class EntityListenersTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->_em->flush(); $this->_em->clear(); - CompanyContractListener::$postLoadCalls = array(); + $this->listener->postLoadCalls = array(); $dql = "SELECT f FROM Doctrine\Tests\Models\Company\CompanyFixContract f WHERE f.id = ?1"; $fix = $this->_em->createQuery($dql)->setParameter(1, $fix->getId())->getSingleResult(); - $this->assertCount(1,CompanyContractListener::$instances); - $this->assertCount(1,CompanyContractListener::$postLoadCalls); + $this->assertCount(1,$this->listener->postLoadCalls); - $this->assertSame($fix, CompanyContractListener::$postLoadCalls[0][0]); + $this->assertSame($fix, $this->listener->postLoadCalls[0][0]); $this->assertInstanceOf( 'Doctrine\Tests\Models\Company\CompanyFixContract', - CompanyContractListener::$postLoadCalls[0][0] + $this->listener->postLoadCalls[0][0] ); $this->assertInstanceOf( 'Doctrine\ORM\Event\LifecycleEventArgs', - CompanyContractListener::$postLoadCalls[0][1] + $this->listener->postLoadCalls[0][1] ); } @@ -80,24 +86,23 @@ class EntityListenersTest extends \Doctrine\Tests\OrmFunctionalTestCase $fix = new CompanyFixContract(); $fix->setFixPrice(2000); - CompanyContractListener::$prePersistCalls = array(); + $this->listener->prePersistCalls = array(); $this->_em->persist($fix); $this->_em->flush(); - $this->assertCount(1,CompanyContractListener::$instances); - $this->assertCount(1,CompanyContractListener::$prePersistCalls); + $this->assertCount(1,$this->listener->prePersistCalls); - $this->assertSame($fix, CompanyContractListener::$prePersistCalls[0][0]); + $this->assertSame($fix, $this->listener->prePersistCalls[0][0]); $this->assertInstanceOf( 'Doctrine\Tests\Models\Company\CompanyFixContract', - CompanyContractListener::$prePersistCalls[0][0] + $this->listener->prePersistCalls[0][0] ); $this->assertInstanceOf( 'Doctrine\ORM\Event\LifecycleEventArgs', - CompanyContractListener::$prePersistCalls[0][1] + $this->listener->prePersistCalls[0][1] ); } @@ -106,24 +111,23 @@ class EntityListenersTest extends \Doctrine\Tests\OrmFunctionalTestCase $fix = new CompanyFixContract(); $fix->setFixPrice(2000); - CompanyContractListener::$postPersistCalls = array(); + $this->listener->postPersistCalls = array(); $this->_em->persist($fix); $this->_em->flush(); - $this->assertCount(1,CompanyContractListener::$instances); - $this->assertCount(1,CompanyContractListener::$postPersistCalls); + $this->assertCount(1,$this->listener->postPersistCalls); - $this->assertSame($fix, CompanyContractListener::$postPersistCalls[0][0]); + $this->assertSame($fix, $this->listener->postPersistCalls[0][0]); $this->assertInstanceOf( 'Doctrine\Tests\Models\Company\CompanyFixContract', - CompanyContractListener::$postPersistCalls[0][0] + $this->listener->postPersistCalls[0][0] ); $this->assertInstanceOf( 'Doctrine\ORM\Event\LifecycleEventArgs', - CompanyContractListener::$postPersistCalls[0][1] + $this->listener->postPersistCalls[0][1] ); } @@ -135,26 +139,25 @@ class EntityListenersTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->_em->persist($fix); $this->_em->flush(); - CompanyContractListener::$preUpdateCalls = array(); + $this->listener->preUpdateCalls = array(); $fix->setFixPrice(2000); $this->_em->persist($fix); $this->_em->flush(); - $this->assertCount(1,CompanyContractListener::$instances); - $this->assertCount(1,CompanyContractListener::$preUpdateCalls); + $this->assertCount(1,$this->listener->preUpdateCalls); - $this->assertSame($fix, CompanyContractListener::$preUpdateCalls[0][0]); + $this->assertSame($fix, $this->listener->preUpdateCalls[0][0]); $this->assertInstanceOf( 'Doctrine\Tests\Models\Company\CompanyFixContract', - CompanyContractListener::$preUpdateCalls[0][0] + $this->listener->preUpdateCalls[0][0] ); $this->assertInstanceOf( 'Doctrine\ORM\Event\PreUpdateEventArgs', - CompanyContractListener::$preUpdateCalls[0][1] + $this->listener->preUpdateCalls[0][1] ); } @@ -166,26 +169,25 @@ class EntityListenersTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->_em->persist($fix); $this->_em->flush(); - CompanyContractListener::$postUpdateCalls = array(); + $this->listener->postUpdateCalls = array(); $fix->setFixPrice(2000); $this->_em->persist($fix); $this->_em->flush(); - $this->assertCount(1,CompanyContractListener::$instances); - $this->assertCount(1,CompanyContractListener::$postUpdateCalls); + $this->assertCount(1,$this->listener->postUpdateCalls); - $this->assertSame($fix, CompanyContractListener::$postUpdateCalls[0][0]); + $this->assertSame($fix, $this->listener->postUpdateCalls[0][0]); $this->assertInstanceOf( 'Doctrine\Tests\Models\Company\CompanyFixContract', - CompanyContractListener::$postUpdateCalls[0][0] + $this->listener->postUpdateCalls[0][0] ); $this->assertInstanceOf( 'Doctrine\ORM\Event\LifecycleEventArgs', - CompanyContractListener::$postUpdateCalls[0][1] + $this->listener->postUpdateCalls[0][1] ); } @@ -197,24 +199,23 @@ class EntityListenersTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->_em->persist($fix); $this->_em->flush(); - CompanyContractListener::$preRemoveCalls = array(); + $this->listener->preRemoveCalls = array(); $this->_em->remove($fix); $this->_em->flush(); - $this->assertCount(1,CompanyContractListener::$instances); - $this->assertCount(1,CompanyContractListener::$preRemoveCalls); + $this->assertCount(1,$this->listener->preRemoveCalls); - $this->assertSame($fix, CompanyContractListener::$preRemoveCalls[0][0]); + $this->assertSame($fix, $this->listener->preRemoveCalls[0][0]); $this->assertInstanceOf( 'Doctrine\Tests\Models\Company\CompanyFixContract', - CompanyContractListener::$preRemoveCalls[0][0] + $this->listener->preRemoveCalls[0][0] ); $this->assertInstanceOf( 'Doctrine\ORM\Event\LifecycleEventArgs', - CompanyContractListener::$preRemoveCalls[0][1] + $this->listener->preRemoveCalls[0][1] ); } @@ -226,24 +227,23 @@ class EntityListenersTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->_em->persist($fix); $this->_em->flush(); - CompanyContractListener::$postRemoveCalls = array(); + $this->listener->postRemoveCalls = array(); $this->_em->remove($fix); $this->_em->flush(); - $this->assertCount(1,CompanyContractListener::$instances); - $this->assertCount(1,CompanyContractListener::$postRemoveCalls); + $this->assertCount(1,$this->listener->postRemoveCalls); - $this->assertSame($fix, CompanyContractListener::$postRemoveCalls[0][0]); + $this->assertSame($fix, $this->listener->postRemoveCalls[0][0]); $this->assertInstanceOf( 'Doctrine\Tests\Models\Company\CompanyFixContract', - CompanyContractListener::$postRemoveCalls[0][0] + $this->listener->postRemoveCalls[0][0] ); $this->assertInstanceOf( 'Doctrine\ORM\Event\LifecycleEventArgs', - CompanyContractListener::$postRemoveCalls[0][1] + $this->listener->postRemoveCalls[0][1] ); } } \ No newline at end of file diff --git a/tests/Doctrine/Tests/ORM/Mapping/AbstractMappingDriverTest.php b/tests/Doctrine/Tests/ORM/Mapping/AbstractMappingDriverTest.php index e4a9e0530..41df5127f 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/AbstractMappingDriverTest.php +++ b/tests/Doctrine/Tests/ORM/Mapping/AbstractMappingDriverTest.php @@ -6,8 +6,6 @@ use Doctrine\ORM\Events; use Doctrine\ORM\Event\LifecycleEventArgs; use Doctrine\Tests\Models\Company\CompanyFixContract; use Doctrine\Tests\Models\Company\CompanyFlexContract; -use Doctrine\Tests\Models\Company\CompanyContractListener; -use Doctrine\Tests\Models\Company\CompanyFlexUltraContractListener; use Doctrine\ORM\Mapping\ClassMetadata; use Doctrine\ORM\Mapping\ClassMetadataInfo; @@ -812,15 +810,18 @@ abstract class AbstractMappingDriverTest extends \Doctrine\Tests\OrmTestCase */ public function testCallEntityListeners() { - $em = $this->_getTestEntityManager(); - $factory = $this->createClassMetadataFactory($em); - $flexClass = $factory->getMetadataFor('Doctrine\Tests\Models\Company\CompanyFixContract'); - $fixClass = $factory->getMetadataFor('Doctrine\Tests\Models\Company\CompanyFlexContract'); - $ultraClass = $factory->getMetadataFor('Doctrine\Tests\Models\Company\CompanyFlexUltraContract'); + $em = $this->_getTestEntityManager(); + $factory = $this->createClassMetadataFactory($em); + $resolver = $em->getConfiguration()->getEntityListenerResolver(); + $flexClass = $factory->getMetadataFor('Doctrine\Tests\Models\Company\CompanyFixContract'); + $fixClass = $factory->getMetadataFor('Doctrine\Tests\Models\Company\CompanyFlexContract'); + $ultraClass = $factory->getMetadataFor('Doctrine\Tests\Models\Company\CompanyFlexUltraContract'); + $contractListener = $resolver->resolve('Doctrine\Tests\Models\Company\CompanyContractListener'); + $ultraContractListener = $resolver->resolve('Doctrine\Tests\Models\Company\CompanyFlexUltraContractListener'); - CompanyContractListener::$prePersistCalls = array(); - CompanyContractListener::$postPersistCalls = array(); - CompanyFlexUltraContractListener::$prePersistCalls = array(); + $contractListener->prePersistCalls = array(); + $contractListener->postPersistCalls = array(); + $ultraContractListener->prePersistCalls = array(); $fix = new CompanyFixContract(); $fixArg = new LifecycleEventArgs($fix, $em); @@ -831,30 +832,29 @@ abstract class AbstractMappingDriverTest extends \Doctrine\Tests\OrmTestCase $ultra = new CompanyFlexContract(); $ultraArg = new LifecycleEventArgs($ultra, $em); - $fixClass->dispatchEntityListeners(Events::prePersist, $fix, $fixArg); - $flexClass->dispatchEntityListeners(Events::prePersist, $flex, $flexArg); - $ultraClass->dispatchEntityListeners(Events::prePersist, $ultra, $ultraArg); + $fixClass->dispatchEntityListeners($resolver, Events::prePersist, $fix, $fixArg); + $flexClass->dispatchEntityListeners($resolver, Events::prePersist, $flex, $flexArg); + $ultraClass->dispatchEntityListeners($resolver, Events::prePersist, $ultra, $ultraArg); - $this->assertCount(3, CompanyContractListener::$prePersistCalls); - $this->assertCount(2, CompanyFlexUltraContractListener::$prePersistCalls); + $this->assertCount(3, $contractListener->prePersistCalls); + $this->assertCount(2, $ultraContractListener->prePersistCalls); - $this->assertSame($fix, CompanyContractListener::$prePersistCalls[0][0]); - $this->assertSame($fixArg, CompanyContractListener::$prePersistCalls[0][1]); + $this->assertSame($fix, $contractListener->prePersistCalls[0][0]); + $this->assertSame($fixArg, $contractListener->prePersistCalls[0][1]); - $this->assertSame($flex, CompanyContractListener::$prePersistCalls[1][0]); - $this->assertSame($flexArg, CompanyContractListener::$prePersistCalls[1][1]); + $this->assertSame($flex, $contractListener->prePersistCalls[1][0]); + $this->assertSame($flexArg, $contractListener->prePersistCalls[1][1]); - $this->assertSame($ultra, CompanyContractListener::$prePersistCalls[2][0]); - $this->assertSame($ultraArg, CompanyContractListener::$prePersistCalls[2][1]); + $this->assertSame($ultra, $contractListener->prePersistCalls[2][0]); + $this->assertSame($ultraArg, $contractListener->prePersistCalls[2][1]); - $this->assertSame($ultra, CompanyFlexUltraContractListener::$prePersistCalls[0][0]); - $this->assertSame($ultraArg, CompanyFlexUltraContractListener::$prePersistCalls[0][1]); + $this->assertSame($ultra, $ultraContractListener->prePersistCalls[0][0]); + $this->assertSame($ultraArg, $ultraContractListener->prePersistCalls[0][1]); - $this->assertSame($ultra, CompanyFlexUltraContractListener::$prePersistCalls[1][0]); - $this->assertSame($ultraArg, CompanyFlexUltraContractListener::$prePersistCalls[1][1]); + $this->assertSame($ultra, $ultraContractListener->prePersistCalls[1][0]); + $this->assertSame($ultraArg, $ultraContractListener->prePersistCalls[1][1]); - $this->assertCount(1, CompanyContractListener::$instances); - $this->assertEmpty(CompanyContractListener::$postPersistCalls); + $this->assertEmpty($contractListener->postPersistCalls); } /** diff --git a/tests/Doctrine/Tests/ORM/Mapping/ListenerResolverTest.php b/tests/Doctrine/Tests/ORM/Mapping/ListenerResolverTest.php new file mode 100644 index 000000000..f54709097 --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Mapping/ListenerResolverTest.php @@ -0,0 +1,99 @@ +resolver = new DefaultEntityListenerResolver(); + } + + public function testResolve() + { + $className = '\Doctrine\Tests\Models\Company\CompanyContractListener'; + $object = $this->resolver->resolve($className); + + $this->assertInstanceOf($className, $object); + $this->assertSame($object, $this->resolver->resolve($className)); + } + + public function testRegisterAndResolve() + { + $className = '\Doctrine\Tests\Models\Company\CompanyContractListener'; + $object = new $className(); + + $this->resolver->register($object); + + $this->assertSame($object, $this->resolver->resolve($className)); + } + + public function testClearOne() + { + $className1 = '\Doctrine\Tests\Models\Company\CompanyContractListener'; + $className2 = '\Doctrine\Tests\Models\Company\CompanyFlexUltraContractListener'; + + $obj1 = $this->resolver->resolve($className1); + $obj2 = $this->resolver->resolve($className2); + + $this->assertInstanceOf($className1, $obj1); + $this->assertInstanceOf($className2, $obj2); + + $this->assertSame($obj1, $this->resolver->resolve($className1)); + $this->assertSame($obj2, $this->resolver->resolve($className2)); + + $this->resolver->clear($className1); + + $this->assertInstanceOf($className1, $this->resolver->resolve($className1)); + $this->assertInstanceOf($className2, $this->resolver->resolve($className2)); + + $this->assertNotSame($obj1, $this->resolver->resolve($className1)); + $this->assertSame($obj2, $this->resolver->resolve($className2)); + } + + public function testClearAll() + { + $className1 = '\Doctrine\Tests\Models\Company\CompanyContractListener'; + $className2 = '\Doctrine\Tests\Models\Company\CompanyFlexUltraContractListener'; + + $obj1 = $this->resolver->resolve($className1); + $obj2 = $this->resolver->resolve($className2); + + $this->assertInstanceOf($className1, $obj1); + $this->assertInstanceOf($className2, $obj2); + + $this->assertSame($obj1, $this->resolver->resolve($className1)); + $this->assertSame($obj2, $this->resolver->resolve($className2)); + + $this->resolver->clear(); + + $this->assertInstanceOf($className1, $this->resolver->resolve($className1)); + $this->assertInstanceOf($className2, $this->resolver->resolve($className2)); + + $this->assertNotSame($obj1, $this->resolver->resolve($className1)); + $this->assertNotSame($obj2, $this->resolver->resolve($className2)); + } + + /** + * @expectedException InvalidArgumentException + * @expectedExceptionMessage An object was expected, but got "string. + */ + public function testRegisterStringException() + { + $this->resolver->register('CompanyContractListener'); + } +} \ No newline at end of file