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