Skip to content

Commit 1eed2a3

Browse files
authored
Merge pull request #260 from PHPCSStandards/feature/tests-ruleset-explain-expand
Tests/ExplainTest: expand the tests
2 parents 8ffe403 + e6f43f6 commit 1eed2a3

File tree

3 files changed

+173
-1
lines changed

3 files changed

+173
-1
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0"?>
2+
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="ExplainCustomRulesetTest" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/PHPCSStandards/PHP_CodeSniffer/master/phpcs.xsd">
3+
4+
<rule ref="PSR12.ControlStructures"/>
5+
<rule ref="Squiz.Scope.MethodScope"/>
6+
<rule ref="PSR1">
7+
<exclude name="PSR1.Files.SideEffects"/>
8+
</rule>
9+
10+
</ruleset>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0"?>
2+
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="ExplainSingleSniffTest" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/PHPCSStandards/PHP_CodeSniffer/master/phpcs.xsd">
3+
4+
<rule ref="Squiz.Scope.MethodScope"/>
5+
6+
</ruleset>

tests/Core/Ruleset/ExplainTest.php

Lines changed: 157 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use PHP_CodeSniffer\Config;
1313
use PHP_CodeSniffer\Ruleset;
14+
use PHP_CodeSniffer\Runner;
1415
use PHPUnit\Framework\TestCase;
1516

