[EventDispatcher] Added class UnmodifiableEventDispatcher
This commit is contained in:
parent
306324ea0a
commit
7a4ba521c4
@ -13,3 +13,4 @@ CHANGELOG
|
||||
* added GenericEvent event class
|
||||
* added the possibility for subscribers to subscribe several times for the
|
||||
same event
|
||||
* added UnmodifiableEventDispatcher
|
||||
|
@ -0,0 +1,106 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\EventDispatcher\Tests;
|
||||
|
||||
use Symfony\Component\EventDispatcher\Event;
|
||||
use Symfony\Component\EventDispatcher\UnmodifiableEventDispatcher;
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||
|
||||
/**
|
||||
* @author Bernhard Schussek <bschussek@gmail.com>
|
||||
*/
|
||||
class UnmodifiableEventDispatcherTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @var \PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
private $innerDispatcher;
|
||||
|
||||
/**
|
||||
* @var UnmodifiableEventDispatcher
|
||||
*/
|
||||
private $dispatcher;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->innerDispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
|
||||
$this->dispatcher = new UnmodifiableEventDispatcher($this->innerDispatcher);
|
||||
}
|
||||
|
||||
public function testDispatchDelegates()
|
||||
{
|
||||
$event = new Event();
|
||||
|
||||
$this->innerDispatcher->expects($this->once())
|
||||
->method('dispatch')
|
||||
->with('event', $event)
|
||||
->will($this->returnValue('result'));
|
||||
|
||||
$this->assertSame('result', $this->dispatcher->dispatch('event', $event));
|
||||
}
|
||||
|
||||
public function testGetListenersDelegates()
|
||||
{
|
||||
$this->innerDispatcher->expects($this->once())
|
||||
->method('getListeners')
|
||||
->with('event')
|
||||
->will($this->returnValue('result'));
|
||||
|
||||
$this->assertSame('result', $this->dispatcher->getListeners('event'));
|
||||
}
|
||||
|
||||
public function testHasListenersDelegates()
|
||||
{
|
||||
$this->innerDispatcher->expects($this->once())
|
||||
->method('hasListeners')
|
||||
->with('event')
|
||||
->will($this->returnValue('result'));
|
||||
|
||||
$this->assertSame('result', $this->dispatcher->hasListeners('event'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \BadMethodCallException
|
||||
*/
|
||||
public function testAddListenerDisallowed()
|
||||
{
|
||||
$this->dispatcher->addListener('event', function () { return 'foo'; });
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \BadMethodCallException
|
||||
*/
|
||||
public function testAddSubscriberDisallowed()
|
||||
{
|
||||
$subscriber = $this->getMock('Symfony\Component\EventDispatcher\EventSubscriberInterface');
|
||||
|
||||
$this->dispatcher->addSubscriber($subscriber);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \BadMethodCallException
|
||||
*/
|
||||
public function testRemoveListenerDisallowed()
|
||||
{
|
||||
$this->dispatcher->removeListener('event', function () { return 'foo'; });
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \BadMethodCallException
|
||||
*/
|
||||
public function testRemoveSubscriberDisallowed()
|
||||
{
|
||||
$subscriber = $this->getMock('Symfony\Component\EventDispatcher\EventSubscriberInterface');
|
||||
|
||||
$this->dispatcher->removeSubscriber($subscriber);
|
||||
}
|
||||
}
|
@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\EventDispatcher;
|
||||
|
||||
/**
|
||||
* A read-only proxy for an event dispatcher.
|
||||
*
|
||||
* @author Bernhard Schussek <bschussek@gmail.com>
|
||||
*/
|
||||
class UnmodifiableEventDispatcher implements EventDispatcherInterface
|
||||
{
|
||||
/**
|
||||
* The proxied dispatcher.
|
||||
* @var EventDispatcherInterface
|
||||
*/
|
||||
private $dispatcher;
|
||||
|
||||
/**
|
||||
* Creates an unmodifiable proxy for an event dispatcher.
|
||||
*
|
||||
* @param EventDispatcherInterface $dispatcher The proxied event dispatcher.
|
||||
*/
|
||||
public function __construct(EventDispatcherInterface $dispatcher)
|
||||
{
|
||||
$this->dispatcher = $dispatcher;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function dispatch($eventName, Event $event = null)
|
||||
{
|
||||
return $this->dispatcher->dispatch($eventName, $event);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function addListener($eventName, $listener, $priority = 0)
|
||||
{
|
||||
throw new \BadMethodCallException('Unmodifiable event dispatchers must not be modified.');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function addSubscriber(EventSubscriberInterface $subscriber)
|
||||
{
|
||||
throw new \BadMethodCallException('Unmodifiable event dispatchers must not be modified.');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function removeListener($eventName, $listener)
|
||||
{
|
||||
throw new \BadMethodCallException('Unmodifiable event dispatchers must not be modified.');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function removeSubscriber(EventSubscriberInterface $subscriber)
|
||||
{
|
||||
throw new \BadMethodCallException('Unmodifiable event dispatchers must not be modified.');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getListeners($eventName = null)
|
||||
{
|
||||
return $this->dispatcher->getListeners($eventName);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function hasListeners($eventName = null)
|
||||
{
|
||||
return $this->dispatcher->hasListeners($eventName);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user