Skip to content

Commit

Permalink
Merge pull request #87 from matej21/fix/removing_listeners
Browse files Browse the repository at this point in the history
EventManager::removeEventListener: fixed emptying list of sorted events
  • Loading branch information
fprochazka committed Nov 28, 2015
2 parents 37a96e5 + 43b4279 commit ab9fcb1
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/Kdyby/Events/EventManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,13 @@ public function removeEventListener($unsubscribe, $subscriber = NULL)
}
if (empty($this->listeners[$eventName])) {
unset($this->listeners[$eventName]);
// there are no listeners for this specific event, so no reason to call sort on next dispatch
$this->sorted[$eventName] = array();
} else {
// otherwise it needs to be sorted again
unset($this->sorted[$eventName]);
}

// there are no listeners for this specific event, so no reason to call sort on next dispatch
$this->sorted[$eventName] = array();
}
}
}
Expand Down
19 changes: 19 additions & 0 deletions tests/KdybyTests/Events/EventManager.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,25 @@ class EventManagerTest extends Tester\TestCase



public function testRemovingSomeListeners()
{
$listener = new EventListenerMock();
$this->manager->addEventListener('onFoo', $listener);
$this->manager->addEventListener('onBar', $listener);
$listener2 = new EventListenerMock2();
$this->manager->addEventListener('onFoo', $listener2);
$this->manager->addEventListener('onBar', $listener2);
Assert::count(2, $this->manager->getListeners('onFoo'));
Assert::count(2, $this->manager->getListeners('onBar'));

$this->manager->removeEventListener($listener);
$this->manager->removeEventListener('onFoo', $listener2);
Assert::count(0, $this->manager->getListeners('onFoo'));
Assert::count(1, $this->manager->getListeners('onBar'));
Assert::same(array('onBar' => array($listener2)), $this->manager->getListeners());
}


public function testListenerDontHaveRequiredMethodException()
{
$evm = $this->manager;
Expand Down
31 changes: 31 additions & 0 deletions tests/KdybyTests/Events/mocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,37 @@ public function onBar(EventArgsMock $args)



/**
* @author Filip Procházka <[email protected]>
*/
class EventListenerMock2 extends Nette\Object implements Kdyby\Events\Subscriber
{

/**
* @return array
*/
public function getSubscribedEvents()
{
return array(
'onFoo',
'onBar'
);
}


public function onFoo()
{
}


public function onBar()
{
}

}



/**
* @author Filip Procházka <[email protected]>
*/
Expand Down

0 comments on commit ab9fcb1

Please sign in to comment.