From 69bfc71b6a14f8c91901c94b987a32edf5b19453 Mon Sep 17 00:00:00 2001 From: "Fabio B. Silva" Date: Thu, 9 Aug 2012 22:25:16 -0300 Subject: [PATCH] test event listeners lifecycle callback --- .../Tests/Models/Company/CompanyPerson.php | 11 ----- .../ORM/Functional/LifecycleCallbackTest.php | 43 +++++++++++++++++-- 2 files changed, 39 insertions(+), 15 deletions(-) diff --git a/tests/Doctrine/Tests/Models/Company/CompanyPerson.php b/tests/Doctrine/Tests/Models/Company/CompanyPerson.php index 0d6a175ad..4636fa4e3 100644 --- a/tests/Doctrine/Tests/Models/Company/CompanyPerson.php +++ b/tests/Doctrine/Tests/Models/Company/CompanyPerson.php @@ -15,9 +15,6 @@ namespace Doctrine\Tests\Models\Company; * "manager" = "CompanyManager", * "employee" = "CompanyEmployee" * }) - * @EntityListeners(callbacks = { - * @LifecycleCallback(\Doctrine\ORM\Events::prePersist, method = "prePersistHandler") - * }) * * @NamedNativeQueries({ * @NamedNativeQuery( @@ -82,8 +79,6 @@ class CompanyPerson */ private $friends; - public $prePersistHandlerCalls = array(); - public function __construct() { $this->friends = new \Doctrine\Common\Collections\ArrayCollection; } @@ -122,12 +117,6 @@ class CompanyPerson } } - public function prePersistHandler($event) - { - $this->prePersistHandlerCalls[] = $event; - } - - public static function loadMetadata(\Doctrine\ORM\Mapping\ClassMetadataInfo $metadata) { diff --git a/tests/Doctrine/Tests/ORM/Functional/LifecycleCallbackTest.php b/tests/Doctrine/Tests/ORM/Functional/LifecycleCallbackTest.php index 4a740ceca..151da181b 100644 --- a/tests/Doctrine/Tests/ORM/Functional/LifecycleCallbackTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/LifecycleCallbackTest.php @@ -11,6 +11,7 @@ class LifecycleCallbackTest extends \Doctrine\Tests\OrmFunctionalTestCase parent::setUp(); try { $this->_schemaTool->createSchema(array( + $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\EntityListenersLifecycleCallback'), $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\LifecycleCallbackEventArgEntity'), $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\LifecycleCallbackTestEntity'), $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\LifecycleCallbackTestUser'), @@ -259,16 +260,22 @@ class LifecycleCallbackTest extends \Doctrine\Tests\OrmFunctionalTestCase */ public function testEventListenersLifecycleCallback() { - $e = new \Doctrine\Tests\Models\Company\CompanyPerson; - $e->setName('Fabio B. Silva'); + $e = new EntityListenersLifecycleCallback; + $e->value = 'foo'; $this->_em->persist($e); $this->_em->flush(); - $this->assertCount(1, $e->prePersistHandlerCalls); + $this->assertCount(2, $e->calls); + $this->assertInstanceOf( 'Doctrine\ORM\Event\LifecycleEventArgs', - $e->prePersistHandlerCalls[0] + $e->calls['prePersistHandler'] + ); + + $this->assertInstanceOf( + 'Doctrine\ORM\Event\LifecycleEventArgs', + $e->calls['postPersistHandler'] ); } } @@ -400,6 +407,34 @@ class LifecycleListenerPreUpdate } +/** + * @Entity + * @EntityListeners(callbacks = { + * @LifecycleCallback(\Doctrine\ORM\Events::prePersist, method = "prePersistHandler"), + * @LifecycleCallback(\Doctrine\ORM\Events::postPersist, method = "postPersistHandler"), + * }) + */ +class EntityListenersLifecycleCallback +{ + /** @Id @Column(type="integer") @GeneratedValue */ + public $id; + + /** @Column() */ + public $value; + + public $calls = array(); + + public function prePersistHandler($event) + { + $this->calls[__FUNCTION__] = $event; + } + + public function postPersistHandler(\Doctrine\ORM\Event\LifecycleEventArgs $event) + { + $this->calls[__FUNCTION__] = $event; + } +} + /** @Entity @HasLifecycleCallbacks */ class LifecycleCallbackEventArgEntity {