2020use function rewind ;
2121use function shuffle ;
2222use function sprintf ;
23+ use function str_repeat ;
2324use function stream_get_contents ;
2425use function substr ;
2526use function trim ;
@@ -128,6 +129,7 @@ public function testFormatErrors(
128129 $ this ->assertSame ($ exitCode , $ formatter ->formatErrors (
129130 $ this ->getAnalysisResult ($ numFileErrors , $ numGenericErrors ),
130131 $ this ->getOutput (),
132+ '' ,
131133 ), sprintf ('%s: response code do not match ' , $ message ));
132134
133135 $ 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
150152 $ formatter ->formatErrors (
151153 $ result ,
152154 $ this ->getOutput (),
155+ '' ,
153156 );
154157
155158 self ::assertSame (
@@ -186,6 +189,7 @@ public function testEscapeDiNeon(): void
186189 $ formatter ->formatErrors (
187190 $ result ,
188191 $ this ->getOutput (),
192+ '' ,
189193 );
190194 self ::assertSame (
191195 trim (
@@ -248,6 +252,7 @@ public function testOutputOrdering(array $errors): void
248252 $ formatter ->formatErrors (
249253 $ result ,
250254 $ this ->getOutput (),
255+ '' ,
251256 );
252257 self ::assertSame (
253258 trim (Neon::encode ([
@@ -300,14 +305,63 @@ public function testOutputOrdering(array $errors): void
300305 */
301306 public function endOfFileNewlinesProvider (): Generator
302307 {
308+ $ existingBaselineContentWithoutEndNewlines = 'parameters:
309+ ignoreErrors:
310+ -
311+ message: "#^Existing error$#"
312+ count: 1
313+ path: TestfileA ' ;
314+
303315 yield 'one error ' => [
304316 'errors ' => [
305317 new Error ('Error #1 ' , 'TestfileA ' , 1 ),
306318 ],
319+ 'existingBaselineContent ' => $ existingBaselineContentWithoutEndNewlines . "\n" ,
320+ 'expectedNewlinesCount ' => 1 ,
307321 ];
308322
309323 yield 'no errors ' => [
310324 '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 ,
311365 ];
312366 }
313367
@@ -316,7 +370,11 @@ public function endOfFileNewlinesProvider(): Generator
316370 *
317371 * @param list<Error> $errors
318372 */
319- public function testEndOfFileNewlines (array $ errors ): void
373+ public function testEndOfFileNewlines (
374+ array $ errors ,
375+ string $ existingBaselineContent ,
376+ int $ expectedNewlinesCount ,
377+ ): void
320378 {
321379 $ formatter = new BaselineNeonErrorFormatter (new SimpleRelativePathHelper (self ::DIRECTORY_PATH ));
322380 $ result = new AnalysisResult (
@@ -341,6 +399,7 @@ public function testEndOfFileNewlines(array $errors): void
341399 $ formatter ->formatErrors (
342400 $ result ,
343401 $ output ,
402+ $ existingBaselineContent ,
344403 );
345404
346405 rewind ($ outputStream ->getStream ());
@@ -350,8 +409,10 @@ public function testEndOfFileNewlines(array $errors): void
350409 throw new ShouldNotHappenException ();
351410 }
352411
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 ));
355416 }
356417
357418}
0 commit comments