diff --git a/src/EventDispatcher/EventDispatcher.php b/src/EventDispatcher/EventDispatcher.php index c4865688a..9f1f77c1a 100644 --- a/src/EventDispatcher/EventDispatcher.php +++ b/src/EventDispatcher/EventDispatcher.php @@ -51,7 +51,7 @@ public function setListeners(array $listeners) public function addListener($eventName, $callable, $class = null, $format = null) { - $this->listeners[$eventName][] = array($callable, null === $class ? null : strtolower($class), $format); + $this->listeners[$eventName][] = array($callable, null === $class ? null : $class, $format); unset($this->classListeners[$eventName]); } @@ -63,7 +63,7 @@ public function addSubscriber(EventSubscriberInterface $subscriber) } $method = isset($eventData['method']) ? $eventData['method'] : self::getDefaultMethodName($eventData['event']); - $class = isset($eventData['class']) ? strtolower($eventData['class']) : null; + $class = isset($eventData['class']) ? $eventData['class'] : null; $format = isset($eventData['format']) ? $eventData['format'] : null; $this->listeners[$eventData['event']][] = array(array($subscriber, $method), $class, $format); unset($this->classListeners[$eventData['event']]); @@ -76,12 +76,11 @@ public function hasListeners($eventName, $class, $format) return false; } - $loweredClass = strtolower($class); - if (!isset($this->classListeners[$eventName][$loweredClass][$format])) { - $this->classListeners[$eventName][$loweredClass][$format] = $this->initializeListeners($eventName, $loweredClass, $format); + if (!isset($this->classListeners[$eventName][$class][$format])) { + $this->classListeners[$eventName][$class][$format] = $this->initializeListeners($eventName, $class, $format); } - return !!$this->classListeners[$eventName][$loweredClass][$format]; + return !!$this->classListeners[$eventName][$class][$format]; } public function dispatch($eventName, $class, $format, Event $event) @@ -90,18 +89,17 @@ public function dispatch($eventName, $class, $format, Event $event) return; } - $loweredClass = strtolower($class); - if (!isset($this->classListeners[$eventName][$loweredClass][$format])) { - $this->classListeners[$eventName][$loweredClass][$format] = $this->initializeListeners($eventName, $loweredClass, $format); + if (!isset($this->classListeners[$eventName][$class][$format])) { + $this->classListeners[$eventName][$class][$format] = $this->initializeListeners($eventName, $class, $format); } - foreach ($this->classListeners[$eventName][$loweredClass][$format] as $listener) { + foreach ($this->classListeners[$eventName][$class][$format] as $listener) { if ($event->isPropagationStopped()) { break; } - \call_user_func($listener, $event, $eventName, $loweredClass, $format, $this); + \call_user_func($listener, $event, $eventName, $class, $format, $this); } } diff --git a/tests/Serializer/EventDispatcher/EventDispatcherTest.php b/tests/Serializer/EventDispatcher/EventDispatcherTest.php index ac8b0866e..b7cff25b6 100644 --- a/tests/Serializer/EventDispatcher/EventDispatcherTest.php +++ b/tests/Serializer/EventDispatcher/EventDispatcherTest.php @@ -18,11 +18,13 @@ namespace JMS\Serializer\Tests\Serializer\EventDispatcher; +use JMS\Serializer\Context; use JMS\Serializer\EventDispatcher\Event; use JMS\Serializer\EventDispatcher\EventDispatcher; use JMS\Serializer\EventDispatcher\EventDispatcherInterface; use JMS\Serializer\EventDispatcher\EventSubscriberInterface; use JMS\Serializer\EventDispatcher\ObjectEvent; +use PHPUnit\Framework\Assert; class EventDispatcherTest extends \PHPUnit_Framework_TestCase { @@ -55,13 +57,13 @@ public function testHasListeners() $this->dispatcher->addListener('baz', function () { }, 'Bar'); $this->assertTrue($this->dispatcher->hasListeners('baz', 'Bar', 'xml')); - $this->assertTrue($this->dispatcher->hasListeners('baz', 'bAr', 'xml')); + //$this->assertTrue($this->dispatcher->hasListeners('baz', 'bAr', 'xml')); } public function testDispatch() { $a = new MockListener(); - $this->dispatcher->addListener('foo', array($a, 'foo')); + $this->dispatcher->addListener('foo', array($a, 'Foo')); $this->dispatch('bar'); $a->_verify('Listener is not called for other event.'); @@ -70,10 +72,11 @@ public function testDispatch() $this->dispatcher->addListener('pre', array($b, 'foo'), 'Foo'); $this->dispatcher->addListener('pre', array($b, 'all')); - $b->bar($this->event, 'pre', 'bar', 'json', $this->dispatcher); - $b->all($this->event, 'pre', 'bar', 'json', $this->dispatcher); - $b->foo($this->event, 'pre', 'foo', 'json', $this->dispatcher); - $b->all($this->event, 'pre', 'foo', 'json', $this->dispatcher); + $b->bar($this->event, 'pre', 'Bar', 'json', $this->dispatcher); + $b->all($this->event, 'pre', 'Bar', 'json', $this->dispatcher); + $b->foo($this->event, 'pre', 'Foo', 'json', $this->dispatcher); + $b->all($this->event, 'pre', 'Foo', 'json', $this->dispatcher); + $b->_replay(); $this->dispatch('pre', 'Bar'); $this->dispatch('pre', 'Foo'); @@ -113,7 +116,7 @@ public function testListenerCanDispatchEvent() $this->assertSame('pre', $eventName); $this->assertSame('json', $format); - $this->assertSame('foo', $loweredClass); + $this->assertSame('Foo', $loweredClass); $dispatcher->dispatch('post', 'Blah', 'xml', $event); }); @@ -127,7 +130,7 @@ public function testListenerCanDispatchEvent() $this->assertSame('post', $eventName); $this->assertSame('xml', $format); - $this->assertSame('blah', $loweredClass); + $this->assertSame('Blah', $loweredClass); }); $this->dispatch('pre'); @@ -159,7 +162,7 @@ public function testAddSubscriber() protected function setUp() { $this->dispatcher = $this->createEventDispatcher(); - $this->event = new ObjectEvent($this->getMockBuilder('JMS\Serializer\Context')->getMock(), new \stdClass(), array('name' => 'foo', 'params' => array())); + $this->event = new ObjectEvent($this->getMockBuilder(Context::class)->getMock(), new \stdClass(), array('name' => 'foo', 'params' => array())); } protected function createEventDispatcher() @@ -207,6 +210,6 @@ public function _replay() public function _verify($message = null) { - \PHPUnit_Framework_Assert::assertSame($this->expected, $this->actual, $message); + Assert::assertSame($this->expected, $this->actual, $message); } } diff --git a/tests/Serializer/EventDispatcher/LazyEventDispatcherTest.php b/tests/Serializer/EventDispatcher/LazyEventDispatcherTest.php index ca392596a..aeeb620fa 100644 --- a/tests/Serializer/EventDispatcher/LazyEventDispatcherTest.php +++ b/tests/Serializer/EventDispatcher/LazyEventDispatcherTest.php @@ -57,10 +57,10 @@ public function testDispatchWithListenerAsService() $this->dispatcher->addListener('pre', array('b', 'foo'), 'Foo'); $this->dispatcher->addListener('pre', array('b', 'all')); - $b->bar($this->event, 'pre', 'bar', 'json', $this->dispatcher); - $b->all($this->event, 'pre', 'bar', 'json', $this->dispatcher); - $b->foo($this->event, 'pre', 'foo', 'json', $this->dispatcher); - $b->all($this->event, 'pre', 'foo', 'json', $this->dispatcher); + $b->bar($this->event, 'pre', 'Bar', 'json', $this->dispatcher); + $b->all($this->event, 'pre', 'Bar', 'json', $this->dispatcher); + $b->foo($this->event, 'pre', 'Foo', 'json', $this->dispatcher); + $b->all($this->event, 'pre', 'Foo', 'json', $this->dispatcher); $b->_replay(); $this->dispatch('pre', 'Bar'); $this->dispatch('pre', 'Foo');