Skip to content

Commit 05f8b21

Browse files
authored
Config: add tests for the --sniffs and --exclude options (#474)
This PR adds tests covering the logic for handling the `--sniffs=...` and `--exclude=...` CLI options, which both take a comma-separated list of sniff codes and will error on anything which is not a sniff code.
1 parent 63a8cbe commit 05f8b21

File tree

1 file changed

+200
-0
lines changed

1 file changed

+200
-0
lines changed
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
<?php
2+
/**
3+
* Tests for the \PHP_CodeSniffer\Config --sniffs and --exclude arguments.
4+
*
5+
* @author Dan Wallis <[email protected]>
6+
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
7+
*/
8+
9+
namespace PHP_CodeSniffer\Tests\Core\Config;
10+
11+
use PHP_CodeSniffer\Tests\ConfigDouble;
12+
use PHPUnit\Framework\TestCase;
13+
14+
/**
15+
* Tests for the \PHP_CodeSniffer\Config --sniffs and --exclude arguments.
16+
*
17+
* @covers \PHP_CodeSniffer\Config::processLongArgument
18+
*/
19+
final class SniffsExcludeArgsTest extends TestCase
20+
{
21+
22+
23+
/**
24+
* Ensure that the expected error message is returned for invalid arguments.
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.
29+
*
30+
* @return void
31+
* @dataProvider dataInvalidSniffs
32+
*/
33+
public function testInvalid($argument, $value, $message)
34+
{
35+
$exception = 'PHP_CodeSniffer\Exceptions\DeepExitException';
36+
37+
if (method_exists($this, 'expectException') === true) {
38+
// PHPUnit 5+.
39+
$this->expectException($exception);
40+
$this->expectExceptionMessage($message);
41+
} else {
42+
// PHPUnit 4.
43+
$this->setExpectedException($exception, $message);
44+
}
45+
46+
new ConfigDouble(["--$argument=$value"]);
47+
48+
}//end testInvalid()
49+
50+
51+
/**
52+
* Data provider for testInvalid().
53+
*
54+
* @see self::testInvalid()
55+
* @return array<string, array<string, string>>
56+
*/
57+
public static function dataInvalidSniffs()
58+
{
59+
$arguments = [
60+
'sniffs',
61+
'exclude',
62+
];
63+
$data = [];
64+
65+
$messageTemplate = 'ERROR: The specified sniff code "%s" is invalid'.PHP_EOL.PHP_EOL;
66+
67+
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+
// A standard is not a valid sniff.
76+
$data[$argument.'; standard'] = [
77+
'argument' => $argument,
78+
'value' => 'Standard',
79+
'message' => sprintf($messageTemplate, 'Standard'),
80+
];
81+
82+
// A category is not a valid sniff.
83+
$data[$argument.'; category'] = [
84+
'argument' => $argument,
85+
'value' => 'Standard.Category',
86+
'message' => sprintf($messageTemplate, 'Standard.Category'),
87+
];
88+
89+
// An error-code is not a valid sniff.
90+
$data[$argument.'; error-code'] = [
91+
'argument' => $argument,
92+
'value' => 'Standard.Category',
93+
'message' => sprintf($messageTemplate, 'Standard.Category'),
94+
];
95+
96+
// Only the first error is reported.
97+
$data[$argument.'; two errors'] = [
98+
'argument' => $argument,
99+
'value' => 'StandardOne,StandardTwo',
100+
'message' => sprintf($messageTemplate, 'StandardOne'),
101+
];
102+
$data[$argument.'; valid followed by invalid'] = [
103+
'argument' => $argument,
104+
'value' => 'StandardOne.Category.Sniff,StandardTwo.Category',
105+
'message' => sprintf($messageTemplate, 'StandardTwo.Category'),
106+
];
107+
}//end foreach
108+
109+
return $data;
110+
111+
}//end dataInvalidSniffs()
112+
113+
114+
/**
115+
* Ensure that the valid data does not throw an exception, and the value is stored.
116+
*
117+
* @param string $argument 'sniffs' or 'exclude'.
118+
* @param string $value List of sniffs to include or exclude.
119+
*
120+
* @return void
121+
* @dataProvider dataValidSniffs
122+
*/
123+
public function testValid($argument, $value)
124+
{
125+
$config = new ConfigDouble(["--$argument=$value"]);
126+
127+
$this->assertSame(explode(',', $value), $config->$argument);
128+
129+
}//end testValid()
130+
131+
132+
/**
133+
* Data provider for testValid().
134+
*
135+
* @see self::testValid()
136+
* @return array<string, array<string, string>>
137+
*/
138+
public static function dataValidSniffs()
139+
{
140+
$arguments = [
141+
'sniffs',
142+
'exclude',
143+
];
144+
$data = [];
145+
146+
foreach ($arguments as $argument) {
147+
$data[$argument.'; one valid sniff'] = [
148+
'argument' => $argument,
149+
'value' => 'Standard.Category.Sniff',
150+
];
151+
$data[$argument.'; two valid sniffs'] = [
152+
'argument' => $argument,
153+
'value' => 'StandardOne.Category.Sniff,StandardTwo.Category.Sniff',
154+
];
155+
}
156+
157+
return $data;
158+
159+
}//end dataValidSniffs()
160+
161+
162+
/**
163+
* Ensure that only the first argument is processed and others are ignored.
164+
*
165+
* @param string $argument 'sniffs' or 'exclude'.
166+
*
167+
* @return void
168+
* @dataProvider dataOnlySetOnce
169+
*/
170+
public function testOnlySetOnce($argument)
171+
{
172+
$config = new ConfigDouble(
173+
[
174+
"--$argument=StandardOne.Category.Sniff",
175+
"--$argument=StandardTwo.Category.Sniff",
176+
"--$argument=Standard.AnotherCategory.Sniff",
177+
]
178+
);
179+
180+
$this->assertSame(['StandardOne.Category.Sniff'], $config->$argument);
181+
182+
}//end testOnlySetOnce()
183+
184+
185+
/**
186+
* Data provider for testOnlySetOnce().
187+
*
188+
* @return array<string, array<string>>
189+
*/
190+
public static function dataOnlySetOnce()
191+
{
192+
return [
193+
'sniffs' => ['sniffs'],
194+
'exclude' => ['exclude'],
195+
];
196+
197+
}//end dataOnlySetOnce()
198+
199+
200+
}//end class

0 commit comments

Comments
 (0)