EventDispatcherTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. namespace JMS\Serializer\Tests\Serializer\EventDispatcher;
  3. use JMS\Serializer\EventDispatcher\Event;
  4. use JMS\Serializer\EventDispatcher\EventDispatcher;
  5. use JMS\Serializer\EventDispatcher\EventDispatcherInterface;
  6. use JMS\Serializer\EventDispatcher\EventSubscriberInterface;
  7. use JMS\Serializer\EventDispatcher\ObjectEvent;
  8. class EventDispatcherTest extends \PHPUnit_Framework_TestCase
  9. {
  10. /**
  11. * @var EventDispatcher
  12. */
  13. protected $dispatcher;
  14. protected $event;
  15. public function testHasListeners()
  16. {
  17. $this->assertFalse($this->dispatcher->hasListeners('foo', 'Foo', 'json'));
  18. $this->dispatcher->addListener('foo', function () {
  19. });
  20. $this->assertTrue($this->dispatcher->hasListeners('foo', 'Foo', 'json'));
  21. $this->assertFalse($this->dispatcher->hasListeners('bar', 'Bar', 'json'));
  22. $this->dispatcher->addListener('bar', function () {
  23. }, 'Foo');
  24. $this->assertFalse($this->dispatcher->hasListeners('bar', 'Bar', 'json'));
  25. $this->dispatcher->addListener('bar', function () {
  26. }, 'Bar', 'xml');
  27. $this->assertFalse($this->dispatcher->hasListeners('bar', 'Bar', 'json'));
  28. $this->dispatcher->addListener('bar', function () {
  29. }, null, 'json');
  30. $this->assertTrue($this->dispatcher->hasListeners('bar', 'Baz', 'json'));
  31. $this->assertTrue($this->dispatcher->hasListeners('bar', 'Bar', 'json'));
  32. $this->assertFalse($this->dispatcher->hasListeners('baz', 'Bar', 'xml'));
  33. $this->dispatcher->addListener('baz', function () {
  34. }, 'Bar');
  35. $this->assertTrue($this->dispatcher->hasListeners('baz', 'Bar', 'xml'));
  36. $this->assertTrue($this->dispatcher->hasListeners('baz', 'bAr', 'xml'));
  37. }
  38. public function testDispatch()
  39. {
  40. $a = new MockListener();
  41. $this->dispatcher->addListener('foo', array($a, 'foo'));
  42. $this->dispatch('bar');
  43. $a->_verify('Listener is not called for other event.');
  44. $b = new MockListener();
  45. $this->dispatcher->addListener('pre', array($b, 'bar'), 'Bar');
  46. $this->dispatcher->addListener('pre', array($b, 'foo'), 'Foo');
  47. $this->dispatcher->addListener('pre', array($b, 'all'));
  48. $b->bar($this->event, 'pre', 'bar', 'json', $this->dispatcher);
  49. $b->all($this->event, 'pre', 'bar', 'json', $this->dispatcher);
  50. $b->foo($this->event, 'pre', 'foo', 'json', $this->dispatcher);
  51. $b->all($this->event, 'pre', 'foo', 'json', $this->dispatcher);
  52. $b->_replay();
  53. $this->dispatch('pre', 'Bar');
  54. $this->dispatch('pre', 'Foo');
  55. $b->_verify();
  56. }
  57. public function testListenerCanStopPropagation()
  58. {
  59. $listener1 = false;
  60. $listener2 = false;
  61. $this->dispatcher->addListener('pre', function (Event $event) use (&$listener1) {
  62. $event->stopPropagation();
  63. $listener1 = true;
  64. });
  65. $this->dispatcher->addListener('pre', function () use (&$listener2) {
  66. $listener2 = true;
  67. });
  68. $this->dispatch('pre');
  69. $this->assertTrue($listener1);
  70. $this->assertFalse($listener2);
  71. }
  72. public function testListenerCanDispatchEvent()
  73. {
  74. $listener1 = false;
  75. $listener2 = false;
  76. $listener3 = false;
  77. $this->dispatcher->addListener('pre', function (Event $event, $eventName, $loweredClass, $format, EventDispatcherInterface $dispatcher) use (&$listener1) {
  78. $listener1 = true;
  79. $event = new Event($event->getContext(), $event->getType());
  80. $this->assertSame('pre', $eventName);
  81. $this->assertSame('json', $format);
  82. $this->assertSame('foo', $loweredClass);
  83. $dispatcher->dispatch('post', 'Blah', 'xml', $event);
  84. });
  85. $this->dispatcher->addListener('pre', function () use (&$listener2) {
  86. $listener2 = true;
  87. });
  88. $this->dispatcher->addListener('post', function (Event $event, $eventName, $loweredClass, $format, EventDispatcherInterface $dispatcher) use (&$listener3) {
  89. $listener3 = true;
  90. $this->assertSame('post', $eventName);
  91. $this->assertSame('xml', $format);
  92. $this->assertSame('blah', $loweredClass);
  93. });
  94. $this->dispatch('pre');
  95. $this->assertTrue($listener1);
  96. $this->assertTrue($listener2);
  97. $this->assertTrue($listener3);
  98. }
  99. public function testAddSubscriber()
  100. {
  101. $subscriber = new MockSubscriber();
  102. MockSubscriber::$events = array(
  103. array('event' => 'foo.bar_baz', 'format' => 'foo'),
  104. array('event' => 'bar', 'method' => 'bar', 'class' => 'foo'),
  105. );
  106. $this->dispatcher->addSubscriber($subscriber);
  107. $this->assertAttributeEquals(array(
  108. 'foo.bar_baz' => array(
  109. array(array($subscriber, 'onfoobarbaz'), null, 'foo'),
  110. ),
  111. 'bar' => array(
  112. array(array($subscriber, 'bar'), 'foo', null),
  113. ),
  114. ), 'listeners', $this->dispatcher);
  115. }
  116. protected function setUp()
  117. {
  118. $this->dispatcher = $this->createEventDispatcher();
  119. $this->event = new ObjectEvent($this->getMockBuilder('JMS\Serializer\Context')->getMock(), new \stdClass(), array('name' => 'foo', 'params' => array()));
  120. }
  121. protected function createEventDispatcher()
  122. {
  123. return new EventDispatcher();
  124. }
  125. protected function dispatch($eventName, $class = 'Foo', $format = 'json', Event $event = null)
  126. {
  127. $this->dispatcher->dispatch($eventName, $class, $format, $event ?: $this->event);
  128. }
  129. }
  130. class MockSubscriber implements EventSubscriberInterface
  131. {
  132. public static $events = array();
  133. public static function getSubscribedEvents()
  134. {
  135. return self::$events;
  136. }
  137. }
  138. class MockListener
  139. {
  140. private $expected = array();
  141. private $actual = array();
  142. private $wasReplayed = false;
  143. public function __call($method, array $args = array())
  144. {
  145. if (!$this->wasReplayed) {
  146. $this->expected[] = array($method, $args);
  147. return;
  148. }
  149. $this->actual[] = array($method, $args);
  150. }
  151. public function _replay()
  152. {
  153. $this->wasReplayed = true;
  154. }
  155. public function _verify($message = null)
  156. {
  157. \PHPUnit_Framework_Assert::assertSame($this->expected, $this->actual, $message);
  158. }
  159. }