EventDispatcherTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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\Components\EventDispatcher;
  10. use Symfony\Components\EventDispatcher\Event;
  11. use Symfony\Components\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. }
  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. $this->assertEquals($event, $e, '->notify() returns the event object');
  48. $listener->reset();
  49. $dispatcher = new EventDispatcher();
  50. $dispatcher->connect('foo', array($listener, 'listenToFooBis'));
  51. $dispatcher->connect('foo', array($listener, 'listenToFoo'));
  52. $dispatcher->notify(new Event(new \stdClass(), 'foo'));
  53. $this->assertEquals('listenToFooBislistenToFoo', $listener->getValue(), '->notify() notifies all registered listeners in order');
  54. }
  55. public function testNotifyUntil()
  56. {
  57. $listener = new Listener();
  58. $dispatcher = new EventDispatcher();
  59. $dispatcher->connect('foo', array($listener, 'listenToFoo'));
  60. $dispatcher->connect('foo', array($listener, 'listenToFooBis'));
  61. $e = $dispatcher->notifyUntil($event = new Event(new \stdClass(), 'foo'));
  62. $this->assertEquals('listenToFoolistenToFooBis', $listener->getValue(), '->notifyUntil() notifies all registered listeners in order and stops if it returns true');
  63. $this->assertEquals($event, $e, '->notifyUntil() returns the event object');
  64. $listener->reset();
  65. $dispatcher = new EventDispatcher();
  66. $dispatcher->connect('foo', array($listener, 'listenToFooBis'));
  67. $dispatcher->connect('foo', array($listener, 'listenToFoo'));
  68. $e = $dispatcher->notifyUntil($event = new Event(new \stdClass(), 'foo'));
  69. $this->assertEquals('listenToFooBis', $listener->getValue(), '->notifyUntil() notifies all registered listeners in order and stops if it returns true');
  70. }
  71. public function testFilter()
  72. {
  73. $listener = new Listener();
  74. $dispatcher = new EventDispatcher();
  75. $dispatcher->connect('foo', array($listener, 'filterFoo'));
  76. $dispatcher->connect('foo', array($listener, 'filterFooBis'));
  77. $e = $dispatcher->filter($event = new Event(new \stdClass(), 'foo'), 'foo');
  78. $this->assertEquals('-*foo*-', $e->getReturnValue(), '->filter() filters a value');
  79. $this->assertEquals($event, $e, '->filter() returns the event object');
  80. $listener->reset();
  81. $dispatcher = new EventDispatcher();
  82. $dispatcher->connect('foo', array($listener, 'filterFooBis'));
  83. $dispatcher->connect('foo', array($listener, 'filterFoo'));
  84. $e = $dispatcher->filter($event = new Event(new \stdClass(), 'foo'), 'foo');
  85. $this->assertEquals('*-foo-*', $e->getReturnValue(), '->filter() filters a value');
  86. }
  87. }
  88. class Listener
  89. {
  90. protected
  91. $value = '';
  92. function filterFoo(Event $event, $foo)
  93. {
  94. return "*$foo*";
  95. }
  96. function filterFooBis(Event $event, $foo)
  97. {
  98. return "-$foo-";
  99. }
  100. function listenToFoo(Event $event)
  101. {
  102. $this->value .= 'listenToFoo';
  103. }
  104. function listenToFooBis(Event $event)
  105. {
  106. $this->value .= 'listenToFooBis';
  107. return true;
  108. }
  109. function getValue()
  110. {
  111. return $this->value;
  112. }
  113. function reset()
  114. {
  115. $this->value = '';
  116. }
  117. }