Skip to content

Commit 48aa595

Browse files
committed
test errors limit
1 parent 94b8fe6 commit 48aa595

File tree

2 files changed

+189
-10
lines changed

2 files changed

+189
-10
lines changed

src/Command/ErrorFormatter/TableErrorFormatter.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,19 @@ public function __construct(
4646
private ?string $editorUrlTitle,
4747
#[AutowiredParameter]
4848
private string $usedLevel,
49+
private ?int $errorsBudget = null,
4950
)
5051
{
52+
if ($this->errorsBudget !== null) {
53+
return;
54+
}
55+
56+
$forceShowAll = getenv(self::FORCE_SHOW_ALL_ERRORS);
57+
if (!in_array($forceShowAll, [false, '0'], true)) {
58+
return;
59+
}
60+
61+
$this->errorsBudget = self::ERRORS_LIMIT;
5162
}
5263

5364
/** @api */
@@ -92,13 +103,7 @@ public function formatErrors(
92103
$fileErrors[$fileSpecificError->getFile()][] = $fileSpecificError;
93104
}
94105

95-
$forceShowAll = getenv(self::FORCE_SHOW_ALL_ERRORS);
96-
if (in_array($forceShowAll, [false, '0'], true)) {
97-
$errorsBudget = self::ERRORS_LIMIT;
98-
} else {
99-
$errorsBudget = null;
100-
}
101-
106+
$errorsBudget = $this->errorsBudget;
102107
$printedErrors = 0;
103108
foreach ($fileErrors as $file => $errors) {
104109
$rows = [];
@@ -186,7 +191,7 @@ public function formatErrors(
186191
}
187192

188193
if ($errorsBudget !== null && $printedErrors > $errorsBudget) {
189-
$style->error(sprintf('Found %s+ errors', self::ERRORS_LIMIT));
194+
$style->error(sprintf('Found %s+ errors', $errorsBudget));
190195

191196
$note = [];
192197
$note[] = sprintf('Result is limited to the first %d errors', $errorsBudget);

tests/PHPStan/Command/ErrorFormatter/TableErrorFormatterTest.php

Lines changed: 176 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)