Skip to content

Commit 4e48479

Browse files
committed
Tests: Add ugly integration test for Reports\Full
The class does not really have individually testable components. Provide a big ugly real report data as the input, and a big ugly real generated report as the output. This is not great, but it's something. The Full report ignores the $phpcsFile argument, so it's easy to mock. Other reports would require a more complicated setup to test. The test data has been generated from a file `simple.php` as follows: ``` <?php // This is a space indented line. $a = 10; // This is a tab indented line. ``` …using the following command for the output: ``` phpcs simple.php --basepath=. --report=full --tab-width=4 --no-colors --standard=generic ``` …and adding this snippet in generateFileReport() for the input: ``` echo var_export($report); ```
1 parent 98c8972 commit 4e48479

File tree

1 file changed

+165
-0
lines changed

1 file changed

+165
-0
lines changed

tests/Core/Reports/FullTest.php

+165
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
<?php
2+
/**
3+
* Tests for the \PHP_CodeSniffer\Reports\Full class.
4+
*
5+
* @author Bartosz Dziewoński <[email protected]>
6+
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
7+
*/
8+
9+
namespace PHP_CodeSniffer\Tests\Core\Reports;
10+
11+
use PHPUnit\Framework\TestCase;
12+
use PHP_CodeSniffer\Config;
13+
use PHP_CodeSniffer\Files\File;
14+
use PHP_CodeSniffer\Reports\Full;
15+
use PHP_CodeSniffer\Ruleset;
16+
use PHP_CodeSniffer\Util\Common;
17+
18+
/**
19+
* Tests for the \PHP_CodeSniffer\Reports\Full class.
20+
*
21+
* @covers \PHP_CodeSniffer\Reports\Full
22+
*/
23+
final class FullTest extends TestCase
24+
{
25+
26+
27+
/**
28+
* Test generating a full PHPCS report.
29+
*
30+
* @param string $reportData Prepared report data.
31+
* @param string $expected Expected function output.
32+
*
33+
* @dataProvider dataGenerateFileReport
34+
*
35+
* @return void
36+
*/
37+
public function testGenerateFileReport($reportData, $expected)
38+
{
39+
$reportClass = new Full();
40+
$config = new Config();
41+
$phpcsFile = new File('', new Ruleset($config), $config);
42+
43+
ob_start();
44+
$result = $reportClass->generateFileReport(
45+
$reportData,
46+
$phpcsFile
47+
);
48+
$this->assertTrue($result);
49+
$generatedReport = ob_get_contents();
50+
ob_end_clean();
51+
52+
// For readability of the output in the test data, omit color codes.
53+
// Their behavior is covered by their own unit tests.
54+
$generatedReport = Common::stripColors($generatedReport);
55+
56+
$this->assertSame(
57+
str_replace("\n", PHP_EOL, trim($expected)),
58+
trim($generatedReport)
59+
);
60+
61+
}//end testGenerateFileReport()
62+
63+
64+
/**
65+
* Data provider.
66+
*
67+
* @see testGenerateFileReport()
68+
*
69+
* @return array<string, array<string, mixed>>
70+
*/
71+
public static function dataGenerateFileReport()
72+
{
73+
return [
74+
'Simple output' => [
75+
'reportData' => [
76+
'filename' => 'simple.php',
77+
'errors' => 6,
78+
'warnings' => 0,
79+
'fixable' => 4,
80+
'messages' => [
81+
1 => [
82+
1 => [
83+
0 => [
84+
'message' => 'The PHP open tag does not have a corresponding PHP close tag',
85+
'source' => 'Generic.PHP.ClosingPHPTag.NotFound',
86+
'severity' => 5,
87+
'fixable' => false,
88+
'type' => 'ERROR',
89+
],
90+
1 => [
91+
'message' => 'Missing required strict_types declaration',
92+
'source' => 'Generic.PHP.RequireStrictTypes.MissingDeclaration',
93+
'severity' => 5,
94+
'fixable' => false,
95+
'type' => 'ERROR',
96+
],
97+
],
98+
],
99+
3 => [
100+
1 => [
101+
0 => [
102+
'message' => 'Tabs must be used to indent lines; spaces are not allowed',
103+
'source' => 'Generic.WhiteSpace.DisallowSpaceIndent.SpacesUsed',
104+
'severity' => 5,
105+
'fixable' => true,
106+
'type' => 'ERROR',
107+
],
108+
],
109+
],
110+
4 => [
111+
1 => [
112+
0 => [
113+
'message' => 'Tabs must be used to indent lines; spaces are not allowed',
114+
'source' => 'Generic.WhiteSpace.DisallowSpaceIndent.SpacesUsed',
115+
'severity' => 5,
116+
'fixable' => true,
117+
'type' => 'ERROR',
118+
],
119+
],
120+
],
121+
5 => [
122+
1 => [
123+
0 => [
124+
'message' => 'Spaces must be used to indent lines; tabs are not allowed',
125+
'source' => 'Generic.WhiteSpace.DisallowTabIndent.TabsUsed',
126+
'severity' => 5,
127+
'fixable' => true,
128+
'type' => 'ERROR',
129+
],
130+
],
131+
5 => [
132+
0 => [
133+
'message' => 'File must not end with a newline character',
134+
'source' => 'Generic.Files.EndFileNoNewline.Found',
135+
'severity' => 5,
136+
'fixable' => true,
137+
'type' => 'ERROR',
138+
],
139+
],
140+
],
141+
],
142+
],
143+
'expected' => <<<EOF
144+
FILE: simple.php
145+
------------------------------------------------------------------------------
146+
FOUND 6 ERRORS AFFECTING 4 LINES
147+
------------------------------------------------------------------------------
148+
1 | ERROR | [ ] The PHP open tag does not have a corresponding PHP close tag
149+
1 | ERROR | [ ] Missing required strict_types declaration
150+
3 | ERROR | [x] Tabs must be used to indent lines; spaces are not allowed
151+
4 | ERROR | [x] Tabs must be used to indent lines; spaces are not allowed
152+
5 | ERROR | [x] Spaces must be used to indent lines; tabs are not allowed
153+
5 | ERROR | [x] File must not end with a newline character
154+
------------------------------------------------------------------------------
155+
PHPCBF CAN FIX THE 4 MARKED SNIFF VIOLATIONS AUTOMATICALLY
156+
------------------------------------------------------------------------------
157+
EOF
158+
,
159+
],
160+
];
161+
162+
}//end dataGenerateFileReport()
163+
164+
165+
}//end class

0 commit comments

Comments
 (0)