@@ -23,16 +23,35 @@ final class SniffsExcludeArgsTest extends TestCase
23
23
/**
24
24
* Ensure that the expected error message is returned for invalid arguments.
25
25
*
26
- * @param string $argument 'sniffs' or 'exclude'.
27
- * @param string $value List of sniffs to include / exclude.
28
- * @param string $message Expected error message text.
26
+ * @param string $argument 'sniffs' or 'exclude'.
27
+ * @param string $value List of sniffs to include / exclude.
28
+ * @param array<string, string> $errors Sniff code and associated help text.
29
+ * @param string|null $suggestion Help text shown to end user with correct syntax for argument.
29
30
*
30
31
* @return void
31
32
* @dataProvider dataInvalidSniffs
32
33
*/
33
- public function testInvalid ($ argument , $ value , $ message )
34
+ public function testInvalid ($ argument , $ value , $ errors , $ suggestion )
34
35
{
35
36
$ exception = 'PHP_CodeSniffer\Exceptions\DeepExitException ' ;
37
+ $ message = '' ;
38
+
39
+ foreach ($ errors as $ error ) {
40
+ list ($ sniffCode , $ helpText ) = $ error ;
41
+ $ message .= "ERROR: The specified sniff code \"$ sniffCode \" is invalid " .PHP_EOL ;
42
+ $ message .= $ helpText .PHP_EOL ;
43
+ $ message .= PHP_EOL ;
44
+ }
45
+
46
+ $ message .= "The -- $ argument option only supports sniff codes. Sniff codes are in the form \"Standard.Category.Sniff \"" .PHP_EOL ;
47
+ $ message .= PHP_EOL ;
48
+
49
+ if ($ suggestion !== null ) {
50
+ $ message .= "Perhaps try -- $ argument= \"$ suggestion \" instead. " .PHP_EOL ;
51
+ }
52
+
53
+ $ message .= 'Run "phpcs --help" for usage information ' .PHP_EOL ;
54
+ $ message .= PHP_EOL ;
36
55
37
56
if (method_exists ($ this , 'expectException ' ) === true ) {
38
57
// PHPUnit 5+.
@@ -62,47 +81,158 @@ public static function dataInvalidSniffs()
62
81
];
63
82
$ data = [];
64
83
65
- $ messageTemplate = 'ERROR: The specified sniff code "%s" is invalid ' .PHP_EOL . PHP_EOL ;
84
+ $ messageTemplate = 'ERROR: The specified sniff code "%s" is invalid ' .PHP_EOL ;
66
85
67
86
foreach ($ arguments as $ argument ) {
68
87
// An empty string is not a valid sniff.
69
88
$ data [$ argument .'; empty string ' ] = [
70
- 'argument ' => $ argument ,
71
- 'value ' => '' ,
72
- 'message ' => sprintf ($ messageTemplate , '' ),
89
+ 'argument ' => $ argument ,
90
+ 'value ' => '' ,
91
+ 'errors ' => [
92
+ [
93
+ '' ,
94
+ 'Empty string detected. Perhaps there is a stray comma somewhere. ' ,
95
+ ],
96
+ ],
97
+ 'suggestion ' => null ,
73
98
];
74
99
75
100
// A standard is not a valid sniff.
76
101
$ data [$ argument .'; standard ' ] = [
77
- 'argument ' => $ argument ,
78
- 'value ' => 'Standard ' ,
79
- 'message ' => sprintf ($ messageTemplate , 'Standard ' ),
102
+ 'argument ' => $ argument ,
103
+ 'value ' => 'Standard ' ,
104
+ 'errors ' => [
105
+ [
106
+ 'Standard ' ,
107
+ 'This appears to be a Standard code. ' ,
108
+ ],
109
+ ],
110
+ 'suggestion ' => null ,
80
111
];
81
112
82
113
// A category is not a valid sniff.
83
114
$ data [$ argument .'; category ' ] = [
84
- 'argument ' => $ argument ,
85
- 'value ' => 'Standard.Category ' ,
86
- 'message ' => sprintf ($ messageTemplate , 'Standard.Category ' ),
115
+ 'argument ' => $ argument ,
116
+ 'value ' => 'Standard.Category ' ,
117
+ 'errors ' => [
118
+ [
119
+ 'Standard.Category ' ,
120
+ 'This appears to be a Category code. ' ,
121
+ ],
122
+ ],
123
+ 'suggestion ' => null ,
87
124
];
88
125
89
126
// An error-code is not a valid sniff.
90
127
$ data [$ argument .'; error-code ' ] = [
91
- 'argument ' => $ argument ,
92
- 'value ' => 'Standard.Category ' ,
93
- 'message ' => sprintf ($ messageTemplate , 'Standard.Category ' ),
128
+ 'argument ' => $ argument ,
129
+ 'value ' => 'Standard.Category.Sniff.Code ' ,
130
+ 'errors ' => [
131
+ [
132
+ 'Standard.Category.Sniff.Code ' ,
133
+ 'This appears to be a Message code. ' ,
134
+ ],
135
+ ],
136
+ 'suggestion ' => 'Standard.Category.Sniff ' ,
94
137
];
95
138
96
- // Only the first error is reported .
139
+ // All errors are reported in one go .
97
140
$ data [$ argument .'; two errors ' ] = [
98
- 'argument ' => $ argument ,
99
- 'value ' => 'StandardOne,StandardTwo ' ,
100
- 'message ' => sprintf ($ messageTemplate , 'StandardOne ' ),
141
+ 'argument ' => $ argument ,
142
+ 'value ' => 'StandardOne,StandardTwo ' ,
143
+ 'errors ' => [
144
+ [
145
+ 'StandardOne ' ,
146
+ 'This appears to be a Standard code. ' ,
147
+ ],
148
+ [
149
+ 'StandardTwo ' ,
150
+ 'This appears to be a Standard code. ' ,
151
+ ],
152
+ ],
153
+ 'suggestion ' => null ,
101
154
];
155
+
156
+ // Order of valid/invalid does not impact error reporting.
102
157
$ data [$ argument .'; valid followed by invalid ' ] = [
103
- 'argument ' => $ argument ,
104
- 'value ' => 'StandardOne.Category.Sniff,StandardTwo.Category ' ,
105
- 'message ' => sprintf ($ messageTemplate , 'StandardTwo.Category ' ),
158
+ 'argument ' => $ argument ,
159
+ 'value ' => 'StandardOne.Category.Sniff,StandardTwo.Category ' ,
160
+ 'errors ' => [
161
+ [
162
+ 'StandardTwo.Category ' ,
163
+ 'This appears to be a Category code. ' ,
164
+ ],
165
+ ],
166
+ 'suggestion ' => 'StandardOne.Category.Sniff ' ,
167
+ ];
168
+ $ data [$ argument .'; invalid followed by valid ' ] = [
169
+ 'argument ' => $ argument ,
170
+ 'value ' => 'StandardOne.Category,StandardTwo.Category.Sniff ' ,
171
+ 'errors ' => [
172
+ [
173
+ 'StandardOne.Category ' ,
174
+ 'This appears to be a Category code. ' ,
175
+ ],
176
+ ],
177
+ 'suggestion ' => 'StandardTwo.Category.Sniff ' ,
178
+ ];
179
+
180
+ // Rogue commas get reported.
181
+ $ data [$ argument .'; one comma alone ' ] = [
182
+ 'argument ' => $ argument ,
183
+ 'value ' => ', ' ,
184
+ 'errors ' => [
185
+ [
186
+ '' ,
187
+ 'Empty string detected. Perhaps there is a stray comma somewhere. ' ,
188
+ ],
189
+ [
190
+ '' ,
191
+ 'Empty string detected. Perhaps there is a stray comma somewhere. ' ,
192
+ ],
193
+ ],
194
+ 'suggestion ' => null ,
195
+ ];
196
+ $ data [$ argument .'; two commas alone ' ] = [
197
+ 'argument ' => $ argument ,
198
+ 'value ' => ',, ' ,
199
+ 'errors ' => [
200
+ [
201
+ '' ,
202
+ 'Empty string detected. Perhaps there is a stray comma somewhere. ' ,
203
+ ],
204
+ [
205
+ '' ,
206
+ 'Empty string detected. Perhaps there is a stray comma somewhere. ' ,
207
+ ],
208
+ [
209
+ '' ,
210
+ 'Empty string detected. Perhaps there is a stray comma somewhere. ' ,
211
+ ],
212
+ ],
213
+ 'suggestion ' => null ,
214
+ ];
215
+ $ data [$ argument .'; trailing comma ' ] = [
216
+ 'argument ' => $ argument ,
217
+ 'value ' => 'Standard.Category.Sniff, ' ,
218
+ 'errors ' => [
219
+ [
220
+ '' ,
221
+ 'Empty string detected. Perhaps there is a stray comma somewhere. ' ,
222
+ ],
223
+ ],
224
+ 'suggestion ' => 'Standard.Category.Sniff ' ,
225
+ ];
226
+ $ data [$ argument .'; double comma between sniffs ' ] = [
227
+ 'argument ' => $ argument ,
228
+ 'value ' => 'StandardOne.Category.Sniff,,StandardTwo.Category.Sniff ' ,
229
+ 'errors ' => [
230
+ [
231
+ '' ,
232
+ 'Empty string detected. Perhaps there is a stray comma somewhere. ' ,
233
+ ],
234
+ ],
235
+ 'suggestion ' => 'StandardOne.Category.Sniff,StandardTwo.Category.Sniff ' ,
106
236
];
107
237
}//end foreach
108
238
0 commit comments