-
Notifications
You must be signed in to change notification settings - Fork 501
/
Copy pathTableErrorFormatterTest.php
288 lines (241 loc) · 7.44 KB
/
TableErrorFormatterTest.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
<?php declare(strict_types = 1);
namespace PHPStan\Command\ErrorFormatter;
use PHPStan\Analyser\Error;
use PHPStan\Command\AnalysisResult;
use PHPStan\File\FuzzyRelativePathHelper;
use PHPStan\File\NullRelativePathHelper;
use PHPStan\File\SimpleRelativePathHelper;
use PHPStan\Testing\ErrorFormatterTestCase;
use function getenv;
use function putenv;
use function sprintf;
use const PHP_VERSION_ID;
class TableErrorFormatterTest extends ErrorFormatterTestCase
{
protected function setUp(): void
{
putenv('GITHUB_ACTIONS');
}
protected function tearDown(): void
{
putenv('COLUMNS');
putenv('TERM_PROGRAM');
}
public function dataFormatterOutputProvider(): iterable
{
yield [
'message' => 'No errors',
'exitCode' => 0,
'numFileErrors' => 0,
'numGenericErrors' => 0,
'extraEnvVars' => [],
'expected' => '
[OK] No errors
',
];
yield [
'message' => 'One file error',
'exitCode' => 1,
'numFileErrors' => 1,
'numGenericErrors' => 0,
'extraEnvVars' => [],
'expected' => ' ------ -------------------------------------------------------------------
Line folder with unicode 😃/file name with "spaces" and unicode 😃.php
------ -------------------------------------------------------------------
4 Foo
------ -------------------------------------------------------------------
[ERROR] Found 1 error
',
];
yield [
'message' => 'One generic error',
'exitCode' => 1,
'numFileErrors' => 0,
'numGenericErrors' => 1,
'extraEnvVars' => [],
'expected' => ' -- ---------------------
Error
-- ---------------------
first generic error
-- ---------------------
[ERROR] Found 1 error
',
];
yield [
'message' => 'Multiple file errors',
'exitCode' => 1,
'numFileErrors' => 4,
'numGenericErrors' => 0,
'extraEnvVars' => [],
'expected' => ' ------ -------------------------------------------------------------------
Line folder with unicode 😃/file name with "spaces" and unicode 😃.php
------ -------------------------------------------------------------------
2 Bar
Bar2
4 Foo
------ -------------------------------------------------------------------
------ ----------
Line foo.php
------ ----------
1 Foo
5 Bar
Bar2
💡 a tip
------ ----------
[ERROR] Found 4 errors
',
];
yield [
'message' => 'Multiple generic errors',
'exitCode' => 1,
'numFileErrors' => 0,
'numGenericErrors' => 2,
'extraEnvVars' => [],
'expected' => ' -- ----------------------
Error
-- ----------------------
first generic error
second generic error
-- ----------------------
[ERROR] Found 2 errors
',
];
yield [
'message' => 'Multiple file, multiple generic errors',
'exitCode' => 1,
'numFileErrors' => 4,
'numGenericErrors' => 2,
'extraEnvVars' => [],
'expected' => ' ------ -------------------------------------------------------------------
Line folder with unicode 😃/file name with "spaces" and unicode 😃.php
------ -------------------------------------------------------------------
2 Bar
Bar2
4 Foo
------ -------------------------------------------------------------------
------ ----------
Line foo.php
------ ----------
1 Foo
5 Bar
Bar2
💡 a tip
------ ----------
-- ----------------------
Error
-- ----------------------
first generic error
second generic error
-- ----------------------
[ERROR] Found 6 errors
',
];
yield [
'message' => 'One file error, called via Visual Studio Code',
'exitCode' => 1,
'numFileErrors' => 1,
'numGenericErrors' => 0,
'extraEnvVars' => ['TERM_PROGRAM=vscode'],
'expected' => ' ------ -------------------------------------------------------------------
Line folder with unicode 😃/file name with "spaces" and unicode 😃.php
------ -------------------------------------------------------------------
:4 Foo
------ -------------------------------------------------------------------
[ERROR] Found 1 error
',
];
}
/**
* @dataProvider dataFormatterOutputProvider
* @param array<string> $extraEnvVars
*/
public function testFormatErrors(
string $message,
int $exitCode,
int $numFileErrors,
int $numGenericErrors,
array $extraEnvVars,
string $expected,
): void
{
if (PHP_VERSION_ID >= 80100) {
self::markTestSkipped('Skipped on PHP 8.1 because of different result');
}
$formatter = $this->createErrorFormatter(null);
// NOTE: extra env vars need to be cleared in tearDown()
foreach ($extraEnvVars as $envVar) {
putenv($envVar);
}
$this->assertSame($exitCode, $formatter->formatErrors(
$this->getAnalysisResult($numFileErrors, $numGenericErrors),
$this->getOutput(),
), sprintf('%s: response code do not match', $message));
$this->assertEquals($expected, $this->getOutputContent(), sprintf('%s: output do not match', $message));
}
public function testEditorUrlWithTrait(): void
{
$formatter = $this->createErrorFormatter('editor://%file%/%line%');
$error = new Error('Test', 'Foo.php (in context of trait)', 12, true, 'Foo.php', 'Bar.php');
$formatter->formatErrors(new AnalysisResult([$error], [], [], [], [], false, null, true, 0, false), $this->getOutput());
$this->assertStringContainsString('Bar.php', $this->getOutputContent());
}
public function testEditorUrlWithRelativePath(): void
{
if (getenv('TERMINAL_EMULATOR') === 'JetBrains-JediTerm') {
$this->markTestSkipped('PhpStorm console does not support links in console.');
}
$formatter = $this->createErrorFormatter('editor://custom/path/%relFile%/%line%');
$error = new Error('Test', 'Foo.php', 12, true, self::DIRECTORY_PATH . '/rel/Foo.php');
$formatter->formatErrors(new AnalysisResult([$error], [], [], [], [], false, null, true, 0, false), $this->getOutput(true));
$this->assertStringContainsString('editor://custom/path/rel/Foo.php', $this->getOutputContent(true));
}
public function testEditorUrlWithCustomTitle(): void
{
$formatter = $this->createErrorFormatter('editor://any', '%relFile%:%line%');
$error = new Error('Test', 'Foo.php', 12, true, self::DIRECTORY_PATH . '/rel/Foo.php');
$formatter->formatErrors(new AnalysisResult([$error], [], [], [], [], false, null, true, 0, false), $this->getOutput(true));
$this->assertStringContainsString('rel/Foo.php:12', $this->getOutputContent(true));
}
public function testBug6727(): void
{
putenv('COLUMNS=30');
$formatter = $this->createErrorFormatter(null);
$formatter->formatErrors(
new AnalysisResult(
[
new Error(
'Method MissingTypehintPromotedProperties\Foo::__construct() has parameter $foo with no value type specified in iterable type array.',
'/var/www/html/app/src/Foo.php (in context of class App\Foo\Bar)',
5,
),
],
[],
[],
[],
[],
false,
null,
true,
0,
false,
),
$this->getOutput(),
);
self::expectNotToPerformAssertions();
}
private function createErrorFormatter(?string $editorUrl, ?string $editorUrlTitle = null): TableErrorFormatter
{
$relativePathHelper = new FuzzyRelativePathHelper(new NullRelativePathHelper(), self::DIRECTORY_PATH, [], '/');
return new TableErrorFormatter(
$relativePathHelper,
new SimpleRelativePathHelper(self::DIRECTORY_PATH),
new CiDetectedErrorFormatter(
new GithubErrorFormatter($relativePathHelper),
new TeamcityErrorFormatter($relativePathHelper),
),
false,
$editorUrl,
$editorUrlTitle,
);
}
}