From 5472039e48e7f98a927fadac4d3249ac5f6f0a1e Mon Sep 17 00:00:00 2001 From: Jeremy Mikola Date: Fri, 9 Sep 2016 15:27:43 -0400 Subject: [PATCH] Throw exceptions for getListenerPriority() incompatibilities For the time being, do not support wildcard patterns with getListenerPriority(). An exception will be thrown if getListenerPriority() is not implemented on the inner dispatcher. The method was added in https://github.com/symfony/symfony/pull/16198 for Symfony 2.8. It was added to the interface in Symfony 3.0, which is relevant for our test cases. --- .../WildcardEventDispatcher.php | 10 ++++++ .../WildcardEventDispatcherTest.php | 34 +++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/src/Jmikola/WildcardEventDispatcher/WildcardEventDispatcher.php b/src/Jmikola/WildcardEventDispatcher/WildcardEventDispatcher.php index b32ffb6..9c525be 100644 --- a/src/Jmikola/WildcardEventDispatcher/WildcardEventDispatcher.php +++ b/src/Jmikola/WildcardEventDispatcher/WildcardEventDispatcher.php @@ -198,9 +198,19 @@ class WildcardEventDispatcher implements EventDispatcherInterface /** * @see EventDispatcherInterface::getListenerPriority() + * @throws \InvalidArgumentException if $eventName contains a wildcard pattern + * @throws \BadMethodCallException if this method is not implemented on the composed EventDispatcher */ public function getListenerPriority($eventName, $listener) { + if ($this->hasWildcards($eventName)) { + throw new \InvalidArgumentException('Wildcard patterns are not supported'); + } + + if ( ! method_exists($this->dispatcher, 'getListenerPriority')) { + throw new \BadMethodCallException('getListenerPriority() is not implemented'); + } + return $this->dispatcher->getListenerPriority($eventName, $listener); } } diff --git a/tests/Jmikola/Tests/WildcardEventDispatcher/WildcardEventDispatcherTest.php b/tests/Jmikola/Tests/WildcardEventDispatcher/WildcardEventDispatcherTest.php index c8076e6..20353b4 100644 --- a/tests/Jmikola/Tests/WildcardEventDispatcher/WildcardEventDispatcherTest.php +++ b/tests/Jmikola/Tests/WildcardEventDispatcher/WildcardEventDispatcherTest.php @@ -146,6 +146,40 @@ class EventDispatcherTest extends \PHPUnit_Framework_TestCase $this->dispatcher->removeSubscriber($subscriber); } + public function testGetListenerPriorityInvokesMethodOnInnerDispather() + { + if ( ! method_exists('Symfony\Component\EventDispatcher\EventDispatcherInterface', 'getListenerPriority')) { + $this->markTestSkipped('getListenerPriority() does not exist on EventDispatcherInterface'); + } + + $this->innerDispatcher->expects($this->once()) + ->method('getListenerPriority') + ->with('core.request', 'callback') + ->will($this->returnValue(1)); + + $this->assertSame(1, $this->dispatcher->getListenerPriority('core.request', 'callback')); + } + + /** + * @expectedException InvalidArgumentException + */ + public function testGetListenerPriorityRequiresEventNameWithoutWildcards() + { + $this->dispatcher->getListenerPriority('core.*', 'callback'); + } + + /** + * @expectedException BadMethodCallException + */ + public function testGetListenerPriorityRequiresMethodOnInnerDispather() + { + if (method_exists('Symfony\Component\EventDispatcher\EventDispatcherInterface', 'getListenerPriority')) { + $this->markTestSkipped('getListenerPriority() exists on EventDispatcherInterface'); + } + + $this->dispatcher->getListenerPriority('core.request', 'callback'); + } + private function getMockEventDispatcher() { return $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')->getMock();