Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ForwardingEmitterTest.php « Hooks « lib « tests - github.com/nextcloud/server.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9b0a51d1bd48c063b1b8536a28d4490ed966ab1d (plain)
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
<?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;
use OC\Hooks\PublicEmitter;

class DummyForwardingEmitter extends \OC\Hooks\ForwardingEmitter {
	public function emitEvent($scope, $method, $arguments = []) {
		$this->emit($scope, $method, $arguments);
	}

	/**
	 * @param \OC\Hooks\Emitter $emitter
	 */
	public function forward(\OC\Hooks\Emitter $emitter) {
		parent::forward($emitter);
	}
}

/**
 * Class ForwardingEmitter
 *
 * allows forwarding all listen calls to other emitters
 *
 * @package OC\Hooks
 */
class ForwardingEmitterTest extends BasicEmitterTest {
	public function testSingleForward() {
		$baseEmitter = new PublicEmitter();
		$forwardingEmitter = new DummyForwardingEmitter();
		$forwardingEmitter->forward($baseEmitter);
		$hookCalled = false;
		$forwardingEmitter->listen('Test', 'test', function () use (&$hookCalled) {
			$hookCalled = true;
		});
		$baseEmitter->emit('Test', 'test');
		$this->assertTrue($hookCalled);
	}

	public function testMultipleForwards() {
		$baseEmitter1 = new PublicEmitter();
		$baseEmitter2 = new PublicEmitter();
		$forwardingEmitter = new DummyForwardingEmitter();
		$forwardingEmitter->forward($baseEmitter1);
		$forwardingEmitter->forward($baseEmitter2);
		$hookCalled = 0;
		$forwardingEmitter->listen('Test', 'test1', function () use (&$hookCalled) {
			$hookCalled++;
		});
		$forwardingEmitter->listen('Test', 'test2', function () use (&$hookCalled) {
			$hookCalled++;
		});
		$baseEmitter1->emit('Test', 'test1');
		$baseEmitter1->emit('Test', 'test2');
		$this->assertEquals(2, $hookCalled);
	}

	public function testForwardExistingHooks() {
		$baseEmitter = new PublicEmitter();
		$forwardingEmitter = new DummyForwardingEmitter();
		$hookCalled = false;
		$forwardingEmitter->listen('Test', 'test', function () use (&$hookCalled) {
			$hookCalled = true;
		});
		$forwardingEmitter->forward($baseEmitter);
		$baseEmitter->emit('Test', 'test');
		$this->assertTrue($hookCalled);
	}
}