EventDispatcherTest.php 5.7 KB

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