Пример #1
0
 public function testDispatchClosureListener()
 {
     $em = new EventManager();
     $listenerCalled = false;
     $listener = function (Event $event) use(&$listenerCalled) {
         $listenerCalled = true;
     };
     $em->addListener('event', $listener);
     $em->dispatch('event', new Event());
     $this->assertTrue($listenerCalled);
 }
Пример #2
0
 public function testStopPropagation()
 {
     $em = new EventManager();
     $instanceOne = new StopPropagationListener();
     $instanceTwo = new StopPropagationListener();
     $em->addListeners('stopPropagationEvent', array(array($instanceOne, 'listener'), array($instanceTwo, 'listener')));
     $event = new Event();
     $this->assertFalse($event->propagationHalted());
     $em->dispatch('stopPropagationEvent', $event);
     $this->assertTrue($instanceOne->listenerCalled);
     $this->assertFalse($instanceTwo->listenerCalled);
     $this->assertTrue($event->propagationHalted());
 }
Пример #3
0
 public function testDispatchMultipleClassesWithAnnotations()
 {
     $reader = new AnnotationDriver();
     $em = new EventManager($reader);
     $instanceOne = new AnnotationListener();
     $em->addClassListeners($instanceOne);
     $instanceTwo = new AnnotationListener();
     $em->addClassListeners($instanceTwo);
     $em->dispatch('eventOne', new Event());
     $this->assertTrue($instanceOne->listenerOneCalled);
     $this->assertTrue($instanceTwo->listenerOneCalled);
     $this->assertFalse($instanceOne->listenerTwoCalled);
     $this->assertFalse($instanceTwo->listenerTwoCalled);
 }
Пример #4
0
 /**
  * Registers listeners for the supplied service instance. Listeners are obtained from ServiceMetadata for
  * the class of the instance.
  *
  * @param object $service
  */
 protected function registerListeners($service)
 {
     $serviceMetadata = $this->serviceMetadataFactory->getMetadataFor(get_class($service));
     foreach ($serviceMetadata->getAllListeners() as $event => $listeners) {
         foreach ($listeners as $listener) {
             $this->eventManager->addListener($event, array($service, $listener));
         }
     }
 }
Пример #5
0
 public function testAddClassListenersWithNoReader()
 {
     $em = new EventManager();
     $this->setExpectedException('BedRest\\Events\\Exception');
     $em->addClassListeners(new \stdClass());
 }