Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EventManager::removeEventListener: fixed emptying list of sorted events #87

Merged
merged 1 commit into from
Nov 28, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does it? If it's empty then it should return an empty array, no?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see now :)

}

// 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