6
6
use Nette \Neon \Neon ;
7
7
use PHPStan \Analyser \Error ;
8
8
use PHPStan \Command \AnalysisResult ;
9
+ use PHPStan \Command \ErrorsConsoleStyle ;
10
+ use PHPStan \Command \Symfony \SymfonyOutput ;
11
+ use PHPStan \Command \Symfony \SymfonyStyle ;
9
12
use PHPStan \File \SimpleRelativePathHelper ;
13
+ use PHPStan \ShouldNotHappenException ;
10
14
use PHPStan \Testing \ErrorFormatterTestCase ;
15
+ use PHPUnit \Framework \Assert ;
16
+ use Symfony \Component \Console \Input \StringInput ;
17
+ use Symfony \Component \Console \Output \StreamOutput ;
18
+ use function fopen ;
11
19
use function mt_srand ;
20
+ use function rewind ;
12
21
use function shuffle ;
13
22
use function sprintf ;
23
+ use function str_repeat ;
24
+ use function stream_get_contents ;
25
+ use function substr ;
14
26
use function trim ;
15
27
16
28
class BaselineNeonErrorFormatterTest extends ErrorFormatterTestCase
@@ -117,6 +129,7 @@ public function testFormatErrors(
117
129
$ this ->assertSame ($ exitCode , $ formatter ->formatErrors (
118
130
$ this ->getAnalysisResult ($ numFileErrors , $ numGenericErrors ),
119
131
$ this ->getOutput (),
132
+ '' ,
120
133
), sprintf ('%s: response code do not match ' , $ message ));
121
134
122
135
$ this ->assertSame (trim (Neon::encode (['parameters ' => ['ignoreErrors ' => $ expected ]], Neon::BLOCK )), trim ($ this ->getOutputContent ()), sprintf ('%s: output do not match ' , $ message ));
@@ -139,6 +152,7 @@ public function testFormatErrorMessagesRegexEscape(): void
139
152
$ formatter ->formatErrors (
140
153
$ result ,
141
154
$ this ->getOutput (),
155
+ '' ,
142
156
);
143
157
144
158
self ::assertSame (
@@ -175,6 +189,7 @@ public function testEscapeDiNeon(): void
175
189
$ formatter ->formatErrors (
176
190
$ result ,
177
191
$ this ->getOutput (),
192
+ '' ,
178
193
);
179
194
self ::assertSame (
180
195
trim (
@@ -237,6 +252,7 @@ public function testOutputOrdering(array $errors): void
237
252
$ formatter ->formatErrors (
238
253
$ result ,
239
254
$ this ->getOutput (),
255
+ '' ,
240
256
);
241
257
self ::assertSame (
242
258
trim (Neon::encode ([
@@ -284,4 +300,137 @@ public function testOutputOrdering(array $errors): void
284
300
);
285
301
}
286
302
303
+ /**
304
+ * @return Generator<string, array{errors: list<Error>}>
305
+ */
306
+ public function endOfFileNewlinesProvider (): Generator
307
+ {
308
+ $ existingBaselineContentWithoutEndNewlines = 'parameters:
309
+ ignoreErrors:
310
+ -
311
+ message: "#^Existing error$#"
312
+ count: 1
313
+ path: TestfileA ' ;
314
+
315
+ yield 'one error ' => [
316
+ 'errors ' => [
317
+ new Error ('Error #1 ' , 'TestfileA ' , 1 ),
318
+ ],
319
+ 'existingBaselineContent ' => $ existingBaselineContentWithoutEndNewlines . "\n" ,
320
+ 'expectedNewlinesCount ' => 1 ,
321
+ ];
322
+
323
+ yield 'no errors ' => [
324
+ 'errors ' => [],
325
+ 'existingBaselineContent ' => $ existingBaselineContentWithoutEndNewlines . "\n" ,
326
+ 'expectedNewlinesCount ' => 1 ,
327
+ ];
328
+
329
+ yield 'one error with 2 newlines ' => [
330
+ 'errors ' => [
331
+ new Error ('Error #1 ' , 'TestfileA ' , 1 ),
332
+ ],
333
+ 'existingBaselineContent ' => $ existingBaselineContentWithoutEndNewlines . "\n\n" ,
334
+ 'expectedNewlinesCount ' => 2 ,
335
+ ];
336
+
337
+ yield 'no errors with 2 newlines ' => [
338
+ 'errors ' => [],
339
+ 'existingBaselineContent ' => $ existingBaselineContentWithoutEndNewlines . "\n\n" ,
340
+ 'expectedNewlinesCount ' => 2 ,
341
+ ];
342
+
343
+ yield 'one error with 0 newlines ' => [
344
+ 'errors ' => [
345
+ new Error ('Error #1 ' , 'TestfileA ' , 1 ),
346
+ ],
347
+ 'existingBaselineContent ' => $ existingBaselineContentWithoutEndNewlines ,
348
+ 'expectedNewlinesCount ' => 0 ,
349
+ ];
350
+
351
+ yield 'one error with 3 newlines ' => [
352
+ 'errors ' => [
353
+ new Error ('Error #1 ' , 'TestfileA ' , 1 ),
354
+ ],
355
+ 'existingBaselineContent ' => $ existingBaselineContentWithoutEndNewlines . "\n\n\n" ,
356
+ 'expectedNewlinesCount ' => 3 ,
357
+ ];
358
+
359
+ yield 'empty existing baseline ' => [
360
+ 'errors ' => [
361
+ new Error ('Error #1 ' , 'TestfileA ' , 1 ),
362
+ ],
363
+ 'existingBaselineContent ' => '' ,
364
+ 'expectedNewlinesCount ' => 1 ,
365
+ ];
366
+
367
+ yield 'empty existing baseline, no new errors ' => [
368
+ 'errors ' => [],
369
+ 'existingBaselineContent ' => '' ,
370
+ 'expectedNewlinesCount ' => 1 ,
371
+ ];
372
+
373
+ yield 'empty existing baseline with a newline, no new errors ' => [
374
+ 'errors ' => [],
375
+ 'existingBaselineContent ' => "\n" ,
376
+ 'expectedNewlinesCount ' => 1 ,
377
+ ];
378
+
379
+ yield 'empty existing baseline with 2 newlines, no new errors ' => [
380
+ 'errors ' => [],
381
+ 'existingBaselineContent ' => "\n\n" ,
382
+ 'expectedNewlinesCount ' => 2 ,
383
+ ];
384
+ }
385
+
386
+ /**
387
+ * @dataProvider endOfFileNewlinesProvider
388
+ *
389
+ * @param list<Error> $errors
390
+ */
391
+ public function testEndOfFileNewlines (
392
+ array $ errors ,
393
+ string $ existingBaselineContent ,
394
+ int $ expectedNewlinesCount ,
395
+ ): void
396
+ {
397
+ $ formatter = new BaselineNeonErrorFormatter (new SimpleRelativePathHelper (self ::DIRECTORY_PATH ));
398
+ $ result = new AnalysisResult (
399
+ $ errors ,
400
+ [],
401
+ [],
402
+ [],
403
+ false ,
404
+ null ,
405
+ true ,
406
+ );
407
+
408
+ $ resource = fopen ('php://memory ' , 'w ' , false );
409
+ if ($ resource === false ) {
410
+ throw new ShouldNotHappenException ();
411
+ }
412
+ $ outputStream = new StreamOutput ($ resource , StreamOutput::VERBOSITY_NORMAL , false );
413
+
414
+ $ errorConsoleStyle = new ErrorsConsoleStyle (new StringInput ('' ), $ outputStream );
415
+ $ output = new SymfonyOutput ($ outputStream , new SymfonyStyle ($ errorConsoleStyle ));
416
+
417
+ $ formatter ->formatErrors (
418
+ $ result ,
419
+ $ output ,
420
+ $ existingBaselineContent ,
421
+ );
422
+
423
+ rewind ($ outputStream ->getStream ());
424
+
425
+ $ content = stream_get_contents ($ outputStream ->getStream ());
426
+ if ($ content === false ) {
427
+ throw new ShouldNotHappenException ();
428
+ }
429
+
430
+ if ($ expectedNewlinesCount > 0 ) {
431
+ Assert::assertSame (str_repeat ("\n" , $ expectedNewlinesCount ), substr ($ content , -$ expectedNewlinesCount ));
432
+ }
433
+ Assert::assertNotSame ("\n" , substr ($ content , -($ expectedNewlinesCount + 1 ), 1 ));
434
+ }
435
+
287
436
}
0 commit comments