Drupal investigation

AbstractEventDispatcherTest.php 14KB

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