NotifyPolicyTest.php 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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->_em->flush();
  37. $this->_em->clear();
  38. $userId = $user->getId();
  39. $groupId = $group->getId();
  40. unset($user, $group);
  41. $user = $this->_em->find(__NAMESPACE__.'\NotifyUser', $userId);
  42. $this->assertEquals(1, $user->getGroups()->count());
  43. $group = $this->_em->find(__NAMESPACE__.'\NotifyGroup', $groupId);
  44. $this->assertEquals(1, $group->getUsers()->count());
  45. $group2 = new NotifyGroup();
  46. $group2->setName('nerds');
  47. $this->_em->persist($group2);
  48. $user->getGroups()->add($group2);
  49. $group2->getUsers()->add($user);
  50. $group->setName('geeks');
  51. $this->_em->flush();
  52. $this->_em->clear();
  53. $group2Id = $group2->getId();
  54. unset($group2, $user);
  55. $user = $this->_em->find(__NAMESPACE__.'\NotifyUser', $userId);
  56. $this->assertEquals(2, $user->getGroups()->count());
  57. $group2 = $this->_em->find(__NAMESPACE__.'\NotifyGroup', $group2Id);
  58. $this->assertEquals(1, $group2->getUsers()->count());
  59. $group = $this->_em->find(__NAMESPACE__.'\NotifyGroup', $groupId);
  60. $this->assertEquals(1, $group->getUsers()->count());
  61. $this->assertEquals('geeks', $group->getName());
  62. }
  63. }
  64. class NotifyBaseEntity implements NotifyPropertyChanged {
  65. private $listeners = array();
  66. public function addPropertyChangedListener(PropertyChangedListener $listener) {
  67. $this->listeners[] = $listener;
  68. }
  69. protected function onPropertyChanged($propName, $oldValue, $newValue) {
  70. if ($this->listeners) {
  71. foreach ($this->listeners as $listener) {
  72. $listener->propertyChanged($this, $propName, $oldValue, $newValue);
  73. }
  74. }
  75. }
  76. }
  77. /** @Entity @ChangeTrackingPolicy("NOTIFY") */
  78. class NotifyUser extends NotifyBaseEntity {
  79. /** @Id @Column(type="integer") @GeneratedValue */
  80. private $id;
  81. /** @Column */
  82. private $name;
  83. /** @ManyToMany(targetEntity="NotifyGroup") */
  84. private $groups;
  85. function __construct() {
  86. $this->groups = new ArrayCollection;
  87. }
  88. function getId() {
  89. return $this->id;
  90. }
  91. function getName() {
  92. return $this->name;
  93. }
  94. function setName($name) {
  95. $this->onPropertyChanged('name', $this->name, $name);
  96. $this->name = $name;
  97. }
  98. function getGroups() {
  99. return $this->groups;
  100. }
  101. }
  102. /** @Entity */
  103. class NotifyGroup extends NotifyBaseEntity {
  104. /** @Id @Column(type="integer") @GeneratedValue */
  105. private $id;
  106. /** @Column */
  107. private $name;
  108. /** @ManyToMany(targetEntity="NotifyUser", mappedBy="groups") */
  109. private $users;
  110. function __construct() {
  111. $this->users = new ArrayCollection;
  112. }
  113. function getId() {
  114. return $this->id;
  115. }
  116. function getName() {
  117. return $this->name;
  118. }
  119. function setName($name) {
  120. $this->onPropertyChanged('name', $this->name, $name);
  121. $this->name = $name;
  122. }
  123. function getUsers() {
  124. return $this->users;
  125. }
  126. }