1
0
mirror of synced 2024-12-15 15:46:02 +03:00
doctrine2/tests/Doctrine/Tests/ORM/Functional/LifecycleCallbackTest.php

122 lines
3.6 KiB
PHP
Raw Normal View History

2009-07-18 22:06:30 +04:00
<?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(
2009-11-13 19:39:28 +03:00
$this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\LifecycleCallbackTestEntity'),
$this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\LifecycleCallbackTestUser')
2009-07-18 22:06:30 +04:00
));
} catch (\Exception $e) {
// Swallow all exceptions. We do not test the schema tool here.
}
}
public function testPreSavePostSaveCallbacksAreInvoked()
{
$entity = new LifecycleCallbackTestEntity;
$entity->value = 'hello';
2009-07-19 20:54:53 +04:00
$this->_em->persist($entity);
2009-07-18 22:06:30 +04:00
$this->_em->flush();
2009-07-24 15:33:38 +04:00
$this->assertTrue($entity->prePersistCallbackInvoked);
$this->assertTrue($entity->postPersistCallbackInvoked);
2009-07-18 22:06:30 +04:00
$this->_em->clear();
$query = $this->_em->createQuery("select e from Doctrine\Tests\ORM\Functional\LifecycleCallbackTestEntity e");
$result = $query->getResult();
2009-07-18 22:06:30 +04:00
$this->assertTrue($result[0]->postLoadCallbackInvoked);
$result[0]->value = 'hello again';
$this->_em->flush();
$this->assertEquals('changed from preUpdate callback!', $result[0]->value);
}
2009-11-13 19:39:28 +03:00
public function testChangesDontGetLost()
{
$user = new LifecycleCallbackTestUser;
$user->setName('Bob');
$user->setValue('');
$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());
}
}
/** @Entity @HasLifecycleCallbacks */
class LifecycleCallbackTestUser {
/** @Id @Column(type="integer") @GeneratedValue(strategy="AUTO") */
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';}
2009-07-18 22:06:30 +04:00
}
/**
* @Entity
* @HasLifecycleCallbacks
* @Table(name="lc_cb_test_entity")
2009-07-18 22:06:30 +04:00
*/
class LifecycleCallbackTestEntity
{
/* test stuff */
2009-07-24 15:33:38 +04:00
public $prePersistCallbackInvoked = false;
public $postPersistCallbackInvoked = false;
2009-07-18 22:06:30 +04:00
public $postLoadCallbackInvoked = false;
/**
* @Id @Column(type="integer")
* @GeneratedValue(strategy="AUTO")
*/
private $id;
/**
2009-11-13 19:39:28 +03:00
* @Column(type="string")
2009-07-18 22:06:30 +04:00
*/
public $value;
2009-07-24 15:33:38 +04:00
/** @PrePersist */
public function doStuffOnPrePersist() {
$this->prePersistCallbackInvoked = true;
2009-07-18 22:06:30 +04:00
}
2009-07-24 15:33:38 +04:00
/** @PostPersist */
public function doStuffOnPostPersist() {
$this->postPersistCallbackInvoked = true;
2009-07-18 22:06:30 +04:00
}
/** @PostLoad */
public function doStuffOnPostLoad() {
$this->postLoadCallbackInvoked = true;
}
/** @PreUpdate */
public function doStuffOnPreUpdate() {
$this->value = 'changed from preUpdate callback!';
}
}