LifecycleCallbackTest.php 6.31 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
<?php

namespace Doctrine\Tests\ORM\Functional;

require_once __DIR__ . '/../../TestInit.php';

class LifecycleCallbackTest extends \Doctrine\Tests\OrmFunctionalTestCase
{
    protected function setUp() {
        parent::setUp();
        try {
            $this->_schemaTool->createSchema(array(
romanb's avatar
romanb committed
13
                $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\LifecycleCallbackTestEntity'),
14 15
                $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\LifecycleCallbackTestUser'),
                $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\LifecycleCallbackCascader'),
16 17 18 19 20 21 22 23 24 25
            ));
        } catch (\Exception $e) {
            // Swallow all exceptions. We do not test the schema tool here.
        }
    }
    
    public function testPreSavePostSaveCallbacksAreInvoked()
    {        
        $entity = new LifecycleCallbackTestEntity;
        $entity->value = 'hello';
romanb's avatar
romanb committed
26
        $this->_em->persist($entity);
27 28
        $this->_em->flush();
        
romanb's avatar
romanb committed
29 30
        $this->assertTrue($entity->prePersistCallbackInvoked);
        $this->assertTrue($entity->postPersistCallbackInvoked);
31 32 33 34
        
        $this->_em->clear();
        
        $query = $this->_em->createQuery("select e from Doctrine\Tests\ORM\Functional\LifecycleCallbackTestEntity e");
35
        $result = $query->getResult();
36 37 38 39 40 41 42 43
        $this->assertTrue($result[0]->postLoadCallbackInvoked);
        
        $result[0]->value = 'hello again';
        
        $this->_em->flush();
        
        $this->assertEquals('changed from preUpdate callback!', $result[0]->value);
    }
romanb's avatar
romanb committed
44 45 46 47 48
    
    public function testChangesDontGetLost()
    {
        $user = new LifecycleCallbackTestUser;
        $user->setName('Bob');
49
        $user->setValue('value');
romanb's avatar
romanb committed
50 51 52 53 54 55 56 57 58 59 60 61 62
        $this->_em->persist($user);
        $this->_em->flush();
        
        $user->setName('Alice');
        $this->_em->flush(); // Triggers preUpdate
        
        $this->_em->clear();
        
        $user2 = $this->_em->find(get_class($user), $user->getId());
        
        $this->assertEquals('Alice', $user2->getName());
        $this->assertEquals('Hello World', $user2->getValue());
    }
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82

    /**
     * @group DDC-194
     */
    public function testGetReferenceWithPostLoadEventIsDelayedUntilProxyTrigger()
    {
        $entity = new LifecycleCallbackTestEntity;
        $entity->value = 'hello';
        $this->_em->persist($entity);
        $this->_em->flush();
        $id = $entity->getId();

        $this->_em->clear();

        $reference = $this->_em->getReference('Doctrine\Tests\ORM\Functional\LifecycleCallbackTestEntity', $id);
        $this->assertFalse($reference->postLoadCallbackInvoked);

        $reference->getId(); // trigger proxy load
        $this->assertTrue($reference->postLoadCallbackInvoked);
    }
83 84 85 86 87 88

    /**
     * @group DDC-113
     */
    public function testCascadedEntitiesCallsPrePersist()
    {
romanb's avatar
romanb committed
89 90
        //$this->_em->getConnection()->getConfiguration()->setSqlLogger(new \Doctrine\DBAL\Logging\EchoSqlLogger);
        
91 92 93 94
        $e1 = new LifecycleCallbackTestEntity;
        $e2 = new LifecycleCallbackTestEntity;

        $c = new LifecycleCallbackCascader();
romanb's avatar
romanb committed
95 96
        $this->_em->persist($c);
        
97 98
        $c->entities[] = $e1;
        $c->entities[] = $e2;
romanb's avatar
romanb committed
99 100 101 102
        $e1->cascader = $c;
        $e2->cascader = $c;
        
        //$this->_em->persist($c);
103 104 105 106 107
        $this->_em->flush();

        $this->assertTrue($e1->prePersistCallbackInvoked);
        $this->assertTrue($e2->prePersistCallbackInvoked);
    }
romanb's avatar
romanb committed
108 109 110 111 112 113
    
