1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Tests\ODM\CouchDB\Functional; |
4
|
|
|
|
5
|
|
|
class EventManagerTest extends \Doctrine\Tests\ODM\CouchDB\CouchDBFunctionalTestCase |
6
|
|
|
{ |
7
|
|
|
private $listener; |
8
|
|
|
private $dm; |
9
|
|
|
|
10
|
|
|
public function setUp() |
11
|
|
|
{ |
12
|
|
|
$this->listener = new TestListener(); |
13
|
|
|
$this->dm = $this->createDocumentManager(); |
14
|
|
|
$this->dm->getEventManager()->addEventListener(array('prePersist', 'preUpdate', 'postUpdate', 'preRemove', 'postRemove', 'onFlush'), $this->listener); |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
public function testTriggerEvents() |
18
|
|
|
{ |
19
|
|
|
$user = new \Doctrine\Tests\Models\CMS\CmsUser(); |
20
|
|
|
$user->name = "beberlei"; |
21
|
|
|
$user->username = "beberlei"; |
22
|
|
|
$user->status = "active"; |
23
|
|
|
|
24
|
|
|
$this->dm->persist($user); |
25
|
|
|
|
26
|
|
|
$this->assertTrue($this->listener->prePersist); |
27
|
|
|
|
28
|
|
|
$this->dm->flush(); |
29
|
|
|
|
30
|
|
|
$this->assertTrue($this->listener->onFlush); |
31
|
|
|
$this->assertTrue($this->listener->preUpdate); |
32
|
|
|
$this->assertTrue($this->listener->postUpdate); |
33
|
|
|
$this->assertFalse($this->listener->preRemove); |
34
|
|
|
$this->assertFalse($this->listener->postRemove); |
35
|
|
|
|
36
|
|
|
$this->dm->remove($user); |
37
|
|
|
|
38
|
|
|
$this->assertTrue($this->listener->preRemove); |
39
|
|
|
$this->assertFalse($this->listener->postRemove); |
40
|
|
|
$this->assertTrue($this->dm->contains($user)); |
41
|
|
|
|
42
|
|
|
$this->dm->flush(); |
43
|
|
|
|
44
|
|
|
$this->assertFalse($this->dm->contains($user)); |
45
|
|
|
$this->assertTrue($this->listener->postRemove); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
class TestListener |
50
|
|
|
{ |
51
|
|
|
public $prePersist = false; |
52
|
|
|
public $preUpdate = false; |
53
|
|
|
public $postUpdate = false; |
54
|
|
|
public $preRemove = false; |
55
|
|
|
public $postRemove = false; |
56
|
|
|
public $onFlush = false; |
57
|
|
|
|
58
|
|
|
public function prePersist() |
59
|
|
|
{ |
60
|
|
|
$this->prePersist = true; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function preUpdate() |
64
|
|
|
{ |
65
|
|
|
$this->preUpdate = true; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function postUpdate() |
69
|
|
|
{ |
70
|
|
|
$this->postUpdate = true; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function preRemove() |
74
|
|
|
{ |
75
|
|
|
$this->preRemove = true; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function postRemove() |
79
|
|
|
{ |
80
|
|
|
$this->postRemove = true; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function onFlush() |
84
|
|
|
{ |
85
|
|
|
$this->onFlush = true; |
86
|
|
|
} |
87
|
|
|
} |