Skip to content

Commit

Permalink
use case sensitive event names
Browse files Browse the repository at this point in the history
  • Loading branch information
goetas committed Apr 2, 2018
1 parent 12b9241 commit d0cf4cb
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 25 deletions.
20 changes: 9 additions & 11 deletions src/EventDispatcher/EventDispatcher.php
Expand Up @@ -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]);
}

Expand All @@ -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']]);
Expand All @@ -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)
Expand All @@ -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);
}
}

Expand Down
23 changes: 13 additions & 10 deletions tests/Serializer/EventDispatcher/EventDispatcherTest.php
Expand Up @@ -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
{
Expand Down Expand Up @@ -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.');

Expand All @@ -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');
Expand Down Expand Up @@ -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);
});
Expand All @@ -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');
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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);
}
}
8 changes: 4 additions & 4 deletions tests/Serializer/EventDispatcher/LazyEventDispatcherTest.php
Expand Up @@ -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');
Expand Down

0 comments on commit d0cf4cb

Please sign in to comment.