EventDispatcherTest.php 5.6 KB

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