-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestCaseAbstract.php
More file actions
236 lines (213 loc) · 8.74 KB
/
TestCaseAbstract.php
File metadata and controls
236 lines (213 loc) · 8.74 KB
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
<?php
declare(strict_types=1);
namespace MagicPush\CliToolkit\Tests\Tests;
use FilesystemIterator;
use LogicException;
use MagicPush\CliToolkit\Parametizer\Exception\ConfigException;
use MagicPush\CliToolkit\Parametizer\Parametizer;
use MagicPush\CliToolkit\Tests\Utils\CliProcess;
use PHPUnit\Framework\TestCase;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use SplFileInfo;
use function PHPUnit\Framework\assertNotFalse;
use function PHPUnit\Framework\assertSame;
use function PHPUnit\Framework\assertStringContainsString;
use function PHPUnit\Framework\assertTrue;
abstract class TestCaseAbstract extends TestCase {
protected static function removeDirectoryRecursively(string $directoryPath): void {
if (!file_exists($directoryPath)) {
return;
}
$directoryIterator = new RecursiveDirectoryIterator(
$directoryPath,
FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::SKIP_DOTS,
);
$filesRecursiveIterator = new RecursiveIteratorIterator(
$directoryIterator,
RecursiveIteratorIterator::CHILD_FIRST,
);
foreach ($filesRecursiveIterator as $item) {
/** @var SplFileInfo $item */
$itemRealPath = $item->getRealPath();
assertNotFalse($itemRealPath);
if ($item->isDir()) {
assertTrue(rmdir($itemRealPath));
} else {
assertTrue(unlink($itemRealPath));
}
}
assertTrue(rmdir($directoryPath));
}
/**
* @param string[] $stdinLines {@see CliProcess::__construct()}
*/
protected static function assertNoErrorsOutput(
string $scriptPath,
string $parametersString = '',
array $stdinLines = [],
int $expectedExitCode = 0,
): CliProcess {
$command = 'php ' . escapeshellarg($scriptPath);
if ('' !== $parametersString) {
$command .= " {$parametersString}";
}
$result = new CliProcess($command, $stdinLines);
// The heading space before a script name is needed for a script being clickable in PhpStorm console log
// as a script file link.
$failedAssertMessage = " {$scriptPath}" . PHP_EOL
. "COMMAND: {$command}" . PHP_EOL
. "Unexpected error output: {$result->getStdErr()}";
assertSame($expectedExitCode, $result->getExitCode(), $failedAssertMessage);
assertSame('', $result->getStdErr(), $failedAssertMessage);
return $result;
}
/**
* Asserts that an error message was printed in {@see STDERR} (without a stack trace) during a script execution.
*/
protected static function assertExecutionErrorOutput(
string $scriptPath,
string $expectedErrorOutputSubstring,
string $parametersString = '',
): CliProcess {
return static::assertAnyErrorOutput(
$scriptPath,
$expectedErrorOutputSubstring,
$parametersString,
shouldAssertExitCode: true,
shouldAssertStdErr: true,
);
}
/**
* Asserts that a {@see ConfigException} exception was printed out during a script configuration.
* Exit code may vary thus is not asserted.
*/
protected static function assertConfigExceptionOutput(
string $scriptPath,
string $expectedErrorOutputSubstring,
string $parametersString = '',
): CliProcess {
return static::assertAnyErrorOutput(
$scriptPath,
$expectedErrorOutputSubstring,
$parametersString,
exceptionHeaderSubstring: ConfigException::class . ': ',
// Config exceptions happen before a specific error code is set for the exception handler:
shouldAssertExitCode: false,
shouldAssertStdErr: false,
);
}
/**
* Asserts that a {@see LogicException} exception was printed in {@see STDERR} (without a stack trace)
* during a script execution.
*/
protected static function assertExecutionLogicExceptionOutput(
string $scriptPath,
string $expectedErrorOutputSubstring,
string $parametersString = '',
): void {
static::assertAnyErrorOutput(
$scriptPath,
$expectedErrorOutputSubstring,
$parametersString,
exceptionHeaderSubstring: LogicException::class . ': ',
shouldAssertExitCode: true,
shouldAssertStdErr: true,
);
}
/**
* @param bool $shouldAssertStdErr Otherwise asserts a substring in {@see STDOUT} and {@see STDERR} concatenated.
* On some configurations unhandled exceptions are shown
* in {@see STDOUT} instead of {@see STDERR}.
*/
protected static function assertAnyErrorOutput(
string $scriptPath,
string $expectedErrorOutputSubstring,
string $parametersString = '',
?string $exceptionHeaderSubstring = null,
bool $shouldAssertExitCode = true,
bool $shouldAssertStdErr = true,
): CliProcess {
$command = 'php ' . escapeshellarg($scriptPath);
if ('' !== $parametersString) {
$command .= " {$parametersString}";
}
$result = new CliProcess($command);
// The heading space before a script name is needed for a script being clickable in PhpStorm console log
// as a script file link.
$assertOutputPrefix = " {$scriptPath}" . PHP_EOL
. "COMMAND: {$command}" . PHP_EOL;
if ($shouldAssertExitCode) {
assertSame(
Parametizer::ERROR_EXIT_CODE,
$result->getExitCode(),
"{$assertOutputPrefix}Unexpected exit code",
);
}
if ($shouldAssertStdErr) {
$actualContents = $result->getStdErr();
$assertOutputMessage = "{$assertOutputPrefix}Unexpected STDERR: {$actualContents}";
} else {
$actualContents = $result->getStdAll();
$assertOutputMessage = "{$assertOutputPrefix}Unexpected output: {$actualContents}";
}
assertStringContainsString(
($exceptionHeaderSubstring ?? '') . $expectedErrorOutputSubstring,
$actualContents,
$assertOutputMessage,
);
return $result;
}
/**
* Asserts an exact match of `$expectedErrorOutput` with STDERR.
*/
protected static function assertFullErrorOutput(
string $scriptPath,
string $expectedErrorOutput,
string $parametersString,
): void {
$command = 'php ' . escapeshellarg($scriptPath);
if ('' !== $parametersString) {
$command .= " {$parametersString}";
}
$result = new CliProcess($command);
$stdErr = $result->getStdErr();
// The heading space before a script name is needed for a script being clickable in PhpStorm console log
// as a script file link.
$assertOutputPrefix = " {$scriptPath}" . PHP_EOL
. "COMMAND: {$command}" . PHP_EOL;
assertSame(
Parametizer::ERROR_EXIT_CODE,
$result->getExitCode(),
"{$assertOutputPrefix}Unexpected exit code",
);
assertSame($expectedErrorOutput, $stdErr, "{$assertOutputPrefix}Unexpected STDERR: {$stdErr}");
}
/**
* Sources a script with alias, then executes an alias-based command in a Bash shell and returns the output.
*/
protected static function getBashAliasExecutionOutput(string $completionScriptPath, string $command): string {
/** @noinspection SpellCheckingInspection */
/**
* To make aliases actually work in a non-interactive shell these conditions must be made:
* 1. Do everything in a single execution.
* Because each time ({@see exec()}` / {@see shell_exec()} / etc.) a new "environment" is created.
* 2. Enable aliases support explicitly by starting with `shopt -s expand_aliases`.
* 3. Launch a command via an alias (with its parameters if needed) strictly on a new line (\n)
* within the shell execution.
* Dividing an alias and other commands with ';' (like `alias something=date; something`) will not work.
* From `man bash`:
* > Aliases are expanded when a command is read, not when it is executed. Therefore, an alias definition
* > appearing on the same line as another command does not take effect until the next line of input is read.
*/
$output = shell_exec(
sprintf(
"bash -c 'shopt -s expand_aliases; source %s%s%s'",
$completionScriptPath,
PHP_EOL,
$command,
),
);
return (string) $output;
}
}