/
TableErrorFormatterTest.php
240 lines (201 loc) · 5.37 KB
/
TableErrorFormatterTest.php
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<?php declare(strict_types = 1);
namespace PHPStan\Command\ErrorFormatter;
use PHPStan\Analyser\Error;
use PHPStan\Command\AnalysisResult;
use PHPStan\File\FuzzyRelativePathHelper;
use PHPStan\File\NullRelativePathHelper;
use PHPStan\File\SimpleRelativePathHelper;
use PHPStan\Testing\ErrorFormatterTestCase;
use function putenv;
use function sprintf;
use const PHP_VERSION_ID;
class TableErrorFormatterTest extends ErrorFormatterTestCase
{
protected function setUp(): void
{
putenv('GITHUB_ACTIONS');
}
protected function tearDown(): void
{
putenv('COLUMNS');
}
public function dataFormatterOutputProvider(): iterable
{
yield [
'No errors',
0,
0,
0,
'
[OK] No errors
',
];
yield [
'One file error',
1,
1,
0,
' ------ -------------------------------------------------------------------
Line folder with unicode 😃/file name with "spaces" and unicode 😃.php
------ -------------------------------------------------------------------
4 Foo
------ -------------------------------------------------------------------
[ERROR] Found 1 error
',
];
yield [
'One generic error',
1,
0,
1,
' -- ---------------------
Error
-- ---------------------
first generic error
-- ---------------------
[ERROR] Found 1 error
',
];
yield [
'Multiple file errors',
1,
4,
0,
' ------ -------------------------------------------------------------------
Line folder with unicode 😃/file name with "spaces" and unicode 😃.php
------ -------------------------------------------------------------------
2 Bar
Bar2
4 Foo
------ -------------------------------------------------------------------
------ ----------
Line foo.php
------ ----------
1 Foo
5 Bar
Bar2
💡 a tip
------ ----------
[ERROR] Found 4 errors
',
];
yield [
'Multiple generic errors',
1,
0,
2,
' -- ----------------------
Error
-- ----------------------
first generic error
second generic error
-- ----------------------
[ERROR] Found 2 errors
',
];
yield [
'Multiple file, multiple generic errors',
1,
4,
2,
' ------ -------------------------------------------------------------------
Line folder with unicode 😃/file name with "spaces" and unicode 😃.php
------ -------------------------------------------------------------------
2 Bar
Bar2
4 Foo
------ -------------------------------------------------------------------
------ ----------
Line foo.php
------ ----------
1 Foo
5 Bar
Bar2
💡 a tip
------ ----------
-- ----------------------
Error
-- ----------------------
first generic error
second generic error
-- ----------------------
[ERROR] Found 6 errors
',
];
}
/**
* @dataProvider dataFormatterOutputProvider
*
*/
public function testFormatErrors(
string $message,
int $exitCode,
int $numFileErrors,
int $numGenericErrors,
string $expected,
): void
{
if (PHP_VERSION_ID >= 80100) {
self::markTestSkipped('Skipped on PHP 8.1 because of different result');
}
$formatter = $this->createErrorFormatter(null);
$this->assertSame($exitCode, $formatter->formatErrors(
$this->getAnalysisResult($numFileErrors, $numGenericErrors),
$this->getOutput(),
), sprintf('%s: response code do not match', $message));
$this->assertEquals($expected, $this->getOutputContent(), sprintf('%s: output do not match', $message));
}
public function testEditorUrlWithTrait(): void
{
$formatter = $this->createErrorFormatter('editor://%file%/%line%');
$error = new Error('Test', 'Foo.php (in context of trait)', 12, true, 'Foo.php', 'Bar.php');
$formatter->formatErrors(new AnalysisResult([$error], [], [], [], [], false, null, true), $this->getOutput());
$this->assertStringContainsString('Bar.php', $this->getOutputContent());
}
public function testEditorUrlWithRelativePath(): void
{
$formatter = $this->createErrorFormatter('editor://custom/path/%relFile%/%line%');
$error = new Error('Test', 'Foo.php', 12, true, self::DIRECTORY_PATH . '/rel/Foo.php');
$formatter->formatErrors(new AnalysisResult([$error], [], [], [], [], false, null, true), $this->getOutput(true));
$this->assertStringContainsString('editor://custom/path/rel/Foo.php', $this->getOutputContent(true));
}
public function testBug6727(): void
{
putenv('COLUMNS=30');
$formatter = $this->createErrorFormatter(null);
$formatter->formatErrors(
new AnalysisResult(
[
new Error(
'Method MissingTypehintPromotedProperties\Foo::__construct() has parameter $foo with no value type specified in iterable type array.',
'/var/www/html/app/src/Foo.php (in context of class App\Foo\Bar)',
5,
),
],
[],
[],
[],
[],
false,
null,
true,
),
$this->getOutput(),
);
self::expectNotToPerformAssertions();
}
private function createErrorFormatter(?string $editorUrl): TableErrorFormatter
{
$relativePathHelper = new FuzzyRelativePathHelper(new NullRelativePathHelper(), self::DIRECTORY_PATH, [], '/');
return new TableErrorFormatter(
$relativePathHelper,
new SimpleRelativePathHelper(self::DIRECTORY_PATH),
new CiDetectedErrorFormatter(
new GithubErrorFormatter($relativePathHelper),
new TeamcityErrorFormatter($relativePathHelper),
),
false,
$editorUrl,
);
}
}