No Description

AbstractEventDispatcherTest.php 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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\Component\EventDispatcher\Tests;
  11. use Symfony\Component\EventDispatcher\Event;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. abstract class AbstractEventDispatcherTest extends \PHPUnit_Framework_TestCase
  14. {
  15. /* Some pseudo events */
  16. const preFoo = 'pre.foo';
  17. const postFoo = 'post.foo';
  18. const preBar = 'pre.bar';
  19. const postBar = 'post.bar';
  20. /**
  21. * @var EventDispatcher
  22. */
  23. private $dispatcher;
  24. private $listener;
  25. protected function setUp()
  26. {
  27. $this->dispatcher = $this->createEventDispatcher();
  28. $this->listener = new TestEventListener();
  29. }
  30. protected function tearDown()
  31. {
  32. $this->dispatcher = null;
  33. $this->listener = null;
  34. }
  35. abstract protected function createEventDispatcher();
  36. public function testInitialState()
  37. {
  38. $this->assertEquals(array(), $this->dispatcher->getListeners());
  39. $this->assertFalse($this->dispatcher->hasListeners(self::preFoo));
  40. $this->assertFalse($this->dispatcher->hasListeners(self::postFoo));
  41. }
  42. public function testAddListener()
  43. {
  44. $this->dispatcher->addListener('pre.foo', array($this->listener, 'preFoo'));
  45. $this->dispatcher->addListener('post.foo', array($this->listener, 'postFoo'));
  46. $this->assertTrue($this->dispatcher->hasListeners(self::preFoo));
  47. $this->assertTrue($this->dispatcher->hasListeners(self::postFoo));
  48. $this->assertCount(1, $this->dispatcher->getListeners(self::preFoo));
  49. $this->assertCount(1, $this->dispatcher->getListeners(self::postFoo));
  50. $this->assertCount(2, $this->dispatcher->getListeners());
  51. }
  52. public function testGetListenersSortsByPriority()
  53. {
  54. $listener1 = new TestEventListener();
  55. $listener2 = new TestEventListener();
  56. $listener3 = new TestEventListener();
  57. $listener1->name = '1';
  58. $listener2->name = '2';
  59. $listener3->name = '3';
  60. $this->dispatcher->addListener('pre.foo', array($listener1, 'preFoo'), -10);
  61. $this->dispatcher->addListener('pre.foo', array($listener2, 'preFoo'), 10);
  62. $this->dispatcher->addListener('pre.foo', array($listener3, 'preFoo'));
  63. $expected = array(
  64. array($listener2, 'preFoo'),
  65. array($listener3, 'preFoo'),
  66. array($listener1, 'preFoo'),
  67. );
  68. $this->assertSame($expected, $this->dispatcher->getListeners('pre.foo'));
  69. }
  70. public function testGetAllListenersSortsByPriority()
  71. {
  72. $listener1 = new TestEventListener();
  73. $listener2 = new TestEventListener();
  74. $listener3 = new TestEventListener();
  75. $listener4 = new TestEventListener();
  76. $listener5 = new TestEventListener();
  77. $listener6 = new TestEventListener();
  78. $this->dispatcher->addListener('pre.foo', $listener1, -10);
  79. $this->dispatcher->addListener('pre.foo', $listener2);
  80. $this->dispatcher->addListener('pre.foo', $listener3, 10);
  81. $this->dispatcher->addListener('post.foo', $listener4, -10);
  82. $this->dispatcher->addListener('post.foo', $listener5);
  83. $this->dispatcher->addListener('post.foo', $listener6, 10);
  84. $expected = array(
  85. 'pre.foo' => array($listener3, $listener2, $listener1),
  86. 'post.foo' => array($listener6, $listener5, $listener4),
  87. );
  88. $this->assertSame($expected, $this->dispatcher->getListeners());
  89. }
  90. public function testDispatch()
  91. {
  92. $this->dispatcher->addListener('pre.foo', array($this->listener, 'preFoo'));
  93. $this->dispatcher->addListener('post.foo', array($this->listener, 'postFoo'));
  94. $this->dispatcher->dispatch(self::preFoo);
  95. $this->assertTrue($this->listener->preFooInvoked);
  96. $this->assertFalse($this->listener->postFooInvoked);
  97. $this->assertInstanceOf('Symfony\Component\EventDispatcher\Event', $this->dispatcher->dispatch('noevent'));
  98. $this->assertInstanceOf('Symfony\Component\EventDispatcher\Event', $this->dispatcher->dispatch(self::preFoo));
  99. $event = new Event();
  100. $return = $this->dispatcher->dispatch(self::preFoo, $event);
  101. $this->assertEquals('pre.foo', $event->getName());
  102. $this->assertSame($event, $return);
  103. }
  104. public function testDispatchForClosure()
  105. {
  106. $invoked = 0;
  107. $listener = function () use (&$invoked) {
  108. $invoked++;
  109. };
  110. $this->dispatcher->addListener('pre.foo', $listener);
  111. $this->dispatcher->addListener('post.foo', $listener);
  112. $this->dispatcher->dispatch(self::preFoo);
  113. $this->assertEquals(1, $invoked);
  114. }
  115. public function testStopEventPropagation()
  116. {
  117. $otherListener = new TestEventListener();
  118. // postFoo() stops the propagation, so only one listener should
  119. // be executed
  120. // Manually set priority to enforce $this->listener to be called first
  121. $this->dispatcher->addListener('post.foo', array($this->listener, 'postFoo'), 10);
  122. $this->dispatcher->addListener('post.foo', array($otherListener, 'preFoo'));
  123. $this->dispatcher->dispatch(self::postFoo);
  124. $this->assertTrue($this->listener->postFooInvoked);
  125. $this->assertFalse($otherListener->postFooInvoked);
  126. }
  127. public function testDispatchByPriority()
  128. {
  129. $invoked = array();
  130. $listener1 = function () use (&$invoked) {
  131. $invoked[] = '1';
  132. };
  133. $listener2 = function () use (&$invoked) {
  134. $invoked[] = '2';
  135. };
  136. $listener3 = function () use (&$invoked) {
  137. $invoked[] = '3';
  138. };
  139. $this->dispatcher->addListener('pre.foo', $listener1, -10);
  140. $this->dispatcher->addListener('pre.foo', $listener2);
  141. $this->dispatcher->addListener('pre.foo', $listener3, 10);
  142. $this->dispatcher->dispatch(self::preFoo);
  143. $this->assertEquals(array('3', '2', '1'), $invoked);
  144. }
  145. public function testRemoveListener()
  146. {
  147. $this->dispatcher->addListener('pre.bar', $this->listener);
  148. $this->assertTrue($this->dispatcher->hasListeners(self::preBar));
  149. $this->dispatcher->removeListener('pre.bar', $this->listener);
  150. $this->assertFalse($this->dispatcher->hasListeners(self::preBar));
  151. $this->dispatcher->removeListener('notExists', $this->listener);
  152. }
  153. public function testAddSubscriber()
  154. {
  155. $eventSubscriber = new TestEventSubscriber();
  156. $this->dispatcher->addSubscriber($eventSubscriber);
  157. $this->assertTrue($this->dispatcher->hasListeners(self::preFoo));
  158. $this->assertTrue($this->dispatcher->hasListeners(self::postFoo));
  159. }
  160. public function testAddSubscriberWithPriorities()
  161. {
  162. $eventSubscriber = new TestEventSubscriber();
  163. $this->dispatcher->addSubscriber($eventSubscriber);
  164. $eventSubscriber = new TestEventSubscriberWithPriorities();
  165. $this->dispatcher->addSubscriber($eventSubscriber);
  166. $listeners = $this->dispatcher->getListeners('pre.foo');
  167. $this->assertTrue($this->dispatcher->hasListeners(self::preFoo));
  168. $this->assertCount(2, $listeners);
  169. $this->assertInstanceOf('Symfony\Component\EventDispatcher\Tests\TestEventSubscriberWithPriorities', $listeners[0][0]);
  170. }
  171. public function testAddSubscriberWithMultipleListeners()
  172. {
  173. $eventSubscriber = new TestEventSubscriberWithMultipleListeners();
  174. $this->dispatcher->addSubscriber($eventSubscriber);
  175. $listeners = $this->dispatcher->getListeners('pre.foo');
  176. $this->assertTrue($this->dispatcher->hasListeners(self::preFoo));
  177. $this->assertCount(2, $listeners);
  178. $this->assertEquals('preFoo2', $listeners[0][1]);
  179. }
  180. public function testRemoveSubscriber()
  181. {
  182. $eventSubscriber = new TestEventSubscriber();
  183. $this->dispatcher->addSubscriber($eventSubscriber);
  184. $this->assertTrue($this->dispatcher->hasListeners(self::preFoo));
  185. $this->assertTrue($this->dispatcher->hasListeners(self::postFoo));
  186. $this->dispatcher->removeSubscriber($eventSubscriber);
  187. $this->assertFalse($this->dispatcher->hasListeners(self::preFoo));
  188. $this->assertFalse($this->dispatcher->hasListeners(self::postFoo));
  189. }
  190. public function testRemoveSubscriberWithPriorities()
  191. {
  192. $eventSubscriber = new TestEventSubscriberWithPriorities();
  193. $this->dispatcher->addSubscriber($eventSubscriber);
  194. $this->assertTrue($this->dispatcher->hasListeners(self::preFoo));
  195. $this->dispatcher->removeSubscriber($eventSubscriber);
  196. $this->assertFalse($this->dispatcher->hasListeners(self::preFoo));
  197. }
  198. public function testRemoveSubscriberWithMultipleListeners()
  199. {
  200. $eventSubscriber = new TestEventSubscriberWithMultipleListeners();
  201. $this->dispatcher->addSubscriber($eventSubscriber);
  202. $this->assertTrue($this->dispatcher->hasListeners(self::preFoo));
  203. $this->assertCount(2, $this->dispatcher->getListeners(self::preFoo));
  204. $this->dispatcher->removeSubscriber($eventSubscriber);
  205. $this->assertFalse($this->dispatcher->hasListeners(self::preFoo));
  206. }
  207. public function testEventReceivesTheDispatcherInstance()
  208. {
  209. $dispatcher = null;
  210. $this->dispatcher->addListener('test', function ($event) use (&$dispatcher) {
  211. $dispatcher = $event->getDispatcher();
  212. });
  213. $this->dispatcher->dispatch('test');
  214. $this->assertSame($this->dispatcher, $dispatcher);
  215. }
  216. public function testEventReceivesTheDispatcherInstanceAsArgument()
  217. {
  218. $listener = new TestWithDispatcher();
  219. $this->dispatcher->addListener('test', array($listener, 'foo'));
  220. $this->assertNull($listener->name);
  221. $this->assertNull($listener->dispatcher);
  222. $this->dispatcher->dispatch('test');
  223. $this->assertEquals('test', $listener->name);
  224. $this->assertSame($this->dispatcher, $listener->dispatcher);
  225. }
  226. /**
  227. * @see https://bugs.php.net/bug.php?id=62976
  228. *
  229. * This bug affects:
  230. * - The PHP 5.3 branch for versions < 5.3.18
  231. * - The PHP 5.4 branch for versions < 5.4.8
  232. * - The PHP 5.5 branch is not affected
  233. */
  234. public function testWorkaroundForPhpBug62976()
  235. {
  236. $dispatcher = $this->createEventDispatcher();
  237. $dispatcher->addListener('bug.62976', new CallableClass());
  238. $dispatcher->removeListener('bug.62976', function () {});
  239. $this->assertTrue($dispatcher->hasListeners('bug.62976'));
  240. }
  241. public function testHasListenersWhenAddedCallbackListenerIsRemoved()
  242. {
  243. $listener = function () {};
  244. $this->dispatcher->addListener('foo', $listener);
  245. $this->dispatcher->removeListener('foo', $listener);
  246. $this->assertFalse($this->dispatcher->hasListeners());
  247. }
  248. public function testGetListenersWhenAddedCallbackListenerIsRemoved()
  249. {
  250. $listener = function () {};
  251. $this->dispatcher->addListener('foo', $listener);
  252. $this->dispatcher->removeListener('foo', $listener);
  253. $this->assertSame(array(), $this->dispatcher->getListeners());
  254. }
  255. public function testHasListenersWithoutEventsReturnsFalseAfterHasListenersWithEventHasBeenCalled()
  256. {
  257. $this->assertFalse($this->dispatcher->hasListeners('foo'));
  258. $this->assertFalse($this->dispatcher->hasListeners());
  259. }
  260. }
  261. class CallableClass
  262. {
  263. public function __invoke()
  264. {
  265. }
  266. }
  267. class TestEventListener
  268. {
  269. public $preFooInvoked = false;
  270. public $postFooInvoked = false;
  271. /* Listener methods */
  272. public function preFoo(Event $e)
  273. {
  274. $this->preFooInvoked = true;
  275. }
  276. public function postFoo(Event $e)
  277. {
  278. $this->postFooInvoked = true;
  279. $e->stopPropagation();
  280. }
  281. }
  282. class TestWithDispatcher
  283. {
  284. public $name;
  285. public $dispatcher;
  286. public function foo(Event $e, $name, $dispatcher)
  287. {
  288. $this->name = $name;
  289. $this->dispatcher = $dispatcher;
  290. }
  291. }
  292. class TestEventSubscriber implements EventSubscriberInterface
  293. {
  294. public static function getSubscribedEvents()
  295. {
  296. return array('pre.foo' => 'preFoo', 'post.foo' => 'postFoo');
  297. }
  298. }
  299. class TestEventSubscriberWithPriorities implements EventSubscriberInterface
  300. {
  301. public static function getSubscribedEvents()
  302. {
  303. return array(
  304. 'pre.foo' => array('preFoo', 10),
  305. 'post.foo' => array('postFoo'),
  306. );
  307. }
  308. }
  309. class TestEventSubscriberWithMultipleListeners implements EventSubscriberInterface
  310. {
  311. public static function getSubscribedEvents()
  312. {
  313. return array('pre.foo' => array(
  314. array('preFoo1'),
  315. array('preFoo2', 10),
  316. ));
  317. }
  318. }