def testReEnteringAScope(self): event = Event(); service1 = Service(); scope = Scope('scope'); container = Container(); container.addScope(scope); container.enterScope('scope'); container.set('service.listener', service1, 'scope'); dispatcher = ContainerAwareEventDispatcher(container); dispatcher.addListenerService('onEvent', ['service.listener', 'onEvent']); dispatcher.dispatch('onEvent', event); service2 = Service(); container.enterScope('scope'); container.set('service.listener', service2, 'scope'); dispatcher.dispatch('onEvent', event); container.leaveScope('scope'); dispatcher.dispatch('onEvent');
def testRemoveAfterDispatch(self): event = Event(); service = Service(); container = Container(); container.set('service.listener', service); dispatcher = ContainerAwareEventDispatcher(container); dispatcher.addListenerService('onEvent', ['service.listener', 'onEvent']); dispatcher.dispatch('onEvent', Event()); dispatcher.removeListener('onEvent', [container.get('service.listener'), 'onEvent']); self.assertFalse(dispatcher.hasListeners('onEvent'));
def testPreventDuplicateListenerService(self): event = Event(); service = Service(); container = Container(); container.set('service.listener', service); dispatcher = ContainerAwareEventDispatcher(container); dispatcher.addListenerService('onEvent', ['service.listener', 'onEvent'], 5); dispatcher.addListenerService('onEvent', ['service.listener', 'onEvent'], 10); dispatcher.dispatch('onEvent', event);
def testHasListenersOnLazyLoad(self): event = Event(); service = Service(); container = Container(); container.set('service.listener', service); dispatcher = ContainerAwareEventDispatcher(container); dispatcher.addListenerService('onEvent', ['service.listener', 'onEvent']); event.setDispatcher(dispatcher); event.setName('onEvent'); self.assertTrue(dispatcher.hasListeners()); if (dispatcher.hasListeners('onEvent')) : dispatcher.dispatch('onEvent');
def testAddASubscriberService(self): event = Event(); service = SubscriberService(); container = Container(); container.set('service.subscriber', service); dispatcher = ContainerAwareEventDispatcher(container); dispatcher.addSubscriberService('service.subscriber', ReflectionClass(SubscriberService).getName()); dispatcher.dispatch('onEvent', event);
def testAddAListenerService(self): if self.__skip: return; event = Event(); service = Service(); container = Container(); container.set('service.listener', service); dispatcher = ContainerAwareEventDispatcher(container); dispatcher.addListenerService('onEvent', ['service.listener', 'onEvent']); dispatcher.dispatch('onEvent', event);
def testGetListenersOnLazyLoad(self): event = Event(); service = Service(); container = Container(); container.set('service.listener', service); dispatcher = ContainerAwareEventDispatcher(container); dispatcher.addListenerService('onEvent', ['service.listener', 'onEvent']); listeners = dispatcher.getListeners(); self.assertTrue('onEvent' in listeners); self.assertEqual(1, len(dispatcher.getListeners('onEvent')));
def testTriggerAListenerServiceOutOfScope(self): """@expectedException InvalidArgumentException """ try: service = Service(); scope = Scope('scope'); container = Container(); container.addScope(scope); container.enterScope('scope'); container.set('service.listener', service, 'scope'); dispatcher = ContainerAwareEventDispatcher(container); dispatcher.addListenerService('onEvent', ['service.listener', 'onEvent']); container.leaveScope('scope'); dispatcher.dispatch('onEvent'); self.fail() except Exception as e: self.assertTrue(isinstance(e, InvalidArgumentException));