|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright (c) 2015 Robin Appelman <[email protected]> |
|
5
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
|
6
|
|
|
* later. |
|
7
|
|
|
* See the COPYING-README file. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Test\Command; |
|
11
|
|
|
|
|
12
|
|
|
use OC\Command\FileAccess; |
|
13
|
|
|
use OCP\Command\IBus; |
|
14
|
|
|
use OCP\Command\ICommand; |
|
15
|
|
|
use Test\BackgroundJob\DummyJobList; |
|
16
|
|
|
use Test\TestCase; |
|
17
|
|
|
|
|
18
|
|
|
class SimpleCommand implements ICommand { |
|
19
|
|
|
public function handle() { |
|
20
|
|
|
AsyncBus::$lastCommand = 'SimpleCommand'; |
|
21
|
|
|
} |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
class StateFullCommand implements ICommand { |
|
25
|
|
|
private $state; |
|
26
|
|
|
|
|
27
|
|
|
function __construct($state) { |
|
28
|
|
|
$this->state = $state; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function handle() { |
|
32
|
|
|
AsyncBus::$lastCommand = $this->state; |
|
33
|
|
|
} |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
class FilesystemCommand implements ICommand { |
|
37
|
|
|
use FileAccess; |
|
38
|
|
|
|
|
39
|
|
|
public function handle() { |
|
40
|
|
|
AsyncBus::$lastCommand = 'FileAccess'; |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
function basicFunction() { |
|
45
|
|
|
AsyncBus::$lastCommand = 'function'; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
// clean class to prevent phpunit putting closure in $this |
|
49
|
|
|
class ThisClosureTest { |
|
50
|
|
|
private function privateMethod() { |
|
51
|
|
|
AsyncBus::$lastCommand = 'closure-this'; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function test(IBus $bus) { |
|
55
|
|
|
$bus->push(function () { |
|
56
|
|
|
$this->privateMethod(); |
|
57
|
|
|
}); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
class AsyncBus extends TestCase { |
|
62
|
|
|
/** |
|
63
|
|
|
* Basic way to check output from a command |
|
64
|
|
|
* |
|
65
|
|
|
* @var string |
|
66
|
|
|
*/ |
|
67
|
|
|
public static $lastCommand; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @var \OCP\BackgroundJob\IJobList |
|
71
|
|
|
*/ |
|
72
|
|
|
private $jobList; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @var \OCP\Command\IBus |
|
76
|
|
|
*/ |
|
77
|
|
|
private $bus; |
|
78
|
|
|
|
|
79
|
|
|
public static function DummyCommand() { |
|
80
|
|
|
self::$lastCommand = 'static'; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function setUp() { |
|
84
|
|
|
$this->jobList = new DummyJobList(); |
|
85
|
|
|
$this->bus = new \OC\Command\AsyncBus($this->jobList); |
|
86
|
|
|
self::$lastCommand = ''; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function testSimpleCommand() { |
|
90
|
|
|
$command = new SimpleCommand(); |
|
91
|
|
|
$this->bus->push($command); |
|
92
|
|
|
$this->runJobs(); |
|
93
|
|
|
$this->assertEquals('SimpleCommand', self::$lastCommand); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
View Code Duplication |
public function testStateFullCommand() { |
|
97
|
|
|
$command = new StateFullCommand('foo'); |
|
98
|
|
|
$this->bus->push($command); |
|
99
|
|
|
$this->runJobs(); |
|
100
|
|
|
$this->assertEquals('foo', self::$lastCommand); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function testStaticCallable() { |
|
104
|
|
|
$this->bus->push(['\Test\Command\AsyncBus', 'DummyCommand']); |
|
105
|
|
|
$this->runJobs(); |
|
106
|
|
|
$this->assertEquals('static', self::$lastCommand); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
View Code Duplication |
public function testMemberCallable() { |
|
110
|
|
|
$command = new StateFullCommand('bar'); |
|
111
|
|
|
$this->bus->push([$command, 'handle']); |
|
112
|
|
|
$this->runJobs(); |
|
113
|
|
|
$this->assertEquals('bar', self::$lastCommand); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
public function testFunctionCallable() { |
|
117
|
|
|
$this->bus->push('\Test\Command\BasicFunction'); |
|
118
|
|
|
$this->runJobs(); |
|
119
|
|
|
$this->assertEquals('function', self::$lastCommand); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function testClosure() { |
|
123
|
|
|
$this->bus->push(function () { |
|
124
|
|
|
AsyncBus::$lastCommand = 'closure'; |
|
125
|
|
|
}); |
|
126
|
|
|
$this->runJobs(); |
|
127
|
|
|
$this->assertEquals('closure', self::$lastCommand); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
public function testClosureSelf() { |
|
131
|
|
|
$this->bus->push(function () { |
|
132
|
|
|
self::$lastCommand = 'closure-self'; |
|
133
|
|
|
}); |
|
134
|
|
|
$this->runJobs(); |
|
135
|
|
|
$this->assertEquals('closure-self', self::$lastCommand); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
|
|
139
|
|
|
public function testClosureThis() { |
|
140
|
|
|
// clean class to prevent phpunit putting closure in $this |
|
141
|
|
|
$test = new ThisClosureTest(); |
|
142
|
|
|
$test->test($this->bus); |
|
143
|
|
|
$this->runJobs(); |
|
144
|
|
|
$this->assertEquals('closure-this', self::$lastCommand); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
public function testClosureBind() { |
|
148
|
|
|
$state = 'bar'; |
|
149
|
|
|
$this->bus->push(function () use ($state) { |
|
150
|
|
|
self::$lastCommand = 'closure-' . $state; |
|
151
|
|
|
}); |
|
152
|
|
|
$this->runJobs(); |
|
153
|
|
|
$this->assertEquals('closure-bar', self::$lastCommand); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
public function testFileFileAccessCommand() { |
|
157
|
|
|
$this->bus->push(new FilesystemCommand()); |
|
158
|
|
|
$this->assertEquals('', self::$lastCommand); |
|
159
|
|
|
$this->runJobs(); |
|
160
|
|
|
$this->assertEquals('FileAccess', self::$lastCommand); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
public function testFileFileAccessCommandSync() { |
|
164
|
|
|
$this->bus->requireSync('\OC\Command\FileAccess'); |
|
165
|
|
|
$this->bus->push(new FilesystemCommand()); |
|
166
|
|
|
$this->assertEquals('FileAccess', self::$lastCommand); |
|
167
|
|
|
self::$lastCommand = ''; |
|
168
|
|
|
$this->runJobs(); |
|
169
|
|
|
$this->assertEquals('', self::$lastCommand); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
|
|
173
|
|
|
private function runJobs() { |
|
174
|
|
|
$jobs = $this->jobList->getAll(); |
|
175
|
|
|
foreach ($jobs as $job) { |
|
176
|
|
|
$job->execute($this->jobList); |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
|