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
|
<?php
// $Id: DispatcherTest.php 284686 2009-07-24 05:22:17Z clockwerx $
/** * Unit tests for Event_Dispatcher package. * * @author Bertrand Mansion <bmansion@mamasam.com> * @package Event_Dispatcher * @subpackage Tests */ require_once 'PHPUnit/Framework/TestCase.php'; require_once 'Event/Dispatcher.php';
class Notified { var $notif;
function notifReceived(&$notif) { $this->notif =& $notif; }
function description() { $notObj =& $this->notif->getNotificationObject(); $name = $this->notif->getNotificationName(); $info = $this->notif->getNotificationInfo(); $desc = $name.':'.implode(':', $info).':'.$notObj->id; return $desc; } }
class Dummy { var $id; function Dummy($id = 'default') { $this->id = $id; } }
class Notifier { var $id = 'notifier'; function Notifier($id) { $this->id = $id; $ed =& Event_Dispatcher::getInstance(); $ed->post($this, 'NotifierInstanciated', array('info')); } }
function notified(&$notif) { $obj = $notif->getNotificationObject(); $obj->id = $notif->getNotificationName().':'.implode(':', $notif->getNotificationInfo()); }
class DispatcherTest extends PHPUnit_Framework_TestCase {
// Get the default dispatch center function test1() { $nf = new Notified(); $dm = new Dummy(); $ed =& Event_Dispatcher::getInstance();
// Generic notification, global observer $ed->addObserver(array(&$nf, 'notifReceived')); $not =& $ed->post($dm, 'test', array('A', 'B')); $this->assertEquals('test:A:B:default', $nf->description(), "Error"); $this->assertEquals(1, $not->getNotificationCount(), "Wrong notification count");
// Object references $dm->id = 'dummy'; $this->assertEquals('test:A:B:dummy', $nf->description(), "Wrong notification description");
// Named notifications $ed->addObserver('notified', 'NotifierInstanciated'); $nt = new Notifier('notifier'); $this->assertEquals('NotifierInstanciated:info', $nt->id, "Wrong notification id");
// Pending notifications $not =& $ed->post($nt, 'PendingNotification'); $ed->addObserver(array(&$nf, 'notifReceived'), 'PendingNotification'); $this->assertEquals('PendingNotification::NotifierInstanciated:info', $nf->description(), "Error"); $this->assertEquals(2, $not->getNotificationCount(), "Error");
// Class filter 1 $ed->addObserver(array(&$nf, 'notifReceived'), 'ClassFilterNotification', 'Dummy'); $not =& $ed->post($nt, 'ClassFilterNotification', array('isGlobal')); $this->assertEquals('ClassFilterNotification:isGlobal:NotifierInstanciated:info', $nf->description(), "Error"); $this->assertEquals(1, $not->getNotificationCount(), "Error");
// Remove observer $ed->removeObserver(array(&$nf, 'notifReceived')); $nt->id = 'reset'; $not =& $ed->post($nt, 'ClassFilterNotification', array('test')); $this->assertEquals('ClassFilterNotification:isGlobal:reset', $nf->description(), "Error"); $this->assertEquals(0, $not->getNotificationCount(), "Error");
// Class filter 2 $not =& $ed->post($dm, 'ClassFilterNotification'); $this->assertEquals('ClassFilterNotification::dummy', $nf->description(), "Error"); $this->assertEquals(1, $not->getNotificationCount(), "Error");
// Re-add the global observer $ed->addObserver(array(&$nf, 'notifReceived')); $not =& $ed->post($dm, 'ClassFilterNotification'); $this->assertEquals('ClassFilterNotification::dummy', $nf->description(), "Error"); $this->assertEquals(2, $not->getNotificationCount(), "Error");
}
// Tests with 2 dispatchers function test2() { $nf = new Notified(); $dm = new Dummy();
$ed2 =& Event_Dispatcher::getInstance('another'); $ed1 =& Event_Dispatcher::getInstance();
$ed2->addObserver(array(&$nf, 'notifReceived')); $not =& $ed2->post($dm, 'test', array('A', 'B')); $this->assertEquals('test:A:B:default', $nf->description(), "Error"); $this->assertEquals(1, $not->getNotificationCount(), "Error");
$not =& $ed1->post($dm, 'test', array('A2', 'B2')); $this->assertEquals(1, $not->getNotificationCount(), "Error"); $not =& $ed1->post($dm, 'test', array('A2', 'B2')); $this->assertEquals(1, $not->getNotificationCount(), "Error");
$ed2->addObserver(array(&$nf, 'notifReceived'), 'ClassFilterNotification', 'Notifier'); $not =& $ed2->post($dm, 'ClassFilterNotification'); $this->assertEquals('ClassFilterNotification::default', $nf->description(), "Error"); $this->assertEquals(1, $not->getNotificationCount(), "Error");
$ed2->addObserver(array(&$nf, 'notifReceived'), 'ClassFilterNotification', 'Dummy'); $not =& $ed2->post($dm, 'ClassFilterNotification'); $this->assertEquals(2, $not->getNotificationCount(), "Error");
} } ?>
|