11
11
12
12
use PHP_CodeSniffer \Config ;
13
13
use PHP_CodeSniffer \Ruleset ;
14
+ use PHP_CodeSniffer \Runner ;
14
15
use PHPUnit \Framework \TestCase ;
15
16
16
17
/**
@@ -30,7 +31,7 @@ final class ExplainTest extends TestCase
30
31
public function testExplain ()
31
32
{
32
33
// Set up the ruleset.
33
- $ config = new Config (['--standard=PSR1 ' , '-e ' ]);
34
+ $ config = new Config (['--standard=PSR1 ' , '-e ' , ' --report-width=80 ' ]);
34
35
$ ruleset = new Ruleset ($ config );
35
36
36
37
$ expected = PHP_EOL ;
@@ -57,4 +58,159 @@ public function testExplain()
57
58
}//end testExplain()
58
59
59
60
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
+
60
216
}//end class
0 commit comments