BasicEmitterTest.php 7.6 KB

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