BasicEmitterTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 = array()) {
  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() {
  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. /**
  46. * @expectedException \Test\Hooks\EmittedException
  47. */
  48. public function testAnonymousFunction() {
  49. $this->emitter->listen('Test', 'test', function () {
  50. throw new EmittedException;
  51. });
  52. $this->emitter->emitEvent('Test', 'test');
  53. }
  54. /**
  55. * @expectedException \Test\Hooks\EmittedException
  56. */
  57. public function testStaticCallback() {
  58. $this->emitter->listen('Test', 'test', array('\Test\Hooks\BasicEmitterTest', 'staticCallBack'));
  59. $this->emitter->emitEvent('Test', 'test');
  60. }
  61. /**
  62. * @expectedException \Test\Hooks\EmittedException
  63. */
  64. public function testNonStaticCallback() {
  65. $this->emitter->listen('Test', 'test', array($this, 'nonStaticCallBack'));
  66. $this->emitter->emitEvent('Test', 'test');
  67. }
  68. public function testOnlyCallOnce() {
  69. $count = 0;
  70. $listener = function () use (&$count) {
  71. $count++;
  72. };
  73. $this->emitter->listen('Test', 'test', $listener);
  74. $this->emitter->listen('Test', 'test', $listener);
  75. $this->emitter->emitEvent('Test', 'test');
  76. $this->assertEquals(1, $count, 'Listener called an invalid number of times (' . $count . ') expected 1');
  77. }
  78. public function testDifferentMethods() {
  79. $count = 0;
  80. $listener = function () use (&$count) {
  81. $count++;
  82. };
  83. $this->emitter->listen('Test', 'test', $listener);
  84. $this->emitter->listen('Test', 'foo', $listener);
  85. $this->emitter->emitEvent('Test', 'test');
  86. $this->emitter->emitEvent('Test', 'foo');
  87. $this->assertEquals(2, $count, 'Listener called an invalid number of times (' . $count . ') expected 2');
  88. }
  89. public function testDifferentScopes() {
  90. $count = 0;
  91. $listener = function () use (&$count) {
  92. $count++;
  93. };
  94. $this->emitter->listen('Test', 'test', $listener);
  95. $this->emitter->listen('Bar', 'test', $listener);
  96. $this->emitter->emitEvent('Test', 'test');
  97. $this->emitter->emitEvent('Bar', 'test');
  98. $this->assertEquals(2, $count, 'Listener called an invalid number of times (' . $count . ') expected 2');
  99. }
  100. public function testDifferentCallbacks() {
  101. $count = 0;
  102. $listener1 = function () use (&$count) {
  103. $count++;
  104. };
  105. $listener2 = function () use (&$count) {
  106. $count++;
  107. };
  108. $this->emitter->listen('Test', 'test', $listener1);
  109. $this->emitter->listen('Test', 'test', $listener2);
  110. $this->emitter->emitEvent('Test', 'test');
  111. $this->assertEquals(2, $count, 'Listener called an invalid number of times (' . $count . ') expected 2');
  112. }
  113. /**
  114. * @expectedException \Test\Hooks\EmittedException
  115. */
  116. public function testArguments() {
  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', array('foo', 'bar'));
  123. }
  124. /**
  125. * @expectedException \Test\Hooks\EmittedException
  126. */
  127. public function testNamedArguments() {
  128. $this->emitter->listen('Test', 'test', function ($foo, $bar) {
  129. if ($foo == 'foo' and $bar == 'bar') {
  130. throw new EmittedException;
  131. }
  132. });
  133. $this->emitter->emitEvent('Test', 'test', array('foo' => 'foo', 'bar' => 'bar'));
  134. }
  135. public function testRemoveAllSpecified() {
  136. $listener = function () {
  137. throw new EmittedException;
  138. };
  139. $this->emitter->listen('Test', 'test', $listener);
  140. $this->emitter->removeListener('Test', 'test', $listener);
  141. $this->emitter->emitEvent('Test', 'test');
  142. $this->assertTrue(true);
  143. }
  144. public function testRemoveWildcardListener() {
  145. $listener1 = function () {
  146. throw new EmittedException;
  147. };
  148. $listener2 = function () {
  149. throw new EmittedException;
  150. };
  151. $this->emitter->listen('Test', 'test', $listener1);
  152. $this->emitter->listen('Test', 'test', $listener2);
  153. $this->emitter->removeListener('Test', 'test');
  154. $this->emitter->emitEvent('Test', 'test');
  155. $this->assertTrue(true);
  156. }
  157. public function testRemoveWildcardMethod() {
  158. $listener = function () {
  159. throw new EmittedException;
  160. };
  161. $this->emitter->listen('Test', 'test', $listener);
  162. $this->emitter->listen('Test', 'foo', $listener);
  163. $this->emitter->removeListener('Test', null, $listener);
  164. $this->emitter->emitEvent('Test', 'test');
  165. $this->emitter->emitEvent('Test', 'foo');
  166. $this->assertTrue(true);
  167. }
  168. public function testRemoveWildcardScope() {
  169. $listener = function () {
  170. throw new EmittedException;
  171. };
  172. $this->emitter->listen('Test', 'test', $listener);
  173. $this->emitter->listen('Bar', 'test', $listener);
  174. $this->emitter->removeListener(null, 'test', $listener);
  175. $this->emitter->emitEvent('Test', 'test');
  176. $this->emitter->emitEvent('Bar', 'test');
  177. $this->assertTrue(true);
  178. }
  179. public function testRemoveWildcardScopeAndMethod() {
  180. $listener = function () {
  181. throw new EmittedException;
  182. };
  183. $this->emitter->listen('Test', 'test', $listener);
  184. $this->emitter->listen('Test', 'foo', $listener);
  185. $this->emitter->listen('Bar', 'foo', $listener);
  186. $this->emitter->removeListener(null, null, $listener);
  187. $this->emitter->emitEvent('Test', 'test');
  188. $this->emitter->emitEvent('Test', 'foo');
  189. $this->emitter->emitEvent('Bar', 'foo');
  190. $this->assertTrue(true);
  191. }
  192. /**
  193. * @expectedException \Test\Hooks\EmittedException
  194. */
  195. public function testRemoveKeepOtherCallback() {
  196. $listener1 = function () {
  197. throw new EmittedException;
  198. };
  199. $listener2 = function () {
  200. throw new EmittedException;
  201. };
  202. $this->emitter->listen('Test', 'test', $listener1);
  203. $this->emitter->listen('Test', 'test', $listener2);
  204. $this->emitter->removeListener('Test', 'test', $listener1);
  205. $this->emitter->emitEvent('Test', 'test');
  206. $this->assertTrue(true);
  207. }
  208. /**
  209. * @expectedException \Test\Hooks\EmittedException
  210. */
  211. public function testRemoveKeepOtherMethod() {
  212. $listener = function () {
  213. throw new EmittedException;
  214. };
  215. $this->emitter->listen('Test', 'test', $listener);
  216. $this->emitter->listen('Test', 'foo', $listener);
  217. $this->emitter->removeListener('Test', 'foo', $listener);
  218. $this->emitter->emitEvent('Test', 'test');
  219. $this->assertTrue(true);
  220. }
  221. /**
  222. * @expectedException \Test\Hooks\EmittedException
  223. */
  224. public function testRemoveKeepOtherScope() {
  225. $listener = function () {
  226. throw new EmittedException;
  227. };
  228. $this->emitter->listen('Test', 'test', $listener);
  229. $this->emitter->listen('Bar', 'test', $listener);
  230. $this->emitter->removeListener('Bar', 'test', $listener);
  231. $this->emitter->emitEvent('Test', 'test');
  232. $this->assertTrue(true);
  233. }
  234. /**
  235. * @expectedException \Test\Hooks\EmittedException
  236. */
  237. public function testRemoveNonExistingName() {
  238. $listener = function () {
  239. throw new EmittedException;
  240. };
  241. $this->emitter->listen('Test', 'test', $listener);
  242. $this->emitter->removeListener('Bar', 'test', $listener);
  243. $this->emitter->emitEvent('Test', 'test');
  244. $this->assertTrue(true);
  245. }
  246. }