These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Doctrine\Tests\ORM\Functional; |
||
4 | |||
5 | use Doctrine\Common\Collections\ArrayCollection; |
||
6 | use Doctrine\Common\NotifyPropertyChanged; |
||
7 | use Doctrine\Common\PropertyChangedListener; |
||
8 | use Doctrine\Tests\OrmFunctionalTestCase; |
||
9 | |||
10 | /** |
||
11 | * NativeQueryTest |
||
12 | * |
||
13 | * @author robo |
||
14 | */ |
||
15 | class NotifyPolicyTest extends OrmFunctionalTestCase |
||
16 | { |
||
17 | protected function setUp() |
||
18 | { |
||
19 | parent::setUp(); |
||
20 | try { |
||
21 | $this->_schemaTool->createSchema( |
||
22 | [ |
||
23 | $this->_em->getClassMetadata(NotifyUser::class), |
||
24 | $this->_em->getClassMetadata(NotifyGroup::class) |
||
25 | ] |
||
26 | ); |
||
27 | } catch (\Exception $e) { |
||
28 | // Swallow all exceptions. We do not test the schema tool here. |
||
29 | } |
||
30 | } |
||
31 | |||
32 | public function testChangeTracking() |
||
33 | { |
||
34 | $user = new NotifyUser(); |
||
35 | $group = new NotifyGroup(); |
||
36 | $user->setName('roman'); |
||
37 | $group->setName('dev'); |
||
38 | |||
39 | $user->getGroups()->add($group); |
||
40 | $group->getUsers()->add($user); |
||
41 | |||
42 | $this->_em->persist($user); |
||
43 | $this->_em->persist($group); |
||
44 | |||
45 | $this->assertEquals(1, count($user->listeners)); |
||
46 | $this->assertEquals(1, count($group->listeners)); |
||
47 | |||
48 | $this->_em->flush(); |
||
49 | $this->_em->clear(); |
||
50 | |||
51 | $this->assertEquals(1, count($user->listeners)); |
||
52 | $this->assertEquals(1, count($group->listeners)); |
||
53 | |||
54 | $userId = $user->getId(); |
||
55 | $groupId = $group->getId(); |
||
56 | unset($user, $group); |
||
57 | |||
58 | $user = $this->_em->find(NotifyUser::class, $userId); |
||
59 | $this->assertEquals(1, $user->getGroups()->count()); |
||
60 | $group = $this->_em->find(NotifyGroup::class, $groupId); |
||
61 | $this->assertEquals(1, $group->getUsers()->count()); |
||
62 | |||
63 | $this->assertEquals(1, count($user->listeners)); |
||
64 | $this->assertEquals(1, count($group->listeners)); |
||
65 | |||
66 | $group2 = new NotifyGroup(); |
||
67 | $group2->setName('nerds'); |
||
68 | $this->_em->persist($group2); |
||
69 | $user->getGroups()->add($group2); |
||
70 | $group2->getUsers()->add($user); |
||
71 | |||
72 | $group->setName('geeks'); |
||
73 | |||
74 | $this->_em->flush(); |
||
75 | $this->_em->clear(); |
||
76 | |||
77 | $this->assertEquals(1, count($user->listeners)); |
||
78 | $this->assertEquals(1, count($group->listeners)); |
||
79 | |||
80 | $group2Id = $group2->getId(); |
||
81 | unset($group2, $user); |
||
82 | |||
83 | $user = $this->_em->find(NotifyUser::class, $userId); |
||
84 | $this->assertEquals(2, $user->getGroups()->count()); |
||
85 | $group2 = $this->_em->find(NotifyGroup::class, $group2Id); |
||
86 | $this->assertEquals(1, $group2->getUsers()->count()); |
||
87 | $group = $this->_em->find(NotifyGroup::class, $groupId); |
||
88 | $this->assertEquals(1, $group->getUsers()->count()); |
||
89 | $this->assertEquals('geeks', $group->getName()); |
||
90 | } |
||
91 | } |
||
92 | |||
93 | View Code Duplication | class NotifyBaseEntity implements NotifyPropertyChanged { |
|
94 | public $listeners = []; |
||
95 | |||
96 | public function addPropertyChangedListener(PropertyChangedListener $listener) { |
||
97 | $this->listeners[] = $listener; |
||
98 | } |
||
99 | |||
100 | protected function onPropertyChanged($propName, $oldValue, $newValue) { |
||
101 | if ($this->listeners) { |
||
0 ignored issues
–
show
|
|||
102 | foreach ($this->listeners as $listener) { |
||
103 | $listener->propertyChanged($this, $propName, $oldValue, $newValue); |
||
104 | } |
||
105 | } |
||
106 | } |
||
107 | } |
||
108 | |||
109 | /** @Entity @ChangeTrackingPolicy("NOTIFY") */ |
||
110 | class NotifyUser extends NotifyBaseEntity { |
||
111 | /** @Id @Column(type="integer") @GeneratedValue */ |
||
112 | private $id; |
||
113 | |||
114 | /** @Column */ |
||
115 | private $name; |
||
116 | |||
117 | /** @ManyToMany(targetEntity="NotifyGroup") */ |
||
118 | private $groups; |
||
119 | |||
120 | function __construct() { |
||
121 | $this->groups = new ArrayCollection; |
||
122 | } |
||
123 | |||
124 | function getId() { |
||
125 | return $this->id; |
||
126 | } |
||
127 | |||
128 | function getName() { |
||
129 | return $this->name; |
||
130 | } |
||
131 | |||
132 | function setName($name) { |
||
133 | $this->onPropertyChanged('name', $this->name, $name); |
||
134 | $this->name = $name; |
||
135 | } |
||
136 | |||
137 | function getGroups() { |
||
138 | return $this->groups; |
||
139 | } |
||
140 | } |
||
141 | |||
142 | /** @Entity */ |
||
143 | class NotifyGroup extends NotifyBaseEntity { |
||
144 | /** @Id @Column(type="integer") @GeneratedValue */ |
||
145 | private $id; |
||
146 | |||
147 | /** @Column */ |
||
148 | private $name; |
||
149 | |||
150 | /** @ManyToMany(targetEntity="NotifyUser", mappedBy="groups") */ |
||
151 | private $users; |
||
152 | |||
153 | function __construct() { |
||
154 | $this->users = new ArrayCollection; |
||
155 | } |
||
156 | |||
157 | function getId() { |
||
158 | return $this->id; |
||
159 | } |
||
160 | |||
161 | function getName() { |
||
162 | return $this->name; |
||
163 | } |
||
164 | |||
165 | function setName($name) { |
||
166 | $this->onPropertyChanged('name', $this->name, $name); |
||
167 | $this->name = $name; |
||
168 | } |
||
169 | |||
170 | function getUsers() { |
||
171 | return $this->users; |
||
172 | } |
||
173 | } |
||
174 | |||
175 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.