1617
/**
@@ -30,7 +31,7 @@ final class ExplainTest extends TestCase
3031
public function testExplain()
3132
{
3233
// Set up the ruleset.
33-
$config = new Config(['--standard=PSR1', '-e']);
34+
$config = new Config(['--standard=PSR1', '-e', '--report-width=80']);
3435
$ruleset = new Ruleset($config);
3536

3637
$expected = PHP_EOL;
@@ -57,4 +58,159 @@ public function testExplain()
5758
}//end testExplain()
5859

5960

61+
/**
62+
* Test the output of the "explain" command is not influenced by a user set report width.
63+
*
64+
* @return void
65+
*/
66+
public function testExplainAlwaysDisplaysCompleteSniffName()
67+
{
68+
// Set up the ruleset.
69+
$config = new Config(['--standard=PSR1', '-e', '--report-width=30']);
70+
$ruleset = new Ruleset($config);
71+
72+
$expected = PHP_EOL;
73+
$expected .= 'The PSR1 standard contains 8 sniffs'.PHP_EOL.PHP_EOL;
74+
$expected .= 'Generic (4 sniffs)'.PHP_EOL;
75+
$expected .= '------------------'.PHP_EOL;
76+
$expected .= ' Generic.Files.ByteOrderMark'.PHP_EOL;
77+
$expected .= ' Generic.NamingConventions.UpperCaseConstantName'.PHP_EOL;
78+
$expected .= ' Generic.PHP.DisallowAlternativePHPTags'.PHP_EOL;
79+
$expected .= ' Generic.PHP.DisallowShortOpenTag'.PHP_EOL.PHP_EOL;
80+
$expected .= 'PSR1 (3 sniffs)'.PHP_EOL;
81+
$expected .= '---------------'.PHP_EOL;
82+
$expected .= ' PSR1.Classes.ClassDeclaration'.PHP_EOL;
83+
$expected .= ' PSR1.Files.SideEffects'.PHP_EOL;
84+
$expected .= ' PSR1.Methods.CamelCapsMethodName'.PHP_EOL.PHP_EOL;
85+
$expected .= 'Squiz (1 sniff)'.PHP_EOL;
86+
$expected .= '---------------'.PHP_EOL;
87+
$expected .= ' Squiz.Classes.ValidClassName'.PHP_EOL;
88+
89+
$this->expectOutputString($expected);
90+
91+
$ruleset->explain();
92+
93+
}//end testExplainAlwaysDisplaysCompleteSniffName()
94+
95+
96+
/**
97+
* Test the output of the "explain" command when a ruleset only contains a single sniff.
98+
*
99+
* This is mostly about making sure that the summary line uses the correct grammar.
100+
*
101+
* @return void
102+
*/
103+
public function testExplainSingleSniff()
104+
{
105+
// Set up the ruleset.
106+
$standard = __DIR__.'/ExplainSingleSniffTest.xml';
107+
$config = new Config(["--standard=$standard", '-e', '--report-width=80']);
108+
$ruleset = new Ruleset($config);
109+
110+
$expected = PHP_EOL;
111+
$expected .= 'The ExplainSingleSniffTest standard contains 1 sniff'.PHP_EOL.PHP_EOL;
112+
$expected .= 'Squiz (1 sniff)'.PHP_EOL;
113+
$expected .= '---------------'.PHP_EOL;
114+
$expected .= ' Squiz.Scope.MethodScope'.PHP_EOL;
115+
116+
$this->expectOutputString($expected);
117+
118+
$ruleset->explain();
119+
120+
}//end testExplainSingleSniff()
121+
122+
123+
/**
124+
* Test that "explain" works correctly with custom rulesets.
125+
*
126+
* Verifies that:
127+
* - The "standard" name is taken from the custom ruleset.
128+
* - Any and all sniff additions and exclusions in the ruleset are taken into account correctly.
129+
* - That the displayed list will have both the standards as well as the sniff names
130+
* ordered alphabetically.
131+
*
132+
* @return void
133+
*/
134+
public function testExplainCustomRuleset()
135+
{
136+
// Set up the ruleset.
137+
$standard = __DIR__.'/ExplainCustomRulesetTest.xml';
138+
$config = new Config(["--standard=$standard", '-e', '--report-width=80']);
139+
$ruleset = new Ruleset($config);
140+
141+
$expected = PHP_EOL;
142+
$expected .= 'The ExplainCustomRulesetTest standard contains 10 sniffs'.PHP_EOL.PHP_EOL;
143+
$expected .= 'Generic (4 sniffs)'.PHP_EOL;
144+
$expected .= '------------------'.PHP_EOL;
145+
$expected .= ' Generic.Files.ByteOrderMark'.PHP_EOL;
146+
$expected .= ' Generic.NamingConventions.UpperCaseConstantName'.PHP_EOL;
147+
$expected .= ' Generic.PHP.DisallowAlternativePHPTags'.PHP_EOL;
148+
$expected .= ' Generic.PHP.DisallowShortOpenTag'.PHP_EOL.PHP_EOL;
149+
$expected .= 'PSR1 (2 sniffs)'.PHP_EOL;
150+
$expected .= '---------------'.PHP_EOL;
151+
$expected .= ' PSR1.Classes.ClassDeclaration'.PHP_EOL;
152+
$expected .= ' PSR1.Methods.CamelCapsMethodName'.PHP_EOL.PHP_EOL;
153+
$expected .= 'PSR12 (2 sniffs)'.PHP_EOL;
154+
$expected .= '----------------'.PHP_EOL;
155+
$expected .= ' PSR12.ControlStructures.BooleanOperatorPlacement'.PHP_EOL;
156+
$expected .= ' PSR12.ControlStructures.ControlStructureSpacing'.PHP_EOL.PHP_EOL;
157+
$expected .= 'Squiz (2 sniffs)'.PHP_EOL;
158+
$expected .= '----------------'.PHP_EOL;
159+
$expected .= ' Squiz.Classes.ValidClassName'.PHP_EOL;
160+
$expected .= ' Squiz.Scope.MethodScope'.PHP_EOL;
161+
162+
$this->expectOutputString($expected);
163+
164+
$ruleset->explain();
165+
166+
}//end testExplainCustomRuleset()
167+
168+
169+
/**
170+
* Test that each standard passed on the command-line is explained separately.
171+
*
172+
* @covers \PHP_CodeSniffer\Runner::runPHPCS
173+
*
174+
* @return void
175+
*/
176+
public function testExplainWillExplainEachStandardSeparately()
177+
{
178+
$standard = __DIR__.'/ExplainSingleSniffTest.xml';
179+
$_SERVER['argv'] = [
180+
'phpcs',
181+
'-e',
182+
"--standard=PSR1,$standard",
183+
'--report-width=80',
184+
];
185+
186+
$expected = PHP_EOL;
187+
$expected .= 'The PSR1 standard contains 8 sniffs'.PHP_EOL.PHP_EOL;
188+
$expected .= 'Generic (4 sniffs)'.PHP_EOL;
189+
$expected .= '------------------'.PHP_EOL;
190+
$expected .= ' Generic.Files.ByteOrderMark'.PHP_EOL;
191+
$expected .= ' Generic.NamingConventions.UpperCaseConstantName'.PHP_EOL;
192+
$expected .= ' Generic.PHP.DisallowAlternativePHPTags'.PHP_EOL;
193+
$expected .= ' Generic.PHP.DisallowShortOpenTag'.PHP_EOL.PHP_EOL;
194+
$expected .= 'PSR1 (3 sniffs)'.PHP_EOL;
195+
$expected .= '---------------'.PHP_EOL;
196+
$expected .= ' PSR1.Classes.ClassDeclaration'.PHP_EOL;
197+
$expected .= ' PSR1.Files.SideEffects'.PHP_EOL;
198+
$expected .= ' PSR1.Methods.CamelCapsMethodName'.PHP_EOL.PHP_EOL;
199+
$expected .= 'Squiz (1 sniff)'.PHP_EOL;
200+
$expected .= '---------------'.PHP_EOL;
201+
$expected .= ' Squiz.Classes.ValidClassName'.PHP_EOL.PHP_EOL;
202+
203+
$expected .= 'The ExplainSingleSniffTest standard contains 1 sniff'.PHP_EOL.PHP_EOL;
204+
$expected .= 'Squiz (1 sniff)'.PHP_EOL;
205+
$expected .= '---------------'.PHP_EOL;
206+
$expected .= ' Squiz.Scope.MethodScope'.PHP_EOL;
207+
208+
$this->expectOutputString($expected);
209+
210+
$runner = new Runner();
211+
$exitCode = $runner->runPHPCS();
212+
213+
}//end testExplainWillExplainEachStandardSeparately()
214+
215+
60216
}//end class

0 commit comments

Comments
 (0)