Permalink
Browse files

test event listeners lifecycle callback

  • Loading branch information...
1 parent 256cecb commit 69bfc71b6a14f8c91901c94b987a32edf5b19453 @FabioBatSilva committed with fabio.silva Aug 10, 2012
@@ -15,9 +15,6 @@
* "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 @@ public function setSpouse(CompanyPerson $spouse) {
}
}
- public function prePersistHandler($event)
- {
- $this->prePersistHandlerCalls[] = $event;
- }
-
-
public static function loadMetadata(\Doctrine\ORM\Mapping\ClassMetadataInfo $metadata)
{
@@ -11,6 +11,7 @@ protected function setUp() {
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 @@ public function testLifecycleCallbackEventArgs()
*/
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->calls['prePersistHandler']
+ );
+
$this->assertInstanceOf(
'Doctrine\ORM\Event\LifecycleEventArgs',
- $e->prePersistHandlerCalls[0]
+ $e->calls['postPersistHandler']
);
}
}
@@ -400,6 +407,34 @@ public function preUpdate(PreUpdateEventArgs $eventArgs)
}
+/**
+ * @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
{

0 comments on commit 69bfc71

Please sign in to comment.