2013-05-07 18:34:09 +04:00
|
|
|
<?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 {
|
2020-03-26 11:30:18 +03:00
|
|
|
public function emitEvent($scope, $method, $arguments = []) {
|
2013-05-07 18:34:09 +04:00
|
|
|
$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 {
|
|
|
|
}
|
|
|
|
|
2016-05-20 16:38:20 +03:00
|
|
|
class BasicEmitterTest extends \Test\TestCase {
|
2013-05-07 18:34:09 +04:00
|
|
|
/**
|
|
|
|
* @var \OC\Hooks\Emitter $emitter
|
|
|
|
*/
|
|
|
|
protected $emitter;
|
|
|
|
|
2019-11-21 18:40:38 +03:00
|
|
|
protected function setUp(): void {
|
2014-11-11 01:30:38 +03:00
|
|
|
parent::setUp();
|
2013-05-07 18:34:09 +04:00
|
|
|
$this->emitter = new DummyEmitter();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function nonStaticCallBack() {
|
|
|
|
throw new EmittedException;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function staticCallBack() {
|
|
|
|
throw new EmittedException;
|
|
|
|
}
|
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
|
2013-05-07 18:34:09 +04:00
|
|
|
public function testAnonymousFunction() {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\Test\Hooks\EmittedException::class);
|
|
|
|
|
2013-05-07 18:34:09 +04:00
|
|
|
$this->emitter->listen('Test', 'test', function () {
|
|
|
|
throw new EmittedException;
|
|
|
|
});
|
|
|
|
$this->emitter->emitEvent('Test', 'test');
|
|
|
|
}
|
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
|
2013-05-07 18:34:09 +04:00
|
|
|
public function testStaticCallback() {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\Test\Hooks\EmittedException::class);
|
|
|
|
|
2020-03-26 11:30:18 +03:00
|
|
|
$this->emitter->listen('Test', 'test', ['\Test\Hooks\BasicEmitterTest', 'staticCallBack']);
|
2013-05-07 18:34:09 +04:00
|
|
|
$this->emitter->emitEvent('Test', 'test');
|
|
|
|
}
|
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
|
2013-05-07 18:34:09 +04:00
|
|
|
public function testNonStaticCallback() {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\Test\Hooks\EmittedException::class);
|
|
|
|
|
2020-03-26 11:30:18 +03:00
|
|
|
$this->emitter->listen('Test', 'test', [$this, 'nonStaticCallBack']);
|
2013-05-07 18:34:09 +04:00
|
|
|
$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');
|
|
|
|
}
|
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
|
2013-05-07 18:34:09 +04:00
|
|
|
public function testArguments() {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\Test\Hooks\EmittedException::class);
|
|
|
|
|
2013-05-07 18:34:09 +04:00
|
|
|
$this->emitter->listen('Test', 'test', function ($foo, $bar) {
|
|
|
|
if ($foo == 'foo' and $bar == 'bar') {
|
|
|
|
throw new EmittedException;
|
|
|
|
}
|
|
|
|
});
|
2020-03-26 11:30:18 +03:00
|
|
|
$this->emitter->emitEvent('Test', 'test', ['foo', 'bar']);
|
2013-05-07 18:34:09 +04:00
|
|
|
}
|
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
|
2013-05-07 18:34:09 +04:00
|
|
|
public function testNamedArguments() {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\Test\Hooks\EmittedException::class);
|
|
|
|
|
2013-05-07 18:34:09 +04:00
|
|
|
$this->emitter->listen('Test', 'test', function ($foo, $bar) {
|
|
|
|
if ($foo == 'foo' and $bar == 'bar') {
|
|
|
|
throw new EmittedException;
|
|
|
|
}
|
|
|
|
});
|
2020-03-26 11:30:18 +03:00
|
|
|
$this->emitter->emitEvent('Test', 'test', ['foo' => 'foo', 'bar' => 'bar']);
|
2013-05-07 18:34:09 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testRemoveAllSpecified() {
|
|
|
|
$listener = function () {
|
|
|
|
throw new EmittedException;
|
|
|
|
};
|
|
|
|
$this->emitter->listen('Test', 'test', $listener);
|
2013-05-10 00:52:44 +04:00
|
|
|
$this->emitter->removeListener('Test', 'test', $listener);
|
2013-05-07 18:34:09 +04:00
|
|
|
$this->emitter->emitEvent('Test', 'test');
|
2013-06-28 17:13:57 +04:00
|
|
|
|
2018-01-25 13:23:12 +03:00
|
|
|
$this->addToAssertionCount(1);
|
2013-05-07 18:34:09 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2013-05-10 00:52:44 +04:00
|
|
|
$this->emitter->removeListener('Test', 'test');
|
2013-05-07 18:34:09 +04:00
|
|
|
$this->emitter->emitEvent('Test', 'test');
|
2013-06-28 17:13:57 +04:00
|
|
|
|
2018-01-25 13:23:12 +03:00
|
|
|
$this->addToAssertionCount(1);
|
2013-05-07 18:34:09 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testRemoveWildcardMethod() {
|
|
|
|
$listener = function () {
|
|
|
|
throw new EmittedException;
|
|
|
|
};
|
|
|
|
$this->emitter->listen('Test', 'test', $listener);
|
|
|
|
$this->emitter->listen('Test', 'foo', $listener);
|
2013-05-10 00:52:44 +04:00
|
|
|
$this->emitter->removeListener('Test', null, $listener);
|
2013-05-07 18:34:09 +04:00
|
|
|
$this->emitter->emitEvent('Test', 'test');
|
|
|
|
$this->emitter->emitEvent('Test', 'foo');
|
2013-06-28 17:13:57 +04:00
|
|
|
|
2018-01-25 13:23:12 +03:00
|
|
|
$this->addToAssertionCount(1);
|
2013-05-07 18:34:09 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testRemoveWildcardScope() {
|
|
|
|
$listener = function () {
|
|
|
|
throw new EmittedException;
|
|
|
|
};
|
|
|
|
$this->emitter->listen('Test', 'test', $listener);
|
|
|
|
$this->emitter->listen('Bar', 'test', $listener);
|
2013-05-10 00:52:44 +04:00
|
|
|
$this->emitter->removeListener(null, 'test', $listener);
|
2013-05-07 18:34:09 +04:00
|
|
|
$this->emitter->emitEvent('Test', 'test');
|
|
|
|
$this->emitter->emitEvent('Bar', 'test');
|
2013-06-28 17:13:57 +04:00
|
|
|
|
2018-01-25 13:23:12 +03:00
|
|
|
$this->addToAssertionCount(1);
|
2013-05-07 18:34:09 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2013-05-10 00:52:44 +04:00
|
|
|
$this->emitter->removeListener(null, null, $listener);
|
2013-05-07 18:34:09 +04:00
|
|
|
$this->emitter->emitEvent('Test', 'test');
|
|
|
|
$this->emitter->emitEvent('Test', 'foo');
|
|
|
|
$this->emitter->emitEvent('Bar', 'foo');
|
2013-06-28 17:13:57 +04:00
|
|
|
|
2018-01-25 13:23:12 +03:00
|
|
|
$this->addToAssertionCount(1);
|
2013-05-07 18:34:09 +04:00
|
|
|
}
|
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
|
2013-05-07 18:34:09 +04:00
|
|
|
public function testRemoveKeepOtherCallback() {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\Test\Hooks\EmittedException::class);
|
|
|
|
|
2013-05-07 18:34:09 +04:00
|
|
|
$listener1 = function () {
|
|
|
|
throw new EmittedException;
|
|
|
|
};
|
|
|
|
$listener2 = function () {
|
|
|
|
throw new EmittedException;
|
|
|
|
};
|
|
|
|
$this->emitter->listen('Test', 'test', $listener1);
|
|
|
|
$this->emitter->listen('Test', 'test', $listener2);
|
2013-05-10 00:52:44 +04:00
|
|
|
$this->emitter->removeListener('Test', 'test', $listener1);
|
2013-05-07 18:34:09 +04:00
|
|
|
$this->emitter->emitEvent('Test', 'test');
|
2013-06-28 17:13:57 +04:00
|
|
|
|
2018-01-25 13:23:12 +03:00
|
|
|
$this->addToAssertionCount(1);
|
2013-05-07 18:34:09 +04:00
|
|
|
}
|
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
|
2013-05-07 18:34:09 +04:00
|
|
|
public function testRemoveKeepOtherMethod() {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\Test\Hooks\EmittedException::class);
|
|
|
|
|
2013-05-07 18:34:09 +04:00
|
|
|
$listener = function () {
|
|
|
|
throw new EmittedException;
|
|
|
|
};
|
|
|
|
$this->emitter->listen('Test', 'test', $listener);
|
|
|
|
$this->emitter->listen('Test', 'foo', $listener);
|
2013-05-10 00:52:44 +04:00
|
|
|
$this->emitter->removeListener('Test', 'foo', $listener);
|
2013-05-07 18:34:09 +04:00
|
|
|
$this->emitter->emitEvent('Test', 'test');
|
2013-06-28 17:13:57 +04:00
|
|
|
|
2018-01-25 13:23:12 +03:00
|
|
|
$this->addToAssertionCount(1);
|
2013-05-07 18:34:09 +04:00
|
|
|
}
|
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
|
2013-05-07 18:34:09 +04:00
|
|
|
public function testRemoveKeepOtherScope() {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\Test\Hooks\EmittedException::class);
|
|
|
|
|
2013-05-07 18:34:09 +04:00
|
|
|
$listener = function () {
|
|
|
|
throw new EmittedException;
|
|
|
|
};
|
|
|
|
$this->emitter->listen('Test', 'test', $listener);
|
|
|
|
$this->emitter->listen('Bar', 'test', $listener);
|
2013-05-10 00:52:44 +04:00
|
|
|
$this->emitter->removeListener('Bar', 'test', $listener);
|
2013-05-07 18:34:09 +04:00
|
|
|
$this->emitter->emitEvent('Test', 'test');
|
2013-06-28 17:13:57 +04:00
|
|
|
|
2018-01-25 13:23:12 +03:00
|
|
|
$this->addToAssertionCount(1);
|
2013-05-07 18:34:09 +04:00
|
|
|
}
|
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
|
2013-05-07 18:34:09 +04:00
|
|
|
public function testRemoveNonExistingName() {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\Test\Hooks\EmittedException::class);
|
|
|
|
|
2013-05-07 18:34:09 +04:00
|
|
|
$listener = function () {
|
|
|
|
throw new EmittedException;
|
|
|
|
};
|
|
|
|
$this->emitter->listen('Test', 'test', $listener);
|
2013-05-10 00:52:44 +04:00
|
|
|
$this->emitter->removeListener('Bar', 'test', $listener);
|
2013-05-07 18:34:09 +04:00
|
|
|
$this->emitter->emitEvent('Test', 'test');
|
2013-06-28 17:13:57 +04:00
|
|
|
|
2018-01-25 13:23:12 +03:00
|
|
|
$this->addToAssertionCount(1);
|
2013-05-07 18:34:09 +04:00
|
|
|
}
|
|
|
|
}
|