-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestCaseAbstract.php
More file actions
151 lines (132 loc) · 5.11 KB
/
Copy pathTestCaseAbstract.php
File metadata and controls
151 lines (132 loc) · 5.11 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
<?php
declare(strict_types=1);
namespace MagicPush\CliToolkit\Tests\Tests;
use LogicException;
use MagicPush\CliToolkit\Parametizer\Exception\ConfigException;
use MagicPush\CliToolkit\Parametizer\Parametizer;
use MagicPush\CliToolkit\Tests\utils\CliProcess;
use PHPUnit\Framework\TestCase;
use function PHPUnit\Framework\assertSame;
use function PHPUnit\Framework\assertStringContainsString;
abstract class TestCaseAbstract extends TestCase {
protected static function assertNoErrorsOutput(string $scriptPath, string $parametersString = ''): 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.
$failedAssertMessage = " {$scriptPath}" . PHP_EOL
. "COMMAND: {$command}" . PHP_EOL
. "Unexpected error output: {$result->getStdErr()}";
assertSame(0, $result->getExitCode(), $failedAssertMessage);
assertSame('', $result->getStdErr(), $failedAssertMessage);
return $result;
}
/**
* Asserts that an error message was printed without a stack trace.
*/
protected static function assertParseErrorOutput(
string $scriptPath,
string $expectedErrorOutputSubstring,
string $parametersString = '',
): CliProcess {
return self::assertAnyErrorOutput(
$scriptPath,
$expectedErrorOutputSubstring,
$parametersString,
shouldAssertExitCode: true,
);
}
/**
* Asserts that a {@see ConfigException} exception was printed out. Exit code may vary thus is not asserted.
*/
protected static function assertConfigExceptionOutput(
string $scriptPath,
string $expectedErrorOutputSubstring,
string $parametersString = '',
): void {
self::assertAnyErrorOutput(
$scriptPath,
$expectedErrorOutputSubstring,
$parametersString,
exceptionHeaderSubstring: ConfigException::class . ': ',
// Config exceptions happen before a specific error code is set for the exception handler:
shouldAssertExitCode: false,
);
}
/**
* Asserts that a {@see LogicException} exception was printed out.
*/
protected static function assertLogicExceptionOutput(
string $scriptPath,
string $expectedErrorOutputSubstring,
string $parametersString = '',
): void {
self::assertAnyErrorOutput(
$scriptPath,
$expectedErrorOutputSubstring,
$parametersString,
exceptionHeaderSubstring: LogicException::class . ': ',
shouldAssertExitCode: true,
);
}
protected static function assertAnyErrorOutput(
string $scriptPath,
string $expectedErrorOutputSubstring,
string $parametersString = '',
?string $exceptionHeaderSubstring = null,
bool $shouldAssertExitCode = false,
): CliProcess {
$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;
if ($shouldAssertExitCode) {
assertSame(
Parametizer::ERROR_EXIT_CODE,
$result->getExitCode(),
"{$assertOutputPrefix}Unexpected exit code",
);
}
$assertOutputMessage = "{$assertOutputPrefix}Unexpected STDERR: {$stdErr}";
assertStringContainsString(
($exceptionHeaderSubstring ?? '') . $expectedErrorOutputSubstring,
$stdErr,
$assertOutputMessage,
);
return $result;
}
/**
* Asserts an exact math of `$expectedErrorOutput` with STDERR.
*/
public 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}");
}
}