scratch – Blame information for rev 115
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
115 | office | 1 | <?php |
2 | |||
3 | /* |
||
4 | * This file is part of Evenement. |
||
5 | * |
||
6 | * (c) Igor Wiedler <igor@wiedler.ch> |
||
7 | * |
||
8 | * For the full copyright and license information, please view the LICENSE |
||
9 | * file that was distributed with this source code. |
||
10 | */ |
||
11 | |||
12 | namespace Evenement\Tests; |
||
13 | |||
14 | use Evenement\EventEmitter; |
||
15 | |||
16 | class EventEmitterTest extends \PHPUnit_Framework_TestCase |
||
17 | { |
||
18 | private $emitter; |
||
19 | |||
20 | public function setUp() |
||
21 | { |
||
22 | $this->emitter = new EventEmitter(); |
||
23 | } |
||
24 | |||
25 | public function testAddListenerWithLambda() |
||
26 | { |
||
27 | $this->emitter->on('foo', function () {}); |
||
28 | } |
||
29 | |||
30 | public function testAddListenerWithMethod() |
||
31 | { |
||
32 | $listener = new Listener(); |
||
33 | $this->emitter->on('foo', [$listener, 'onFoo']); |
||
34 | } |
||
35 | |||
36 | public function testAddListenerWithStaticMethod() |
||
37 | { |
||
38 | $this->emitter->on('bar', ['Evenement\Tests\Listener', 'onBar']); |
||
39 | } |
||
40 | |||
41 | public function testAddListenerWithInvalidListener() |
||
42 | { |
||
43 | try { |
||
44 | $this->emitter->on('foo', 'not a callable'); |
||
45 | $this->fail(); |
||
46 | } catch (\Exception $e) { |
||
47 | } |
||
48 | } |
||
49 | |||
50 | public function testOnce() |
||
51 | { |
||
52 | $listenerCalled = 0; |
||
53 | |||
54 | $this->emitter->once('foo', function () use (&$listenerCalled) { |
||
55 | $listenerCalled++; |
||
56 | }); |
||
57 | |||
58 | $this->assertSame(0, $listenerCalled); |
||
59 | |||
60 | $this->emitter->emit('foo'); |
||
61 | |||
62 | $this->assertSame(1, $listenerCalled); |
||
63 | |||
64 | $this->emitter->emit('foo'); |
||
65 | |||
66 | $this->assertSame(1, $listenerCalled); |
||
67 | } |
||
68 | |||
69 | public function testOnceWithArguments() |
||
70 | { |
||
71 | $capturedArgs = []; |
||
72 | |||
73 | $this->emitter->once('foo', function ($a, $b) use (&$capturedArgs) { |
||
74 | $capturedArgs = array($a, $b); |
||
75 | }); |
||
76 | |||
77 | $this->emitter->emit('foo', array('a', 'b')); |
||
78 | |||
79 | $this->assertSame(array('a', 'b'), $capturedArgs); |
||
80 | } |
||
81 | |||
82 | public function testEmitWithoutArguments() |
||
83 | { |
||
84 | $listenerCalled = false; |
||
85 | |||
86 | $this->emitter->on('foo', function () use (&$listenerCalled) { |
||
87 | $listenerCalled = true; |
||
88 | }); |
||
89 | |||
90 | $this->assertSame(false, $listenerCalled); |
||
91 | $this->emitter->emit('foo'); |
||
92 | $this->assertSame(true, $listenerCalled); |
||
93 | } |
||
94 | |||
95 | public function testEmitWithOneArgument() |
||
96 | { |
||
97 | $test = $this; |
||
98 | |||
99 | $listenerCalled = false; |
||
100 | |||
101 | $this->emitter->on('foo', function ($value) use (&$listenerCalled, $test) { |
||
102 | $listenerCalled = true; |
||
103 | |||
104 | $test->assertSame('bar', $value); |
||
105 | }); |
||
106 | |||
107 | $this->assertSame(false, $listenerCalled); |
||
108 | $this->emitter->emit('foo', ['bar']); |
||
109 | $this->assertSame(true, $listenerCalled); |
||
110 | } |
||
111 | |||
112 | public function testEmitWithTwoArguments() |
||
113 | { |
||
114 | $test = $this; |
||
115 | |||
116 | $listenerCalled = false; |
||
117 | |||
118 | $this->emitter->on('foo', function ($arg1, $arg2) use (&$listenerCalled, $test) { |
||
119 | $listenerCalled = true; |
||
120 | |||
121 | $test->assertSame('bar', $arg1); |
||
122 | $test->assertSame('baz', $arg2); |
||
123 | }); |
||
124 | |||
125 | $this->assertSame(false, $listenerCalled); |
||
126 | $this->emitter->emit('foo', ['bar', 'baz']); |
||
127 | $this->assertSame(true, $listenerCalled); |
||
128 | } |
||
129 | |||
130 | public function testEmitWithNoListeners() |
||
131 | { |
||
132 | $this->emitter->emit('foo'); |
||
133 | $this->emitter->emit('foo', ['bar']); |
||
134 | $this->emitter->emit('foo', ['bar', 'baz']); |
||
135 | } |
||
136 | |||
137 | public function testEmitWithTwoListeners() |
||
138 | { |
||
139 | $listenersCalled = 0; |
||
140 | |||
141 | $this->emitter->on('foo', function () use (&$listenersCalled) { |
||
142 | $listenersCalled++; |
||
143 | }); |
||
144 | |||
145 | $this->emitter->on('foo', function () use (&$listenersCalled) { |
||
146 | $listenersCalled++; |
||
147 | }); |
||
148 | |||
149 | $this->assertSame(0, $listenersCalled); |
||
150 | $this->emitter->emit('foo'); |
||
151 | $this->assertSame(2, $listenersCalled); |
||
152 | } |
||
153 | |||
154 | public function testRemoveListenerMatching() |
||
155 | { |
||
156 | $listenersCalled = 0; |
||
157 | |||
158 | $listener = function () use (&$listenersCalled) { |
||
159 | $listenersCalled++; |
||
160 | }; |
||
161 | |||
162 | $this->emitter->on('foo', $listener); |
||
163 | $this->emitter->removeListener('foo', $listener); |
||
164 | |||
165 | $this->assertSame(0, $listenersCalled); |
||
166 | $this->emitter->emit('foo'); |
||
167 | $this->assertSame(0, $listenersCalled); |
||
168 | } |
||
169 | |||
170 | public function testRemoveListenerNotMatching() |
||
171 | { |
||
172 | $listenersCalled = 0; |
||
173 | |||
174 | $listener = function () use (&$listenersCalled) { |
||
175 | $listenersCalled++; |
||
176 | }; |
||
177 | |||
178 | $this->emitter->on('foo', $listener); |
||
179 | $this->emitter->removeListener('bar', $listener); |
||
180 | |||
181 | $this->assertSame(0, $listenersCalled); |
||
182 | $this->emitter->emit('foo'); |
||
183 | $this->assertSame(1, $listenersCalled); |
||
184 | } |
||
185 | |||
186 | public function testRemoveAllListenersMatching() |
||
187 | { |
||
188 | $listenersCalled = 0; |
||
189 | |||
190 | $this->emitter->on('foo', function () use (&$listenersCalled) { |
||
191 | $listenersCalled++; |
||
192 | }); |
||
193 | |||
194 | $this->emitter->removeAllListeners('foo'); |
||
195 | |||
196 | $this->assertSame(0, $listenersCalled); |
||
197 | $this->emitter->emit('foo'); |
||
198 | $this->assertSame(0, $listenersCalled); |
||
199 | } |
||
200 | |||
201 | public function testRemoveAllListenersNotMatching() |
||
202 | { |
||
203 | $listenersCalled = 0; |
||
204 | |||
205 | $this->emitter->on('foo', function () use (&$listenersCalled) { |
||
206 | $listenersCalled++; |
||
207 | }); |
||
208 | |||
209 | $this->emitter->removeAllListeners('bar'); |
||
210 | |||
211 | $this->assertSame(0, $listenersCalled); |
||
212 | $this->emitter->emit('foo'); |
||
213 | $this->assertSame(1, $listenersCalled); |
||
214 | } |
||
215 | |||
216 | public function testRemoveAllListenersWithoutArguments() |
||
217 | { |
||
218 | $listenersCalled = 0; |
||
219 | |||
220 | $this->emitter->on('foo', function () use (&$listenersCalled) { |
||
221 | $listenersCalled++; |
||
222 | }); |
||
223 | |||
224 | $this->emitter->on('bar', function () use (&$listenersCalled) { |
||
225 | $listenersCalled++; |
||
226 | }); |
||
227 | |||
228 | $this->emitter->removeAllListeners(); |
||
229 | |||
230 | $this->assertSame(0, $listenersCalled); |
||
231 | $this->emitter->emit('foo'); |
||
232 | $this->emitter->emit('bar'); |
||
233 | $this->assertSame(0, $listenersCalled); |
||
234 | } |
||
235 | } |