AbstractEventDispatcherTest.php 14 KB

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