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
|
<?php
/**
* Copyright (c) 2015 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\Command;
use OC\Command\FileAccess;
use OCP\Command\IBus;
use OCP\Command\ICommand;
use Test\TestCase;
class SimpleCommand implements ICommand {
public function handle() {
AsyncBusTest::$lastCommand = 'SimpleCommand';
}
}
class StateFullCommand implements ICommand {
private $state;
function __construct($state) {
$this->state = $state;
}
public function handle() {
AsyncBusTest::$lastCommand = $this->state;
}
}
class FilesystemCommand implements ICommand {
use FileAccess;
public function handle() {
AsyncBusTest::$lastCommand = 'FileAccess';
}
}
function basicFunction() {
AsyncBusTest::$lastCommand = 'function';
}
// clean class to prevent phpunit putting closure in $this
class ThisClosureTest {
private function privateMethod() {
AsyncBusTest::$lastCommand = 'closure-this';
}
public function test(IBus $bus) {
$bus->push(function () {
$this->privateMethod();
});
}
}
abstract class AsyncBusTest extends TestCase {
/**
* Basic way to check output from a command
*
* @var string
*/
public static $lastCommand;
/**
* @var \OCP\Command\IBus
*/
private $bus;
public static function DummyCommand() {
self::$lastCommand = 'static';
}
/**
* @return IBus
*/
protected function getBus() {
if (!$this->bus instanceof IBus) {
$this->bus = $this->createBus();
}
return $this->bus;
}
/**
* @return IBus
*/
abstract protected function createBus();
protected function setUp(): void {
self::$lastCommand = '';
}
public function testSimpleCommand() {
$command = new SimpleCommand();
$this->getBus()->push($command);
$this->runJobs();
$this->assertEquals('SimpleCommand', self::$lastCommand);
}
public function testStateFullCommand() {
$command = new StateFullCommand('foo');
$this->getBus()->push($command);
$this->runJobs();
$this->assertEquals('foo', self::$lastCommand);
}
public function testStaticCallable() {
$this->getBus()->push(['\Test\Command\AsyncBusTest', 'DummyCommand']);
$this->runJobs();
$this->assertEquals('static', self::$lastCommand);
}
public function testMemberCallable() {
$command = new StateFullCommand('bar');
$this->getBus()->push([$command, 'handle']);
$this->runJobs();
$this->assertEquals('bar', self::$lastCommand);
}
public function testFunctionCallable() {
$this->getBus()->push('\Test\Command\BasicFunction');
$this->runJobs();
$this->assertEquals('function', self::$lastCommand);
}
public function testClosure() {
$this->getBus()->push(function () {
AsyncBusTest::$lastCommand = 'closure';
});
$this->runJobs();
$this->assertEquals('closure', self::$lastCommand);
}
public function testClosureSelf() {
$this->getBus()->push(function () {
AsyncBusTest::$lastCommand = 'closure-self';
});
$this->runJobs();
$this->assertEquals('closure-self', self::$lastCommand);
}
public function testClosureThis() {
// clean class to prevent phpunit putting closure in $this
$test = new ThisClosureTest();
$test->test($this->getBus());
$this->runJobs();
$this->assertEquals('closure-this', self::$lastCommand);
}
public function testClosureBind() {
$state = 'bar';
$this->getBus()->push(function () use ($state) {
AsyncBusTest::$lastCommand = 'closure-' . $state;
});
$this->runJobs();
$this->assertEquals('closure-bar', self::$lastCommand);
}
public function testFileFileAccessCommand() {
$this->getBus()->push(new FilesystemCommand());
$this->assertEquals('', self::$lastCommand);
$this->runJobs();
$this->assertEquals('FileAccess', self::$lastCommand);
}
public function testFileFileAccessCommandSync() {
$this->getBus()->requireSync('\OC\Command\FileAccess');
$this->getBus()->push(new FilesystemCommand());
$this->assertEquals('FileAccess', self::$lastCommand);
self::$lastCommand = '';
$this->runJobs();
$this->assertEquals('', self::$lastCommand);
}
abstract protected function runJobs();
}
|