@@ -23,16 +23,31 @@ 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 = 'ERROR: The -- ' .$ argument .' option only supports sniff codes. ' .PHP_EOL ;
38
+ $ message .= 'Sniff codes are in the form "Standard.Category.Sniff" ' .PHP_EOL ;
39
+ $ message .= PHP_EOL ;
40
+ $ message .= 'The following problems were detected: ' .PHP_EOL ;
41
+ $ message .= '* ' .implode (PHP_EOL .'* ' , $ errors ).PHP_EOL ;
42
+
43
+ if ($ suggestion !== null ) {
44
+ $ message .= PHP_EOL ;
45
+ $ message .= "Perhaps try -- $ argument= \"$ suggestion \" instead. " .PHP_EOL ;
46
+ }
47
+
48
+ $ message .= PHP_EOL ;
49
+ $ message .= 'Run "phpcs --help" for usage information ' .PHP_EOL ;
50
+ $ message .= PHP_EOL ;
36
51
37
52
if (method_exists ($ this , 'expectException ' ) === true ) {
38
53
// PHPUnit 5+.
@@ -62,47 +77,76 @@ public static function dataInvalidSniffs()
62
77
];
63
78
$ data = [];
64
79
65
- $ messageTemplate = 'ERROR: The specified sniff code "%s" is invalid ' .PHP_EOL . PHP_EOL ;
80
+ $ messageTemplate = 'ERROR: The specified sniff code "%s" is invalid ' .PHP_EOL ;
66
81
67
82
foreach ($ arguments as $ argument ) {
68
- // An empty string is not a valid sniff.
69
- $ data [$ argument .'; empty string ' ] = [
70
- 'argument ' => $ argument ,
71
- 'value ' => '' ,
72
- 'message ' => sprintf ($ messageTemplate , '' ),
73
- ];
74
-
75
83
// A standard is not a valid sniff.
76
84
$ data [$ argument .'; standard ' ] = [
77
- 'argument ' => $ argument ,
78
- 'value ' => 'Standard ' ,
79
- 'message ' => sprintf ($ messageTemplate , 'Standard ' ),
85
+ 'argument ' => $ argument ,
86
+ 'value ' => 'Standard ' ,
87
+ 'errors ' => [
88
+ 'Standard codes are not supported: Standard ' ,
89
+ ],
90
+ 'suggestion ' => null ,
80
91
];
81
92
82
93
// A category is not a valid sniff.
83
94
$ data [$ argument .'; category ' ] = [
84
- 'argument ' => $ argument ,
85
- 'value ' => 'Standard.Category ' ,
86
- 'message ' => sprintf ($ messageTemplate , 'Standard.Category ' ),
95
+ 'argument ' => $ argument ,
96
+ 'value ' => 'Standard.Category ' ,
97
+ 'errors ' => [
98
+ 'Category codes are not supported: Standard.Category ' ,
99
+ ],
100
+ 'suggestion ' => null ,
87
101
];
88
102
89
103
// An error-code is not a valid sniff.
90
104
$ data [$ argument .'; error-code ' ] = [
91
- 'argument ' => $ argument ,
92
- 'value ' => 'Standard.Category ' ,
93
- 'message ' => sprintf ($ messageTemplate , 'Standard.Category ' ),
105
+ 'argument ' => $ argument ,
106
+ 'value ' => 'Standard.Category.Sniff.Code ' ,
107
+ 'errors ' => [
108
+ 'Message codes are not supported: Standard.Category.Sniff.Code ' ,
109
+ ],
110
+ 'suggestion ' => 'Standard.Category.Sniff ' ,
111
+ ];
112
+
113
+ // Too many dots.
114
+ $ data [$ argument .'; too many dots ' ] = [
115
+ 'argument ' => $ argument ,
116
+ 'value ' => 'Standard.Category.Sniff.Code.Extra ' ,
117
+ 'errors ' => [
118
+ 'Too many parts: Standard.Category.Sniff.Code.Extra ' ,
119
+ ],
120
+ 'suggestion ' => 'Standard.Category.Sniff ' ,
94
121
];
95
122
96
- // Only the first error is reported .
123
+ // All errors are reported in one go .
97
124
$ data [$ argument .'; two errors ' ] = [
98
- 'argument ' => $ argument ,
99
- 'value ' => 'StandardOne,StandardTwo ' ,
100
- 'message ' => sprintf ($ messageTemplate , 'StandardOne ' ),
125
+ 'argument ' => $ argument ,
126
+ 'value ' => 'StandardOne,StandardTwo ' ,
127
+ 'errors ' => [
128
+ 'Standard codes are not supported: StandardOne ' ,
129
+ 'Standard codes are not supported: StandardTwo ' ,
130
+ ],
131
+ 'suggestion ' => null ,
101
132
];
133
+
134
+ // Order of valid/invalid does not impact error reporting.
102
135
$ data [$ argument .'; valid followed by invalid ' ] = [
103
- 'argument ' => $ argument ,
104
- 'value ' => 'StandardOne.Category.Sniff,StandardTwo.Category ' ,
105
- 'message ' => sprintf ($ messageTemplate , 'StandardTwo.Category ' ),
136
+ 'argument ' => $ argument ,
137
+ 'value ' => 'StandardOne.Category.Sniff,StandardTwo.Category ' ,
138
+ 'errors ' => [
139
+ 'Category codes are not supported: StandardTwo.Category ' ,
140
+ ],
141
+ 'suggestion ' => 'StandardOne.Category.Sniff ' ,
142
+ ];
143
+ $ data [$ argument .'; invalid followed by valid ' ] = [
144
+ 'argument ' => $ argument ,
145
+ 'value ' => 'StandardOne.Category,StandardTwo.Category.Sniff ' ,
146
+ 'errors ' => [
147
+ 'Category codes are not supported: StandardOne.Category ' ,
148
+ ],
149
+ 'suggestion ' => 'StandardTwo.Category.Sniff ' ,
106
150
];
107
151
}//end foreach
108
152
@@ -114,17 +158,18 @@ public static function dataInvalidSniffs()
114
158
/**
115
159
* Ensure that the valid data does not throw an exception, and the value is stored.
116
160
*
117
- * @param string $argument 'sniffs' or 'exclude'.
118
- * @param string $value List of sniffs to include or exclude.
161
+ * @param string $argument 'sniffs' or 'exclude'.
162
+ * @param string $value List of sniffs to include or exclude.
163
+ * @param string[] $result Expected sniffs to be set on the Config object.
119
164
*
120
165
* @return void
121
166
* @dataProvider dataValidSniffs
122
167
*/
123
- public function testValid ($ argument , $ value )
168
+ public function testValid ($ argument , $ value, $ result )
124
169
{
125
170
$ config = new ConfigDouble (["-- $ argument= $ value " ]);
126
171
127
- $ this ->assertSame (explode ( ' , ' , $ value ) , $ config ->$ argument );
172
+ $ this ->assertSame ($ result , $ config ->$ argument );
128
173
129
174
}//end testValid()
130
175
@@ -144,15 +189,50 @@ public static function dataValidSniffs()
144
189
$ data = [];
145
190
146
191
foreach ($ arguments as $ argument ) {
192
+ $ data [$ argument .'; empty string ' ] = [
193
+ 'argument ' => $ argument ,
194
+ 'value ' => '' ,
195
+ 'result ' => [],
196
+ ];
147
197
$ data [$ argument .'; one valid sniff ' ] = [
148
198
'argument ' => $ argument ,
149
199
'value ' => 'Standard.Category.Sniff ' ,
200
+ 'result ' => ['Standard.Category.Sniff ' ],
150
201
];
151
202
$ data [$ argument .'; two valid sniffs ' ] = [
152
203
'argument ' => $ argument ,
153
204
'value ' => 'StandardOne.Category.Sniff,StandardTwo.Category.Sniff ' ,
205
+ 'result ' => [
206
+ 'StandardOne.Category.Sniff ' ,
207
+ 'StandardTwo.Category.Sniff ' ,
208
+ ],
154
209
];
155
- }
210
+
211
+ // Rogue commas are quietly ignored.
212
+ $ data [$ argument .'; one comma alone ' ] = [
213
+ 'argument ' => $ argument ,
214
+ 'value ' => ', ' ,
215
+ 'result ' => [],
216
+ ];
217
+ $ data [$ argument .'; two commas alone ' ] = [
218
+ 'argument ' => $ argument ,
219
+ 'value ' => ',, ' ,
220
+ 'result ' => [],
221
+ ];
222
+ $ data [$ argument .'; trailing comma ' ] = [
223
+ 'argument ' => $ argument ,
224
+ 'value ' => 'Standard.Category.Sniff, ' ,
225
+ 'result ' => ['Standard.Category.Sniff ' ],
226
+ ];
227
+ $ data [$ argument .'; double comma between sniffs ' ] = [
228
+ 'argument ' => $ argument ,
229
+ 'value ' => 'StandardOne.Category.Sniff,,StandardTwo.Category.Sniff ' ,
230
+ 'result ' => [
231
+ 'StandardOne.Category.Sniff ' ,
232
+ 'StandardTwo.Category.Sniff ' ,
233
+ ],
234
+ ];
235
+ }//end foreach
156
236
157
237
return $ data ;
158
238
0 commit comments