LazyEventDispatcherTest.php 2.0 KB

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