bug #18388 [EventDispatcher] check for method to exist (xabbuh)

This PR was merged into the 2.8 branch.

Discussion
----------

[EventDispatcher] check for method to exist

| Q             | A
| ------------- | ---
| Branch?       | 2.8
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | https://github.com/symfony/symfony/pull/16301#issuecomment-193150055
| License       | MIT
| Doc PR        |

This change must be reverted after being merged into the `3.0` branch (the `getListenerPriority()` method was added to the interface in Symfony 3.0).

Commits
-------

78ae2ad [EventDispatcher] check for method to exist
This commit is contained in:
Fabien Potencier 2016-05-03 20:59:18 +02:00
commit 27c122e0ca
2 changed files with 14 additions and 0 deletions

View File

@ -104,6 +104,10 @@ class TraceableEventDispatcher implements TraceableEventDispatcherInterface
*/
public function getListenerPriority($eventName, $listener)
{
if (!method_exists($this->dispatcher, 'getListenerPriority')) {
return 0;
}
return $this->dispatcher->getListenerPriority($eventName, $listener);
}

View File

@ -73,6 +73,16 @@ class TraceableEventDispatcherTest extends \PHPUnit_Framework_TestCase
$this->assertSame(123, $tdispatcher->getListenerPriority('foo', $listeners[0]));
}
public function testGetListenerPriorityReturnsZeroWhenWrappedMethodDoesNotExist()
{
$dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
$traceableEventDispatcher = new TraceableEventDispatcher($dispatcher, new Stopwatch());
$traceableEventDispatcher->addListener('foo', function () {}, 123);
$listeners = $traceableEventDispatcher->getListeners('foo');
$this->assertSame(0, $traceableEventDispatcher->getListenerPriority('foo', $listeners[0]));
}
public function testAddRemoveSubscriber()
{
$dispatcher = new EventDispatcher();