    public function testLifecycleCallbacksGetInherited()
    {
        $childMeta = $this->_em->getClassMetadata(__NAMESPACE__ . '\LifecycleCallbackChildEntity');
        $this->assertEquals(array('prePersist' => array(0 => 'doStuff')), $childMeta->lifecycleCallbacks);
    }
romanb's avatar
romanb committed
114 115 116 117
}

/** @Entity @HasLifecycleCallbacks */
class LifecycleCallbackTestUser {
romanb's avatar
romanb committed
118
    /** @Id @Column(type="integer") @GeneratedValue */
romanb's avatar
romanb committed
119 120 121 122 123 124 125 126 127 128 129 130
    private $id;
    /** @Column(type="string") */
    private $value;
    /** @Column(type="string") */
    private $name;
    public function getId() {return $this->id;}
    public function getValue() {return $this->value;}
    public function setValue($value) {$this->value = $value;}
    public function getName() {return $this->name;}
    public function setName($name) {$this->name = $name;}
    /** @PreUpdate */
    public function testCallback() {$this->value = 'Hello World';}
131 132 133 134
}

/**
 * @Entity
135
 * @HasLifecycleCallbacks
136
 * @Table(name="lc_cb_test_entity")
137 138 139 140
 */
class LifecycleCallbackTestEntity
{
    /* test stuff */
romanb's avatar
romanb committed
141 142
    public $prePersistCallbackInvoked = false;
    public $postPersistCallbackInvoked = false;
143 144 145 146 147 148 149 150
    public $postLoadCallbackInvoked = false;
    
    /**
     * @Id @Column(type="integer")
     * @GeneratedValue(strategy="AUTO")
     */
    private $id;
    /**
151
     * @Column(type="string", nullable=true)
152 153
     */
    public $value;
154

155 156 157 158 159 160
    /**
     * @ManyToOne(targetEntity="LifecycleCallbackCascader")
     * @JoinColumn(name="cascader_id", referencedColumnName="id")
     */
    public $cascader;

161 162 163
    public function getId() {
        return $this->id;
    }
164
    
romanb's avatar
romanb committed
165 166 167
    /** @PrePersist */
    public function doStuffOnPrePersist() {
        $this->prePersistCallbackInvoked = true;
168 169
    }
    
romanb's avatar
romanb committed
170 171 172
    /** @PostPersist */
    public function doStuffOnPostPersist() {
        $this->postPersistCallbackInvoked = true;
173 174 175 176 177 178 179 180 181 182 183
    }
    
    /** @PostLoad */
    public function doStuffOnPostLoad() {
        $this->postLoadCallbackInvoked = true;
    }
    
    /** @PreUpdate */
    public function doStuffOnPreUpdate() {
        $this->value = 'changed from preUpdate callback!';
    }
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
}

/**
 * @Entity
 * @Table(name="lc_cb_test_cascade")
 */
class LifecycleCallbackCascader
{
    /**
     * @Id @Column(type="integer")
     * @GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
romanb's avatar
romanb committed
199
     * @OneToMany(targetEntity="LifecycleCallbackTestEntity", mappedBy="cascader", cascade={"persist"})
200 201 202 203 204 205 206
     */
    public $entities;

    public function __construct()
    {
        $this->entities = new \Doctrine\Common\Collections\ArrayCollection();
    }
romanb's avatar
romanb committed
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
}

/** @MappedSuperclass @HasLifecycleCallbacks */
class LifecycleCallbackParentEntity {
    /** @PrePersist */
    function doStuff() {
        
    }
}

/** @Entity @Table(name="lc_cb_childentity") */
class LifecycleCallbackChildEntity extends LifecycleCallbackParentEntity {
    /** @Id @Column(type="integer") @GeneratedValue */
    private $id;
}