updated eventdispatcher
This commit is contained in:
parent
0ca7d2dd15
commit
f97109a75b
@ -1,3 +0,0 @@
|
||||
vendor/
|
||||
composer.lock
|
||||
phpunit.xml
|
@ -1,6 +1,13 @@
|
||||
CHANGELOG
|
||||
=========
|
||||
|
||||
2.5.0
|
||||
-----
|
||||
|
||||
* added Debug\TraceableEventDispatcher (originally in HttpKernel)
|
||||
* changed Debug\TraceableEventDispatcherInterface to extend EventDispatcherInterface
|
||||
* added RegisterListenersPass (originally in HttpKernel)
|
||||
|
||||
2.1.0
|
||||
-----
|
||||
|
||||
|
@ -15,7 +15,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
|
||||
/**
|
||||
* Lazily loads listeners and subscribers from the dependency injection
|
||||
* container
|
||||
* container.
|
||||
*
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
* @author Bernhard Schussek <bschussek@gmail.com>
|
||||
@ -24,19 +24,22 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
class ContainerAwareEventDispatcher extends EventDispatcher
|
||||
{
|
||||
/**
|
||||
* The container from where services are loaded
|
||||
* The container from where services are loaded.
|
||||
*
|
||||
* @var ContainerInterface
|
||||
*/
|
||||
private $container;
|
||||
|
||||
/**
|
||||
* The service IDs of the event listeners and subscribers
|
||||
* The service IDs of the event listeners and subscribers.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $listenerIds = array();
|
||||
|
||||
/**
|
||||
* The services registered as listeners
|
||||
* The services registered as listeners.
|
||||
*
|
||||
* @var 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 array $callback The service ID of the listener service & the method
|
||||
* name that has to be called
|
||||
* @param integer $priority The higher this value, the earlier an event listener
|
||||
* will be triggered in the chain.
|
||||
* Defaults to 0.
|
||||
* name that has to be called
|
||||
* @param int $priority The higher this value, the earlier an event listener
|
||||
* will be triggered in the chain.
|
||||
* Defaults to 0.
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
@ -76,21 +79,18 @@ class ContainerAwareEventDispatcher extends EventDispatcher
|
||||
{
|
||||
$this->lazyLoad($eventName);
|
||||
|
||||
if (isset($this->listeners[$eventName])) {
|
||||
foreach ($this->listeners[$eventName] as $key => $l) {
|
||||
foreach ($this->listenerIds[$eventName] as $i => $args) {
|
||||
list($serviceId, $method, $priority) = $args;
|
||||
if ($key === $serviceId.'.'.$method) {
|
||||
if ($listener === array($l, $method)) {
|
||||
unset($this->listeners[$eventName][$key]);
|
||||
if (empty($this->listeners[$eventName])) {
|
||||
unset($this->listeners[$eventName]);
|
||||
}
|
||||
unset($this->listenerIds[$eventName][$i]);
|
||||
if (empty($this->listenerIds[$eventName])) {
|
||||
unset($this->listenerIds[$eventName]);
|
||||
}
|
||||
}
|
||||
if (isset($this->listenerIds[$eventName])) {
|
||||
foreach ($this->listenerIds[$eventName] as $i => $args) {
|
||||
list($serviceId, $method, $priority) = $args;
|
||||
$key = $serviceId.'.'.$method;
|
||||
if (isset($this->listeners[$eventName][$key]) && $listener === array($this->listeners[$eventName][$key], $method)) {
|
||||
unset($this->listeners[$eventName][$key]);
|
||||
if (empty($this->listeners[$eventName])) {
|
||||
unset($this->listeners[$eventName]);
|
||||
}
|
||||
unset($this->listenerIds[$eventName][$i]);
|
||||
if (empty($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)
|
||||
{
|
||||
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])) {
|
||||
@ -116,7 +116,7 @@ class ContainerAwareEventDispatcher extends EventDispatcher
|
||||
}
|
||||
|
||||
/**
|
||||
* @see EventDispatcherInterface::getListeners
|
||||
* @see EventDispatcherInterface::getListeners()
|
||||
*/
|
||||
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 $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
|
||||
* container.
|
||||
|
@ -11,10 +11,12 @@
|
||||
|
||||
namespace Symfony\Component\EventDispatcher\Debug;
|
||||
|
||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||
|
||||
/**
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
*/
|
||||
interface TraceableEventDispatcherInterface
|
||||
interface TraceableEventDispatcherInterface extends EventDispatcherInterface
|
||||
{
|
||||
/**
|
||||
* Gets the called listeners.
|
||||
|
@ -20,17 +20,17 @@ namespace Symfony\Component\EventDispatcher;
|
||||
* You can call the method stopPropagation() to abort the execution of
|
||||
* further listeners in your event listener.
|
||||
*
|
||||
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
|
||||
* @author Jonathan Wage <jonwage@gmail.com>
|
||||
* @author Roman Borschel <roman@code-factory.org>
|
||||
* @author Bernhard Schussek <bschussek@gmail.com>
|
||||
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
|
||||
* @author Jonathan Wage <jonwage@gmail.com>
|
||||
* @author Roman Borschel <roman@code-factory.org>
|
||||
* @author Bernhard Schussek <bschussek@gmail.com>
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
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;
|
||||
|
||||
@ -47,8 +47,9 @@ class Event
|
||||
/**
|
||||
* Returns whether further event listeners should be triggered.
|
||||
*
|
||||
* @see Event::stopPropagation
|
||||
* @return Boolean Whether propagation was already stopped for this event.
|
||||
* @see Event::stopPropagation()
|
||||
*
|
||||
* @return bool Whether propagation was already stopped for this event.
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
@ -72,7 +73,7 @@ class Event
|
||||
}
|
||||
|
||||
/**
|
||||
* Stores the EventDispatcher that dispatches this Event
|
||||
* Stores the EventDispatcher that dispatches this Event.
|
||||
*
|
||||
* @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
|
||||
*
|
||||
|
@ -17,13 +17,13 @@ namespace Symfony\Component\EventDispatcher;
|
||||
* Listeners are registered on the manager and events are dispatched through the
|
||||
* manager.
|
||||
*
|
||||
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
|
||||
* @author Jonathan Wage <jonwage@gmail.com>
|
||||
* @author Roman Borschel <roman@code-factory.org>
|
||||
* @author Bernhard Schussek <bschussek@gmail.com>
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
* @author Jordi Boggiano <j.boggiano@seld.be>
|
||||
* @author Jordan Alliot <jordan.alliot@gmail.com>
|
||||
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
|
||||
* @author Jonathan Wage <jonwage@gmail.com>
|
||||
* @author Roman Borschel <roman@code-factory.org>
|
||||
* @author Bernhard Schussek <bschussek@gmail.com>
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
* @author Jordi Boggiano <j.boggiano@seld.be>
|
||||
* @author Jordan Alliot <jordan.alliot@gmail.com>
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
@ -33,7 +33,7 @@ class EventDispatcher implements EventDispatcherInterface
|
||||
private $sorted = array();
|
||||
|
||||
/**
|
||||
* @see EventDispatcherInterface::dispatch
|
||||
* @see EventDispatcherInterface::dispatch()
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
@ -56,7 +56,7 @@ class EventDispatcher implements EventDispatcherInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* @see EventDispatcherInterface::getListeners
|
||||
* @see EventDispatcherInterface::getListeners()
|
||||
*/
|
||||
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)
|
||||
{
|
||||
return (Boolean) count($this->getListeners($eventName));
|
||||
return (bool) count($this->getListeners($eventName));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see EventDispatcherInterface::addListener
|
||||
* @see EventDispatcherInterface::addListener()
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
@ -97,7 +97,7 @@ class EventDispatcher implements EventDispatcherInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* @see EventDispatcherInterface::removeListener
|
||||
* @see EventDispatcherInterface::removeListener()
|
||||
*/
|
||||
public function removeListener($eventName, $listener)
|
||||
{
|
||||
@ -113,7 +113,7 @@ class EventDispatcher implements EventDispatcherInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* @see EventDispatcherInterface::addSubscriber
|
||||
* @see EventDispatcherInterface::addSubscriber()
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
@ -133,7 +133,7 @@ class EventDispatcher implements EventDispatcherInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* @see EventDispatcherInterface::removeSubscriber
|
||||
* @see EventDispatcherInterface::removeSubscriber()
|
||||
*/
|
||||
public function removeSubscriber(EventSubscriberInterface $subscriber)
|
||||
{
|
||||
|
@ -28,7 +28,7 @@ interface EventDispatcherInterface
|
||||
* @param string $eventName The name of the event to dispatch. The name of
|
||||
* the event is the name of the method that is
|
||||
* 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.
|
||||
*
|
||||
* @return Event
|
||||
@ -42,7 +42,7 @@ interface EventDispatcherInterface
|
||||
*
|
||||
* @param string $eventName The event to listen on
|
||||
* @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)
|
||||
*
|
||||
* @api
|
||||
@ -64,8 +64,8 @@ interface EventDispatcherInterface
|
||||
/**
|
||||
* Removes an event listener from the specified events.
|
||||
*
|
||||
* @param string|array $eventName The event(s) to remove a listener from
|
||||
* @param callable $listener The listener to remove
|
||||
* @param string $eventName The event to remove a listener from
|
||||
* @param callable $listener The listener to remove
|
||||
*/
|
||||
public function removeListener($eventName, $listener);
|
||||
|
||||
@ -90,7 +90,7 @@ interface EventDispatcherInterface
|
||||
*
|
||||
* @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);
|
||||
}
|
||||
|
@ -17,10 +17,10 @@ namespace Symfony\Component\EventDispatcher;
|
||||
* {@link getSubscribedEvents} and registers the subscriber as a listener for all
|
||||
* returned events.
|
||||
*
|
||||
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
|
||||
* @author Jonathan Wage <jonwage@gmail.com>
|
||||
* @author Roman Borschel <roman@code-factory.org>
|
||||
* @author Bernhard Schussek <bschussek@gmail.com>
|
||||
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
|
||||
* @author Jonathan Wage <jonwage@gmail.com>
|
||||
* @author Roman Borschel <roman@code-factory.org>
|
||||
* @author Bernhard Schussek <bschussek@gmail.com>
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
|
@ -118,7 +118,7 @@ class GenericEvent extends Event implements \ArrayAccess, \IteratorAggregate
|
||||
*
|
||||
* @param string $key Key of arguments array.
|
||||
*
|
||||
* @return boolean
|
||||
* @return bool
|
||||
*/
|
||||
public function hasArgument($key)
|
||||
{
|
||||
@ -167,7 +167,7 @@ class GenericEvent extends Event implements \ArrayAccess, \IteratorAggregate
|
||||
*
|
||||
* @param string $key Array key.
|
||||
*
|
||||
* @return boolean
|
||||
* @return bool
|
||||
*/
|
||||
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
|
||||
*/
|
||||
|
@ -20,6 +20,7 @@ class ImmutableEventDispatcher implements EventDispatcherInterface
|
||||
{
|
||||
/**
|
||||
* The proxied dispatcher.
|
||||
*
|
||||
* @var EventDispatcherInterface
|
||||
*/
|
||||
private $dispatcher;
|
||||
|
@ -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
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
|
@ -1,19 +1,21 @@
|
||||
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.
|
||||
|
||||
use Symfony\Component\EventDispatcher\EventDispatcher;
|
||||
use Symfony\Component\EventDispatcher\Event;
|
||||
```php
|
||||
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
|
||||
---------
|
||||
|
@ -17,8 +17,15 @@ use Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher;
|
||||
use Symfony\Component\EventDispatcher\Event;
|
||||
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()
|
||||
{
|
||||
$event = new Event();
|
||||
@ -232,8 +239,6 @@ class SubscriberService implements EventSubscriberInterface
|
||||
public static function getSubscribedEvents()
|
||||
{
|
||||
return array(
|
||||
'onEvent' => 'onEvent',
|
||||
'onEvent' => array('onEvent', 10),
|
||||
'onEvent' => array('onEvent'),
|
||||
);
|
||||
}
|
||||
|
@ -11,336 +11,12 @@
|
||||
|
||||
namespace Symfony\Component\EventDispatcher\Tests;
|
||||
|
||||
use Symfony\Component\EventDispatcher\Event;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcher;
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||
|
||||
class EventDispatcherTest extends \PHPUnit_Framework_TestCase
|
||||
class EventDispatcherTest extends AbstractEventDispatcherTest
|
||||
{
|
||||
/* Some pseudo events */
|
||||
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()
|
||||
protected function createEventDispatcher()
|
||||
{
|
||||
$this->dispatcher = 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)
|
||||
));
|
||||
return new EventDispatcher();
|
||||
}
|
||||
}
|
||||
|
@ -60,24 +60,28 @@ class EventTest extends \PHPUnit_Framework_TestCase
|
||||
$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->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());
|
||||
}
|
||||
|
||||
public function testGetName()
|
||||
public function testLegacyGetName()
|
||||
{
|
||||
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);
|
||||
$this->assertNull($this->event->getName());
|
||||
}
|
||||
|
||||
public function testSetName()
|
||||
public function testLegacySetName()
|
||||
{
|
||||
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);
|
||||
$this->event->setName('foo');
|
||||
$this->assertEquals('foo', $this->event->getName());
|
||||
}
|
||||
|
@ -18,7 +18,6 @@ use Symfony\Component\EventDispatcher\GenericEvent;
|
||||
*/
|
||||
class GenericEventTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* @var GenericEvent
|
||||
*/
|
||||
@ -34,7 +33,7 @@ class GenericEventTest extends \PHPUnit_Framework_TestCase
|
||||
parent::setUp();
|
||||
|
||||
$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()
|
||||
{
|
||||
|
@ -19,7 +19,11 @@
|
||||
"php": ">=5.3.3"
|
||||
},
|
||||
"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": {
|
||||
"symfony/dependency-injection": "",
|
||||
@ -32,7 +36,7 @@
|
||||
"minimum-stability": "dev",
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-master": "2.4-dev"
|
||||
"dev-master": "2.6-dev"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,16 +1,15 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<phpunit backupGlobals="false"
|
||||
backupStaticAttributes="false"
|
||||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.1/phpunit.xsd"
|
||||
backupGlobals="false"
|
||||
colors="true"
|
||||
convertErrorsToExceptions="true"
|
||||
convertNoticesToExceptions="true"
|
||||
convertWarningsToExceptions="true"
|
||||
processIsolation="false"
|
||||
stopOnFailure="false"
|
||||
syntaxCheck="false"
|
||||
bootstrap="vendor/autoload.php"
|
||||
>
|
||||
<php>
|
||||
<!-- Silence E_USER_DEPRECATED (-16385 == -1 & ~E_USER_DEPRECATED) -->
|
||||
<ini name="error_reporting" value="-16385"/>
|
||||
</php>
|
||||
<testsuites>
|
||||
<testsuite name="Symfony EventDispatcher Component Test Suite">
|
||||
<directory>./Tests/</directory>
|
||||
|
Loading…
Reference in New Issue
Block a user