EventDispatcherTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace JMS\SerializerBundle\Tests\Serializer\EventDispatcher;
  3. use JMS\SerializerBundle\Serializer\EventDispatcher\Event;
  4. use JMS\SerializerBundle\Serializer\EventDispatcher\EventDispatcher;
  5. use JMS\SerializerBundle\Serializer\EventDispatcher\EventSubscriberInterface;
  6. class EventDispatcherTest extends \PHPUnit_Framework_TestCase
  7. {
  8. private $dispatcher;
  9. private $event;
  10. public function testHasListeners()
  11. {
  12. $this->assertFalse($this->dispatcher->hasListeners('foo', 'Foo', 'json'));
  13. $this->dispatcher->addListener('foo', function() { });
  14. $this->assertTrue($this->dispatcher->hasListeners('foo', 'Foo', 'json'));
  15. $this->assertFalse($this->dispatcher->hasListeners('bar', 'Bar', 'json'));
  16. $this->dispatcher->addListener('bar', function() { }, 'Foo');
  17. $this->assertFalse($this->dispatcher->hasListeners('bar', 'Bar', 'json'));
  18. $this->dispatcher->addListener('bar', function() { }, 'Bar', 'xml');
  19. $this->assertFalse($this->dispatcher->hasListeners('bar', 'Bar', 'json'));
  20. $this->dispatcher->addListener('bar', function() { }, null, 'json');
  21. $this->assertTrue($this->dispatcher->hasListeners('bar', 'Baz', 'json'));
  22. $this->assertTrue($this->dispatcher->hasListeners('bar', 'Bar', 'json'));
  23. $this->assertFalse($this->dispatcher->hasListeners('baz', 'Bar', 'xml'));
  24. $this->dispatcher->addListener('baz', function() { }, 'Bar');
  25. $this->assertTrue($this->dispatcher->hasListeners('baz', 'Bar', 'xml'));
  26. $this->assertTrue($this->dispatcher->hasListeners('baz', 'bAr', 'xml'));
  27. }
  28. public function testDispatch()
  29. {
  30. $a = new MockListener();
  31. $this->dispatcher->addListener('foo', array($a, 'foo'));
  32. $this->dispatch('bar');
  33. $a->_verify('Listener is not called for other event.');
  34. $b = new MockListener();
  35. $this->dispatcher->addListener('pre', array($b, 'bar'), 'Bar');
  36. $this->dispatcher->addListener('pre', array($b, 'foo'), 'Foo');
  37. $this->dispatcher->addListener('pre', array($b, 'all'));
  38. $b->bar($this->event);
  39. $b->all($this->event);
  40. $b->foo($this->event);
  41. $b->all($this->event);
  42. $b->_replay();
  43. $this->dispatch('pre', 'Bar');
  44. $this->dispatch('pre', 'Foo');
  45. $b->_verify();
  46. }
  47. public function testAddSubscriber()
  48. {
  49. $subscriber = new MockSubscriber();
  50. $subscriber::$events = array(
  51. array('event' => 'foo.bar_baz', 'format' => 'foo'),
  52. array('event' => 'bar', 'method' => 'bar', 'class' => 'foo'),
  53. );
  54. $this->dispatcher->addSubscriber($subscriber);
  55. $this->assertAttributeEquals(array(
  56. 'foo.bar_baz' => array(
  57. array(array($subscriber, 'onfoobarbaz'), null, 'foo'),
  58. ),
  59. 'bar' => array(
  60. array(array($subscriber, 'bar'), 'foo', null),
  61. ),
  62. ), 'listeners', $this->dispatcher);
  63. }
  64. protected function setUp()
  65. {
  66. $this->dispatcher = new EventDispatcher();
  67. $this->event = new Event($this->getMock('JMS\SerializerBundle\Serializer\VisitorInterface'), new \stdClass(), array('name' => 'foo', 'params' => array()));
  68. }
  69. private function dispatch($eventName, $class = 'Foo', $format = 'json', Event $event = null)
  70. {
  71. $this->dispatcher->dispatch($eventName, $class, $format, $event ?: $this->event);
  72. }
  73. }
  74. class MockSubscriber implements EventSubscriberInterface
  75. {
  76. public static $events = array();
  77. public static function getSubscribedEvents()
  78. {
  79. return self::$events;
  80. }
  81. }
  82. class MockListener
  83. {
  84. private $expected = array();
  85. private $actual = array();
  86. private $wasReplayed = false;
  87. public function __call($method, array $args = array())
  88. {
  89. if ( ! $this->wasReplayed) {
  90. $this->expected[] = array($method, $args);
  91. return;
  92. }
  93. $this->actual[] = array($method, $args);
  94. }
  95. public function _replay()
  96. {
  97. $this->wasReplayed = true;
  98. }
  99. public function _verify($message = null)
  100. {
  101. \PHPUnit_Framework_Assert::assertSame($this->expected, $this->actual, $message);
  102. }
  103. }