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