示例#1
0
 /**
  * "Attaching event listeners by event name fails if preceded by
  * detachment of all listeners for that type."
  *
  * Test contains 4 steps:
  * - assigning event manager to dummy service with single log event
  *   listener attached
  * - attaching second log listener
  * - detaching all log listeners
  * - attaching different listener
  *
  * NOTE: This test looks the same as above but it checks detachAll()
  * instead of detachAll() method. To be DELETED when detachAll()
  * will not supported any more.
  *
  * @see https://github.com/phalcon/cphalcon/issues/1331
  */
 public function testBug1331BackwardCompatibility()
 {
     $di = new Phalcon\Di();
     $di->set('componentX', function () use($di) {
         $component = new LeDummyComponent();
         $eventsManager = new Phalcon\Events\Manager();
         $eventsManager->attach('log', $di->get('MyFirstWeakrefListener'));
         $component->setEventsManager($eventsManager);
         return $component;
     });
     $di->set('firstListener', 'MyFirstWeakrefListener');
     $di->set('secondListener', 'MySecondWeakrefListener');
     // ----- TESTING STEP 1 - SIGNLE 'LOG' LISTENER ATTACHED
     $component = $di->get('componentX');
     $logListeners = $component->getEventsManager()->getListeners('log');
     $this->assertCount(1, $logListeners);
     $this->assertInstanceOf('MyFirstWeakrefListener', $logListeners[0]);
     // ----- TESTING STEP 2 - SECOND 'LOG' LISTENER ATTACHED
     $component->getEventsManager()->attach('log', $di->get('MySecondWeakrefListener'));
     $logListeners = $component->getEventsManager()->getListeners('log');
     $this->assertCount(2, $logListeners);
     $firstLister = array_shift($logListeners);
     $secondLister = array_shift($logListeners);
     $this->assertInstanceOf('MyFirstWeakrefListener', $firstLister);
     $this->assertInstanceOf('MySecondWeakrefListener', $secondLister);
     // ----- TESTING STEP 3 - ALL 'LOG' LISTENER DETACHED
     $component->getEventsManager()->detachAll('log');
     $logListeners = $component->getEventsManager()->getListeners('log');
     $this->assertEmpty($logListeners);
     // ----- TESTING STEP 4 - SINGLE 'LOG' LISTENER ATTACHED SECOND TIME
     $component->getEventsManager()->attach('log', $di->get('MySecondWeakrefListener'));
     $logListeners = $component->getEventsManager()->getListeners('log');
     $this->assertCount(1, $logListeners);
     $this->assertInstanceOf('MySecondWeakrefListener', $logListeners[0]);
 }