EventDispatcherTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. namespace Symfony\Tests\Component\EventDispatcher;
  10. use Symfony\Component\EventDispatcher\Event;
  11. use Symfony\Component\EventDispatcher\EventDispatcher;
  12. class EventDispatcherTest extends \PHPUnit_Framework_TestCase
  13. {
  14. public function testConnectAndDisconnect()
  15. {
  16. $dispatcher = new EventDispatcher();
  17. $dispatcher->connect('bar', 'listenToBar');
  18. $this->assertEquals(array('listenToBar'), $dispatcher->getListeners('bar'), '->connect() connects a listener to an event name');
  19. $dispatcher->connect('bar', 'listenToBarBar');
  20. $this->assertEquals(array('listenToBar', 'listenToBarBar'), $dispatcher->getListeners('bar'), '->connect() can connect several listeners for the same event name');
  21. $dispatcher->connect('barbar', 'listenToBarBar');
  22. $dispatcher->disconnect('bar', 'listenToBarBar');
  23. $this->assertEquals(array('listenToBar'), $dispatcher->getListeners('bar'), '->disconnect() disconnects a listener for an event name');
  24. $this->assertEquals(array('listenToBarBar'), $dispatcher->getListeners('barbar'), '->disconnect() disconnects a listener for an event name');
  25. $this->assertFalse($dispatcher->disconnect('foobar', 'listen'), '->disconnect() returns false if the listener does not exist');
  26. $dispatcher->disconnect('bar');
  27. $this->assertEquals(array(), $dispatcher->getListeners('bar'), '->disconnect() without a listener disconnects all listeners of for an event name');
  28. $this->assertEquals(array('listenToBarBar'), $dispatcher->getListeners('barbar'), '->disconnect() without a listener disconnects all listeners of for an event name');
  29. }
  30. public function testGetHasListeners()
  31. {
  32. $dispatcher = new EventDispatcher();
  33. $this->assertFalse($dispatcher->hasListeners('foo'), '->hasListeners() returns false if the event has no listener');
  34. $dispatcher->connect('foo', 'listenToFoo');
  35. $this->assertEquals(true, $dispatcher->hasListeners('foo'), '->hasListeners() returns true if the event has some listeners');
  36. $dispatcher->disconnect('foo', 'listenToFoo');
  37. $this->assertFalse($dispatcher->hasListeners('foo'), '->hasListeners() returns false if the event has no listener');
  38. $dispatcher->connect('bar', 'listenToBar');
  39. $this->assertEquals(array('listenToBar'), $dispatcher->getListeners('bar'), '->getListeners() returns an array of listeners connected to the given event name');
  40. $this->assertEquals(array(), $dispatcher->getListeners('foobar'), '->getListeners() returns an empty array if no listener are connected to the given event name');
  41. }
  42. public function testNotify()
  43. {
  44. $listener = new Listener();
  45. $dispatcher = new EventDispatcher();
  46. $dispatcher->connect('foo', array($listener, 'listenToFoo'));
  47. $dispatcher->connect('foo', array($listener, 'listenToFooBis'));
  48. $e = $dispatcher->notify($event = new Event(new \stdClass(), 'foo'));
  49. $this->assertEquals('listenToFoolistenToFooBis', $listener->getValue(), '->notify() notifies all registered listeners in order');
  50. $this->assertEquals($event, $e, '->notify() returns the event object');
  51. $listener->reset();
  52. $dispatcher = new EventDispatcher();
  53. $dispatcher->connect('foo', array($listener, 'listenToFooBis'));
  54. $dispatcher->connect('foo', array($listener, 'listenToFoo'));
  55. $dispatcher->notify(new Event(new \stdClass(), 'foo'));
  56. $this->assertEquals('listenToFooBislistenToFoo', $listener->getValue(), '->notify() notifies all registered listeners in order');
  57. }
  58. public function testNotifyUntil()
  59. {
  60. $listener = new Listener();
  61. $dispatcher = new EventDispatcher();
  62. $dispatcher->connect('foo', array($listener, 'listenToFoo'));
  63. $dispatcher->connect('foo', array($listener, 'listenToFooBis'));
  64. $e = $dispatcher->notifyUntil($event = new Event(new \stdClass(), 'foo'));
  65. $this->assertEquals('listenToFoolistenToFooBis', $listener->getValue(), '->notifyUntil() notifies all registered listeners in order and stops if it returns true');
  66. $this->assertEquals($event, $e, '->notifyUntil() returns the event object');
  67. $listener->reset();
  68. $dispatcher = new EventDispatcher();
  69. $dispatcher->connect('foo', array($listener, 'listenToFooBis'));
  70. $dispatcher->connect('foo', array($listener, 'listenToFoo'));
  71. $e = $dispatcher->notifyUntil($event = new Event(new \stdClass(), 'foo'));
  72. $this->assertEquals('listenToFooBis', $listener->getValue(), '->notifyUntil() notifies all registered listeners in order and stops if it returns true');
  73. }
  74. public function testFilter()
  75. {
  76. $listener = new Listener();
  77. $dispatcher = new EventDispatcher();
  78. $dispatcher->connect('foo', array($listener, 'filterFoo'));
  79. $dispatcher->connect('foo', array($listener, 'filterFooBis'));
  80. $e = $dispatcher->filter($event = new Event(new \stdClass(), 'foo'), 'foo');
  81. $this->assertEquals('-*foo*-', $e->getReturnValue(), '->filter() filters a value');
  82. $this->assertEquals($event, $e, '->filter() returns the event object');
  83. $listener->reset();
  84. $dispatcher = new EventDispatcher();
  85. $dispatcher->connect('foo', array($listener, 'filterFooBis'));
  86. $dispatcher->connect('foo', array($listener, 'filterFoo'));
  87. $e = $dispatcher->filter($event = new Event(new \stdClass(), 'foo'), 'foo');
  88. $this->assertEquals('*-foo-*', $e->getReturnValue(), '->filter() filters a value');
  89. }
  90. }
  91. class Listener
  92. {
  93. protected
  94. $value = '';
  95. function filterFoo(Event $event, $foo)
  96. {
  97. return "*$foo*";
  98. }
  99. function filterFooBis(Event $event, $foo)
  100. {
  101. return "-$foo-";
  102. }
  103. function listenToFoo(Event $event)
  104. {
  105. $this->value .= 'listenToFoo';
  106. }
  107. function listenToFooBis(Event $event)
  108. {
  109. $this->value .= 'listenToFooBis';
  110. return true;
  111. }
  112. function getValue()
  113. {
  114. return $this->value;
  115. }
  116. function reset()
  117. {
  118. $this->value = '';
  119. }
  120. }