@@ -35,6 +35,7 @@ protected function tearDown(): void
3535 putenv ('COLUMNS ' );
3636 putenv ('TERM_PROGRAM ' );
3737 putenv ('TERMINAL_EMULATOR ' . ($ this ->terminalEmulator !== false ? '= ' . $ this ->terminalEmulator : '' ));
38+ putenv ('PHPSTAN_TABLE_ERROR_FORMATTER_FORCE_SHOW_ALL_ERRORS ' );
3839 }
3940
4041 public static function dataFormatterOutputProvider (): iterable
@@ -294,6 +295,173 @@ public function testFormatErrors(
294295 $ this ->assertSame ($ expected , $ this ->getOutputContent (false , $ verbose ), sprintf ('%s: output do not match ' , $ message ));
295296 }
296297
298+ public static function dataErrorLimit (): iterable
299+ {
300+ yield [
301+ 'errorsBudget ' => null ,
302+ 'expected ' => ' ------ -------------------------------
303+ Line Foo.php (in context of trait)
304+ ------ -------------------------------
305+ 12 Test
306+ 13 Test
307+ 14 Test
308+ 15 Test
309+ ------ -------------------------------
310+
311+
312+ [ERROR] Found 4 errors
313+
314+ ' ,
315+ ];
316+ yield [
317+ 'errorsBudget ' => 1 ,
318+ 'expected ' => ' ------ -------------------------------
319+ Line Foo.php (in context of trait)
320+ ------ -------------------------------
321+ 12 Test
322+ ------ -------------------------------
323+
324+
325+ [ERROR] Found 1+ errors
326+
327+ ! [NOTE] Result is limited to the first 1 errors
328+ ! - Pass PHPSTAN_TABLE_ERROR_FORMATTER_FORCE_SHOW_ALL_ERRORS=1
329+ ! environment variable to show all errors
330+ ! - Consider using PHPStan Pro for more comfortable error browsing
331+ ! Learn more: https://phpstan.com
332+
333+ ' ,
334+ ];
335+
336+ yield [
337+ 'errorsBudget ' => 3 ,
338+ 'usedLevel ' => '8 ' ,
339+ 'expected ' => ' ------ -------------------------------
340+ Line Foo.php (in context of trait)
341+ ------ -------------------------------
342+ 12 Test
343+ 13 Test
344+ 14 Test
345+ ------ -------------------------------
346+
347+
348+ [ERROR] Found 3+ errors
349+
350+ ! [NOTE] Result is limited to the first 3 errors
351+ ! - Consider lowering the PHPStan level
352+ ! - Pass PHPSTAN_TABLE_ERROR_FORMATTER_FORCE_SHOW_ALL_ERRORS=1
353+ ! environment variable to show all errors
354+ ! - Consider using PHPStan Pro for more comfortable error browsing
355+ ! Learn more: https://phpstan.com
356+
357+ ' ,
358+ ];
359+
360+ yield [
361+ 'errorsBudget ' => 3 ,
362+ 'expected ' => ' ------ -------------------------------
363+ Line Foo.php (in context of trait)
364+ ------ -------------------------------
365+ 12 Test
366+ 13 Test
367+ 14 Test
368+ ------ -------------------------------
369+
370+
371+ [ERROR] Found 3+ errors
372+
373+ ! [NOTE] Result is limited to the first 3 errors
374+ ! - Pass PHPSTAN_TABLE_ERROR_FORMATTER_FORCE_SHOW_ALL_ERRORS=1
375+ ! environment variable to show all errors
376+ ! - Consider using PHPStan Pro for more comfortable error browsing
377+ ! Learn more: https://phpstan.com
378+
379+ ' ,
380+ ];
381+
382+ yield [
383+ 'errorsBudget ' => 4 ,
384+ 'expected ' => ' ------ -------------------------------
385+ Line Foo.php (in context of trait)
386+ ------ -------------------------------
387+ 12 Test
388+ 13 Test
389+ 14 Test
390+ 15 Test
391+ ------ -------------------------------
392+
393+
394+ [ERROR] Found 4 errors
395+
396+ ' ,
397+ ];
398+ yield [
399+ 'errorsBudget ' => 5 ,
400+ 'expected ' => ' ------ -------------------------------
401+ Line Foo.php (in context of trait)
402+ ------ -------------------------------
403+ 12 Test
404+ 13 Test
405+ 14 Test
406+ 15 Test
407+ ------ -------------------------------
408+
409+
410+ [ERROR] Found 4 errors
411+
412+ ' ,
413+ ];
414+
415+ yield [
416+ 'usedLevel ' => '8 ' ,
417+ 'showAllErrors ' => true ,
418+ 'expected ' => ' ------ -------------------------------
419+ Line Foo.php (in context of trait)
420+ ------ -------------------------------
421+ 12 Test
422+ 13 Test
423+ 14 Test
424+ 15 Test
425+ ------ -------------------------------
426+
427+
428+ [ERROR] Found 4 errors
429+
430+ ' ,
431+ ];
432+ }
433+
434+ #[DataProvider('dataErrorLimit ' )]
435+ public function testErrorLimit (
436+ string $ expected ,
437+ ?int $ errorsBudget = null ,
438+ string $ usedLevel = CommandHelper::DEFAULT_LEVEL ,
439+ bool $ showAllErrors = false ,
440+ ): void
441+ {
442+ if ($ showAllErrors ) {
443+ if ($ errorsBudget !== null ) {
444+ $ this ->fail ('showAllErrors cannot be true when errorsBudget is set ' );
445+ }
446+ putenv ('PHPSTAN_TABLE_ERROR_FORMATTER_FORCE_SHOW_ALL_ERRORS=1 ' );
447+ $ errorsBudget = null ;
448+ }
449+
450+ $ formatter = $ this ->createErrorFormatter (
451+ null ,
452+ usedLevel: $ usedLevel ,
453+ errorsBudget: $ errorsBudget ,
454+ );
455+ $ errors = [];
456+ $ errors [] = new Error ('Test ' , 'Foo.php (in context of trait) ' , 12 , filePath: 'Foo.php ' , traitFilePath: 'Bar.php ' );
457+ $ errors [] = new Error ('Test ' , 'Foo.php (in context of trait) ' , 13 , filePath: 'Foo.php ' , traitFilePath: 'Bar.php ' );
458+ $ errors [] = new Error ('Test ' , 'Foo.php (in context of trait) ' , 14 , filePath: 'Foo.php ' , traitFilePath: 'Bar.php ' );
459+ $ errors [] = new Error ('Test ' , 'Foo.php (in context of trait) ' , 15 , filePath: 'Foo.php ' , traitFilePath: 'Bar.php ' );
460+ $ formatter ->formatErrors (new AnalysisResult ($ errors , [], [], [], [], false , null , true , 0 , false , []), $ this ->getOutput ());
461+
462+ $ this ->assertStringContainsString ($ expected , $ this ->getOutputContent ());
463+ }
464+
297465 public function testEditorUrlWithTrait (): void
298466 {
299467 $ formatter = $ this ->createErrorFormatter ('editor://%file%/%line% ' );
@@ -450,7 +618,12 @@ public function testJetBrainsTerminalRelativePath(): void
450618 $ this ->assertStringContainsString ('at rel/Foo.php:12 ' , $ this ->getOutputContent (true ));
451619 }
452620
453- private function createErrorFormatter (?string $ editorUrl , ?string $ editorUrlTitle = null ): TableErrorFormatter
621+ private function createErrorFormatter (
622+ ?string $ editorUrl ,
623+ ?string $ editorUrlTitle = null ,
624+ string $ usedLevel = CommandHelper::DEFAULT_LEVEL ,
625+ ?int $ errorsBudget = null ,
626+ ): TableErrorFormatter
454627 {
455628 $ relativePathHelper = new FuzzyRelativePathHelper (new NullRelativePathHelper (), self ::DIRECTORY_PATH , [], '/ ' );
456629
@@ -464,7 +637,8 @@ private function createErrorFormatter(?string $editorUrl, ?string $editorUrlTitl
464637 false ,
465638 $ editorUrl ,
466639 $ editorUrlTitle ,
467- CommandHelper::DEFAULT_LEVEL ,
640+ $ usedLevel ,
641+ $ errorsBudget ,
468642 );
469643 }
470644
0 commit comments