From 7ec59878a17d97fefd617ba3c45dcae503158d81 Mon Sep 17 00:00:00 2001 From: Guilherme Blanco Date: Tue, 6 Aug 2013 01:47:42 -0400 Subject: [PATCH] Added postLoad behavioral event coverage as a set of functional test. --- .../ORM/Functional/PostLoadEventTest.php | 231 ++++++++++++++++++ 1 file changed, 231 insertions(+) create mode 100644 tests/Doctrine/Tests/ORM/Functional/PostLoadEventTest.php diff --git a/tests/Doctrine/Tests/ORM/Functional/PostLoadEventTest.php b/tests/Doctrine/Tests/ORM/Functional/PostLoadEventTest.php new file mode 100644 index 000000000..bd965eff2 --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Functional/PostLoadEventTest.php @@ -0,0 +1,231 @@ + + */ +class PostLoadEventTest extends \Doctrine\Tests\OrmFunctionalTestCase +{ + /** + * @var integer + */ + private $userId; + + /** + * {@inheritdoc} + */ + protected function setUp() + { + $this->useModelSet('cms'); + + parent::setUp(); + + $this->loadFixture(); + } + + public function testLoadedEntityUsingFindShouldTriggerEvent() + { + $mockListener = $this->getMock('Doctrine\Tests\ORM\Functional\PostLoadListener'); + + // CmsUser and CmsAddres, because it's a ToOne inverse side on CmsUser + $mockListener + ->expects($this->exactly(2)) + ->method('postLoad') + ->will($this->returnValue(true)); + + $eventManager = $this->_em->getEventManager(); + + $eventManager->addEventListener(array(Events::postLoad), $mockListener); + + $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId); + } + + public function testLoadedEntityUsingQueryShouldTriggerEvent() + { + $mockListener = $this->getMock('Doctrine\Tests\ORM\Functional\PostLoadListener'); + + // CmsUser and CmsAddres, because it's a ToOne inverse side on CmsUser + $mockListener + ->expects($this->exactly(2)) + ->method('postLoad') + ->will($this->returnValue(true)); + + $eventManager = $this->_em->getEventManager(); + + $eventManager->addEventListener(array(Events::postLoad), $mockListener); + + $query = $this->_em->createQuery('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :id'); + + $query->setParameter('id', $this->userId); + $query->getResult(); + } + + public function testLoadedAssociationToOneShouldTriggerEvent() + { + $mockListener = $this->getMock('Doctrine\Tests\ORM\Functional\PostLoadListener'); + + // CmsUser (root), CmsAddress (ToOne inverse side), CmsEmail (joined association) + $mockListener + ->expects($this->exactly(3)) + ->method('postLoad') + ->will($this->returnValue(true)); + + $eventManager = $this->_em->getEventManager(); + + $eventManager->addEventListener(array(Events::postLoad), $mockListener); + + $query = $this->_em->createQuery('SELECT u, e FROM Doctrine\Tests\Models\CMS\CmsUser u JOIN u.email e WHERE u.id = :id'); + + $query->setParameter('id', $this->userId); + $query->getResult(); + } + + public function testLoadedAssociationToManyShouldTriggerEvent() + { + $mockListener = $this->getMock('Doctrine\Tests\ORM\Functional\PostLoadListener'); + + // CmsUser (root), CmsAddress (ToOne inverse side), 2 CmsPhonenumber (joined association) + $mockListener + ->expects($this->exactly(4)) + ->method('postLoad') + ->will($this->returnValue(true)); + + $eventManager = $this->_em->getEventManager(); + + $eventManager->addEventListener(array(Events::postLoad), $mockListener); + + $query = $this->_em->createQuery('SELECT u, p FROM Doctrine\Tests\Models\CMS\CmsUser u JOIN u.phonenumbers p WHERE u.id = :id'); + + $query->setParameter('id', $this->userId); + $query->getResult(); + } + + public function testLoadedProxyEntityShouldTriggerEvent() + { + $eventManager = $this->_em->getEventManager(); + + // Should not be invoked during getReference call + $mockListener = $this->getMock('Doctrine\Tests\ORM\Functional\PostLoadListener'); + + $mockListener + ->expects($this->never()) + ->method('postLoad') + ->will($this->returnValue(true)); + + $eventManager->addEventListener(array(Events::postLoad), $mockListener); + + $userProxy = $this->_em->getReference('Doctrine\Tests\Models\CMS\CmsUser', $this->userId); + + // Now deactivate original listener and attach new one + $eventManager->removeEventListener(array(Events::postLoad), $mockListener); + + $mockListener2 = $this->getMock('Doctrine\Tests\ORM\Functional\PostLoadListener'); + + $mockListener2 + ->expects($this->exactly(2)) + ->method('postLoad') + ->will($this->returnValue(true)); + + $eventManager->addEventListener(array(Events::postLoad), $mockListener2); + + $userProxy->getName(); + } + + public function testLoadedProxyAssociationToOneShouldTriggerEvent() + { + $user = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId); + + $mockListener = $this->getMock('Doctrine\Tests\ORM\Functional\PostLoadListener'); + + // CmsEmail (proxy) + $mockListener + ->expects($this->exactly(1)) + ->method('postLoad') + ->will($this->returnValue(true)); + + $eventManager = $this->_em->getEventManager(); + + $eventManager->addEventListener(array(Events::postLoad), $mockListener); + + $emailProxy = $user->getEmail(); + + $emailProxy->getEmail(); + } + + public function testLoadedProxyAssociationToManyShouldTriggerEvent() + { + $user = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId); + + $mockListener = $this->getMock('Doctrine\Tests\ORM\Functional\PostLoadListener'); + + // 2 CmsPhonenumber (proxy) + $mockListener + ->expects($this->exactly(2)) + ->method('postLoad') + ->will($this->returnValue(true)); + + $eventManager = $this->_em->getEventManager(); + + $eventManager->addEventListener(array(Events::postLoad), $mockListener); + + $phonenumbersCol = $user->getPhonenumbers(); + + $phonenumbersCol->first(); + } + + private function loadFixture() + { + $user = new CmsUser; + $user->name = 'Roman'; + $user->username = 'romanb'; + $user->status = 'developer'; + + $address = new CmsAddress; + $address->country = 'Germany'; + $address->city = 'Berlin'; + $address->zip = '12345'; + + $user->setAddress($address); + + $email = new CmsEmail; + $email->setEmail('roman@domain.com'); + + $user->setEmail($email); + + $ph1 = new CmsPhonenumber; + $ph1->phonenumber = "0301234"; + + $ph2 = new CmsPhonenumber; + $ph2->phonenumber = "987654321"; + + $user->addPhonenumber($ph1); + $user->addPhonenumber($ph2); + + $this->_em->persist($user); + $this->_em->flush(); + + $this->userId = $user->getId(); + + $this->_em->clear(); + } +} + +class PostLoadListener +{ + public function postLoad(LifecycleEventArgs $event) + { + // Expected to be mocked out + echo 'Should never be called!'; + } +} \ No newline at end of file