public function testListenerCanRemoveItselfWhenExecuted()
 {
     $eventDispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());
     $listener1 = function () use($eventDispatcher, &$listener1) {
         $eventDispatcher->removeListener('foo', $listener1);
     };
     $eventDispatcher->addListener('foo', $listener1);
     $eventDispatcher->addListener('foo', function () {
     });
     $eventDispatcher->dispatch('foo');
     $this->assertCount(1, $eventDispatcher->getListeners('foo'), 'expected listener1 to be removed');
 }
 public function testAddListenerNested()
 {
     $called1 = false;
     $called2 = false;
     $dispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());
     $dispatcher->addListener('my-event', function () use($dispatcher, &$called1, &$called2) {
         $called1 = true;
         $dispatcher->addListener('my-event', function () use(&$called2) {
             $called2 = true;
         });
     });
     $dispatcher->dispatch('my-event');
     $this->assertTrue($called1);
     $this->assertFalse($called2);
     $dispatcher->dispatch('my-event');
     $this->assertTrue($called2);
 }
 public function testDispatchNested()
 {
     $dispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());
     $loop = 1;
     $dispatcher->addListener('foo', $listener1 = function () use($dispatcher, &$loop) {
         ++$loop;
         if (2 == $loop) {
             $dispatcher->dispatch('foo');
         }
     });
     $dispatcher->dispatch('foo');
 }
 public function testDispatchReusedEventNested()
 {
     $nestedCall = false;
     $dispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());
     $dispatcher->addListener('foo', function (Event $e) use($dispatcher) {
         $dispatcher->dispatch('bar', $e);
     });
     $dispatcher->addListener('bar', function (Event $e) use(&$nestedCall) {
         $nestedCall = true;
     });
     $this->assertFalse($nestedCall);
     $dispatcher->dispatch('foo');
     $this->assertTrue($nestedCall);
 }
 public function testDispatchCallListeners()
 {
     $called = array();
     $dispatcher = new EventDispatcher();
     $tdispatcher = new TraceableEventDispatcher($dispatcher, new Stopwatch());
     $tdispatcher->addListener('foo', $listener1 = function () use(&$called) {
         $called[] = 'foo1';
     });
     $tdispatcher->addListener('foo', $listener2 = function () use(&$called) {
         $called[] = 'foo2';
     });
     $tdispatcher->dispatch('foo');
     $this->assertEquals(array('foo1', 'foo2'), $called);
 }