/
BaselineNeonErrorFormatterTest.php
418 lines (383 loc) · 9.47 KB
/
BaselineNeonErrorFormatterTest.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
<?php declare(strict_types = 1);
namespace PHPStan\Command\ErrorFormatter;
use Generator;
use Nette\Neon\Neon;
use PHPStan\Analyser\Error;
use PHPStan\Command\AnalysisResult;
use PHPStan\Command\ErrorsConsoleStyle;
use PHPStan\Command\Symfony\SymfonyOutput;
use PHPStan\Command\Symfony\SymfonyStyle;
use PHPStan\File\SimpleRelativePathHelper;
use PHPStan\ShouldNotHappenException;
use PHPStan\Testing\ErrorFormatterTestCase;
use PHPUnit\Framework\Assert;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Output\StreamOutput;
use function fopen;
use function mt_srand;
use function rewind;
use function shuffle;
use function sprintf;
use function str_repeat;
use function stream_get_contents;
use function substr;
use function trim;
class BaselineNeonErrorFormatterTest extends ErrorFormatterTestCase
{
public function dataFormatterOutputProvider(): iterable
{
yield [
'No errors',
0,
0,
0,
[],
];
yield [
'One file error',
1,
1,
0,
[
[
'message' => '#^Foo$#',
'count' => 1,
'path' => 'folder with unicode 😃/file name with "spaces" and unicode 😃.php',
],
],
];
yield [
'Multiple file errors',
1,
4,
0,
[
[
'message' => "#^Bar\nBar2$#",
'count' => 1,
'path' => 'folder with unicode 😃/file name with "spaces" and unicode 😃.php',
],
[
'message' => '#^Foo$#',
'count' => 1,
'path' => 'folder with unicode 😃/file name with "spaces" and unicode 😃.php',
],
[
'message' => "#^Bar\nBar2$#",
'count' => 1,
'path' => 'foo.php',
],
[
'message' => '#^Foo$#',
'count' => 1,
'path' => 'foo.php',
],
],
];
yield [
'Multiple file, multiple generic errors',
1,
4,
2,
[
[
'message' => "#^Bar\nBar2$#",
'count' => 1,
'path' => 'folder with unicode 😃/file name with "spaces" and unicode 😃.php',
],
[
'message' => '#^Foo$#',
'count' => 1,
'path' => 'folder with unicode 😃/file name with "spaces" and unicode 😃.php',
],
[
'message' => "#^Bar\nBar2$#",
'count' => 1,
'path' => 'foo.php',
],
[
'message' => '#^Foo$#',
'count' => 1,
'path' => 'foo.php',
],
],
];
}
/**
* @dataProvider dataFormatterOutputProvider
*
* @param mixed[] $expected
*/
public function testFormatErrors(
string $message,
int $exitCode,
int $numFileErrors,
int $numGenericErrors,
array $expected,
): void
{
$formatter = new BaselineNeonErrorFormatter(new SimpleRelativePathHelper(self::DIRECTORY_PATH));
$this->assertSame($exitCode, $formatter->formatErrors(
$this->getAnalysisResult($numFileErrors, $numGenericErrors),
$this->getOutput(),
'',
), sprintf('%s: response code do not match', $message));
$this->assertSame(trim(Neon::encode(['parameters' => ['ignoreErrors' => $expected]], Neon::BLOCK)), trim($this->getOutputContent()), sprintf('%s: output do not match', $message));
}
public function testFormatErrorMessagesRegexEscape(): void
{
$formatter = new BaselineNeonErrorFormatter(new SimpleRelativePathHelper(self::DIRECTORY_PATH));
$result = new AnalysisResult(
[new Error('Escape Regex with file # ~ \' ()', 'Testfile')],
['Escape Regex without file # ~ <> \' ()'],
[],
[],
false,
null,
true,
);
$formatter->formatErrors(
$result,
$this->getOutput(),
'',
);
self::assertSame(
trim(
Neon::encode([
'parameters' => [
'ignoreErrors' => [
[
'message' => "#^Escape Regex with file \\# ~ ' \\(\\)$#",
'count' => 1,
'path' => 'Testfile',
],
],
],
], Neon::BLOCK),
),
trim($this->getOutputContent()),
);
}
public function testEscapeDiNeon(): void
{
$formatter = new BaselineNeonErrorFormatter(new SimpleRelativePathHelper(self::DIRECTORY_PATH));
$result = new AnalysisResult(
[new Error('Test %value%', 'Testfile')],
[],
[],
[],
false,
null,
true,
);
$formatter->formatErrors(
$result,
$this->getOutput(),
'',
);
self::assertSame(
trim(
Neon::encode([
'parameters' => [
'ignoreErrors' => [
[
'message' => '#^Test %%value%%$#',
'count' => 1,
'path' => 'Testfile',
],
],
],
], Neon::BLOCK),
),
trim($this->getOutputContent()),
);
}
/**
* @return Generator<int, array{list<Error>}, void, void>
*/
public function outputOrderingProvider(): Generator
{
$errors = [
new Error('Error #2', 'TestfileA', 1),
new Error('A different error #1', 'TestfileA', 3),
new Error('Second error in a different file', 'TestfileB', 4),
new Error('Error #1 in a different file', 'TestfileB', 5),
new Error('Second error in a different file', 'TestfileB', 6),
new Error('Error with Windows directory separators', 'TestFiles\\TestA', 1),
new Error('Error with Unix directory separators', 'TestFiles/TestA', 1),
new Error('Error without directory separators', 'TestFilesFoo', 1),
];
yield [$errors];
mt_srand(0);
for ($i = 0; $i < 3; ++$i) {
shuffle($errors);
yield [$errors];
}
}
/**
* @dataProvider outputOrderingProvider
* @param list<Error> $errors
*/
public function testOutputOrdering(array $errors): void
{
$formatter = new BaselineNeonErrorFormatter(new SimpleRelativePathHelper(self::DIRECTORY_PATH));
$result = new AnalysisResult(
$errors,
[],
[],
[],
false,
null,
true,
);
$formatter->formatErrors(
$result,
$this->getOutput(),
'',
);
self::assertSame(
trim(Neon::encode([
'parameters' => [
'ignoreErrors' => [
[
'message' => '#^Error with Unix directory separators$#',
'count' => 1,
'path' => 'TestFiles/TestA',
],
[
'message' => '#^Error with Windows directory separators$#',
'count' => 1,
'path' => 'TestFiles/TestA',
],
[
'message' => '#^Error without directory separators$#',
'count' => 1,
'path' => 'TestFilesFoo',
],
[
'message' => '#^A different error \\#1$#',
'count' => 1,
'path' => 'TestfileA',
],
[
'message' => '#^Error \\#2$#',
'count' => 1,
'path' => 'TestfileA',
],
[
'message' => '#^Error \\#1 in a different file$#',
'count' => 1,
'path' => 'TestfileB',
],
[
'message' => '#^Second error in a different file$#',
'count' => 2,
'path' => 'TestfileB',
],
],
],
], Neon::BLOCK)),
$f = trim($this->getOutputContent()),
);
}
/**
* @return Generator<string, array{errors: list<Error>}>
*/
public function endOfFileNewlinesProvider(): Generator
{
$existingBaselineContentWithoutEndNewlines = 'parameters:
ignoreErrors:
-
message: "#^Existing error$#"
count: 1
path: TestfileA';
yield 'one error' => [
'errors' => [
new Error('Error #1', 'TestfileA', 1),
],
'existingBaselineContent' => $existingBaselineContentWithoutEndNewlines . "\n",
'expectedNewlinesCount' => 1,
];
yield 'no errors' => [
'errors' => [],
'existingBaselineContent' => $existingBaselineContentWithoutEndNewlines . "\n",
'expectedNewlinesCount' => 1,
];
yield 'one error with 2 newlines' => [
'errors' => [
new Error('Error #1', 'TestfileA', 1),
],
'existingBaselineContent' => $existingBaselineContentWithoutEndNewlines . "\n\n",
'expectedNewlinesCount' => 2,
];
yield 'no errors with 2 newlines' => [
'errors' => [],
'existingBaselineContent' => $existingBaselineContentWithoutEndNewlines . "\n\n",
'expectedNewlinesCount' => 2,
];
yield 'one error with 0 newlines' => [
'errors' => [
new Error('Error #1', 'TestfileA', 1),
],
'existingBaselineContent' => $existingBaselineContentWithoutEndNewlines,
'expectedNewlinesCount' => 0,
];
yield 'one error with 3 newlines' => [
'errors' => [
new Error('Error #1', 'TestfileA', 1),
],
'existingBaselineContent' => $existingBaselineContentWithoutEndNewlines . "\n\n\n",
'expectedNewlinesCount' => 3,
];
yield 'empty existing baseline' => [
'errors' => [
new Error('Error #1', 'TestfileA', 1),
],
'existingBaselineContent' => '',
'expectedNewlinesCount' => 1,
];
}
/**
* @dataProvider endOfFileNewlinesProvider
*
* @param list<Error> $errors
*/
public function testEndOfFileNewlines(
array $errors,
string $existingBaselineContent,
int $expectedNewlinesCount,
): void
{
$formatter = new BaselineNeonErrorFormatter(new SimpleRelativePathHelper(self::DIRECTORY_PATH));
$result = new AnalysisResult(
$errors,
[],
[],
[],
false,
null,
true,
);
$resource = fopen('php://memory', 'w', false);
if ($resource === false) {
throw new ShouldNotHappenException();
}
$outputStream = new StreamOutput($resource, StreamOutput::VERBOSITY_NORMAL, false);
$errorConsoleStyle = new ErrorsConsoleStyle(new StringInput(''), $outputStream);
$output = new SymfonyOutput($outputStream, new SymfonyStyle($errorConsoleStyle));
$formatter->formatErrors(
$result,
$output,
$existingBaselineContent,
);
rewind($outputStream->getStream());
$content = stream_get_contents($outputStream->getStream());
if ($content === false) {
throw new ShouldNotHappenException();
}
if ($expectedNewlinesCount > 0) {
Assert::assertSame(str_repeat("\n", $expectedNewlinesCount), substr($content, -$expectedNewlinesCount));
}
Assert::assertNotSame("\n", substr($content, -($expectedNewlinesCount + 1), 1));
}
}