updated eventdispatcher

This commit is contained in:
kremsy 2015-01-19 11:11:45 +01:00
parent 0ca7d2dd15
commit f97109a75b
18 changed files with 123 additions and 426 deletions

View File

@ -1,3 +0,0 @@
vendor/
composer.lock
phpunit.xml

View File

@ -1,6 +1,13 @@
CHANGELOG CHANGELOG
========= =========
2.5.0
-----
* added Debug\TraceableEventDispatcher (originally in HttpKernel)
* changed Debug\TraceableEventDispatcherInterface to extend EventDispatcherInterface
* added RegisterListenersPass (originally in HttpKernel)
2.1.0 2.1.0
----- -----

View File

@ -15,7 +15,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
/** /**
* Lazily loads listeners and subscribers from the dependency injection * Lazily loads listeners and subscribers from the dependency injection
* container * container.
* *
* @author Fabien Potencier <fabien@symfony.com> * @author Fabien Potencier <fabien@symfony.com>
* @author Bernhard Schussek <bschussek@gmail.com> * @author Bernhard Schussek <bschussek@gmail.com>
@ -24,19 +24,22 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
class ContainerAwareEventDispatcher extends EventDispatcher class ContainerAwareEventDispatcher extends EventDispatcher
{ {
/** /**
* The container from where services are loaded * The container from where services are loaded.
*
* @var ContainerInterface * @var ContainerInterface
*/ */
private $container; private $container;
/** /**
* The service IDs of the event listeners and subscribers * The service IDs of the event listeners and subscribers.
*
* @var array * @var array
*/ */
private $listenerIds = array(); private $listenerIds = array();
/** /**
* The services registered as listeners * The services registered as listeners.
*
* @var array * @var array
*/ */
private $listeners = array(); private $listeners = array();
@ -52,14 +55,14 @@ class ContainerAwareEventDispatcher extends EventDispatcher
} }
/** /**
* Adds a service as event listener * Adds a service as event listener.
* *
* @param string $eventName Event for which the listener is added * @param string $eventName Event for which the listener is added
* @param array $callback The service ID of the listener service & the method * @param array $callback The service ID of the listener service & the method
* name that has to be called * name that has to be called
* @param integer $priority The higher this value, the earlier an event listener * @param int $priority The higher this value, the earlier an event listener
* will be triggered in the chain. * will be triggered in the chain.
* Defaults to 0. * Defaults to 0.
* *
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
*/ */
@ -76,21 +79,18 @@ class ContainerAwareEventDispatcher extends EventDispatcher
{ {
$this->lazyLoad($eventName); $this->lazyLoad($eventName);
if (isset($this->listeners[$eventName])) { if (isset($this->listenerIds[$eventName])) {
foreach ($this->listeners[$eventName] as $key => $l) { foreach ($this->listenerIds[$eventName] as $i => $args) {
foreach ($this->listenerIds[$eventName] as $i => $args) { list($serviceId, $method, $priority) = $args;
list($serviceId, $method, $priority) = $args; $key = $serviceId.'.'.$method;
if ($key === $serviceId.'.'.$method) { if (isset($this->listeners[$eventName][$key]) && $listener === array($this->listeners[$eventName][$key], $method)) {
if ($listener === array($l, $method)) { unset($this->listeners[$eventName][$key]);
unset($this->listeners[$eventName][$key]); if (empty($this->listeners[$eventName])) {
if (empty($this->listeners[$eventName])) { unset($this->listeners[$eventName]);
unset($this->listeners[$eventName]); }
} unset($this->listenerIds[$eventName][$i]);
unset($this->listenerIds[$eventName][$i]); if (empty($this->listenerIds[$eventName])) {
if (empty($this->listenerIds[$eventName])) { unset($this->listenerIds[$eventName]);
unset($this->listenerIds[$eventName]);
}
}
} }
} }
} }
@ -100,12 +100,12 @@ class ContainerAwareEventDispatcher extends EventDispatcher
} }
/** /**
* @see EventDispatcherInterface::hasListeners * @see EventDispatcherInterface::hasListeners()
*/ */
public function hasListeners($eventName = null) public function hasListeners($eventName = null)
{ {
if (null === $eventName) { if (null === $eventName) {
return (Boolean) count($this->listenerIds) || (Boolean) count($this->listeners); return (bool) count($this->listenerIds) || (bool) count($this->listeners);
} }
if (isset($this->listenerIds[$eventName])) { if (isset($this->listenerIds[$eventName])) {
@ -116,7 +116,7 @@ class ContainerAwareEventDispatcher extends EventDispatcher
} }
/** /**
* @see EventDispatcherInterface::getListeners * @see EventDispatcherInterface::getListeners()
*/ */
public function getListeners($eventName = null) public function getListeners($eventName = null)
{ {
@ -132,7 +132,7 @@ class ContainerAwareEventDispatcher extends EventDispatcher
} }
/** /**
* Adds a service as event subscriber * Adds a service as event subscriber.
* *
* @param string $serviceId The service ID of the subscriber service * @param string $serviceId The service ID of the subscriber service
* @param string $class The service's class name (which must implement EventSubscriberInterface) * @param string $class The service's class name (which must implement EventSubscriberInterface)
@ -153,7 +153,7 @@ class ContainerAwareEventDispatcher extends EventDispatcher
} }
/** /**
* {@inheritDoc} * {@inheritdoc}
* *
* Lazily loads listeners for this event from the dependency injection * Lazily loads listeners for this event from the dependency injection
* container. * container.

View File

@ -11,10 +11,12 @@
namespace Symfony\Component\EventDispatcher\Debug; namespace Symfony\Component\EventDispatcher\Debug;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
/** /**
* @author Fabien Potencier <fabien@symfony.com> * @author Fabien Potencier <fabien@symfony.com>
*/ */
interface TraceableEventDispatcherInterface interface TraceableEventDispatcherInterface extends EventDispatcherInterface
{ {
/** /**
* Gets the called listeners. * Gets the called listeners.

View File

@ -20,17 +20,17 @@ namespace Symfony\Component\EventDispatcher;
* You can call the method stopPropagation() to abort the execution of * You can call the method stopPropagation() to abort the execution of
* further listeners in your event listener. * further listeners in your event listener.
* *
* @author Guilherme Blanco <guilhermeblanco@hotmail.com> * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
* @author Jonathan Wage <jonwage@gmail.com> * @author Jonathan Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org> * @author Roman Borschel <roman@code-factory.org>
* @author Bernhard Schussek <bschussek@gmail.com> * @author Bernhard Schussek <bschussek@gmail.com>
* *
* @api * @api
*/ */
class Event class Event
{ {
/** /**
* @var Boolean Whether no further event listeners should be triggered * @var bool Whether no further event listeners should be triggered
*/ */
private $propagationStopped = false; private $propagationStopped = false;
@ -47,8 +47,9 @@ class Event
/** /**
* Returns whether further event listeners should be triggered. * Returns whether further event listeners should be triggered.
* *
* @see Event::stopPropagation * @see Event::stopPropagation()
* @return Boolean Whether propagation was already stopped for this event. *
* @return bool Whether propagation was already stopped for this event.
* *
* @api * @api
*/ */
@ -72,7 +73,7 @@ class Event
} }
/** /**
* Stores the EventDispatcher that dispatches this Event * Stores the EventDispatcher that dispatches this Event.
* *
* @param EventDispatcherInterface $dispatcher * @param EventDispatcherInterface $dispatcher
* *
@ -86,7 +87,7 @@ class Event
} }
/** /**
* Returns the EventDispatcher that dispatches this Event * Returns the EventDispatcher that dispatches this Event.
* *
* @return EventDispatcherInterface * @return EventDispatcherInterface
* *

View File

@ -17,13 +17,13 @@ namespace Symfony\Component\EventDispatcher;
* Listeners are registered on the manager and events are dispatched through the * Listeners are registered on the manager and events are dispatched through the
* manager. * manager.
* *
* @author Guilherme Blanco <guilhermeblanco@hotmail.com> * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
* @author Jonathan Wage <jonwage@gmail.com> * @author Jonathan Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org> * @author Roman Borschel <roman@code-factory.org>
* @author Bernhard Schussek <bschussek@gmail.com> * @author Bernhard Schussek <bschussek@gmail.com>
* @author Fabien Potencier <fabien@symfony.com> * @author Fabien Potencier <fabien@symfony.com>
* @author Jordi Boggiano <j.boggiano@seld.be> * @author Jordi Boggiano <j.boggiano@seld.be>
* @author Jordan Alliot <jordan.alliot@gmail.com> * @author Jordan Alliot <jordan.alliot@gmail.com>
* *
* @api * @api
*/ */
@ -33,7 +33,7 @@ class EventDispatcher implements EventDispatcherInterface
private $sorted = array(); private $sorted = array();
/** /**
* @see EventDispatcherInterface::dispatch * @see EventDispatcherInterface::dispatch()
* *
* @api * @api
*/ */
@ -56,7 +56,7 @@ class EventDispatcher implements EventDispatcherInterface
} }
/** /**
* @see EventDispatcherInterface::getListeners * @see EventDispatcherInterface::getListeners()
*/ */
public function getListeners($eventName = null) public function getListeners($eventName = null)
{ {
@ -74,19 +74,19 @@ class EventDispatcher implements EventDispatcherInterface
} }
} }
return $this->sorted; return array_filter($this->sorted);
} }
/** /**
* @see EventDispatcherInterface::hasListeners * @see EventDispatcherInterface::hasListeners()
*/ */
public function hasListeners($eventName = null) public function hasListeners($eventName = null)
{ {
return (Boolean) count($this->getListeners($eventName)); return (bool) count($this->getListeners($eventName));
} }
/** /**
* @see EventDispatcherInterface::addListener * @see EventDispatcherInterface::addListener()
* *
* @api * @api
*/ */
@ -97,7 +97,7 @@ class EventDispatcher implements EventDispatcherInterface
} }
/** /**
* @see EventDispatcherInterface::removeListener * @see EventDispatcherInterface::removeListener()
*/ */
public function removeListener($eventName, $listener) public function removeListener($eventName, $listener)
{ {
@ -113,7 +113,7 @@ class EventDispatcher implements EventDispatcherInterface
} }
/** /**
* @see EventDispatcherInterface::addSubscriber * @see EventDispatcherInterface::addSubscriber()
* *
* @api * @api
*/ */
@ -133,7 +133,7 @@ class EventDispatcher implements EventDispatcherInterface
} }
/** /**
* @see EventDispatcherInterface::removeSubscriber * @see EventDispatcherInterface::removeSubscriber()
*/ */
public function removeSubscriber(EventSubscriberInterface $subscriber) public function removeSubscriber(EventSubscriberInterface $subscriber)
{ {

View File

@ -28,7 +28,7 @@ interface EventDispatcherInterface
* @param string $eventName The name of the event to dispatch. The name of * @param string $eventName The name of the event to dispatch. The name of
* the event is the name of the method that is * the event is the name of the method that is
* invoked on listeners. * invoked on listeners.
* @param Event $event The event to pass to the event handlers/listeners. * @param Event $event The event to pass to the event handlers/listeners.
* If not supplied, an empty Event instance is created. * If not supplied, an empty Event instance is created.
* *
* @return Event * @return Event
@ -42,7 +42,7 @@ interface EventDispatcherInterface
* *
* @param string $eventName The event to listen on * @param string $eventName The event to listen on
* @param callable $listener The listener * @param callable $listener The listener
* @param integer $priority The higher this value, the earlier an event * @param int $priority The higher this value, the earlier an event
* listener will be triggered in the chain (defaults to 0) * listener will be triggered in the chain (defaults to 0)
* *
* @api * @api
@ -64,8 +64,8 @@ interface EventDispatcherInterface
/** /**
* Removes an event listener from the specified events. * Removes an event listener from the specified events.
* *
* @param string|array $eventName The event(s) to remove a listener from * @param string $eventName The event to remove a listener from
* @param callable $listener The listener to remove * @param callable $listener The listener to remove
*/ */
public function removeListener($eventName, $listener); public function removeListener($eventName, $listener);
@ -90,7 +90,7 @@ interface EventDispatcherInterface
* *
* @param string $eventName The name of the event * @param string $eventName The name of the event
* *
* @return Boolean true if the specified event has any listeners, false otherwise * @return bool true if the specified event has any listeners, false otherwise
*/ */
public function hasListeners($eventName = null); public function hasListeners($eventName = null);
} }

