Skip to content

Commit

Permalink
bug #22541 [EventDispatcher] fix: unwrap listeners for correct info (…
Browse files Browse the repository at this point in the history
…dmaicher)

This PR was merged into the 2.8 branch.

Discussion
----------

[EventDispatcher] fix: unwrap listeners for correct info

| Q             | A
| ------------- | ---
| Branch?       | 2.8
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | related to #22520
| License       | MIT
| Doc PR        | -

Fixes problem where listeners were displayed as `Symfony\Component\EventDispatcher\Debug\WrappedListener::__invoke() ` in the profiler. It now shows the correct callable.

Commits
-------

d8c5869 [EventDispatcher] fix: unwrap listeners for correct info
  • Loading branch information
fabpot committed Apr 26, 2017
2 parents 1bee0ad + d8c5869 commit e9e4c79
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
Expand Up @@ -306,6 +306,12 @@ private function getListenerInfo($listener, $eventName)
'event' => $eventName,
'priority' => $this->getListenerPriority($eventName, $listener),
);

// unwrap for correct listener info
if ($listener instanceof WrappedListener) {
$listener = $listener->getWrappedListener();
}

if ($listener instanceof \Closure) {
$info += array(
'type' => 'Closure',
Expand Down
Expand Up @@ -13,6 +13,7 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher;
use Symfony\Component\EventDispatcher\Debug\WrappedListener;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\EventDispatcher;
Expand Down Expand Up @@ -100,21 +101,41 @@ public function testAddRemoveSubscriber()
$this->assertCount(0, $dispatcher->getListeners('foo'));
}

public function testGetCalledListeners()
/**
* @dataProvider isWrappedDataProvider
*
* @param bool $isWrapped
*/
public function testGetCalledListeners($isWrapped)
{
$dispatcher = new EventDispatcher();
$tdispatcher = new TraceableEventDispatcher($dispatcher, new Stopwatch());
$tdispatcher->addListener('foo', $listener = function () {});
$stopWatch = new Stopwatch();
$tdispatcher = new TraceableEventDispatcher($dispatcher, $stopWatch);

$listener = function () {};
if ($isWrapped) {
$listener = new WrappedListener($listener, 'foo', $stopWatch, $dispatcher);
}

$tdispatcher->addListener('foo', $listener, 5);

$this->assertEquals(array(), $tdispatcher->getCalledListeners());
$this->assertEquals(array('foo.closure' => array('event' => 'foo', 'type' => 'Closure', 'pretty' => 'closure', 'priority' => 0)), $tdispatcher->getNotCalledListeners());
$this->assertEquals(array('foo.closure' => array('event' => 'foo', 'type' => 'Closure', 'pretty' => 'closure', 'priority' => 5)), $tdispatcher->getNotCalledListeners());

$tdispatcher->dispatch('foo');

$this->assertEquals(array('foo.closure' => array('event' => 'foo', 'type' => 'Closure', 'pretty' => 'closure', 'priority' => null)), $tdispatcher->getCalledListeners());
$this->assertEquals(array('foo.closure' => array('event' => 'foo', 'type' => 'Closure', 'pretty' => 'closure', 'priority' => 5)), $tdispatcher->getCalledListeners());
$this->assertEquals(array(), $tdispatcher->getNotCalledListeners());
}

public function isWrappedDataProvider()
{
return array(
array(false),
array(true),
);
}

public function testGetCalledListenersNested()
{
$tdispatcher = null;
Expand Down

0 comments on commit e9e4c79

Please sign in to comment.