Repository navigation
Expand file tree
/
Copy pathGithubErrorFormatterTest.php
More file actions
143 lines (125 loc) · 4.19 KB
/
Copy pathGithubErrorFormatterTest.php
File metadata and controls
143 lines (125 loc) · 4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php declare(strict_types = 1);
namespace PHPStan\Command\ErrorFormatter;
use PHPStan\Analyser\Error;
use PHPStan\Analyser\FixedErrorDiff;
use PHPStan\Command\AnalysisResult;
use PHPStan\File\FuzzyRelativePathHelper;
use PHPStan\File\NullRelativePathHelper;
use PHPStan\Testing\ErrorFormatterTestCase;
use PHPUnit\Framework\Attributes\DataProvider;
use function sprintf;
class GithubErrorFormatterTest extends ErrorFormatterTestCase
{
public static function dataFormatterOutputProvider(): iterable
{
yield [
'No errors',
0,
0,
0,
'',
];
yield [
'One file error',
1,
1,
0,
'::error file=folder with unicode 😃/file name with "spaces" and unicode 😃.php,line=4,col=0::Foo
',
];
yield [
'One generic error',
1,
0,
1,
'::error ::first generic error
',
];
yield [
'Multiple file errors',
1,
4,
0,
'::error file=folder with unicode 😃/file name with "spaces" and unicode 😃.php,line=2,col=0::Bar%0ABar2
::error file=folder with unicode 😃/file name with "spaces" and unicode 😃.php,line=4,col=0::Foo
::error file=foo.php,line=1,col=0::Foo<Bar>
::error file=foo.php,line=5,col=0::Bar%0ABar2%0A💡 a tip
',
];
yield [
'Multiple generic errors',
1,
0,
2,
'::error ::first generic error
::error ::second generic<error>
',
];
yield [
'Multiple file, multiple generic errors',
1,
4,
2,
'::error file=folder with unicode 😃/file name with "spaces" and unicode 😃.php,line=2,col=0::Bar%0ABar2
::error file=folder with unicode 😃/file name with "spaces" and unicode 😃.php,line=4,col=0::Foo
::error file=foo.php,line=1,col=0::Foo<Bar>
::error file=foo.php,line=5,col=0::Bar%0ABar2%0A💡 a tip
::error ::first generic error
::error ::second generic<error>
',
];
yield [
'One file, with @ tags',
1,
[6, 1],
0,
'::error file=bar.php,line=5,col=0::Error with `@param` or `@phpstan-param` and class@anonymous in the message.
',
];
}
/**
* @param array{int, int}|int $numFileErrors
*/
#[DataProvider('dataFormatterOutputProvider')]
public function testFormatErrors(
string $message,
int $exitCode,
array|int $numFileErrors,
int $numGenericErrors,
string $expected,
): void
{
$relativePathHelper = new FuzzyRelativePathHelper(new NullRelativePathHelper(), self::DIRECTORY_PATH, [], '/');
$formatter = new GithubErrorFormatter(
$relativePathHelper,
);
$this->assertSame($exitCode, $formatter->formatErrors(
$this->getAnalysisResult($numFileErrors, $numGenericErrors),
$this->getOutput(),
), sprintf('%s: response code do not match', $message));
$this->assertSame($expected, $this->getOutputContent(), sprintf('%s: output do not match', $message));
}
public function testFormatErrorsLikeTable(): void
{
$relativePathHelper = new FuzzyRelativePathHelper(new NullRelativePathHelper(), self::DIRECTORY_PATH, [], '/');
$formatter = new GithubErrorFormatter($relativePathHelper);
$errors = [
new Error('Fixable error', self::DIRECTORY_PATH . '/foo.php', 1, identifier: 'foo.bar', fixedErrorDiffs: [new FixedErrorDiff('foo.php', 'hash', 'diff')]),
new Error('Non-ignorable error', self::DIRECTORY_PATH . '/foo.php', 2, canBeIgnored: false, identifier: 'foo.baz'),
new Error('Multi-line tip', self::DIRECTORY_PATH . '/foo.php', 3, tip: "• First line\n• Second <fg=cyan>line</> in %configurationFile%"),
new Error('Tip with type', self::DIRECTORY_PATH . '/foo.php', 4, tip: 'Use array<int, string> instead.'),
];
$this->assertSame(1, $formatter->formatErrors(
new AnalysisResult($errors, [], [], [], [], false, self::DIRECTORY_PATH . '/phpstan.neon', true, 0, false, []),
$this->getOutput(),
));
$this->assertSame(
'::error file=foo.php,line=1,col=0::Fixable error%0A🔧 Can be fixed automatically. Run PHPStan again with --fix.%0A🪪 foo.bar: https://phpstan.org/error-identifiers/foo.bar
::error file=foo.php,line=2,col=0::Non-ignorable error%0A🪪 foo.baz: https://phpstan.org/error-identifiers/foo.baz (non-ignorable)
::error file=foo.php,line=3,col=0::Multi-line tip%0A💡 First line%0A💡 Second line in phpstan.neon
::error file=foo.php,line=4,col=0::Tip with type%0A💡 Use array<int, string> instead.
',
$this->getOutputContent(),
);
}
}