123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- <?php
- namespace JMS\SerializerBundle\Tests\EventDispatcher;
- use JMS\SerializerBundle\EventDispatcher\EventSubscriberInterface;
- use JMS\SerializerBundle\Metadata\ClassMetadata;
- use JMS\SerializerBundle\EventDispatcher\Event;
- use JMS\SerializerBundle\EventDispatcher\EventDispatcher;
- class EventDispatcherTest extends \PHPUnit_Framework_TestCase
- {
- private $dispatcher;
- private $event;
- public function testHasListeners()
- {
- $this->assertFalse($this->dispatcher->hasListeners('foo', 'Foo', 'json'));
- $this->dispatcher->addListener('foo', function() { });
- $this->assertTrue($this->dispatcher->hasListeners('foo', 'Foo', 'json'));
- $this->assertFalse($this->dispatcher->hasListeners('bar', 'Bar', 'json'));
- $this->dispatcher->addListener('bar', function() { }, 'Foo');
- $this->assertFalse($this->dispatcher->hasListeners('bar', 'Bar', 'json'));
- $this->dispatcher->addListener('bar', function() { }, 'Bar', 'xml');
- $this->assertFalse($this->dispatcher->hasListeners('bar', 'Bar', 'json'));
- $this->dispatcher->addListener('bar', function() { }, null, 'json');
- $this->assertTrue($this->dispatcher->hasListeners('bar', 'Baz', 'json'));
- $this->assertTrue($this->dispatcher->hasListeners('bar', 'Bar', 'json'));
- $this->assertFalse($this->dispatcher->hasListeners('baz', 'Bar', 'xml'));
- $this->dispatcher->addListener('baz', function() { }, 'Bar');
- $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->dispatch('bar');
- $a->_verify('Listener is not called for other event.');
- $b = new MockListener();
- $this->dispatcher->addListener('pre', array($b, 'bar'), 'Bar');
- $this->dispatcher->addListener('pre', array($b, 'foo'), 'Foo');
- $this->dispatcher->addListener('pre', array($b, 'all'));
- $b->bar($this->event);
- $b->all($this->event);
- $b->foo($this->event);
- $b->all($this->event);
- $b->_replay();
- $this->dispatch('pre', 'Bar');
- $this->dispatch('pre', 'Foo');
- $b->_verify();
- }
- public function testAddSubscriber()
- {
- $subscriber = new MockSubscriber();
- $subscriber::$events = array(
- array('event' => 'foo.bar_baz', 'format' => 'foo'),
- array('event' => 'bar', 'method' => 'bar', 'class' => 'foo'),
- );
- $this->dispatcher->addSubscriber($subscriber);
- $this->assertAttributeEquals(array(
- 'foo.bar_baz' => array(
- array(array($subscriber, 'onfoobarbaz'), null, 'foo'),
- ),
- 'bar' => array(
- array(array($subscriber, 'bar'), 'foo', null),
- ),
- ), 'listeners', $this->dispatcher);
- }
- protected function setUp()
- {
- $this->dispatcher = new EventDispatcher();
- $this->event = new Event($this->getMock('JMS\SerializerBundle\Serializer\VisitorInterface'), new \stdClass(), new ClassMetadata('stdClass'));
- }
- private function dispatch($eventName, $class = 'Foo', $format = 'json', Event $event = null)
- {
- $this->dispatcher->dispatch($eventName, $class, $format, $event ?: $this->event);
- }
- }
- class MockSubscriber implements EventSubscriberInterface
- {
- public static $events = array();
- public static function getSubscribedEvents()
- {
- return self::$events;
- }
- }
- class MockListener
- {
- private $expected = array();
- private $actual = array();
- private $wasReplayed = false;
- public function __call($method, array $args = array())
- {
- if ( ! $this->wasReplayed) {
- $this->expected[] = array($method, $args);
- return;
- }
- $this->actual[] = array($method, $args);
- }
- public function _replay()
- {
- $this->wasReplayed = true;
- }
- public function _verify($message = null)
- {
- \PHPUnit_Framework_Assert::assertSame($this->expected, $this->actual, $message);
- }
- }
|