View File

@ -17,10 +17,10 @@ namespace Symfony\Component\EventDispatcher;
* {@link getSubscribedEvents} and registers the subscriber as a listener for all * {@link getSubscribedEvents} and registers the subscriber as a listener for all
* returned events. * returned events.
* *
* @author Guilherme Blanco <guilhermeblanco@hotmail.com> * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
* @author Jonathan Wage <jonwage@gmail.com> * @author Jonathan Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org> * @author Roman Borschel <roman@code-factory.org>
* @author Bernhard Schussek <bschussek@gmail.com> * @author Bernhard Schussek <bschussek@gmail.com>
* *
* @api * @api
*/ */

View File

@ -118,7 +118,7 @@ class GenericEvent extends Event implements \ArrayAccess, \IteratorAggregate
* *
* @param string $key Key of arguments array. * @param string $key Key of arguments array.
* *
* @return boolean * @return bool
*/ */
public function hasArgument($key) public function hasArgument($key)
{ {
@ -167,7 +167,7 @@ class GenericEvent extends Event implements \ArrayAccess, \IteratorAggregate
* *
* @param string $key Array key. * @param string $key Array key.
* *
* @return boolean * @return bool
*/ */
public function offsetExists($key) public function offsetExists($key)
{ {
@ -175,7 +175,7 @@ class GenericEvent extends Event implements \ArrayAccess, \IteratorAggregate
} }
/** /**
* IteratorAggregate for iterating over the object like an array * IteratorAggregate for iterating over the object like an array.
* *
* @return \ArrayIterator * @return \ArrayIterator
*/ */

View File

@ -20,6 +20,7 @@ class ImmutableEventDispatcher implements EventDispatcherInterface
{ {
/** /**
* The proxied dispatcher. * The proxied dispatcher.
*
* @var EventDispatcherInterface * @var EventDispatcherInterface
*/ */
private $dispatcher; private $dispatcher;

View File

@ -1,4 +1,4 @@
Copyright (c) 2004-2014 Fabien Potencier Copyright (c) 2004-2015 Fabien Potencier
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal

View File

@ -1,19 +1,21 @@
EventDispatcher Component EventDispatcher Component
========================= =========================
The Symfony2 EventDispatcher component implements the Mediator pattern in a The Symfony EventDispatcher component implements the Mediator pattern in a
simple and effective way to make your projects truly extensible. simple and effective way to make your projects truly extensible.
use Symfony\Component\EventDispatcher\EventDispatcher; ```php
use Symfony\Component\EventDispatcher\Event; use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\Event;
$dispatcher = new EventDispatcher(); $dispatcher = new EventDispatcher();
$dispatcher->addListener('event_name', function (Event $event) { $dispatcher->addListener('event_name', function (Event $event) {
// ... // ...
}); });
$dispatcher->dispatch('event_name'); $dispatcher->dispatch('event_name');
```
Resources Resources
--------- ---------

View File

@ -17,8 +17,15 @@ use Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher;
use Symfony\Component\EventDispatcher\Event; use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ContainerAwareEventDispatcherTest extends \PHPUnit_Framework_TestCase class ContainerAwareEventDispatcherTest extends AbstractEventDispatcherTest
{ {
protected function createEventDispatcher()
{
$container = new Container();
return new ContainerAwareEventDispatcher($container);
}
public function testAddAListenerService() public function testAddAListenerService()
{ {
$event = new Event(); $event = new Event();
@ -232,8 +239,6 @@ class SubscriberService implements EventSubscriberInterface
public static function getSubscribedEvents() public static function getSubscribedEvents()
{ {
return array( return array(
'onEvent' => 'onEvent',
'onEvent' => array('onEvent', 10),
'onEvent' => array('onEvent'), 'onEvent' => array('onEvent'),
); );
} }

View File

@ -11,336 +11,12 @@
namespace Symfony\Component\EventDispatcher\Tests; namespace Symfony\Component\EventDispatcher\Tests;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class EventDispatcherTest extends \PHPUnit_Framework_TestCase class EventDispatcherTest extends AbstractEventDispatcherTest
{ {
/* Some pseudo events */ protected function createEventDispatcher()
const preFoo = 'pre.foo';
const postFoo = 'post.foo';
const preBar = 'pre.bar';
const postBar = 'post.bar';
/**
* @var EventDispatcher
*/
private $dispatcher;
private $listener;
protected function setUp()
{ {
$this->dispatcher = new EventDispatcher(); return new EventDispatcher();
$this->listener = new TestEventListener();
}
protected function tearDown()
{
$this->dispatcher = null;
$this->listener = null;
}
public function testInitialState()
{
$this->assertEquals(array(), $this->dispatcher->getListeners());
$this->assertFalse($this->dispatcher->hasListeners(self::preFoo));
$this->assertFalse($this->dispatcher->hasListeners(self::postFoo));
}
public function testAddListener()
{
$this->dispatcher->addListener('pre.foo', array($this->listener, 'preFoo'));
$this->dispatcher->addListener('post.foo', array($this->listener, 'postFoo'));
$this->assertTrue($this->dispatcher->hasListeners(self::preFoo));
$this->assertTrue($this->dispatcher->hasListeners(self::postFoo));
$this->assertCount(1, $this->dispatcher->getListeners(self::preFoo));
$this->assertCount(1, $this->dispatcher->getListeners(self::postFoo));
$this->assertCount(2, $this->dispatcher->getListeners());
}
public function testGetListenersSortsByPriority()
{
$listener1 = new TestEventListener();
$listener2 = new TestEventListener();
$listener3 = new TestEventListener();
$listener1->name = '1';
$listener2->name = '2';
$listener3->name = '3';
$this->dispatcher->addListener('pre.foo', array($listener1, 'preFoo'), -10);
$this->dispatcher->addListener('pre.foo', array($listener2, 'preFoo'), 10);
$this->dispatcher->addListener('pre.foo', array($listener3, 'preFoo'));
$expected = array(
array($listener2, 'preFoo'),
array($listener3, 'preFoo'),
array($listener1, 'preFoo'),
);
$this->assertSame($expected, $this->dispatcher->getListeners('pre.foo'));
}
public function testGetAllListenersSortsByPriority()
{
$listener1 = new TestEventListener();
$listener2 = new TestEventListener();
$listener3 = new TestEventListener();
$listener4 = new TestEventListener();
$listener5 = new TestEventListener();
$listener6 = new TestEventListener();
$this->dispatcher->addListener('pre.foo', $listener1, -10);
$this->dispatcher->addListener('pre.foo', $listener2);
$this->dispatcher->addListener('pre.foo', $listener3, 10);
$this->dispatcher->addListener('post.foo', $listener4, -10);
$this->dispatcher->addListener('post.foo', $listener5);
$this->dispatcher->addListener('post.foo', $listener6, 10);
$expected = array(
'pre.foo' => array($listener3, $listener2, $listener1),
'post.foo' => array($listener6, $listener5, $listener4),
);
$this->assertSame($expected, $this->dispatcher->getListeners());
}
public function testDispatch()
{
$this->dispatcher->addListener('pre.foo', array($this->listener, 'preFoo'));
$this->dispatcher->addListener('post.foo', array($this->listener, 'postFoo'));
$this->dispatcher->dispatch(self::preFoo);
$this->assertTrue($this->listener->preFooInvoked);
$this->assertFalse($this->listener->postFooInvoked);
$this->assertInstanceOf('Symfony\Component\EventDispatcher\Event', $this->dispatcher->dispatch('noevent'));
$this->assertInstanceOf('Symfony\Component\EventDispatcher\Event', $this->dispatcher->dispatch(self::preFoo));
$event = new Event();
$return = $this->dispatcher->dispatch(self::preFoo, $event);
$this->assertEquals('pre.foo', $event->getName());
$this->assertSame($event, $return);
}
public function testDispatchForClosure()
{
$invoked = 0;
$listener = function () use (&$invoked) {
$invoked++;
};
$this->dispatcher->addListener('pre.foo', $listener);
$this->dispatcher->addListener('post.foo', $listener);
$this->dispatcher->dispatch(self::preFoo);
$this->assertEquals(1, $invoked);
}
public function testStopEventPropagation()
{
$otherListener = new TestEventListener();
// postFoo() stops the propagation, so only one listener should
// be executed
// Manually set priority to enforce $this->listener to be called first
$this->dispatcher->addListener('post.foo', array($this->listener, 'postFoo'), 10);
$this->dispatcher->addListener('post.foo', array($otherListener, 'preFoo'));
$this->dispatcher->dispatch(self::postFoo);
$this->assertTrue($this->listener->postFooInvoked);
$this->assertFalse($otherListener->postFooInvoked);
}
public function testDispatchByPriority()
{
$invoked = array();
$listener1 = function () use (&$invoked) {
$invoked[] = '1';
};
$listener2 = function () use (&$invoked) {
$invoked[] = '2';
};
$listener3 = function () use (&$invoked) {
$invoked[] = '3';
};
$this->dispatcher->addListener('pre.foo', $listener1, -10);
$this->dispatcher->addListener('pre.foo', $listener2);
$this->dispatcher->addListener('pre.foo', $listener3, 10);
$this->dispatcher->dispatch(self::preFoo);
$this->assertEquals(array('3', '2', '1'), $invoked);
}
public function testRemoveListener()
{
$this->dispatcher->addListener('pre.bar', $this->listener);
$this->assertTrue($this->dispatcher->hasListeners(self::preBar));
$this->dispatcher->removeListener('pre.bar', $this->listener);
$this->assertFalse($this->dispatcher->hasListeners(self::preBar));
$this->dispatcher->removeListener('notExists', $this->listener);
}
public function testAddSubscriber()
{
$eventSubscriber = new TestEventSubscriber();
$this->dispatcher->addSubscriber($eventSubscriber);
$this->assertTrue($this->dispatcher->hasListeners(self::preFoo));
$this->assertTrue($this->dispatcher->hasListeners(self::postFoo));
}
public function testAddSubscriberWithPriorities()
{
$eventSubscriber = new TestEventSubscriber();
$this->dispatcher->addSubscriber($eventSubscriber);
$eventSubscriber = new TestEventSubscriberWithPriorities();
$this->dispatcher->addSubscriber($eventSubscriber);
$listeners = $this->dispatcher->getListeners('pre.foo');
$this->assertTrue($this->dispatcher->hasListeners(self::preFoo));
$this->assertCount(2, $listeners);
$this->assertInstanceOf('Symfony\Component\EventDispatcher\Tests\TestEventSubscriberWithPriorities', $listeners[0][0]);
}
public function testAddSubscriberWithMultipleListeners()
{
$eventSubscriber = new TestEventSubscriberWithMultipleListeners();
$this->dispatcher->addSubscriber($eventSubscriber);
$listeners = $this->dispatcher->getListeners('pre.foo');
$this->assertTrue($this->dispatcher->hasListeners(self::preFoo));
$this->assertCount(2, $listeners);
$this->assertEquals('preFoo2', $listeners[0][1]);
}
public function testRemoveSubscriber()
{
$eventSubscriber = new TestEventSubscriber();
$this->dispatcher->addSubscriber($eventSubscriber);
$this->assertTrue($this->dispatcher->hasListeners(self::preFoo));
$this->assertTrue($this->dispatcher->hasListeners(self::postFoo));
$this->dispatcher->removeSubscriber($eventSubscriber);
$this->assertFalse($this->dispatcher->hasListeners(self::preFoo));
$this->assertFalse($this->dispatcher->hasListeners(self::postFoo));
}
public function testRemoveSubscriberWithPriorities()
{
$eventSubscriber = new TestEventSubscriberWithPriorities();
$this->dispatcher->addSubscriber($eventSubscriber);
$this->assertTrue($this->dispatcher->hasListeners(self::preFoo));
$this->dispatcher->removeSubscriber($eventSubscriber);
$this->assertFalse($this->dispatcher->hasListeners(self::preFoo));
}
public function testRemoveSubscriberWithMultipleListeners()
{
$eventSubscriber = new TestEventSubscriberWithMultipleListeners();
$this->dispatcher->addSubscriber($eventSubscriber);
$this->assertTrue($this->dispatcher->hasListeners(self::preFoo));
$this->assertCount(2, $this->dispatcher->getListeners(self::preFoo));
$this->dispatcher->removeSubscriber($eventSubscriber);
$this->assertFalse($this->dispatcher->hasListeners(self::preFoo));
}
public function testEventReceivesTheDispatcherInstance()
{
$dispatcher = null;
$this->dispatcher->addListener('test', function ($event) use (&$dispatcher) {
$dispatcher = $event->getDispatcher();
});
$this->dispatcher->dispatch('test');
$this->assertSame($this->dispatcher, $dispatcher);
}
public function testEventReceivesTheDispatcherInstanceAsArgument()
{
$listener = new TestWithDispatcher();
$this->dispatcher->addListener('test', array($listener, 'foo'));
$this->assertNull($listener->name);
$this->assertNull($listener->dispatcher);
$this->dispatcher->dispatch('test');
$this->assertEquals('test', $listener->name);
$this->assertSame($this->dispatcher, $listener->dispatcher);
}
/**
* @see https://bugs.php.net/bug.php?id=62976
*
* This bug affects:
* - The PHP 5.3 branch for versions < 5.3.18
* - The PHP 5.4 branch for versions < 5.4.8
* - The PHP 5.5 branch is not affected
*/
public function testWorkaroundForPhpBug62976()
{
$dispatcher = new EventDispatcher();
$dispatcher->addListener('bug.62976', new CallableClass());
$dispatcher->removeListener('bug.62976', function () {});
$this->assertTrue($dispatcher->hasListeners('bug.62976'));
}
}
class CallableClass
{
public function __invoke()
{
}
}
class TestEventListener
{
public $preFooInvoked = false;
public $postFooInvoked = false;
/* Listener methods */
public function preFoo(Event $e)
{
$this->preFooInvoked = true;
}
public function postFoo(Event $e)
{
$this->postFooInvoked = true;
$e->stopPropagation();
}
}
class TestWithDispatcher
{
public $name;
public $dispatcher;
public function foo(Event $e, $name, $dispatcher)
{
$this->name = $name;
$this->dispatcher = $dispatcher;
}
}
class TestEventSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return array('pre.foo' => 'preFoo', 'post.foo' => 'postFoo');
}
}
class TestEventSubscriberWithPriorities implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return array(
'pre.foo' => array('preFoo', 10),
'post.foo' => array('postFoo'),
);
}
}
class TestEventSubscriberWithMultipleListeners implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return array('pre.foo' => array(
array('preFoo1'),
array('preFoo2', 10)
));
} }
} }

View File

@ -60,24 +60,28 @@ class EventTest extends \PHPUnit_Framework_TestCase
$this->assertTrue($this->event->isPropagationStopped()); $this->assertTrue($this->event->isPropagationStopped());
} }
public function testSetDispatcher() public function testLegacySetDispatcher()
{ {
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);
$this->event->setDispatcher($this->dispatcher); $this->event->setDispatcher($this->dispatcher);
$this->assertSame($this->dispatcher, $this->event->getDispatcher()); $this->assertSame($this->dispatcher, $this->event->getDispatcher());
} }
public function testGetDispatcher() public function testLegacyGetDispatcher()
{ {
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);
$this->assertNull($this->event->getDispatcher()); $this->assertNull($this->event->getDispatcher());
} }
public function testGetName() public function testLegacyGetName()
{ {
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);
$this->assertNull($this->event->getName()); $this->assertNull($this->event->getName());
} }
public function testSetName() public function testLegacySetName()
{ {
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);
$this->event->setName('foo'); $this->event->setName('foo');
$this->assertEquals('foo', $this->event->getName()); $this->assertEquals('foo', $this->event->getName());
} }

