Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

implements a entity listener resolver

  • Loading branch information...
commit a01d6583d3bcb24984b9904d51c135b66b51403a 1 parent 27745bb
@FabioBatSilva authored fabio.silva committed
View
28 lib/Doctrine/ORM/Configuration.php
@@ -29,6 +29,8 @@
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 @@ public function getQuoteStrategy()
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'];
+ }
}
View
20 lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php
@@ -27,6 +27,7 @@
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
use Doctrine\Common\ClassLoader;
use Doctrine\Common\EventArgs;
+use Doctrine\ORM\Mapping\EntityListenerResolver;
/**
* A <tt>ClassMetadata</tt> instance holds all the object-relational mapping metadata
@@ -445,11 +446,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.
*
* The mapping definition array supports the following keys:
@@ -2520,21 +2516,19 @@ public function addEntityListener($eventName, $class, $method)
/**
* 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'];
-
- if ( ! isset(self::$entityListenerInstances[$class])) {
- self::$entityListenerInstances[$class] = new $class();
- }
+ $class = $listener['class'];
+ $method = $listener['method'];
+ $instance = $resolver->resolve($class);
- self::$entityListenerInstances[$class]->{$method}($entity, $arg);
+ $instance->{$method}($entity, $arg);
}
}
View
76 lib/Doctrine/ORM/Mapping/DefaultEntityListenerResolver.php
@@ -0,0 +1,76 @@
+<?php
+
+/*
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * This software consists of voluntary contributions made by many individuals
+ * and is licensed under the MIT license. For more information, see
+ * <http://www.doctrine-project.org>.
+ */
+
+namespace Doctrine\ORM\Mapping;
+
+/**
+ * The default DefaultEntityListene
+ *
+ * @since 2.4
+ * @author Fabio B. Silva <fabio.bat.silva@gmail.com>
+ */
+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();
+ }
+}
View
55 lib/Doctrine/ORM/Mapping/EntityListenerResolver.php
@@ -0,0 +1,55 @@
+<?php
+
+/*
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * This software consists of voluntary contributions made by many individuals
+ * and is licensed under the MIT license. For more information, see
+ * <http://www.doctrine-project.org>.
+ */
+
+namespace Doctrine\ORM\Mapping;
+
+/**
+ * A resolver is used to instantiate an entity listener.
+ *
+ * @since 2.4
+ * @author Fabio B. Silva <fabio.bat.silva@gmail.com>
+ */
+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);
+}
View
28 lib/Doctrine/ORM/UnitOfWork.php
@@ -219,6 +219,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.
*
* @var array
@@ -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 @@ public function computeChangeSet(ClassMetadata $class, $entity)
// 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 @@ private function persistNew($class, $entity)
}
if ($hasEntityListeners) {
- $class->dispatchEntityListeners(Events::prePersist, $entity, $event);
+ $class->dispatchEntityListeners($this->entityListenerResolver, Events::prePersist, $entity, $event);
}
if ($hasListeners) {
@@ -986,7 +994,7 @@ private function executeInserts($class)
}
if ($hasEntityListeners) {
- $class->dispatchEntityListeners(Events::postPersist, $entity, $event);
+ $class->dispatchEntityListeners($this->entityListenerResolver, Events::postPersist, $entity, $event);
}
if ($hasListeners) {
@@ -1035,7 +1043,7 @@ private function executeUpdates($class)
}
if ($hasPreUpdateEntityListeners) {
- $class->dispatchEntityListeners(Events::preUpdate, $entity, $preEvent);
+ $class->dispatchEntityListeners($this->entityListenerResolver, Events::preUpdate, $entity, $preEvent);
}
if ($hasPreUpdateListeners) {
@@ -1053,7 +1061,7 @@ private function executeUpdates($class)
}
if ($hasPostUpdateEntityListeners) {
- $class->dispatchEntityListeners(Events::postUpdate, $entity, $postEvent);
+ $class->dispatchEntityListeners($this->entityListenerResolver, Events::postUpdate, $entity, $postEvent);
}
if ($hasPostUpdateListeners) {
@@ -1108,7 +1116,7 @@ private function executeDeletions($class)
}
if ($hasEntityListeners) {
- $class->dispatchEntityListeners(Events::postRemove, $entity, $event);
+ $class->dispatchEntityListeners($this->entityListenerResolver, Events::postRemove, $entity, $event);
}
if ($hasListeners) {
@@ -1765,7 +1773,7 @@ private function doRemove($entity, array &$visited)
}
if ($hasEntityListeners) {
- $class->dispatchEntityListeners(Events::preRemove, $entity, $event);
+ $class->dispatchEntityListeners($this->entityListenerResolver, Events::preRemove, $entity, $event);
}
if ($hasListeners) {
@@ -2781,7 +2789,7 @@ public function createEntity($className, array $data, &$hints = array())
}
if ($hasEntityListeners) {
- $class->dispatchEntityListeners(Events::postLoad, $entity, $event);
+ $class->dispatchEntityListeners($this->entityListenerResolver, Events::postLoad, $entity, $event);
}
if ($hasListeners) {
View
39 tests/Doctrine/Tests/Models/Company/CompanyContractListener.php
@@ -4,32 +4,25 @@
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 @@ public function postPersistHandler(CompanyContract $contract)
*/
public function prePersistHandler(CompanyContract $contract)
{
- self::$prePersistCalls[] = func_get_args();
+ $this->prePersistCalls[] = func_get_args();
}
/**
@@ -45,7 +38,7 @@ public function prePersistHandler(CompanyContract $contract)
*/
public function postUpdateHandler(CompanyContract $contract)
{
- self::$postUpdateCalls[] = func_get_args();
+ $this->postUpdateCalls[] = func_get_args();
}
/**
@@ -53,7 +46,7 @@ public function postUpdateHandler(CompanyContract $contract)
*/
public function preUpdateHandler(CompanyContract $contract)
{
- self::$preUpdateCalls[] = func_get_args();
+ $this->preUpdateCalls[] = func_get_args();
}
/**
@@ -61,7 +54,7 @@ public function preUpdateHandler(CompanyContract $contract)
*/
public function postRemoveHandler(CompanyContract $contract)
{
- self::$postRemoveCalls[] = func_get_args();
+ $this->postRemoveCalls[] = func_get_args();
}
/**
@@ -69,7 +62,7 @@ public function postRemoveHandler(CompanyContract $contract)
*/
public function preRemoveHandler(CompanyContract $contract)
{
- self::$preRemoveCalls[] = func_get_args();
+ $this->preRemoveCalls[] = func_get_args();
}
/**
@@ -77,7 +70,7 @@ public function preRemoveHandler(CompanyContract $contract)
*/
public function preFlushHandler(CompanyContract $contract)
{
- self::$preFlushCalls[] = func_get_args();
+ $this->preFlushCalls[] = func_get_args();
}
/**
@@ -85,7 +78,7 @@ public function preFlushHandler(CompanyContract $contract)
*/
public function postLoadHandler(CompanyContract $contract)
{
- self::$postLoadCalls[] = func_get_args();
+ $this->postLoadCalls[] = func_get_args();
}
}
View
12 tests/Doctrine/Tests/Models/Company/CompanyFlexUltraContractListener.php
@@ -6,20 +6,14 @@
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 @@ public function prePersistHandler1(CompanyContract $contract, LifecycleEventArgs
*/
public function prePersistHandler2(CompanyContract $contract, LifecycleEventArgs $args)
{
- self::$prePersistCalls[] = func_get_args();
+ $this->prePersistCalls[] = func_get_args();
}
}
View
12 tests/Doctrine/Tests/ORM/ConfigurationTest.php
@@ -260,6 +260,18 @@ public function testSetGetQuoteStrategy()
$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
View
98 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 @@
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 @@ public function testPreFlushListeners()
$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 @@ public function testPostLoadListeners()
$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 @@ public function testPrePersistListeners()
$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 @@ public function testPostPersistListeners()
$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 @@ public function testPreUpdateListeners()
$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 @@ public function testPostUpdateListeners()
$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 @@ public function testPreRemoveListeners()
$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 @@ public function testPostRemoveListeners()
$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]
);
}
}
View
56 tests/Doctrine/Tests/ORM/Mapping/AbstractMappingDriverTest.php
@@ -6,8 +6,6 @@
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 @@ public function testEntityListeners()
*/
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');
-
- CompanyContractListener::$prePersistCalls = array();
- CompanyContractListener::$postPersistCalls = array();
- CompanyFlexUltraContractListener::$prePersistCalls = array();
+ $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');
+
+ $contractListener->prePersistCalls = array();
+ $contractListener->postPersistCalls = array();
+ $ultraContractListener->prePersistCalls = array();
$fix = new CompanyFixContract();
$fixArg = new LifecycleEventArgs($fix, $em);
@@ -831,30 +832,29 @@ public function testCallEntityListeners()
$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);
}
/**
View
99 tests/Doctrine/Tests/ORM/Mapping/ListenerResolverTest.php
@@ -0,0 +1,99 @@
+<?php
+
+namespace Doctrine\Tests\ORM\Mapping;
+
+use Doctrine\ORM\Mapping\DefaultEntityListenerResolver;
+
+require_once __DIR__ . '/../../TestInit.php';
+
+/**
+ * @group DDC-1955
+ */
+class ListenerResolverTest extends \Doctrine\Tests\OrmTestCase
+{
+
+ /**
+ * @var \Doctrine\ORM\Mapping\DefaultEntityListenerResolver
+ */
+ private $resolver;
+
+ protected function setUp()
+ {
+ parent::setUp();
+ $this->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');
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.