-
-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathExplainTest.php
258 lines (214 loc) · 9.81 KB
/
ExplainTest.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<?php
/**
* Tests to verify that the "explain" command functions as expected.
*
* @author Juliette Reinders Folmer <[email protected]>
* @copyright 2023 Juliette Reinders Folmer. All rights reserved.
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/
namespace PHP_CodeSniffer\Tests\Core\Ruleset;
use PHP_CodeSniffer\Ruleset;
use PHP_CodeSniffer\Runner;
use PHP_CodeSniffer\Tests\ConfigDouble;
use PHPUnit\Framework\TestCase;
/**
* Test the Ruleset::explain() function.
*
* @covers \PHP_CodeSniffer\Ruleset::explain
*/
final class ExplainTest extends TestCase
{
/**
* Test the output of the "explain" command.
*
* @return void
*/
public function testExplain()
{
// Set up the ruleset.
$config = new ConfigDouble(['--standard=PSR1', '-e']);
$ruleset = new Ruleset($config);
$expected = PHP_EOL;
$expected .= 'The PSR1 standard contains 8 sniffs'.PHP_EOL.PHP_EOL;
$expected .= 'Generic (4 sniffs)'.PHP_EOL;
$expected .= '------------------'.PHP_EOL;
$expected .= ' Generic.Files.ByteOrderMark'.PHP_EOL;
$expected .= ' Generic.NamingConventions.UpperCaseConstantName'.PHP_EOL;
$expected .= ' Generic.PHP.DisallowAlternativePHPTags'.PHP_EOL;
$expected .= ' Generic.PHP.DisallowShortOpenTag'.PHP_EOL.PHP_EOL;
$expected .= 'PSR1 (3 sniffs)'.PHP_EOL;
$expected .= '---------------'.PHP_EOL;
$expected .= ' PSR1.Classes.ClassDeclaration'.PHP_EOL;
$expected .= ' PSR1.Files.SideEffects'.PHP_EOL;
$expected .= ' PSR1.Methods.CamelCapsMethodName'.PHP_EOL.PHP_EOL;
$expected .= 'Squiz (1 sniff)'.PHP_EOL;
$expected .= '---------------'.PHP_EOL;
$expected .= ' Squiz.Classes.ValidClassName'.PHP_EOL;
$this->expectOutputString($expected);
$ruleset->explain();
}//end testExplain()
/**
* Test the output of the "explain" command is not influenced by a user set report width.
*
* @return void
*/
public function testExplainAlwaysDisplaysCompleteSniffName()
{
// Set up the ruleset.
$config = new ConfigDouble(['--standard=PSR1', '-e', '--report-width=30']);
$ruleset = new Ruleset($config);
$expected = PHP_EOL;
$expected .= 'The PSR1 standard contains 8 sniffs'.PHP_EOL.PHP_EOL;
$expected .= 'Generic (4 sniffs)'.PHP_EOL;
$expected .= '------------------'.PHP_EOL;
$expected .= ' Generic.Files.ByteOrderMark'.PHP_EOL;
$expected .= ' Generic.NamingConventions.UpperCaseConstantName'.PHP_EOL;
$expected .= ' Generic.PHP.DisallowAlternativePHPTags'.PHP_EOL;
$expected .= ' Generic.PHP.DisallowShortOpenTag'.PHP_EOL.PHP_EOL;
$expected .= 'PSR1 (3 sniffs)'.PHP_EOL;
$expected .= '---------------'.PHP_EOL;
$expected .= ' PSR1.Classes.ClassDeclaration'.PHP_EOL;
$expected .= ' PSR1.Files.SideEffects'.PHP_EOL;
$expected .= ' PSR1.Methods.CamelCapsMethodName'.PHP_EOL.PHP_EOL;
$expected .= 'Squiz (1 sniff)'.PHP_EOL;
$expected .= '---------------'.PHP_EOL;
$expected .= ' Squiz.Classes.ValidClassName'.PHP_EOL;
$this->expectOutputString($expected);
$ruleset->explain();
}//end testExplainAlwaysDisplaysCompleteSniffName()
/**
* Test the output of the "explain" command when a ruleset only contains a single sniff.
*
* This is mostly about making sure that the summary line uses the correct grammar.
*
* @return void
*/
public function testExplainSingleSniff()
{
// Set up the ruleset.
$standard = __DIR__.'/ExplainSingleSniffTest.xml';
$config = new ConfigDouble(["--standard=$standard", '-e']);
$ruleset = new Ruleset($config);
$expected = PHP_EOL;
$expected .= 'The ExplainSingleSniffTest standard contains 1 sniff'.PHP_EOL.PHP_EOL;
$expected .= 'Squiz (1 sniff)'.PHP_EOL;
$expected .= '---------------'.PHP_EOL;
$expected .= ' Squiz.Scope.MethodScope'.PHP_EOL;
$this->expectOutputString($expected);
$ruleset->explain();
}//end testExplainSingleSniff()
/**
* Test that "explain" works correctly with custom rulesets.
*
* Verifies that:
* - The "standard" name is taken from the custom ruleset.
* - Any and all sniff additions and exclusions in the ruleset are taken into account correctly.
* - That the displayed list will have both the standards as well as the sniff names
* ordered alphabetically.
*
* @return void
*/
public function testExplainCustomRuleset()
{
// Set up the ruleset.
$standard = __DIR__.'/ExplainCustomRulesetTest.xml';
$config = new ConfigDouble(["--standard=$standard", '-e']);
$ruleset = new Ruleset($config);
$expected = PHP_EOL;
$expected .= 'The ExplainCustomRulesetTest standard contains 10 sniffs'.PHP_EOL.PHP_EOL;
$expected .= 'Generic (4 sniffs)'.PHP_EOL;
$expected .= '------------------'.PHP_EOL;
$expected .= ' Generic.Files.ByteOrderMark'.PHP_EOL;
$expected .= ' Generic.NamingConventions.UpperCaseConstantName'.PHP_EOL;
$expected .= ' Generic.PHP.DisallowAlternativePHPTags'.PHP_EOL;
$expected .= ' Generic.PHP.DisallowShortOpenTag'.PHP_EOL.PHP_EOL;
$expected .= 'PSR1 (2 sniffs)'.PHP_EOL;
$expected .= '---------------'.PHP_EOL;
$expected .= ' PSR1.Classes.ClassDeclaration'.PHP_EOL;
$expected .= ' PSR1.Methods.CamelCapsMethodName'.PHP_EOL.PHP_EOL;
$expected .= 'PSR12 (2 sniffs)'.PHP_EOL;
$expected .= '----------------'.PHP_EOL;
$expected .= ' PSR12.ControlStructures.BooleanOperatorPlacement'.PHP_EOL;
$expected .= ' PSR12.ControlStructures.ControlStructureSpacing'.PHP_EOL.PHP_EOL;
$expected .= 'Squiz (2 sniffs)'.PHP_EOL;
$expected .= '----------------'.PHP_EOL;
$expected .= ' Squiz.Classes.ValidClassName'.PHP_EOL;
$expected .= ' Squiz.Scope.MethodScope'.PHP_EOL;
$this->expectOutputString($expected);
$ruleset->explain();
}//end testExplainCustomRuleset()
/**
* Test the output of the "explain" command for a standard containing both deprecated
* and non-deprecated sniffs.
*
* Tests that:
* - Deprecated sniffs are marked with an asterix in the list.
* - A footnote is displayed explaining the asterix.
* - And that the "standard uses # deprecated sniffs" listing is **not** displayed.
*
* @return void
*/
public function testExplainWithDeprecatedSniffs()
{
// Set up the ruleset.
$standard = __DIR__."/ShowSniffDeprecationsTest.xml";
$config = new ConfigDouble(["--standard=$standard", '-e']);
$ruleset = new Ruleset($config);
$expected = PHP_EOL;
$expected .= 'The SniffDeprecationTest standard contains 9 sniffs'.PHP_EOL.PHP_EOL;
$expected .= 'Fixtures (9 sniffs)'.PHP_EOL;
$expected .= '-------------------'.PHP_EOL;
$expected .= ' Fixtures.Deprecated.WithLongReplacement *'.PHP_EOL;
$expected .= ' Fixtures.Deprecated.WithoutReplacement *'.PHP_EOL;
$expected .= ' Fixtures.Deprecated.WithReplacement *'.PHP_EOL;
$expected .= ' Fixtures.Deprecated.WithReplacementContainingLinuxNewlines *'.PHP_EOL;
$expected .= ' Fixtures.Deprecated.WithReplacementContainingNewlines *'.PHP_EOL;
$expected .= ' Fixtures.SetProperty.AllowedAsDeclared'.PHP_EOL;
$expected .= ' Fixtures.SetProperty.AllowedViaMagicMethod'.PHP_EOL;
$expected .= ' Fixtures.SetProperty.AllowedViaStdClass'.PHP_EOL;
$expected .= ' Fixtures.SetProperty.NotAllowedViaAttribute'.PHP_EOL.PHP_EOL;
$expected .= '* Sniffs marked with an asterix are deprecated.'.PHP_EOL;
$this->expectOutputString($expected);
$ruleset->explain();
}//end testExplainWithDeprecatedSniffs()
/**
* Test that each standard passed on the command-line is explained separately.
*
* @covers \PHP_CodeSniffer\Runner::runPHPCS
*
* @return void
*/
public function testExplainWillExplainEachStandardSeparately()
{
$standard = __DIR__.'/ExplainSingleSniffTest.xml';
$_SERVER['argv'] = [
'phpcs',
'-e',
"--standard=PSR1,$standard",
'--report-width=80',
];
$expected = PHP_EOL;
$expected .= 'The PSR1 standard contains 8 sniffs'.PHP_EOL.PHP_EOL;
$expected .= 'Generic (4 sniffs)'.PHP_EOL;
$expected .= '------------------'.PHP_EOL;
$expected .= ' Generic.Files.ByteOrderMark'.PHP_EOL;
$expected .= ' Generic.NamingConventions.UpperCaseConstantName'.PHP_EOL;
$expected .= ' Generic.PHP.DisallowAlternativePHPTags'.PHP_EOL;
$expected .= ' Generic.PHP.DisallowShortOpenTag'.PHP_EOL.PHP_EOL;
$expected .= 'PSR1 (3 sniffs)'.PHP_EOL;
$expected .= '---------------'.PHP_EOL;
$expected .= ' PSR1.Classes.ClassDeclaration'.PHP_EOL;
$expected .= ' PSR1.Files.SideEffects'.PHP_EOL;
$expected .= ' PSR1.Methods.CamelCapsMethodName'.PHP_EOL.PHP_EOL;
$expected .= 'Squiz (1 sniff)'.PHP_EOL;
$expected .= '---------------'.PHP_EOL;
$expected .= ' Squiz.Classes.ValidClassName'.PHP_EOL.PHP_EOL;
$expected .= 'The ExplainSingleSniffTest standard contains 1 sniff'.PHP_EOL.PHP_EOL;
$expected .= 'Squiz (1 sniff)'.PHP_EOL;
$expected .= '---------------'.PHP_EOL;
$expected .= ' Squiz.Scope.MethodScope'.PHP_EOL;
$this->expectOutputString($expected);
$runner = new Runner();
$exitCode = $runner->runPHPCS();
}//end testExplainWillExplainEachStandardSeparately()
}//end class