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