public function testMultipleEmits() { $test = null; $this->emitter->on('foo', function ($arg) use(&$test) { $test = $arg; }); $this->emitter->on('foo', function ($arg) use(&$test) { $test = $arg; }); $this->emitter->emit('foo', 'bar'); $this->assertNotEmpty($test); }
public function testEventPriority() { $result = null; $this->emitter->on('init', function () use(&$result) { $result = 'early'; }); $this->emitter->on('init', function () use(&$result) { $result = 'late'; }, 11); $this->emitter->emit('init'); $this->assertEquals('early', $result); }
public function emit($event, array $arguments = array()) { foreach ($this->anyListeners as $listener) { call_user_func_array($listener, $arguments); } parent::emit($event, $arguments); }
function testRemoveAllListeners() { $self = $this; $ok = false; $bad = false; $eventEmitter = new EventEmitter(); $listener1 = function () use(&$bad) { $bad = true; }; $listener2 = function () use(&$bad) { $bad = true; }; $eventEmitter->on('test', $listener1); $eventEmitter->on('test', $listener2); $eventEmitter->removeAllListeners('test'); $eventEmitter->emit('test'); $this->assertFalse($bad); }
public function testManyMulti() { $GLOBALS['emit_result'] = 0; $closure = function () { $GLOBALS['emit_result']++; }; $this->event->many(array('a', 'b'), 2, $closure); $this->assertEquals(0, $GLOBALS['emit_result']); $this->event->emit('a'); $this->assertEquals(1, $GLOBALS['emit_result']); $this->event->emit('a'); $this->assertEquals(2, $GLOBALS['emit_result']); $this->event->emit('a'); $this->assertEquals(2, $GLOBALS['emit_result']); $this->event->emit('b'); $this->assertEquals(3, $GLOBALS['emit_result']); $this->event->emit('b'); $this->assertEquals(4, $GLOBALS['emit_result']); }
function testContinueCallBackBreakByHandler() { $ee = new EventEmitter(); $handlerCounter = 0; $bla = function () use(&$handlerCounter) { $handlerCounter++; return false; }; $ee->on('foo', $bla); $ee->on('foo', $bla); $ee->on('foo', $bla); $continueCounter = 0; $r = $ee->emit('foo', [], function () use(&$continueCounter) { $continueCounter++; return false; }); $this->assertFalse($r); $this->assertEquals(1, $handlerCounter); $this->assertEquals(0, $continueCounter); }
function testUnregisterAllListenersForMultipleEvents() { $a = 0; $b = 0; $ee = new EventEmitter(); $ee->on(['foo', 'bar', 'qux'], function () use(&$a) { $a++; }); $ee->on(['bar', 'qux'], function () use(&$b) { $b++; }); $ee->removeAllListeners(['foo', 'bar']); $ee->emit('foo'); $ee->emit('bar'); $ee->emit('qux'); $this->assertEquals(1, $a); $this->assertEquals(1, $b); }
<?php require '../vendor/autoload.php'; $listener = new EventEmitter(); /* $listener->on( 'hello', function( $data, $callback ){ print "Callback\n"; }); $listener->on( 'hello', function( $data, $callback ){ print "Callback 2\n"; }); $listener->on( 'hello', function( $data, $callback ){ print "Callback !!\n"; }); */ $remove_fn = function ($data, $callback) { print "Callback 3\n"; $callback(); }; $listener->on('hello', $remove_fn); $listener->emit('hello', 'second_arg', function () { print "emit_callback\n"; }); $listener->on('goodbye', function () { print "Goodbye world\n"; }); $listener->removeListener('hello', $remove_fn); $listener->emit('hello', 'second_arg', function () { print "emit_callback second time trhough\n"; }); $listener->emit('goodbye');
/** * @depends testCancelEvent */ function testPriorityOnce() { $argResult = 0; $ee = new EventEmitter(); $ee->once('foo', function ($arg) use(&$argResult) { $argResult = 1; return false; }); $ee->once('foo', function ($arg) use(&$argResult) { $argResult = 2; return false; }, 1); $this->assertFalse($ee->emit('foo', ['bar'])); $this->assertEquals(2, $argResult); }
public function testPriorityListeners() { $sum = 0; $emitter = new EventEmitter(); $emitter->on('add', function () use(&$sum) { $sum <<= 1; }, -999); // low priority $emitter->on('add', function ($a, $b) use(&$sum) { $sum = $a + $b; }); $this->assertTrue($emitter->emit('add', [100, 200])); $this->assertSame(600, $sum); }
public function testLoadPluginIfRegisteredAfterPluginBootEvent() { $emitter = new EventEmitter(); $manager = new PluginManager($this->mockInjector, $emitter); $emitter->emit(Engine::ENGINE_BOOTUP_EVENT); $plugin = new BootCalledPlugin(); $manager->registerPlugin($plugin); $this->assertTrue($plugin->wasCalled()); }