View File

@ -18,7 +18,6 @@ use Symfony\Component\EventDispatcher\GenericEvent;
*/ */
class GenericEventTest extends \PHPUnit_Framework_TestCase class GenericEventTest extends \PHPUnit_Framework_TestCase
{ {
/** /**
* @var GenericEvent * @var GenericEvent
*/ */
@ -34,7 +33,7 @@ class GenericEventTest extends \PHPUnit_Framework_TestCase
parent::setUp(); parent::setUp();
$this->subject = new \stdClass(); $this->subject = new \stdClass();
$this->event = new GenericEvent($this->subject, array('name' => 'Event'), 'foo'); $this->event = new GenericEvent($this->subject, array('name' => 'Event'));
} }
/** /**
@ -54,7 +53,7 @@ class GenericEventTest extends \PHPUnit_Framework_TestCase
} }
/** /**
* Tests Event->getArgs() * Tests Event->getArgs().
*/ */
public function testGetArguments() public function testGetArguments()
{ {

View File

@ -19,7 +19,11 @@
"php": ">=5.3.3" "php": ">=5.3.3"
}, },
"require-dev": { "require-dev": {
"symfony/dependency-injection": "~2.0" "symfony/dependency-injection": "~2.6",
"symfony/expression-language": "~2.6",
"symfony/config": "~2.0,>=2.0.5",
"symfony/stopwatch": "~2.3",
"psr/log": "~1.0"
}, },
"suggest": { "suggest": {
"symfony/dependency-injection": "", "symfony/dependency-injection": "",
@ -32,7 +36,7 @@
"minimum-stability": "dev", "minimum-stability": "dev",
"extra": { "extra": {
"branch-alias": { "branch-alias": {
"dev-master": "2.4-dev" "dev-master": "2.6-dev"
} }
} }
} }

View File

@ -1,16 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false" <phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
backupStaticAttributes="false" xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.1/phpunit.xsd"
backupGlobals="false"
colors="true" colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="vendor/autoload.php" bootstrap="vendor/autoload.php"
> >
<php>
<!-- Silence E_USER_DEPRECATED (-16385 == -1 & ~E_USER_DEPRECATED) -->
<ini name="error_reporting" value="-16385"/>
</php>
<testsuites> <testsuites>
<testsuite name="Symfony EventDispatcher Component Test Suite"> <testsuite name="Symfony EventDispatcher Component Test Suite">
<directory>./Tests/</directory> <directory>./Tests/</directory>