1<?php 2 3namespace Sabre\Event; 4 5class EventEmitterTest extends \PHPUnit_Framework_TestCase { 6 7 function testInit() { 8 9 $ee = new EventEmitter(); 10 $this->assertInstanceOf('Sabre\\Event\\EventEmitter', $ee); 11 12 } 13 14 function testListeners() { 15 16 $ee = new EventEmitter(); 17 18 $callback1 = function() { }; 19 $callback2 = function() { }; 20 $ee->on('foo', $callback1, 200); 21 $ee->on('foo', $callback2, 100); 22 23 $this->assertEquals([$callback2, $callback1], $ee->listeners('foo')); 24 25 } 26 27 /** 28 * @depends testInit 29 */ 30 function testHandleEvent() { 31 32 $argResult = null; 33 34 $ee = new EventEmitter(); 35 $ee->on('foo', function($arg) use (&$argResult) { 36 37 $argResult = $arg; 38 39 }); 40 41 $this->assertTrue( 42 $ee->emit('foo', ['bar']) 43 ); 44 45 $this->assertEquals('bar', $argResult); 46 47 } 48 49 /** 50 * @depends testHandleEvent 51 */ 52 function testCancelEvent() { 53 54 $argResult = 0; 55 56 $ee = new EventEmitter(); 57 $ee->on('foo', function($arg) use (&$argResult) { 58 59 $argResult = 1; 60 return false; 61 62 }); 63 $ee->on('foo', function($arg) use (&$argResult) { 64 65 $argResult = 2; 66 67 }); 68 69 $this->assertFalse( 70 $ee->emit('foo', ['bar']) 71 ); 72 73 $this->assertEquals(1, $argResult); 74 75 } 76 77 /** 78 * @depends testCancelEvent 79 */ 80 function testPriority() { 81 82 $argResult = 0; 83 84 $ee = new EventEmitter(); 85 $ee->on('foo', function($arg) use (&$argResult) { 86 87 $argResult = 1; 88 return false; 89 90 }); 91 $ee->on('foo', function($arg) use (&$argResult) { 92 93 $argResult = 2; 94 return false; 95 96 }, 1); 97 98 $this->assertFalse( 99 $ee->emit('foo', ['bar']) 100 ); 101 102 $this->assertEquals(2, $argResult); 103 104 } 105 106 /** 107 * @depends testPriority 108 */ 109 function testPriority2() { 110 111 $result = []; 112 $ee = new EventEmitter(); 113 114 $ee->on('foo', function() use (&$result) { 115 116 $result[] = 'a'; 117 118 }, 200); 119 $ee->on('foo', function() use (&$result) { 120 121 $result[] = 'b'; 122 123 }, 50); 124 $ee->on('foo', function() use (&$result) { 125 126 $result[] = 'c'; 127 128 }, 300); 129 $ee->on('foo', function() use (&$result) { 130 131 $result[] = 'd'; 132 133 }); 134 135 $ee->emit('foo'); 136 $this->assertEquals(['b', 'd', 'a', 'c'], $result); 137 138 } 139 140 function testRemoveListener() { 141 142 $result = false; 143 144 $callBack = function() use (&$result) { 145 146 $result = true; 147 148 }; 149 150 $ee = new EventEmitter(); 151 152 $ee->on('foo', $callBack); 153 154 $ee->emit('foo'); 155 $this->assertTrue($result); 156 $result = false; 157 158 $this->assertTrue( 159 $ee->removeListener('foo', $callBack) 160 ); 161 162 $ee->emit('foo'); 163 $this->assertFalse($result); 164 165 } 166 167 function testRemoveUnknownListener() { 168 169 $result = false; 170 171 $callBack = function() use (&$result) { 172 173 $result = true; 174 175 }; 176 177 $ee = new EventEmitter(); 178 179 $ee->on('foo', $callBack); 180 181 $ee->emit('foo'); 182 $this->assertTrue($result); 183 $result = false; 184 185 $this->assertFalse($ee->removeListener('bar', $callBack)); 186 187 $ee->emit('foo'); 188 $this->assertTrue($result); 189 190 } 191 192 function testRemoveListenerTwice() { 193 194 $result = false; 195 196 $callBack = function() use (&$result) { 197 198 $result = true; 199 200 }; 201 202 $ee = new EventEmitter(); 203 204 $ee->on('foo', $callBack); 205 206 $ee->emit('foo'); 207 $this->assertTrue($result); 208 $result = false; 209 210 $this->assertTrue( 211 $ee->removeListener('foo', $callBack) 212 ); 213 $this->assertFalse( 214 $ee->removeListener('foo', $callBack) 215 ); 216 217 $ee->emit('foo'); 218 $this->assertFalse($result); 219 220 } 221 222 function testRemoveAllListeners() { 223 224 $result = false; 225 $callBack = function() use (&$result) { 226 227 $result = true; 228 229 }; 230 231 $ee = new EventEmitter(); 232 $ee->on('foo', $callBack); 233 234 $ee->emit('foo'); 235 $this->assertTrue($result); 236 $result = false; 237 238 $ee->removeAllListeners('foo'); 239 240 $ee->emit('foo'); 241 $this->assertFalse($result); 242 243 } 244 245 function testRemoveAllListenersNoArg() { 246 247 $result = false; 248 249 $callBack = function() use (&$result) { 250 251 $result = true; 252 253 }; 254 255 256 $ee = new EventEmitter(); 257 $ee->on('foo', $callBack); 258 259 $ee->emit('foo'); 260 $this->assertTrue($result); 261 $result = false; 262 263 $ee->removeAllListeners(); 264 265 $ee->emit('foo'); 266 $this->assertFalse($result); 267 268 } 269 270 function testOnce() { 271 272 $result = 0; 273 274 $callBack = function() use (&$result) { 275 276 $result++; 277 278 }; 279 280 $ee = new EventEmitter(); 281 $ee->once('foo', $callBack); 282 283 $ee->emit('foo'); 284 $ee->emit('foo'); 285 286 $this->assertEquals(1, $result); 287 288 } 289 290 /** 291 * @depends testCancelEvent 292 */ 293 function testPriorityOnce() { 294 295 $argResult = 0; 296 297 $ee = new EventEmitter(); 298 $ee->once('foo', function($arg) use (&$argResult) { 299 300 $argResult = 1; 301 return false; 302 303 }); 304 $ee->once('foo', function($arg) use (&$argResult) { 305 306 $argResult = 2; 307 return false; 308 309 }, 1); 310 311 $this->assertFalse( 312 $ee->emit('foo', ['bar']) 313 ); 314 315 $this->assertEquals(2, $argResult); 316 317 } 318} 319