EventDispatcherTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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');
  23. $this->assertEquals(array(), $dispatcher->getListeners('bar'), '->disconnect() without a listener disconnects all listeners of for an event name');
  24. $this->assertEquals(array('listenToBarBar'), $dispatcher->getListeners('barbar'), '->disconnect() without a listener disconnects all listeners of for an event name');
  25. }
  26. public function testGetHasListeners()
  27. {
  28. $dispatcher = new EventDispatcher();
  29. $this->assertFalse($dispatcher->hasListeners('foo'), '->hasListeners() returns false if the event has no listener');
  30. $dispatcher->connect('foo', 'listenToFoo');
  31. $this->assertEquals(true, $dispatcher->hasListeners('foo'), '->hasListeners() returns true if the event has some listeners');
  32. $dispatcher->disconnect('foo', 'listenToFoo');
  33. $this->assertFalse($dispatcher->hasListeners('foo'), '->hasListeners() returns false if the event has no listener');
  34. $dispatcher->connect('bar', 'listenToBar');
  35. $this->assertEquals(array('listenToBar'), $dispatcher->getListeners('bar'), '->getListeners() returns an array of listeners connected to the given event name');
  36. $this->assertEquals(array(), $dispatcher->getListeners('foobar'), '->getListeners() returns an empty array if no listener are connected to the given event name');
  37. }
  38. public function testNotify()
  39. {
  40. $listener = new Listener();
  41. $dispatcher = new EventDispatcher();
  42. $dispatcher->connect('foo', array($listener, 'listenToFoo'));
  43. $dispatcher->connect('foo', array($listener, 'listenToFooBis'));
  44. $e = $dispatcher->notify($event = new Event(new \stdClass(), 'foo'));
  45. $this->assertEquals('listenToFoolistenToFooBis', $listener->getValue(), '->notify() notifies all registered listeners in order');
  46. $this->assertEquals($event, $e, '->notify() returns the event object');
  47. $listener->reset();
  48. $dispatcher = new EventDispatcher();
  49. $dispatcher->connect('foo', array($listener, 'listenToFooBis'));
  50. $dispatcher->connect('foo', array($listener, 'listenToFoo'));
  51. $dispatcher->notify(new Event(new \stdClass(), 'foo'));
  52. $this->assertEquals('listenToFooBislistenToFoo', $listener->getValue(), '->notify() notifies all registered listeners in order');
  53. }
  54. public function testNotifyUntil()
  55. {
  56. $listener = new Listener();
  57. $dispatcher = new EventDispatcher();
  58. $dispatcher->connect('foo', array($listener, 'listenToFoo'));
  59. $dispatcher->connect('foo', array($listener, 'listenToFooBis'));
  60. $e = $dispatcher->notifyUntil($event = new Event(new \stdClass(), 'foo'));
  61. $this->assertEquals('listenToFoolistenToFooBis', $listener->getValue(), '->notifyUntil() notifies all registered listeners in order and stops if it returns true');
  62. $this->assertEquals($event, $e, '->notifyUntil() returns the event object');
  63. $listener->reset();
  64. $dispatcher = new EventDispatcher();
  65. $dispatcher->connect('foo', array($listener, 'listenToFooBis'));
  66. $dispatcher->connect('foo', array($listener, 'listenToFoo'));
  67. $e = $dispatcher->notifyUntil($event = new Event(new \stdClass(), 'foo'));
  68. $this->assertEquals('listenToFooBis', $listener->getValue(), '->notifyUntil() notifies all registered listeners in order and stops if it returns true');
  69. }
  70. public function testFilter()
  71. {
  72. $listener = new Listener();
  73. $dispatcher = new EventDispatcher();
  74. $dispatcher->connect('foo', array($listener, 'filterFoo'));
  75. $dispatcher->connect('foo', array($listener, 'filterFooBis'));
  76. $e = $dispatcher->filter($event = new Event(new \stdClass(), 'foo'), 'foo');
  77. $this->assertEquals('-*foo*-', $e->getReturnValue(), '->filter() filters a value');
  78. $this->assertEquals($event, $e, '->filter() returns the event object');
  79. $listener->reset();
  80. $dispatcher = new EventDispatcher();
  81. $dispatcher->connect('foo', array($listener, 'filterFooBis'));
  82. $dispatcher->connect('foo', array($listener, 'filterFoo'));
  83. $e = $dispatcher->filter($event = new Event(new \stdClass(), 'foo'), 'foo');
  84. $this->assertEquals('*-foo-*', $e->getReturnValue(), '->filter() filters a value');
  85. }
  86. }
  87. class Listener
  88. {
  89. protected
  90. $value = '';
  91. function filterFoo(Event $event, $foo)
  92. {
  93. return "*$foo*";
  94. }
  95. function filterFooBis(Event $event, $foo)
  96. {
  97. return "-$foo-";
  98. }
  99. function listenToFoo(Event $event)
  100. {
  101. $this->value .= 'listenToFoo';
  102. }
  103. function listenToFooBis(Event $event)
  104. {
  105. $this->value .= 'listenToFooBis';
  106. return true;
  107. }
  108. function getValue()
  109. {
  110. return $this->value;
  111. }
  112. function reset()
  113. {
  114. $this->value = '';
  115. }
  116. }