1<?php 2 3declare(strict_types=1); 4 5namespace JMS\Serializer\Tests\Serializer\EventDispatcher; 6 7use JMS\Serializer\EventDispatcher\LazyEventDispatcher; 8 9abstract class LazyEventDispatcherTest extends EventDispatcherTest 10{ 11 protected $container; 12 13 protected function setUp() 14 { 15 $this->container = $this->createContainer(); 16 17 parent::setUp(); 18 } 19 20 public function testHasListenersWithListenerAsService() 21 { 22 $a = new MockListener(); 23 $this->registerListenerService('a', $a); 24 25 self::assertFalse($this->dispatcher->hasListeners('foo', 'Foo', 'json')); 26 $this->dispatcher->addListener('foo', ['a', 'foo']); 27 self::assertTrue($this->dispatcher->hasListeners('foo', 'Foo', 'json')); 28 } 29 30 public function testDispatchWithListenerAsService() 31 { 32 $a = new MockListener(); 33 $this->registerListenerService('a', $a); 34 35 $this->dispatcher->addListener('foo', ['a', 'foo']); 36 $this->dispatch('bar'); 37 $a->_verify('Listener is not called for other event.'); 38 39 $b = new MockListener(); 40 $this->registerListenerService('b', $b); 41 42 $this->dispatcher->addListener('pre', ['b', 'bar'], 'Bar'); 43 $this->dispatcher->addListener('pre', ['b', 'foo'], 'Foo'); 44 $this->dispatcher->addListener('pre', ['b', 'all']); 45 46 $b->bar($this->event, 'pre', 'Bar', 'json', $this->dispatcher); 47 $b->all($this->event, 'pre', 'Bar', 'json', $this->dispatcher); 48 $b->foo($this->event, 'pre', 'Foo', 'json', $this->dispatcher); 49 $b->all($this->event, 'pre', 'Foo', 'json', $this->dispatcher); 50 $b->_replay(); 51 $this->dispatch('pre', 'Bar'); 52 $this->dispatch('pre', 'Foo'); 53 $b->_verify(); 54 } 55 56 protected function createEventDispatcher() 57 { 58 return new LazyEventDispatcher($this->container); 59 } 60 61 abstract protected function createContainer(); 62 63 abstract protected function registerListenerService($serviceId, MockListener $listener); 64} 65