-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
ConsoleIntegrationTestTrait.php
328 lines (294 loc) · 9.38 KB
/
ConsoleIntegrationTestTrait.php
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
<?php
declare(strict_types=1);
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @since 3.7.0
* @license https://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Console\TestSuite;
use Cake\Command\Command;
use Cake\Console\CommandRunner;
use Cake\Console\ConsoleIo;
use Cake\Console\Exception\StopException;
use Cake\Console\TestSuite\Constraint\ContentsContain;
use Cake\Console\TestSuite\Constraint\ContentsContainRow;
use Cake\Console\TestSuite\Constraint\ContentsEmpty;
use Cake\Console\TestSuite\Constraint\ContentsNotContain;
use Cake\Console\TestSuite\Constraint\ContentsRegExp;
use Cake\Console\TestSuite\Constraint\ExitCode;
use Cake\Core\ConsoleApplicationInterface;
use Cake\Core\TestSuite\ContainerStubTrait;
use InvalidArgumentException;
/**
* A bundle of methods that makes testing commands
* and shell classes easier.
*
* Enables you to call commands/shells with a
* full application context.
*/
trait ConsoleIntegrationTestTrait
{
use ContainerStubTrait;
/**
* Last exit code
*
* @var int|null
*/
protected ?int $_exitCode = null;
/**
* Console output stub
*
* @var \Cake\Console\TestSuite\StubConsoleOutput|null
*/
protected ?StubConsoleOutput $_out = null;
/**
* Console error output stub
*
* @var \Cake\Console\TestSuite\StubConsoleOutput|null
*/
protected ?StubConsoleOutput $_err = null;
/**
* Console input mock
*
* @var \Cake\Console\TestSuite\StubConsoleInput|null
*/
protected ?StubConsoleInput $_in = null;
/**
* Runs CLI integration test
*
* @param string $command Command to run
* @param array $input Input values to pass to an interactive shell
* @throws \Cake\Console\TestSuite\MissingConsoleInputException
* @throws \InvalidArgumentException
* @return void
*/
public function exec(string $command, array $input = []): void
{
$runner = $this->makeRunner();
$this->_out ??= new StubConsoleOutput();
$this->_err ??= new StubConsoleOutput();
if ($this->_in === null) {
$this->_in = new StubConsoleInput($input);
} elseif ($input) {
throw new InvalidArgumentException(
'You can use `$input` only if `$_in` property is null and will be reset.'
);
}
$args = $this->commandStringToArgs("cake $command");
$io = new ConsoleIo($this->_out, $this->_err, $this->_in);
try {
$this->_exitCode = $runner->run($args, $io);
} catch (MissingConsoleInputException $e) {
$messages = $this->_out->messages();
if (count($messages)) {
$e->setQuestion($messages[count($messages) - 1]);
}
throw $e;
} catch (StopException $exception) {
$this->_exitCode = $exception->getCode();
}
}
/**
* Cleans state to get ready for the next test
*
* @after
* @return void
* @psalm-suppress PossiblyNullPropertyAssignmentValue
*/
public function cleanupConsoleTrait(): void
{
$this->_exitCode = null;
$this->_out = null;
$this->_err = null;
$this->_in = null;
}
/**
* Asserts shell exited with the expected code
*
* @param int $expected Expected exit code
* @param string $message Failure message
* @return void
*/
public function assertExitCode(int $expected, string $message = ''): void
{
$this->assertThat($expected, new ExitCode($this->_exitCode), $message);
}
/**
* Asserts shell exited with the Command::CODE_SUCCESS
*
* @param string $message Failure message
* @return void
*/
public function assertExitSuccess(string $message = ''): void
{
$this->assertThat(Command::CODE_SUCCESS, new ExitCode($this->_exitCode), $message);
}
/**
* Asserts shell exited with Command::CODE_ERROR
*
* @param string $message Failure message
* @return void
*/
public function assertExitError(string $message = ''): void
{
$this->assertThat(Command::CODE_ERROR, new ExitCode($this->_exitCode), $message);
}
/**
* Asserts that `stdout` is empty
*
* @param string $message The message to output when the assertion fails.
* @return void
*/
public function assertOutputEmpty(string $message = ''): void
{
$this->assertThat(null, new ContentsEmpty($this->_out->messages(), 'output'), $message);
}
/**
* Asserts `stdout` contains expected output
*
* @param string $expected Expected output
* @param string $message Failure message
* @return void
*/
public function assertOutputContains(string $expected, string $message = ''): void
{
$this->assertThat($expected, new ContentsContain($this->_out->messages(), 'output'), $message);
}
/**
* Asserts `stdout` does not contain expected output
*
* @param string $expected Expected output
* @param string $message Failure message
* @return void
*/
public function assertOutputNotContains(string $expected, string $message = ''): void
{
$this->assertThat($expected, new ContentsNotContain($this->_out->messages(), 'output'), $message);
}
/**
* Asserts `stdout` contains expected regexp
*
* @param string $pattern Expected pattern
* @param string $message Failure message
* @return void
*/
public function assertOutputRegExp(string $pattern, string $message = ''): void
{
$this->assertThat($pattern, new ContentsRegExp($this->_out->messages(), 'output'), $message);
}
/**
* Check that a row of cells exists in the output.
*
* @param array $row Row of cells to ensure exist in the output.
* @param string $message Failure message.
* @return void
*/
protected function assertOutputContainsRow(array $row, string $message = ''): void
{
$this->assertThat($row, new ContentsContainRow($this->_out->messages(), 'output'), $message);
}
/**
* Asserts `stderr` contains expected output
*
* @param string $expected Expected output
* @param string $message Failure message
* @return void
*/
public function assertErrorContains(string $expected, string $message = ''): void
{
$this->assertThat($expected, new ContentsContain($this->_err->messages(), 'error output'), $message);
}
/**
* Asserts `stderr` contains expected regexp
*
* @param string $pattern Expected pattern
* @param string $message Failure message
* @return void
*/
public function assertErrorRegExp(string $pattern, string $message = ''): void
{
$this->assertThat($pattern, new ContentsRegExp($this->_err->messages(), 'error output'), $message);
}
/**
* Asserts that `stderr` is empty
*
* @param string $message The message to output when the assertion fails.
* @return void
*/
public function assertErrorEmpty(string $message = ''): void
{
$this->assertThat(null, new ContentsEmpty($this->_err->messages(), 'error output'), $message);
}
/**
* Builds the appropriate command dispatcher
*
* @return \Cake\Console\CommandRunner
*/
protected function makeRunner(): CommandRunner
{
$app = $this->createApp();
assert($app instanceof ConsoleApplicationInterface);
return new CommandRunner($app);
}
/**
* Creates an $argv array from a command string
*
* @param string $command Command string
* @return array<string>
*/
protected function commandStringToArgs(string $command): array
{
$charCount = strlen($command);
$argv = [];
$arg = '';
$inDQuote = false;
$inSQuote = false;
for ($i = 0; $i < $charCount; $i++) {
$char = substr($command, $i, 1);
// end of argument
if ($char === ' ' && !$inDQuote && !$inSQuote) {
if ($arg !== '') {
$argv[] = $arg;
}
$arg = '';
continue;
}
// exiting single quote
if ($inSQuote && $char === "'") {
$inSQuote = false;
continue;
}
// exiting double quote
if ($inDQuote && $char === '"') {
$inDQuote = false;
continue;
}
// entering double quote
if ($char === '"' && !$inSQuote) {
$inDQuote = true;
continue;
}
// entering single quote
if ($char === "'" && !$inDQuote) {
$inSQuote = true;
continue;
}
$arg .= $char;
}
$argv[] = $arg;
return $argv;
}
}
// phpcs:disable
class_alias(
'Cake\Console\TestSuite\ConsoleIntegrationTestTrait',
'Cake\TestSuite\ConsoleIntegrationTestTrait'
);
// phpcs:enable