-
Notifications
You must be signed in to change notification settings - Fork 283
/
BlameableTest.php
208 lines (160 loc) · 6.36 KB
/
BlameableTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
namespace Tests\Knp\DoctrineBehaviors\ORM;
use Knp\DoctrineBehaviors\Reflection\ClassAnalyzer;
use Doctrine\Common\EventManager;
require_once 'EntityManagerProvider.php';
class BlameableTest extends \PHPUnit_Framework_TestCase
{
private $subscriber;
use EntityManagerProvider;
protected function getUsedEntityFixtures()
{
return [
'BehaviorFixtures\\ORM\\BlameableEntity',
'BehaviorFixtures\\ORM\\UserEntity'
];
}
protected function getEventManager($user = null, $userCallback = null, $userEntity = null)
{
$em = new EventManager;
$this->subscriber = new \Knp\DoctrineBehaviors\ORM\Blameable\BlameableSubscriber(
new ClassAnalyzer(),
false,
'Knp\DoctrineBehaviors\Model\Blameable\Blameable',
$userCallback,
$userEntity
);
$this->subscriber->setUser($user);
$em->addEventSubscriber($this->subscriber);
return $em;
}
public function testCreate()
{
$em = $this->getEntityManager($this->getEventManager('user'));
$entity = new \BehaviorFixtures\ORM\BlameableEntity();
$em->persist($entity);
$em->flush();
$this->assertEquals('user', $entity->getCreatedBy());
$this->assertEquals('user', $entity->getUpdatedBy());
$this->assertNull($entity->getDeletedBy());
}
public function testUpdate()
{
$em = $this->getEntityManager($this->getEventManager('user'));
$entity = new \BehaviorFixtures\ORM\BlameableEntity();
$em->persist($entity);
$em->flush();
$id = $entity->getId();
$createdBy = $entity->getCreatedBy();
$em->clear();
$subscribers = $em->getEventManager()->getListeners()['preUpdate'];
$subscriber = array_pop($subscribers);
$subscriber->setUser('user2');
$entity = $em->getRepository('BehaviorFixtures\ORM\BlameableEntity')->find($id);
$entity->setTitle('test'); // need to modify at least one column to trigger onUpdate
$em->flush();
$em->clear();
//$entity = $em->getRepository('BehaviorFixtures\ORM\BlameableEntity')->find($id);
$this->assertEquals($createdBy, $entity->getCreatedBy(), 'createdBy is constant');
$this->assertEquals('user2', $entity->getUpdatedBy());
$this->assertNotEquals(
$entity->getCreatedBy(),
$entity->getUpdatedBy(),
'createBy and updatedBy have diverged since new update'
);
}
public function testRemove()
{
$em = $this->getEntityManager($this->getEventManager('user'));
$entity = new \BehaviorFixtures\ORM\BlameableEntity();
$em->persist($entity);
$em->flush();
$id = $entity->getId();
$em->clear();
$subscribers = $em->getEventManager()->getListeners()['preRemove'];
$subscriber = array_pop($subscribers);
$subscriber->setUser('user3');
$entity = $em->getRepository('BehaviorFixtures\ORM\BlameableEntity')->find($id);
$em->remove($entity);
$em->flush();
$em->clear();
$this->assertEquals('user3', $entity->getDeletedBy());
}
public function testSubscriberWithUserCallback()
{
$user = new \BehaviorFixtures\ORM\UserEntity();
$user->setUsername('user');
$user2 = new \BehaviorFixtures\ORM\UserEntity();
$user2->setUsername('user2');
$userCallback = function() use($user) {
return $user;
};
$em = $this->getEntityManager($this->getEventManager(null, $userCallback, get_class($user)));
$em->persist($user);
$em->persist($user2);
$em->flush();
$entity = new \BehaviorFixtures\ORM\BlameableEntity();
$em->persist($entity);
$em->flush();
$id = $entity->getId();
$createdBy = $entity->getCreatedBy();
$this->subscriber->setUser($user2); // switch user for update
$entity = $em->getRepository('BehaviorFixtures\ORM\BlameableEntity')->find($id);
$entity->setTitle('test'); // need to modify at least one column to trigger onUpdate
$em->flush();
$em->clear();
$this->assertInstanceOf('BehaviorFixtures\\ORM\\UserEntity', $entity->getCreatedBy(), 'createdBy is a user object');
$this->assertEquals($createdBy->getUsername(), $entity->getCreatedBy()->getUsername(), 'createdBy is constant');
$this->assertEquals($user2->getUsername(), $entity->getUpdatedBy()->getUsername());
$this->assertNotEquals(
$entity->getCreatedBy(),
$entity->getUpdatedBy(),
'createBy and updatedBy have diverged since new update'
);
}
/**
* @test
*/
public function should_only_persist_user_entity()
{
$user = new \BehaviorFixtures\ORM\UserEntity();
$user->setUsername('user');
$userCallback = function() use($user) {
return $user;
};
$em = $this->getEntityManager($this->getEventManager('anon.', $userCallback, get_class($user)));
$em->persist($user);
$em->flush();
$entity = new \BehaviorFixtures\ORM\BlameableEntity();
$em->persist($entity);
$em->flush();
$this->assertNull($entity->getCreatedBy(), 'createdBy is a not updated because not a user entity object');
$this->assertNull($entity->getUpdatedBy(), 'updatedBy is a not updated because not a user entity object');
}
/**
* @test
*/
public function should_only_persist_user_string()
{
$user = new \BehaviorFixtures\ORM\UserEntity();
$em = $this->getEntityManager($this->getEventManager($user));
$entity = new \BehaviorFixtures\ORM\BlameableEntity();
$em->persist($entity);
$em->flush();
$this->assertNull($entity->getCreatedBy(), 'createdBy is a not updated because not a user entity object');
$this->assertNull($entity->getUpdatedBy(), 'updatedBy is a not updated because not a user entity object');
}
/**
* @test
* @expectedException Exception
*/
public function should_notice_deprecation()
{
set_error_handler(function() {throw new \Exception; }, E_USER_DEPRECATED);
new \Knp\DoctrineBehaviors\ORM\Blameable\BlameableListener;
}
public function tearDown()
{
restore_error_handler();
}
}