EventEmitterTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <?php
  2. /*
  3. * This file is part of Evenement.
  4. *
  5. * (c) Igor Wiedler <igor@wiedler.ch>
  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 Evenement\Tests;
  11. use Evenement\EventEmitter;
  12. use PHPUnit\Framework\TestCase;
  13. class EventEmitterTest extends TestCase
  14. {
  15. private $emitter;
  16. public function setUp()
  17. {
  18. $this->emitter = new EventEmitter();
  19. }
  20. public function testAddListenerWithLambda()
  21. {
  22. $this->emitter->on('foo', function () {});
  23. }
  24. public function testAddListenerWithMethod()
  25. {
  26. $listener = new Listener();
  27. $this->emitter->on('foo', [$listener, 'onFoo']);
  28. }
  29. public function testAddListenerWithStaticMethod()
  30. {
  31. $this->emitter->on('bar', ['Evenement\Tests\Listener', 'onBar']);
  32. }
  33. public function testAddListenerWithInvalidListener()
  34. {
  35. try {
  36. $this->emitter->on('foo', 'not a callable');
  37. $this->fail();
  38. } catch (\Exception $e) {
  39. } catch (\TypeError $e) {
  40. }
  41. }
  42. public function testOnce()
  43. {
  44. $listenerCalled = 0;
  45. $this->emitter->once('foo', function () use (&$listenerCalled) {
  46. $listenerCalled++;
  47. });
  48. $this->assertSame(0, $listenerCalled);
  49. $this->emitter->emit('foo');
  50. $this->assertSame(1, $listenerCalled);
  51. $this->emitter->emit('foo');
  52. $this->assertSame(1, $listenerCalled);
  53. }
  54. public function testOnceWithArguments()
  55. {
  56. $capturedArgs = [];
  57. $this->emitter->once('foo', function ($a, $b) use (&$capturedArgs) {
  58. $capturedArgs = array($a, $b);
  59. });
  60. $this->emitter->emit('foo', array('a', 'b'));
  61. $this->assertSame(array('a', 'b'), $capturedArgs);
  62. }
  63. public function testEmitWithoutArguments()
  64. {
  65. $listenerCalled = false;
  66. $this->emitter->on('foo', function () use (&$listenerCalled) {
  67. $listenerCalled = true;
  68. });
  69. $this->assertSame(false, $listenerCalled);
  70. $this->emitter->emit('foo');
  71. $this->assertSame(true, $listenerCalled);
  72. }
  73. public function testEmitWithOneArgument()
  74. {
  75. $test = $this;
  76. $listenerCalled = false;
  77. $this->emitter->on('foo', function ($value) use (&$listenerCalled, $test) {
  78. $listenerCalled = true;
  79. $test->assertSame('bar', $value);
  80. });
  81. $this->assertSame(false, $listenerCalled);
  82. $this->emitter->emit('foo', ['bar']);
  83. $this->assertSame(true, $listenerCalled);
  84. }
  85. public function testEmitWithTwoArguments()
  86. {
  87. $test = $this;
  88. $listenerCalled = false;
  89. $this->emitter->on('foo', function ($arg1, $arg2) use (&$listenerCalled, $test) {
  90. $listenerCalled = true;
  91. $test->assertSame('bar', $arg1);
  92. $test->assertSame('baz', $arg2);
  93. });
  94. $this->assertSame(false, $listenerCalled);
  95. $this->emitter->emit('foo', ['bar', 'baz']);
  96. $this->assertSame(true, $listenerCalled);
  97. }
  98. public function testEmitWithNoListeners()
  99. {
  100. $this->emitter->emit('foo');
  101. $this->emitter->emit('foo', ['bar']);
  102. $this->emitter->emit('foo', ['bar', 'baz']);
  103. }
  104. public function testEmitWithTwoListeners()
  105. {
  106. $listenersCalled = 0;
  107. $this->emitter->on('foo', function () use (&$listenersCalled) {
  108. $listenersCalled++;
  109. });
  110. $this->emitter->on('foo', function () use (&$listenersCalled) {
  111. $listenersCalled++;
  112. });
  113. $this->assertSame(0, $listenersCalled);
  114. $this->emitter->emit('foo');
  115. $this->assertSame(2, $listenersCalled);
  116. }
  117. public function testRemoveListenerMatching()
  118. {
  119. $listenersCalled = 0;
  120. $listener = function () use (&$listenersCalled) {
  121. $listenersCalled++;
  122. };
  123. $this->emitter->on('foo', $listener);
  124. $this->emitter->removeListener('foo', $listener);
  125. $this->assertSame(0, $listenersCalled);
  126. $this->emitter->emit('foo');
  127. $this->assertSame(0, $listenersCalled);
  128. }
  129. public function testRemoveListenerNotMatching()
  130. {
  131. $listenersCalled = 0;
  132. $listener = function () use (&$listenersCalled) {
  133. $listenersCalled++;
  134. };
  135. $this->emitter->on('foo', $listener);
  136. $this->emitter->removeListener('bar', $listener);
  137. $this->assertSame(0, $listenersCalled);
  138. $this->emitter->emit('foo');
  139. $this->assertSame(1, $listenersCalled);
  140. }
  141. public function testRemoveAllListenersMatching()
  142. {
  143. $listenersCalled = 0;
  144. $this->emitter->on('foo', function () use (&$listenersCalled) {
  145. $listenersCalled++;
  146. });
  147. $this->emitter->removeAllListeners('foo');
  148. $this->assertSame(0, $listenersCalled);
  149. $this->emitter->emit('foo');
  150. $this->assertSame(0, $listenersCalled);
  151. }
  152. public function testRemoveAllListenersNotMatching()
  153. {
  154. $listenersCalled = 0;
  155. $this->emitter->on('foo', function () use (&$listenersCalled) {
  156. $listenersCalled++;
  157. });
  158. $this->emitter->removeAllListeners('bar');
  159. $this->assertSame(0, $listenersCalled);
  160. $this->emitter->emit('foo');
  161. $this->assertSame(1, $listenersCalled);
  162. }
  163. public function testRemoveAllListenersWithoutArguments()
  164. {
  165. $listenersCalled = 0;
  166. $this->emitter->on('foo', function () use (&$listenersCalled) {
  167. $listenersCalled++;
  168. });
  169. $this->emitter->on('bar', function () use (&$listenersCalled) {
  170. $listenersCalled++;
  171. });
  172. $this->emitter->removeAllListeners();
  173. $this->assertSame(0, $listenersCalled);
  174. $this->emitter->emit('foo');
  175. $this->emitter->emit('bar');
  176. $this->assertSame(0, $listenersCalled);
  177. }
  178. public function testCallablesClosure()
  179. {
  180. $calledWith = null;
  181. $this->emitter->on('foo', function ($data) use (&$calledWith) {
  182. $calledWith = $data;
  183. });
  184. $this->emitter->emit('foo', ['bar']);
  185. self::assertSame('bar', $calledWith);
  186. }
  187. public function testCallablesClass()
  188. {
  189. $listener = new Listener();
  190. $this->emitter->on('foo', [$listener, 'onFoo']);
  191. $this->emitter->emit('foo', ['bar']);
  192. self::assertSame(['bar'], $listener->getData());
  193. }
  194. public function testCallablesClassInvoke()
  195. {
  196. $listener = new Listener();
  197. $this->emitter->on('foo', $listener);
  198. $this->emitter->emit('foo', ['bar']);
  199. self::assertSame(['bar'], $listener->getMagicData());
  200. }
  201. public function testCallablesStaticClass()
  202. {
  203. $this->emitter->on('foo', '\Evenement\Tests\Listener::onBar');
  204. $this->emitter->emit('foo', ['bar']);
  205. self::assertSame(['bar'], Listener::getStaticData());
  206. }
  207. public function testCallablesFunction()
  208. {
  209. $this->emitter->on('foo', '\Evenement\Tests\setGlobalTestData');
  210. $this->emitter->emit('foo', ['bar']);
  211. self::assertSame('bar', $GLOBALS['evenement-evenement-test-data']);
  212. unset($GLOBALS['evenement-evenement-test-data']);
  213. }
  214. }