EventDispatcherTest.php 4.1 KB

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