20
20
use function rewind ;
21
21
use function shuffle ;
22
22
use function sprintf ;
23
+ use function str_repeat ;
23
24
use function stream_get_contents ;
24
25
use function substr ;
25
26
use function trim ;
@@ -128,6 +129,7 @@ public function testFormatErrors(
128
129
$ this ->assertSame ($ exitCode , $ formatter ->formatErrors (
129
130
$ this ->getAnalysisResult ($ numFileErrors , $ numGenericErrors ),
130
131
$ this ->getOutput (),
132
+ '' ,
131
133
), sprintf ('%s: response code do not match ' , $ message ));
132
134
133
135
$ this ->assertSame (trim (Neon::encode (['parameters ' => ['ignoreErrors ' => $ expected ]], Neon::BLOCK )), trim ($ this ->getOutputContent ()), sprintf ('%s: output do not match ' , $ message ));
@@ -150,6 +152,7 @@ public function testFormatErrorMessagesRegexEscape(): void
150
152
$ formatter ->formatErrors (
151
153
$ result ,
152
154
$ this ->getOutput (),
155
+ '' ,
153
156
);
154
157
155
158
self ::assertSame (
@@ -186,6 +189,7 @@ public function testEscapeDiNeon(): void
186
189
$ formatter ->formatErrors (
187
190
$ result ,
188
191
$ this ->getOutput (),
192
+ '' ,
189
193
);
190
194
self ::assertSame (
191
195
trim (
@@ -248,6 +252,7 @@ public function testOutputOrdering(array $errors): void
248
252
$ formatter ->formatErrors (
249
253
$ result ,
250
254
$ this ->getOutput (),
255
+ '' ,
251
256
);
252
257
self ::assertSame (
253
258
trim (Neon::encode ([
@@ -300,14 +305,63 @@ public function testOutputOrdering(array $errors): void
300
305
*/
301
306
public function endOfFileNewlinesProvider (): Generator
302
307
{
308
+ $ existingBaselineContentWithoutEndNewlines = 'parameters:
309
+ ignoreErrors:
310
+ -
311
+ message: "#^Existing error$#"
312
+ count: 1
313
+ path: TestfileA ' ;
314
+
303
315
yield 'one error ' => [
304
316
'errors ' => [
305
317
new Error ('Error #1 ' , 'TestfileA ' , 1 ),
306
318
],
319
+ 'existingBaselineContent ' => $ existingBaselineContentWithoutEndNewlines . "\n" ,
320
+ 'expectedNewlinesCount ' => 1 ,
307
321
];
308
322
309
323
yield 'no errors ' => [
310
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 ,
311
365
];
312
366
}
313
367
@@ -316,7 +370,11 @@ public function endOfFileNewlinesProvider(): Generator
316
370
*
317
371
* @param list<Error> $errors
318
372
*/
319
- public function testEndOfFileNewlines (array $ errors ): void
373
+ public function testEndOfFileNewlines (
374
+ array $ errors ,
375
+ string $ existingBaselineContent ,
376
+ int $ expectedNewlinesCount ,
377
+ ): void
320
378
{
321
379
$ formatter = new BaselineNeonErrorFormatter (new SimpleRelativePathHelper (self ::DIRECTORY_PATH ));
322
380
$ result = new AnalysisResult (
@@ -341,6 +399,7 @@ public function testEndOfFileNewlines(array $errors): void
341
399
$ formatter ->formatErrors (
342
400
$ result ,
343
401
$ output ,
402
+ $ existingBaselineContent ,
344
403
);
345
404
346
405
rewind ($ outputStream ->getStream ());
@@ -350,8 +409,10 @@ public function testEndOfFileNewlines(array $errors): void
350
409
throw new ShouldNotHappenException ();
351
410
}
352
411
353
- Assert::assertSame ("\n" , substr ($ content , -1 ));
354
- Assert::assertNotSame ("\n" , substr ($ content , -2 , 1 ));
412
+ if ($ expectedNewlinesCount > 0 ) {
413
+ Assert::assertSame (str_repeat ("\n" , $ expectedNewlinesCount ), substr ($ content , -$ expectedNewlinesCount ));
414
+ }
415
+ Assert::assertNotSame ("\n" , substr ($ content , -($ expectedNewlinesCount + 1 ), 1 ));
355
416
}
356
417
357
418
}
0 commit comments