AbstractEventDispatcherTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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 testDispatch()
  92. {
  93. $this->dispatcher->addListener('pre.foo', array($this->listener, 'preFoo'));
  94. $this->dispatcher->addListener('post.foo', array($this->listener, 'postFoo'));
  95. $this->dispatcher->dispatch(self::preFoo);
  96. $this->assertTrue($this->listener->preFooInvoked);
  97. $this->assertFalse($this->listener->postFooInvoked);
  98. $this->assertInstanceOf('Symfony\Component\EventDispatcher\Event', $this->dispatcher->dispatch('noevent'));
  99. $this->assertInstanceOf('Symfony\Component\EventDispatcher\Event', $this->dispatcher->dispatch(self::preFoo));
  100. $event = new Event();
  101. $return = $this->dispatcher->dispatch(self::preFoo, $event);
  102. $this->assertSame($event, $return);
  103. }
  104. /**
  105. * @group legacy
  106. */
  107. public function testLegacyDispatch()
  108. {
  109. $event = new Event();
  110. $return = $this->dispatcher->dispatch(self::preFoo, $event);
  111. $this->assertEquals('pre.foo', $event->getName());
  112. }
  113. public function testDispatchForClosure()
  114. {
  115. $invoked = 0;
  116. $listener = function () use (&$invoked) {
  117. ++$invoked;
  118. };
  119. $this->dispatcher->addListener('pre.foo', $listener);
  120. $this->dispatcher->addListener('post.foo', $listener);
  121. $this->dispatcher->dispatch(self::preFoo);
  122. $this->assertEquals(1, $invoked);
  123. }
  124. public function testStopEventPropagation()
  125. {
  126. $otherListener = new TestEventListener();
  127. // postFoo() stops the propagation, so only one listener should
  128. // be executed
  129. // Manually set priority to enforce $this->listener to be called first
  130. $this->dispatcher->addListener('post.foo', array($this->listener, 'postFoo'), 10);
  131. $this->dispatcher->addListener('post.foo', array($otherListener, 'preFoo'));
  132. $this->dispatcher->dispatch(self::postFoo);
  133. $this->assertTrue($this->listener->postFooInvoked);
  134. $this->assertFalse($otherListener->postFooInvoked);
  135. }
  136. public function testDispatchByPriority()
  137. {
  138. $invoked = array();
  139. $listener1 = function () use (&$invoked) {
  140. $invoked[] = '1';
  141. };
  142. $listener2 = function () use (&$invoked) {
  143. $invoked[] = '2';
  144. };
  145. $listener3 = function () use (&$invoked) {
  146. $invoked[] = '3';
  147. };
  148. $this->dispatcher->addListener('pre.foo', $listener1, -10);
  149. $this->dispatcher->addListener('pre.foo', $listener2);
  150. $this->dispatcher->addListener('pre.foo', $listener3, 10);
  151. $this->dispatcher->dispatch(self::preFoo);
  152. $this->assertEquals(array('3', '2', '1'), $invoked);
  153. }
  154. public function testRemoveListener()
  155. {
  156. $this->dispatcher->addListener('pre.bar', $this->listener);
  157. $this->assertTrue($this->dispatcher->hasListeners(self::preBar));
  158. $this->dispatcher->removeListener('pre.bar', $this->listener);
  159. $this->assertFalse($this->dispatcher->hasListeners(self::preBar));
  160. $this->dispatcher->removeListener('notExists', $this->listener);
  161. }
  162. public function testAddSubscriber()
  163. {
  164. $eventSubscriber = new TestEventSubscriber();
  165. $this->dispatcher->addSubscriber($eventSubscriber);
  166. $this->assertTrue($this->dispatcher->hasListeners(self::preFoo));
  167. $this->assertTrue($this->dispatcher->hasListeners(self::postFoo));
  168. }
  169. public function testAddSubscriberWithPriorities()
  170. {
  171. $eventSubscriber = new TestEventSubscriber();
  172. $this->dispatcher->addSubscriber($eventSubscriber);
  173. $eventSubscriber = new TestEventSubscriberWithPriorities();
  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->assertInstanceOf('Symfony\Component\EventDispatcher\Tests\TestEventSubscriberWithPriorities', $listeners[0][0]);
  179. }
  180. public function testAddSubscriberWithMultipleListeners()
  181. {
  182. $eventSubscriber = new TestEventSubscriberWithMultipleListeners();
  183. $this->dispatcher->addSubscriber($eventSubscriber);
  184. $listeners = $this->dispatcher->getListeners('pre.foo');
  185. $this->assertTrue($this->dispatcher->hasListeners(self::preFoo));
  186. $this->assertCount(2, $listeners);
  187. $this->assertEquals('preFoo2', $listeners[0][1]);
  188. }
  189. public function testRemoveSubscriber()
  190. {
  191. $eventSubscriber = new TestEventSubscriber();
  192. $this->dispatcher->addSubscriber($eventSubscriber);
  193. $this->assertTrue($this->dispatcher->hasListeners(self::preFoo));
  194. $this->assertTrue($this->dispatcher->hasListeners(self::postFoo));
  195. $this->dispatcher->removeSubscriber($eventSubscriber);
  196. $this->assertFalse($this->dispatcher->hasListeners(self::preFoo));
  197. $this->assertFalse($this->dispatcher->hasListeners(self::postFoo));
  198. }
  199. public function testRemoveSubscriberWithPriorities()
  200. {
  201. $eventSubscriber = new TestEventSubscriberWithPriorities();
  202. $this->dispatcher->addSubscriber($eventSubscriber);
  203. $this->assertTrue($this->dispatcher->hasListeners(self::preFoo));
  204. $this->dispatcher->removeSubscriber($eventSubscriber);
  205. $this->assertFalse($this->dispatcher->hasListeners(self::preFoo));
  206. }
  207. public function testRemoveSubscriberWithMultipleListeners()
  208. {
  209. $eventSubscriber = new TestEventSubscriberWithMultipleListeners();
  210. $this->dispatcher->addSubscriber($eventSubscriber);
  211. $this->assertTrue($this->dispatcher->hasListeners(self::preFoo));
  212. $this->assertCount(2, $this->dispatcher->getListeners(self::preFoo));
  213. $this->dispatcher->removeSubscriber($eventSubscriber);
  214. $this->assertFalse($this->dispatcher->hasListeners(self::preFoo));
  215. }
  216. /**
  217. * @group legacy
  218. */
  219. public function testLegacyEventReceivesTheDispatcherInstance()
  220. {
  221. $dispatcher = null;
  222. $this->dispatcher->addListener('test', function ($event) use (&$dispatcher) {
  223. $dispatcher = $event->getDispatcher();
  224. });
  225. $this->dispatcher->dispatch('test');
  226. $this->assertSame($this->dispatcher, $dispatcher);
  227. }
  228. public function testEventReceivesTheDispatcherInstanceAsArgument()
  229. {
  230. $listener = new TestWithDispatcher();
  231. $this->dispatcher->addListener('test', array($listener, 'foo'));
  232. $this->assertNull($listener->name);
  233. $this->assertNull($listener->dispatcher);
  234. $this->dispatcher->dispatch('test');
  235. $this->assertEquals('test', $listener->name);
  236. $this->assertSame($this->dispatcher, $listener->dispatcher);
  237. }
  238. /**
  239. * @see https://bugs.php.net/bug.php?id=62976
  240. *
  241. * This bug affects:
  242. * - The PHP 5.3 branch for versions < 5.3.18
  243. * - The PHP 5.4 branch for versions < 5.4.8
  244. * - The PHP 5.5 branch is not affected
  245. */
  246. public function testWorkaroundForPhpBug62976()
  247. {
  248. $dispatcher = $this->createEventDispatcher();
  249. $dispatcher->addListener('bug.62976', new CallableClass());
  250. $dispatcher->removeListener('bug.62976', function () {});
  251. $this->assertTrue($dispatcher->hasListeners('bug.62976'));
  252. }
  253. public function testHasListenersWhenAddedCallbackListenerIsRemoved()
  254. {
  255. $listener = function () {};
  256. $this->dispatcher->addListener('foo', $listener);
  257. $this->dispatcher->removeListener('foo', $listener);
  258. $this->assertFalse($this->dispatcher->hasListeners());
  259. }
  260. public function testGetListenersWhenAddedCallbackListenerIsRemoved()
  261. {
  262. $listener = function () {};
  263. $this->dispatcher->addListener('foo', $listener);
  264. $this->dispatcher->removeListener('foo', $listener);
  265. $this->assertSame(array(), $this->dispatcher->getListeners());
  266. }
  267. public function testHasListenersWithoutEventsReturnsFalseAfterHasListenersWithEventHasBeenCalled()
  268. {
  269. $this->assertFalse($this->dispatcher->hasListeners('foo'));
  270. $this->assertFalse($this->dispatcher->hasListeners());
  271. }
  272. }
  273. class CallableClass
  274. {
  275. public function __invoke()
  276. {
  277. }
  278. }
  279. class TestEventListener
  280. {
  281. public $preFooInvoked = false;
  282. public $postFooInvoked = false;
  283. /* Listener methods */
  284. public function preFoo(Event $e)
  285. {
  286. $this->preFooInvoked = true;
  287. }
  288. public function postFoo(Event $e)
  289. {
  290. $this->postFooInvoked = true;
  291. $e->stopPropagation();
  292. }
  293. }
  294. class TestWithDispatcher
  295. {
  296. public $name;
  297. public $dispatcher;
  298. public function foo(Event $e, $name, $dispatcher)
  299. {
  300. $this->name = $name;
  301. $this->dispatcher = $dispatcher;
  302. }
  303. }
  304. class TestEventSubscriber implements EventSubscriberInterface
  305. {
  306. public static function getSubscribedEvents()
  307. {
  308. return array('pre.foo' => 'preFoo', 'post.foo' => 'postFoo');
  309. }
  310. }
  311. class TestEventSubscriberWithPriorities implements EventSubscriberInterface
  312. {
  313. public static function getSubscribedEvents()
  314. {
  315. return array(
  316. 'pre.foo' => array('preFoo', 10),
  317. 'post.foo' => array('postFoo'),
  318. );
  319. }
  320. }
  321. class TestEventSubscriberWithMultipleListeners implements EventSubscriberInterface
  322. {
  323. public static function getSubscribedEvents()
  324. {
  325. return array('pre.foo' => array(
  326. array('preFoo1'),
  327. array('preFoo2', 10),
  328. ));
  329. }
  330. }