BasicEmitterTest.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2018-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test\Hooks;
  8. /**
  9. * Class DummyEmitter
  10. *
  11. * class to make BasicEmitter::emit publicly available
  12. *
  13. * @package Test\Hooks
  14. */
  15. class DummyEmitter extends \OC\Hooks\BasicEmitter {
  16. public function emitEvent($scope, $method, $arguments = []) {
  17. $this->emit($scope, $method, $arguments);
  18. }
  19. }
  20. /**
  21. * Class EmittedException
  22. *
  23. * a dummy exception so we can check if an event is emitted
  24. *
  25. * @package Test\Hooks
  26. */
  27. class EmittedException extends \Exception {
  28. }
  29. class BasicEmitterTest extends \Test\TestCase {
  30. /**
  31. * @var \OC\Hooks\Emitter $emitter
  32. */
  33. protected $emitter;
  34. protected function setUp(): void {
  35. parent::setUp();
  36. $this->emitter = new DummyEmitter();
  37. }
  38. public function nonStaticCallBack() {
  39. throw new EmittedException;
  40. }
  41. public static function staticCallBack() {
  42. throw new EmittedException;
  43. }
  44. public function testAnonymousFunction(): void {
  45. $this->expectException(\Test\Hooks\EmittedException::class);
  46. $this->emitter->listen('Test', 'test', function () {
  47. throw new EmittedException;
  48. });
  49. $this->emitter->emitEvent('Test', 'test');
  50. }
  51. public function testStaticCallback(): void {
  52. $this->expectException(\Test\Hooks\EmittedException::class);
  53. $this->emitter->listen('Test', 'test', ['\Test\Hooks\BasicEmitterTest', 'staticCallBack']);
  54. $this->emitter->emitEvent('Test', 'test');
  55. }
  56. public function testNonStaticCallback(): void {
  57. $this->expectException(\Test\Hooks\EmittedException::class);
  58. $this->emitter->listen('Test', 'test', [$this, 'nonStaticCallBack']);
  59. $this->emitter->emitEvent('Test', 'test');
  60. }
  61. public function testOnlyCallOnce(): void {
  62. $count = 0;
  63. $listener = function () use (&$count) {
  64. $count++;
  65. };
  66. $this->emitter->listen('Test', 'test', $listener);
  67. $this->emitter->listen('Test', 'test', $listener);
  68. $this->emitter->emitEvent('Test', 'test');
  69. $this->assertEquals(1, $count, 'Listener called an invalid number of times (' . $count . ') expected 1');
  70. }
  71. public function testDifferentMethods(): void {
  72. $count = 0;
  73. $listener = function () use (&$count) {
  74. $count++;
  75. };
  76. $this->emitter->listen('Test', 'test', $listener);
  77. $this->emitter->listen('Test', 'foo', $listener);
  78. $this->emitter->emitEvent('Test', 'test');
  79. $this->emitter->emitEvent('Test', 'foo');
  80. $this->assertEquals(2, $count, 'Listener called an invalid number of times (' . $count . ') expected 2');
  81. }
  82. public function testDifferentScopes(): void {
  83. $count = 0;
  84. $listener = function () use (&$count) {
  85. $count++;
  86. };
  87. $this->emitter->listen('Test', 'test', $listener);
  88. $this->emitter->listen('Bar', 'test', $listener);
  89. $this->emitter->emitEvent('Test', 'test');
  90. $this->emitter->emitEvent('Bar', 'test');
  91. $this->assertEquals(2, $count, 'Listener called an invalid number of times (' . $count . ') expected 2');
  92. }
  93. public function testDifferentCallbacks(): void {
  94. $count = 0;
  95. $listener1 = function () use (&$count) {
  96. $count++;
  97. };
  98. $listener2 = function () use (&$count) {
  99. $count++;
  100. };
  101. $this->emitter->listen('Test', 'test', $listener1);
  102. $this->emitter->listen('Test', 'test', $listener2);
  103. $this->emitter->emitEvent('Test', 'test');
  104. $this->assertEquals(2, $count, 'Listener called an invalid number of times (' . $count . ') expected 2');
  105. }
  106. public function testArguments(): void {
  107. $this->expectException(\Test\Hooks\EmittedException::class);
  108. $this->emitter->listen('Test', 'test', function ($foo, $bar) {
  109. if ($foo == 'foo' and $bar == 'bar') {
  110. throw new EmittedException;
  111. }
  112. });
  113. $this->emitter->emitEvent('Test', 'test', ['foo', 'bar']);
  114. }
  115. public function testNamedArguments(): void {
  116. $this->expectException(\Test\Hooks\EmittedException::class);
  117. $this->emitter->listen('Test', 'test', function ($foo, $bar) {
  118. if ($foo == 'foo' and $bar == 'bar') {
  119. throw new EmittedException;
  120. }
  121. });
  122. $this->emitter->emitEvent('Test', 'test', ['foo' => 'foo', 'bar' => 'bar']);
  123. }
  124. public function testRemoveAllSpecified(): void {
  125. $listener = function () {
  126. throw new EmittedException;
  127. };
  128. $this->emitter->listen('Test', 'test', $listener);
  129. $this->emitter->removeListener('Test', 'test', $listener);
  130. $this->emitter->emitEvent('Test', 'test');
  131. $this->addToAssertionCount(1);
  132. }
  133. public function testRemoveWildcardListener(): void {
  134. $listener1 = function () {
  135. throw new EmittedException;
  136. };
  137. $listener2 = function () {
  138. throw new EmittedException;
  139. };
  140. $this->emitter->listen('Test', 'test', $listener1);
  141. $this->emitter->listen('Test', 'test', $listener2);
  142. $this->emitter->removeListener('Test', 'test');
  143. $this->emitter->emitEvent('Test', 'test');
  144. $this->addToAssertionCount(1);
  145. }
  146. public function testRemoveWildcardMethod(): void {
  147. $listener = function () {
  148. throw new EmittedException;
  149. };
  150. $this->emitter->listen('Test', 'test', $listener);
  151. $this->emitter->listen('Test', 'foo', $listener);
  152. $this->emitter->removeListener('Test', null, $listener);
  153. $this->emitter->emitEvent('Test', 'test');
  154. $this->emitter->emitEvent('Test', 'foo');
  155. $this->addToAssertionCount(1);
  156. }
  157. public function testRemoveWildcardScope(): void {
  158. $listener = function () {
  159. throw new EmittedException;
  160. };
  161. $this->emitter->listen('Test', 'test', $listener);
  162. $this->emitter->listen('Bar', 'test', $listener);
  163. $this->emitter->removeListener(null, 'test', $listener);
  164. $this->emitter->emitEvent('Test', 'test');
  165. $this->emitter->emitEvent('Bar', 'test');
  166. $this->addToAssertionCount(1);
  167. }
  168. public function testRemoveWildcardScopeAndMethod(): void {
  169. $listener = function () {
  170. throw new EmittedException;
  171. };
  172. $this->emitter->listen('Test', 'test', $listener);
  173. $this->emitter->listen('Test', 'foo', $listener);
  174. $this->emitter->listen('Bar', 'foo', $listener);
  175. $this->emitter->removeListener(null, null, $listener);
  176. $this->emitter->emitEvent('Test', 'test');
  177. $this->emitter->emitEvent('Test', 'foo');
  178. $this->emitter->emitEvent('Bar', 'foo');
  179. $this->addToAssertionCount(1);
  180. }
  181. public function testRemoveKeepOtherCallback(): void {
  182. $this->expectException(\Test\Hooks\EmittedException::class);
  183. $listener1 = function () {
  184. throw new EmittedException;
  185. };
  186. $listener2 = function () {
  187. throw new EmittedException;
  188. };
  189. $this->emitter->listen('Test', 'test', $listener1);
  190. $this->emitter->listen('Test', 'test', $listener2);
  191. $this->emitter->removeListener('Test', 'test', $listener1);
  192. $this->emitter->emitEvent('Test', 'test');
  193. $this->addToAssertionCount(1);
  194. }
  195. public function testRemoveKeepOtherMethod(): void {
  196. $this->expectException(\Test\Hooks\EmittedException::class);
  197. $listener = function () {
  198. throw new EmittedException;
  199. };
  200. $this->emitter->listen('Test', 'test', $listener);
  201. $this->emitter->listen('Test', 'foo', $listener);
  202. $this->emitter->removeListener('Test', 'foo', $listener);
  203. $this->emitter->emitEvent('Test', 'test');
  204. $this->addToAssertionCount(1);
  205. }
  206. public function testRemoveKeepOtherScope(): void {
  207. $this->expectException(\Test\Hooks\EmittedException::class);
  208. $listener = function () {
  209. throw new EmittedException;
  210. };
  211. $this->emitter->listen('Test', 'test', $listener);
  212. $this->emitter->listen('Bar', 'test', $listener);
  213. $this->emitter->removeListener('Bar', 'test', $listener);
  214. $this->emitter->emitEvent('Test', 'test');
  215. $this->addToAssertionCount(1);
  216. }
  217. public function testRemoveNonExistingName(): void {
  218. $this->expectException(\Test\Hooks\EmittedException::class);
  219. $listener = function () {
  220. throw new EmittedException;
  221. };
  222. $this->emitter->listen('Test', 'test', $listener);
  223. $this->emitter->removeListener('Bar', 'test', $listener);
  224. $this->emitter->emitEvent('Test', 'test');
  225. $this->addToAssertionCount(1);
  226. }
  227. }