NotifyPolicyTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. namespace Doctrine\Tests\ORM\Functional;
  3. use Doctrine\Common\Collections\ArrayCollection,
  4. Doctrine\Common\NotifyPropertyChanged,
  5. Doctrine\Common\PropertyChangedListener;
  6. require_once __DIR__ . '/../../TestInit.php';
  7. /**
  8. * NativeQueryTest
  9. *
  10. * @author robo
  11. */
  12. class NotifyPolicyTest extends \Doctrine\Tests\OrmFunctionalTestCase
  13. {
  14. protected function setUp() {
  15. parent::setUp();
  16. try {
  17. $this->_schemaTool->createSchema(array(
  18. $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\NotifyUser'),
  19. $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\NotifyGroup')
  20. ));
  21. } catch (\Exception $e) {
  22. // Swallow all exceptions. We do not test the schema tool here.
  23. }
  24. }
  25. public function testChangeTracking()
  26. {
  27. //$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
  28. $user = new NotifyUser();
  29. $group = new NotifyGroup();
  30. $user->setName('roman');
  31. $group->setName('dev');
  32. $user->getGroups()->add($group);
  33. $group->getUsers()->add($user);
  34. $this->_em->persist($user);
  35. $this->_em->persist($group);
  36. $this->assertEquals(1, count($user->listeners));
  37. $this->assertEquals(1, count($group->listeners));
  38. $this->_em->flush();
  39. $this->_em->clear();
  40. $this->assertEquals(1, count($user->listeners));
  41. $this->assertEquals(1, count($group->listeners));
  42. $userId = $user->getId();
  43. $groupId = $group->getId();
  44. unset($user, $group);
  45. $user = $this->_em->find(__NAMESPACE__.'\NotifyUser', $userId);
  46. $this->assertEquals(1, $user->getGroups()->count());
  47. $group = $this->_em->find(__NAMESPACE__.'\NotifyGroup', $groupId);
  48. $this->assertEquals(1, $group->getUsers()->count());
  49. $this->assertEquals(1, count($user->listeners));
  50. $this->assertEquals(1, count($group->listeners));
  51. $group2 = new NotifyGroup();
  52. $group2->setName('nerds');
  53. $this->_em->persist($group2);
  54. $user->getGroups()->add($group2);
  55. $group2->getUsers()->add($user);
  56. $group->setName('geeks');
  57. $this->_em->flush();
  58. $this->_em->clear();
  59. $this->assertEquals(1, count($user->listeners));
  60. $this->assertEquals(1, count($group->listeners));
  61. $group2Id = $group2->getId();
  62. unset($group2, $user);
  63. $user = $this->_em->find(__NAMESPACE__.'\NotifyUser', $userId);
  64. $this->assertEquals(2, $user->getGroups()->count());
  65. $group2 = $this->_em->find(__NAMESPACE__.'\NotifyGroup', $group2Id);
  66. $this->assertEquals(1, $group2->getUsers()->count());
  67. $group = $this->_em->find(__NAMESPACE__.'\NotifyGroup', $groupId);
  68. $this->assertEquals(1, $group->getUsers()->count());
  69. $this->assertEquals('geeks', $group->getName());
  70. }
  71. }
  72. class NotifyBaseEntity implements NotifyPropertyChanged {
  73. public $listeners = array();
  74. public function addPropertyChangedListener(PropertyChangedListener $listener) {
  75. $this->listeners[] = $listener;
  76. }
  77. protected function onPropertyChanged($propName, $oldValue, $newValue) {
  78. if ($this->listeners) {
  79. foreach ($this->listeners as $listener) {
  80. $listener->propertyChanged($this, $propName, $oldValue, $newValue);
  81. }
  82. }
  83. }
  84. }
  85. /** @Entity @ChangeTrackingPolicy("NOTIFY") */
  86. class NotifyUser extends NotifyBaseEntity {
  87. /** @Id @Column(type="integer") @GeneratedValue */
  88. private $id;
  89. /** @Column */
  90. private $name;
  91. /** @ManyToMany(targetEntity="NotifyGroup") */
  92. private $groups;
  93. function __construct() {
  94. $this->groups = new ArrayCollection;
  95. }
  96. function getId() {
  97. return $this->id;
  98. }
  99. function getName() {
  100. return $this->name;
  101. }
  102. function setName($name) {
  103. $this->onPropertyChanged('name', $this->name, $name);
  104. $this->name = $name;
  105. }
  106. function getGroups() {
  107. return $this->groups;
  108. }
  109. }
  110. /** @Entity */
  111. class NotifyGroup extends NotifyBaseEntity {
  112. /** @Id @Column(type="integer") @GeneratedValue */
  113. private $id;
  114. /** @Column */
  115. private $name;
  116. /** @ManyToMany(targetEntity="NotifyUser", mappedBy="groups") */
  117. private $users;
  118. function __construct() {
  119. $this->users = new ArrayCollection;
  120. }
  121. function getId() {
  122. return $this->id;
  123. }
  124. function getName() {
  125. return $this->name;
  126. }
  127. function setName($name) {
  128. $this->onPropertyChanged('name', $this->name, $name);
  129. $this->name = $name;
  130. }
  131. function getUsers() {
  132. return $this->users;
  133. }
  134. }