123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- <?php
- /**
- * SPDX-FileCopyrightText: 2018-2024 Nextcloud GmbH and Nextcloud contributors
- * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
- * SPDX-License-Identifier: AGPL-3.0-or-later
- */
- 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 = []) {
- $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 BasicEmitterTest extends \Test\TestCase {
- /**
- * @var \OC\Hooks\Emitter $emitter
- */
- protected $emitter;
- protected function setUp(): void {
- parent::setUp();
- $this->emitter = new DummyEmitter();
- }
- public function nonStaticCallBack() {
- throw new EmittedException;
- }
- public static function staticCallBack() {
- throw new EmittedException;
- }
-
- public function testAnonymousFunction() {
- $this->expectException(\Test\Hooks\EmittedException::class);
- $this->emitter->listen('Test', 'test', function () {
- throw new EmittedException;
- });
- $this->emitter->emitEvent('Test', 'test');
- }
-
- public function testStaticCallback() {
- $this->expectException(\Test\Hooks\EmittedException::class);
- $this->emitter->listen('Test', 'test', ['\Test\Hooks\BasicEmitterTest', 'staticCallBack']);
- $this->emitter->emitEvent('Test', 'test');
- }
-
- public function testNonStaticCallback() {
- $this->expectException(\Test\Hooks\EmittedException::class);
- $this->emitter->listen('Test', 'test', [$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');
- }
-
- public function testArguments() {
- $this->expectException(\Test\Hooks\EmittedException::class);
- $this->emitter->listen('Test', 'test', function ($foo, $bar) {
- if ($foo == 'foo' and $bar == 'bar') {
- throw new EmittedException;
- }
- });
- $this->emitter->emitEvent('Test', 'test', ['foo', 'bar']);
- }
-
- public function testNamedArguments() {
- $this->expectException(\Test\Hooks\EmittedException::class);
- $this->emitter->listen('Test', 'test', function ($foo, $bar) {
- if ($foo == 'foo' and $bar == 'bar') {
- throw new EmittedException;
- }
- });
- $this->emitter->emitEvent('Test', 'test', ['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->addToAssertionCount(1);
- }
- 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->addToAssertionCount(1);
- }
- 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->addToAssertionCount(1);
- }
- 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->addToAssertionCount(1);
- }
- 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->addToAssertionCount(1);
- }
-
- public function testRemoveKeepOtherCallback() {
- $this->expectException(\Test\Hooks\EmittedException::class);
- $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->addToAssertionCount(1);
- }
-
- public function testRemoveKeepOtherMethod() {
- $this->expectException(\Test\Hooks\EmittedException::class);
- $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->addToAssertionCount(1);
- }
-
- public function testRemoveKeepOtherScope() {
- $this->expectException(\Test\Hooks\EmittedException::class);
- $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->addToAssertionCount(1);
- }
-
- public function testRemoveNonExistingName() {
- $this->expectException(\Test\Hooks\EmittedException::class);
- $listener = function () {
- throw new EmittedException;
- };
- $this->emitter->listen('Test', 'test', $listener);
- $this->emitter->removeListener('Bar', 'test', $listener);
- $this->emitter->emitEvent('Test', 'test');
- $this->addToAssertionCount(1);
- }
- }
|