2013-05-07 10:34:09 -04:00
|
|
|
<?php
|
2025-06-30 09:04:05 -04:00
|
|
|
|
2013-05-07 10:34:09 -04:00
|
|
|
/**
|
2024-05-10 09:09:14 -04:00
|
|
|
* SPDX-FileCopyrightText: 2018-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2013-05-07 10:34:09 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace Test\Hooks;
|
|
|
|
|
|
2025-06-12 12:31:58 -04:00
|
|
|
use OC\Hooks\BasicEmitter;
|
2025-06-30 10:56:59 -04:00
|
|
|
use OC\Hooks\Emitter;
|
2025-06-12 12:31:58 -04:00
|
|
|
|
2013-05-07 10:34:09 -04:00
|
|
|
/**
|
|
|
|
|
* Class DummyEmitter
|
|
|
|
|
*
|
|
|
|
|
* class to make BasicEmitter::emit publicly available
|
|
|
|
|
*
|
|
|
|
|
* @package Test\Hooks
|
|
|
|
|
*/
|
2025-06-12 12:31:58 -04:00
|
|
|
class DummyEmitter extends BasicEmitter {
|
2020-03-26 04:30:18 -04:00
|
|
|
public function emitEvent($scope, $method, $arguments = []) {
|
2013-05-07 10: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 09:38:20 -04:00
|
|
|
class BasicEmitterTest extends \Test\TestCase {
|
2013-05-07 10:34:09 -04:00
|
|
|
/**
|
2025-06-30 10:56:59 -04:00
|
|
|
* @var Emitter $emitter
|
2013-05-07 10:34:09 -04:00
|
|
|
*/
|
|
|
|
|
protected $emitter;
|
|
|
|
|
|
2019-11-21 10:40:38 -05:00
|
|
|
protected function setUp(): void {
|
2014-11-10 17:30:38 -05:00
|
|
|
parent::setUp();
|
2013-05-07 10:34:09 -04:00
|
|
|
$this->emitter = new DummyEmitter();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function nonStaticCallBack() {
|
|
|
|
|
throw new EmittedException;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function staticCallBack() {
|
|
|
|
|
throw new EmittedException;
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-12 12:31:58 -04:00
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testAnonymousFunction(): void {
|
2019-11-27 09:27:18 -05:00
|
|
|
$this->expectException(\Test\Hooks\EmittedException::class);
|
|
|
|
|
|
2025-06-12 12:31:58 -04:00
|
|
|
$this->emitter->listen('Test', 'test', function (): void {
|
2013-05-07 10:34:09 -04:00
|
|
|
throw new EmittedException;
|
|
|
|
|
});
|
|
|
|
|
$this->emitter->emitEvent('Test', 'test');
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-12 12:31:58 -04:00
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testStaticCallback(): void {
|
2019-11-27 09:27:18 -05:00
|
|
|
$this->expectException(\Test\Hooks\EmittedException::class);
|
|
|
|
|
|
2020-03-26 04:30:18 -04:00
|
|
|
$this->emitter->listen('Test', 'test', ['\Test\Hooks\BasicEmitterTest', 'staticCallBack']);
|
2013-05-07 10:34:09 -04:00
|
|
|
$this->emitter->emitEvent('Test', 'test');
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-12 12:31:58 -04:00
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testNonStaticCallback(): void {
|
2019-11-27 09:27:18 -05:00
|
|
|
$this->expectException(\Test\Hooks\EmittedException::class);
|
|
|
|
|
|
2020-03-26 04:30:18 -04:00
|
|
|
$this->emitter->listen('Test', 'test', [$this, 'nonStaticCallBack']);
|
2013-05-07 10:34:09 -04:00
|
|
|
$this->emitter->emitEvent('Test', 'test');
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testOnlyCallOnce(): void {
|
2013-05-07 10:34:09 -04:00
|
|
|
$count = 0;
|
2025-06-12 12:31:58 -04:00
|
|
|
$listener = function () use (&$count): void {
|
2013-05-07 10:34:09 -04:00
|
|
|
$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');
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testDifferentMethods(): void {
|
2013-05-07 10:34:09 -04:00
|
|
|
$count = 0;
|
2025-06-12 12:31:58 -04:00
|
|
|
$listener = function () use (&$count): void {
|
2013-05-07 10:34:09 -04:00
|
|
|
$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');
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testDifferentScopes(): void {
|
2013-05-07 10:34:09 -04:00
|
|
|
$count = 0;
|
2025-06-12 12:31:58 -04:00
|
|
|
$listener = function () use (&$count): void {
|
2013-05-07 10:34:09 -04:00
|
|
|
$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');
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testDifferentCallbacks(): void {
|
2013-05-07 10:34:09 -04:00
|
|
|
$count = 0;
|
2025-06-12 12:31:58 -04:00
|
|
|
$listener1 = function () use (&$count): void {
|
2013-05-07 10:34:09 -04:00
|
|
|
$count++;
|
|
|
|
|
};
|
2025-06-12 12:31:58 -04:00
|
|
|
$listener2 = function () use (&$count): void {
|
2013-05-07 10:34:09 -04:00
|
|
|
$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');
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-12 12:31:58 -04:00
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testArguments(): void {
|
2019-11-27 09:27:18 -05:00
|
|
|
$this->expectException(\Test\Hooks\EmittedException::class);
|
|
|
|
|
|
2025-06-12 12:31:58 -04:00
|
|
|
$this->emitter->listen('Test', 'test', function ($foo, $bar): void {
|
2025-09-27 16:56:38 -04:00
|
|
|
if ($foo === 'foo' && $bar === 'bar') {
|
2013-05-07 10:34:09 -04:00
|
|
|
throw new EmittedException;
|
|
|
|
|
}
|
|
|
|
|
});
|
2020-03-26 04:30:18 -04:00
|
|
|
$this->emitter->emitEvent('Test', 'test', ['foo', 'bar']);
|
2013-05-07 10:34:09 -04:00
|
|
|
}
|
|
|
|
|
|
2025-06-12 12:31:58 -04:00
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testNamedArguments(): void {
|
2019-11-27 09:27:18 -05:00
|
|
|
$this->expectException(\Test\Hooks\EmittedException::class);
|
|
|
|
|
|
2025-06-12 12:31:58 -04:00
|
|
|
$this->emitter->listen('Test', 'test', function ($foo, $bar): void {
|
2025-09-27 16:56:38 -04:00
|
|
|
if ($foo === 'foo' && $bar === 'bar') {
|
2013-05-07 10:34:09 -04:00
|
|
|
throw new EmittedException;
|
|
|
|
|
}
|
|
|
|
|
});
|
2020-03-26 04:30:18 -04:00
|
|
|
$this->emitter->emitEvent('Test', 'test', ['foo' => 'foo', 'bar' => 'bar']);
|
2013-05-07 10:34:09 -04:00
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testRemoveAllSpecified(): void {
|
2025-06-12 12:31:58 -04:00
|
|
|
$listener = function (): void {
|
2013-05-07 10:34:09 -04:00
|
|
|
throw new EmittedException;
|
|
|
|
|
};
|
|
|
|
|
$this->emitter->listen('Test', 'test', $listener);
|
2013-05-09 16:52:44 -04:00
|
|
|
$this->emitter->removeListener('Test', 'test', $listener);
|
2013-05-07 10:34:09 -04:00
|
|
|
$this->emitter->emitEvent('Test', 'test');
|
2013-06-28 09:13:57 -04:00
|
|
|
|
2018-01-25 05:23:12 -05:00
|
|
|
$this->addToAssertionCount(1);
|
2013-05-07 10:34:09 -04:00
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testRemoveWildcardListener(): void {
|
2025-06-12 12:31:58 -04:00
|
|
|
$listener1 = function (): void {
|
2013-05-07 10:34:09 -04:00
|
|
|
throw new EmittedException;
|
|
|
|
|
};
|
2025-06-12 12:31:58 -04:00
|
|
|
$listener2 = function (): void {
|
2013-05-07 10:34:09 -04:00
|
|
|
throw new EmittedException;
|
|
|
|
|
};
|
|
|
|
|
$this->emitter->listen('Test', 'test', $listener1);
|
|
|
|
|
$this->emitter->listen('Test', 'test', $listener2);
|
2013-05-09 16:52:44 -04:00
|
|
|
$this->emitter->removeListener('Test', 'test');
|
2013-05-07 10:34:09 -04:00
|
|
|
$this->emitter->emitEvent('Test', 'test');
|
2013-06-28 09:13:57 -04:00
|
|
|
|
2018-01-25 05:23:12 -05:00
|
|
|
$this->addToAssertionCount(1);
|
2013-05-07 10:34:09 -04:00
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testRemoveWildcardMethod(): void {
|
2025-06-12 12:31:58 -04:00
|
|
|
$listener = function (): void {
|
2013-05-07 10:34:09 -04:00
|
|
|
throw new EmittedException;
|
|
|
|
|
};
|
|
|
|
|
$this->emitter->listen('Test', 'test', $listener);
|
|
|
|
|
$this->emitter->listen('Test', 'foo', $listener);
|
2013-05-09 16:52:44 -04:00
|
|
|
$this->emitter->removeListener('Test', null, $listener);
|
2013-05-07 10:34:09 -04:00
|
|
|
$this->emitter->emitEvent('Test', 'test');
|
|
|
|
|
$this->emitter->emitEvent('Test', 'foo');
|
2013-06-28 09:13:57 -04:00
|
|
|
|
2018-01-25 05:23:12 -05:00
|
|
|
$this->addToAssertionCount(1);
|
2013-05-07 10:34:09 -04:00
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testRemoveWildcardScope(): void {
|
2025-06-12 12:31:58 -04:00
|
|
|
$listener = function (): void {
|
2013-05-07 10:34:09 -04:00
|
|
|
throw new EmittedException;
|
|
|
|
|
};
|
|
|
|
|
$this->emitter->listen('Test', 'test', $listener);
|
|
|
|
|
$this->emitter->listen('Bar', 'test', $listener);
|
2013-05-09 16:52:44 -04:00
|
|
|
$this->emitter->removeListener(null, 'test', $listener);
|
2013-05-07 10:34:09 -04:00
|
|
|
$this->emitter->emitEvent('Test', 'test');
|
|
|
|
|
$this->emitter->emitEvent('Bar', 'test');
|
2013-06-28 09:13:57 -04:00
|
|
|
|
2018-01-25 05:23:12 -05:00
|
|
|
$this->addToAssertionCount(1);
|
2013-05-07 10:34:09 -04:00
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testRemoveWildcardScopeAndMethod(): void {
|
2025-06-12 12:31:58 -04:00
|
|
|
$listener = function (): void {
|
2013-05-07 10:34:09 -04:00
|
|
|
throw new EmittedException;
|
|
|
|
|
};
|
|
|
|
|
$this->emitter->listen('Test', 'test', $listener);
|
|
|
|
|
$this->emitter->listen('Test', 'foo', $listener);
|
|
|
|
|
$this->emitter->listen('Bar', 'foo', $listener);
|
2013-05-09 16:52:44 -04:00
|
|
|
$this->emitter->removeListener(null, null, $listener);
|
2013-05-07 10:34:09 -04:00
|
|
|
$this->emitter->emitEvent('Test', 'test');
|
|
|
|
|
$this->emitter->emitEvent('Test', 'foo');
|
|
|
|
|
$this->emitter->emitEvent('Bar', 'foo');
|
2013-06-28 09:13:57 -04:00
|
|
|
|
2018-01-25 05:23:12 -05:00
|
|
|
$this->addToAssertionCount(1);
|
2013-05-07 10:34:09 -04:00
|
|
|
}
|
|
|
|
|
|
2025-06-12 12:31:58 -04:00
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testRemoveKeepOtherCallback(): void {
|
2019-11-27 09:27:18 -05:00
|
|
|
$this->expectException(\Test\Hooks\EmittedException::class);
|
|
|
|
|
|
2025-06-12 12:31:58 -04:00
|
|
|
$listener1 = function (): void {
|
2013-05-07 10:34:09 -04:00
|
|
|
throw new EmittedException;
|
|
|
|
|
};
|
2025-06-12 12:31:58 -04:00
|
|
|
$listener2 = function (): void {
|
2013-05-07 10:34:09 -04:00
|
|
|
throw new EmittedException;
|
|
|
|
|
};
|
|
|
|
|
$this->emitter->listen('Test', 'test', $listener1);
|
|
|
|
|
$this->emitter->listen('Test', 'test', $listener2);
|
2013-05-09 16:52:44 -04:00
|
|
|
$this->emitter->removeListener('Test', 'test', $listener1);
|
2013-05-07 10:34:09 -04:00
|
|
|
$this->emitter->emitEvent('Test', 'test');
|
2013-06-28 09:13:57 -04:00
|
|
|
|
2018-01-25 05:23:12 -05:00
|
|
|
$this->addToAssertionCount(1);
|
2013-05-07 10:34:09 -04:00
|
|
|
}
|
|
|
|
|
|
2025-06-12 12:31:58 -04:00
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testRemoveKeepOtherMethod(): void {
|
2019-11-27 09:27:18 -05:00
|
|
|
$this->expectException(\Test\Hooks\EmittedException::class);
|
|
|
|
|
|
2025-06-12 12:31:58 -04:00
|
|
|
$listener = function (): void {
|
2013-05-07 10:34:09 -04:00
|
|
|
throw new EmittedException;
|
|
|
|
|
};
|
|
|
|
|
$this->emitter->listen('Test', 'test', $listener);
|
|
|
|
|
$this->emitter->listen('Test', 'foo', $listener);
|
2013-05-09 16:52:44 -04:00
|
|
|
$this->emitter->removeListener('Test', 'foo', $listener);
|
2013-05-07 10:34:09 -04:00
|
|
|
$this->emitter->emitEvent('Test', 'test');
|
2013-06-28 09:13:57 -04:00
|
|
|
|
2018-01-25 05:23:12 -05:00
|
|
|
$this->addToAssertionCount(1);
|
2013-05-07 10:34:09 -04:00
|
|
|
}
|
|
|
|
|
|
2025-06-12 12:31:58 -04:00
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testRemoveKeepOtherScope(): void {
|
2019-11-27 09:27:18 -05:00
|
|
|
$this->expectException(\Test\Hooks\EmittedException::class);
|
|
|
|
|
|
2025-06-12 12:31:58 -04:00
|
|
|
$listener = function (): void {
|
2013-05-07 10:34:09 -04:00
|
|
|
throw new EmittedException;
|
|
|
|
|
};
|
|
|
|
|
$this->emitter->listen('Test', 'test', $listener);
|
|
|
|
|
$this->emitter->listen('Bar', 'test', $listener);
|
2013-05-09 16:52:44 -04:00
|
|
|
$this->emitter->removeListener('Bar', 'test', $listener);
|
2013-05-07 10:34:09 -04:00
|
|
|
$this->emitter->emitEvent('Test', 'test');
|
2013-06-28 09:13:57 -04:00
|
|
|
|
2018-01-25 05:23:12 -05:00
|
|
|
$this->addToAssertionCount(1);
|
2013-05-07 10:34:09 -04:00
|
|
|
}
|
|
|
|
|
|
2025-06-12 12:31:58 -04:00
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testRemoveNonExistingName(): void {
|
2019-11-27 09:27:18 -05:00
|
|
|
$this->expectException(\Test\Hooks\EmittedException::class);
|
|
|
|
|
|
2025-06-12 12:31:58 -04:00
|
|
|
$listener = function (): void {
|
2013-05-07 10:34:09 -04:00
|
|
|
throw new EmittedException;
|
|
|
|
|
};
|
|
|
|
|
$this->emitter->listen('Test', 'test', $listener);
|
2013-05-09 16:52:44 -04:00
|
|
|
$this->emitter->removeListener('Bar', 'test', $listener);
|
2013-05-07 10:34:09 -04:00
|
|
|
$this->emitter->emitEvent('Test', 'test');
|
2013-06-28 09:13:57 -04:00
|
|
|
|
2018-01-25 05:23:12 -05:00
|
|
|
$this->addToAssertionCount(1);
|
2013-05-07 10:34:09 -04:00
|
|
|
}
|
|
|
|
|
}
|