PageRenderTime 36ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/doctrine/orm/tests/Doctrine/Tests/ORM/Functional/LifecycleCallbackTest.php

https://bitbucket.org/iiic/iszp
PHP | 311 lines | 194 code | 63 blank | 54 comment | 0 complexity | 0a73310b6674233e620a21290cf10b9c MD5 | raw file
  1. <?php
  2. namespace Doctrine\Tests\ORM\Functional;
  3. use Doctrine\ORM\Event\PreUpdateEventArgs;
  4. require_once __DIR__ . '/../../TestInit.php';
  5. class LifecycleCallbackTest extends \Doctrine\Tests\OrmFunctionalTestCase
  6. {
  7. protected function setUp() {
  8. parent::setUp();
  9. try {
  10. $this->_schemaTool->createSchema(array(
  11. $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\LifecycleCallbackTestEntity'),
  12. $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\LifecycleCallbackTestUser'),
  13. $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\LifecycleCallbackCascader'),
  14. ));
  15. } catch (\Exception $e) {
  16. // Swallow all exceptions. We do not test the schema tool here.
  17. }
  18. }
  19. public function testPreSavePostSaveCallbacksAreInvoked()
  20. {
  21. $entity = new LifecycleCallbackTestEntity;
  22. $entity->value = 'hello';
  23. $this->_em->persist($entity);
  24. $this->_em->flush();
  25. $this->assertTrue($entity->prePersistCallbackInvoked);
  26. $this->assertTrue($entity->postPersistCallbackInvoked);
  27. $this->_em->clear();
  28. $query = $this->_em->createQuery("select e from Doctrine\Tests\ORM\Functional\LifecycleCallbackTestEntity e");
  29. $result = $query->getResult();
  30. $this->assertTrue($result[0]->postLoadCallbackInvoked);
  31. $result[0]->value = 'hello again';
  32. $this->_em->flush();
  33. $this->assertEquals('changed from preUpdate callback!', $result[0]->value);
  34. }
  35. public function testPreFlushCallbacksAreInvoked()
  36. {
  37. $entity = new LifecycleCallbackTestEntity;
  38. $entity->value = 'hello';
  39. $this->_em->persist($entity);
  40. $this->_em->flush();
  41. $this->assertTrue($entity->prePersistCallbackInvoked);
  42. $this->assertTrue($entity->preFlushCallbackInvoked);
  43. $entity->preFlushCallbackInvoked = false;
  44. $this->_em->flush();
  45. $this->assertTrue($entity->preFlushCallbackInvoked);
  46. $entity->value = 'bye';
  47. $entity->preFlushCallbackInvoked = false;
  48. $this->_em->flush();
  49. $this->assertTrue($entity->preFlushCallbackInvoked);
  50. }
  51. public function testChangesDontGetLost()
  52. {
  53. $user = new LifecycleCallbackTestUser;
  54. $user->setName('Bob');
  55. $user->setValue('value');
  56. $this->_em->persist($user);
  57. $this->_em->flush();
  58. $user->setName('Alice');
  59. $this->_em->flush(); // Triggers preUpdate
  60. $this->_em->clear();
  61. $user2 = $this->_em->find(get_class($user), $user->getId());
  62. $this->assertEquals('Alice', $user2->getName());
  63. $this->assertEquals('Hello World', $user2->getValue());
  64. }
  65. /**
  66. * @group DDC-194
  67. */
  68. public function testGetReferenceWithPostLoadEventIsDelayedUntilProxyTrigger()
  69. {
  70. $entity = new LifecycleCallbackTestEntity;
  71. $entity->value = 'hello';
  72. $this->_em->persist($entity);
  73. $this->_em->flush();
  74. $id = $entity->getId();
  75. $this->_em->clear();
  76. $reference = $this->_em->getReference('Doctrine\Tests\ORM\Functional\LifecycleCallbackTestEntity', $id);
  77. $this->assertFalse($reference->postLoadCallbackInvoked);
  78. $reference->getValue(); // trigger proxy load
  79. $this->assertTrue($reference->postLoadCallbackInvoked);
  80. }
  81. /**
  82. * @group DDC-958
  83. */
  84. public function testPostLoadTriggeredOnRefresh()
  85. {
  86. $entity = new LifecycleCallbackTestEntity;
  87. $entity->value = 'hello';
  88. $this->_em->persist($entity);
  89. $this->_em->flush();
  90. $id = $entity->getId();
  91. $this->_em->clear();
  92. $reference = $this->_em->find('Doctrine\Tests\ORM\Functional\LifecycleCallbackTestEntity', $id);
  93. $this->assertTrue($reference->postLoadCallbackInvoked);
  94. $reference->postLoadCallbackInvoked = false;
  95. $this->_em->refresh($reference);
  96. $this->assertTrue($reference->postLoadCallbackInvoked, "postLoad should be invoked when refresh() is called.");
  97. }
  98. /**
  99. * @group DDC-113
  100. */
  101. public function testCascadedEntitiesCallsPrePersist()
  102. {
  103. //$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
  104. $e1 = new LifecycleCallbackTestEntity;
  105. $e2 = new LifecycleCallbackTestEntity;
  106. $c = new LifecycleCallbackCascader();
  107. $this->_em->persist($c);
  108. $c->entities[] = $e1;
  109. $c->entities[] = $e2;
  110. $e1->cascader = $c;
  111. $e2->cascader = $c;
  112. //$this->_em->persist($c);
  113. $this->_em->flush();
  114. $this->assertTrue($e1->prePersistCallbackInvoked);
  115. $this->assertTrue($e2->prePersistCallbackInvoked);
  116. }
  117. public function testLifecycleCallbacksGetInherited()
  118. {
  119. $childMeta = $this->_em->getClassMetadata(__NAMESPACE__ . '\LifecycleCallbackChildEntity');
  120. $this->assertEquals(array('prePersist' => array(0 => 'doStuff')), $childMeta->lifecycleCallbacks);
  121. }
  122. public function testLifecycleListener_ChangeUpdateChangeSet()
  123. {
  124. $listener = new LifecycleListenerPreUpdate;
  125. $this->_em->getEventManager()->addEventListener(array('preUpdate'), $listener);
  126. $user = new LifecycleCallbackTestUser;
  127. $user->setName('Bob');
  128. $user->setValue('value');
  129. $this->_em->persist($user);
  130. $this->_em->flush();
  131. $this->_em->clear();
  132. $dql = "SELECT u FROM Doctrine\Tests\ORM\Functional\LifecycleCallbackTestUser u WHERE u.name = 'Bob'";
  133. $bob = $this->_em->createQuery($dql)->getSingleResult();
  134. $bob->setName('Alice');
  135. $this->_em->flush(); // preUpdate reverts Alice to Bob
  136. $this->_em->clear();
  137. $this->_em->getEventManager()->removeEventListener(array('preUpdate'), $listener);
  138. $bob = $this->_em->createQuery($dql)->getSingleResult();
  139. $this->assertEquals('Bob', $bob->getName());
  140. }
  141. }
  142. /** @Entity @HasLifecycleCallbacks */
  143. class LifecycleCallbackTestUser {
  144. /** @Id @Column(type="integer") @GeneratedValue */
  145. private $id;
  146. /** @Column(type="string") */
  147. private $value;
  148. /** @Column(type="string") */
  149. private $name;
  150. public function getId() {return $this->id;}
  151. public function getValue() {return $this->value;}
  152. public function setValue($value) {$this->value = $value;}
  153. public function getName() {return $this->name;}
  154. public function setName($name) {$this->name = $name;}
  155. /** @PreUpdate */
  156. public function testCallback() {$this->value = 'Hello World';}
  157. }
  158. /**
  159. * @Entity
  160. * @HasLifecycleCallbacks
  161. * @Table(name="lc_cb_test_entity")
  162. */
  163. class LifecycleCallbackTestEntity
  164. {
  165. /* test stuff */
  166. public $prePersistCallbackInvoked = false;
  167. public $postPersistCallbackInvoked = false;
  168. public $postLoadCallbackInvoked = false;
  169. public $preFlushCallbackInvoked = false;
  170. /**
  171. * @Id @Column(type="integer")
  172. * @GeneratedValue(strategy="AUTO")
  173. */
  174. private $id;
  175. /**
  176. * @Column(type="string", nullable=true)
  177. */
  178. public $value;
  179. /**
  180. * @ManyToOne(targetEntity="LifecycleCallbackCascader")
  181. * @JoinColumn(name="cascader_id", referencedColumnName="id")
  182. */
  183. public $cascader;
  184. public function getId() {
  185. return $this->id;
  186. }
  187. public function getValue() {
  188. return $this->value;
  189. }
  190. /** @PrePersist */
  191. public function doStuffOnPrePersist() {
  192. $this->prePersistCallbackInvoked = true;
  193. }
  194. /** @PostPersist */
  195. public function doStuffOnPostPersist() {
  196. $this->postPersistCallbackInvoked = true;
  197. }
  198. /** @PostLoad */
  199. public function doStuffOnPostLoad() {
  200. $this->postLoadCallbackInvoked = true;
  201. }
  202. /** @PreUpdate */
  203. public function doStuffOnPreUpdate() {
  204. $this->value = 'changed from preUpdate callback!';
  205. }
  206. /** @PreFlush */
  207. public function doStuffOnPreFlush() {
  208. $this->preFlushCallbackInvoked = true;
  209. }
  210. }
  211. /**
  212. * @Entity
  213. * @Table(name="lc_cb_test_cascade")
  214. */
  215. class LifecycleCallbackCascader
  216. {
  217. /**
  218. * @Id @Column(type="integer")
  219. * @GeneratedValue(strategy="AUTO")
  220. */
  221. private $id;
  222. /**
  223. * @OneToMany(targetEntity="LifecycleCallbackTestEntity", mappedBy="cascader", cascade={"persist"})
  224. */
  225. public $entities;
  226. public function __construct()
  227. {
  228. $this->entities = new \Doctrine\Common\Collections\ArrayCollection();
  229. }
  230. }
  231. /** @MappedSuperclass @HasLifecycleCallbacks */
  232. class LifecycleCallbackParentEntity {
  233. /** @PrePersist */
  234. function doStuff() {
  235. }
  236. }
  237. /** @Entity @Table(name="lc_cb_childentity") */
  238. class LifecycleCallbackChildEntity extends LifecycleCallbackParentEntity {
  239. /** @Id @Column(type="integer") @GeneratedValue */
  240. private $id;
  241. }
  242. class LifecycleListenerPreUpdate
  243. {
  244. public function preUpdate(PreUpdateEventArgs $eventArgs)
  245. {
  246. $eventArgs->setNewValue('name', 'Bob');
  247. }
  248. }