Skip to content

Commit 25b6b69

Browse files
committed
Tests: remove work-arounds for --sniffs not being respected
Now that the `--sniffs` CLI argument will be respected in the tests, a number of work-arounds previously introduced can be removed. Note: there may be a few more work-arounds which can be removed, like removing some fixture rulesets in favour of test inline sniff selection, but this should be a good first step.
1 parent 41d955a commit 25b6b69

File tree

6 files changed

+23
-103
lines changed

6 files changed

+23
-103
lines changed

tests/Core/Generators/GeneratorTest.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -194,16 +194,6 @@ public function testGetTitleFallbackToFilename()
194194
$config = new ConfigDouble(["--standard=$standard", "--sniffs=$sniffs"]);
195195
$ruleset = new Ruleset($config);
196196

197-
// In tests, the `--sniffs` setting doesn't work out of the box.
198-
$sniffParts = explode('.', $sniffs);
199-
$sniffFile = __DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.$sniffParts[0].DIRECTORY_SEPARATOR;
200-
$sniffFile .= 'Sniffs'.DIRECTORY_SEPARATOR.$sniffParts[1].DIRECTORY_SEPARATOR.$sniffParts[2].'Sniff.php';
201-
202-
$sniffParts = array_map('strtolower', $sniffParts);
203-
$sniffName = $sniffParts[0].'\sniffs\\'.$sniffParts[1].'\\'.$sniffParts[2].'sniff';
204-
$restrictions = [$sniffName => true];
205-
$ruleset->registerSniffs([$sniffFile], $restrictions, []);
206-
207197
// Make the test OS independent.
208198
$this->expectOutputString('Documentation Title PCRE Fallback'.PHP_EOL);
209199

tests/Core/Generators/HTMLTest.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,6 @@ public function testDocSpecifics($sniffs, $pathToExpected)
9494
$config = new ConfigDouble(["--standard=$standard", "--sniffs=$sniffs"]);
9595
$ruleset = new Ruleset($config);
9696

97-
// In tests, the `--sniffs` setting doesn't work out of the box.
98-
$sniffParts = explode('.', $sniffs);
99-
$sniffFile = __DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.$sniffParts[0].DIRECTORY_SEPARATOR;
100-
$sniffFile .= 'Sniffs'.DIRECTORY_SEPARATOR.$sniffParts[1].DIRECTORY_SEPARATOR.$sniffParts[2].'Sniff.php';
101-
102-
$sniffParts = array_map('strtolower', $sniffParts);
103-
$sniffName = $sniffParts[0].'\sniffs\\'.$sniffParts[1].'\\'.$sniffParts[2].'sniff';
104-
$restrictions = [$sniffName => true];
105-
$ruleset->registerSniffs([$sniffFile], $restrictions, []);
106-
10797
$expected = file_get_contents($pathToExpected);
10898
$this->assertNotFalse($expected, 'Output expectation file could not be found');
10999

tests/Core/Generators/MarkdownTest.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,6 @@ public function testDocSpecifics($sniffs, $pathToExpected)
9494
$config = new ConfigDouble(["--standard=$standard", "--sniffs=$sniffs"]);
9595
$ruleset = new Ruleset($config);
9696

97-
// In tests, the `--sniffs` setting doesn't work out of the box.
98-
$sniffParts = explode('.', $sniffs);
99-
$sniffFile = __DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.$sniffParts[0].DIRECTORY_SEPARATOR;
100-
$sniffFile .= 'Sniffs'.DIRECTORY_SEPARATOR.$sniffParts[1].DIRECTORY_SEPARATOR.$sniffParts[2].'Sniff.php';
101-
102-
$sniffParts = array_map('strtolower', $sniffParts);
103-
$sniffName = $sniffParts[0].'\sniffs\\'.$sniffParts[1].'\\'.$sniffParts[2].'sniff';
104-
$restrictions = [$sniffName => true];
105-
$ruleset->registerSniffs([$sniffFile], $restrictions, []);
106-
10797
$expected = file_get_contents($pathToExpected);
10898
$this->assertNotFalse($expected, 'Output expectation file could not be found');
10999

tests/Core/Generators/TextTest.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,6 @@ public function testDocSpecifics($sniffs, $pathToExpected)
9494
$config = new ConfigDouble(["--standard=$standard", "--sniffs=$sniffs"]);
9595
$ruleset = new Ruleset($config);
9696

97-
// In tests, the `--sniffs` setting doesn't work out of the box.
98-
$sniffParts = explode('.', $sniffs);
99-
$sniffFile = __DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.$sniffParts[0].DIRECTORY_SEPARATOR;
100-
$sniffFile .= 'Sniffs'.DIRECTORY_SEPARATOR.$sniffParts[1].DIRECTORY_SEPARATOR.$sniffParts[2].'Sniff.php';
101-
102-
$sniffParts = array_map('strtolower', $sniffParts);
103-
$sniffName = $sniffParts[0].'\sniffs\\'.$sniffParts[1].'\\'.$sniffParts[2].'sniff';
104-
$restrictions = [$sniffName => true];
105-
$ruleset->registerSniffs([$sniffFile], $restrictions, []);
106-
10797
$expected = file_get_contents($pathToExpected);
10898
$this->assertNotFalse($expected, 'Output expectation file could not be found');
10999

tests/Core/Ruleset/RegisterSniffsTest.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -140,28 +140,28 @@ public function testRegisteredSniffsShouldBeTheSame()
140140
/**
141141
* Test that if only specific sniffs are requested, only those are registered.
142142
*
143-
* {@internal Can't test this via the CLI arguments due to some code in the Ruleset class
144-
* related to sniff tests.}
145-
*
146143
* @return void
147144
*/
148145
public function testRegisteredSniffsWithRestrictions()
149146
{
150-
$restrictions = [
151-
'psr1\\sniffs\\classes\\classdeclarationsniff' => true,
152-
'psr1\\sniffs\\files\\sideeffectssniff' => true,
153-
'psr1\\sniffs\\methods\\camelcapsmethodnamesniff' => true,
147+
// Set up the ruleset.
148+
$args = [
149+
'--standard=PSR1',
150+
'--sniffs=PSR1.Classes.ClassDeclaration,PSR1.Files.SideEffects,PSR1.Methods.CamelCapsMethodName',
154151
];
152+
$config = new ConfigDouble($args);
153+
$ruleset = new Ruleset($config);
155154

156155
$expected = [
157156
'PHP_CodeSniffer\\Standards\\PSR1\\Sniffs\\Classes\\ClassDeclarationSniff',
158157
'PHP_CodeSniffer\\Standards\\PSR1\\Sniffs\\Files\\SideEffectsSniff',
159158
'PHP_CodeSniffer\\Standards\\PSR1\\Sniffs\\Methods\\CamelCapsMethodNameSniff',
160159
];
161160

162-
self::$ruleset->registerSniffs(self::$psr1SniffAbsolutePaths, $restrictions, []);
161+
$actual = array_keys($ruleset->sniffs);
162+
sort($actual);
163163

164-
$this->assertSame($expected, array_keys(self::$ruleset->sniffs));
164+
$this->assertSame($expected, $actual);
165165

166166
}//end testRegisteredSniffsWithRestrictions()
167167

tests/Core/Ruleset/ShowSniffDeprecationsTest.php

Lines changed: 14 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -169,35 +169,13 @@ public static function dataDeprecatedSniffsListDoesNotShowNeedsCsMode()
169169
public function testDeprecatedSniffsListDoesNotShowWhenSelectedSniffsAreNotDeprecated()
170170
{
171171
$standard = __DIR__.'/ShowSniffDeprecationsTest.xml';
172-
$config = new ConfigDouble(['.', "--standard=$standard"]);
173-
$ruleset = new Ruleset($config);
174-
175-
/*
176-
* Apply sniff restrictions.
177-
* For tests we need to manually trigger this if the standard is "installed", like with the fixtures these tests use.
178-
*/
179-
180-
$restrictions = [];
181-
$sniffs = [
182-
'TestStandard.SetProperty.AllowedAsDeclared',
183-
'TestStandard.SetProperty.AllowedViaStdClass',
172+
$cliArgs = [
173+
'.',
174+
"--standard=$standard",
175+
'--sniffs=TestStandard.SetProperty.AllowedAsDeclared,TestStandard.SetProperty.AllowedViaStdClass',
184176
];
185-
foreach ($sniffs as $sniffCode) {
186-
$parts = explode('.', strtolower($sniffCode));
187-
$sniffName = $parts[0].'\\sniffs\\'.$parts[1].'\\'.$parts[2].'sniff';
188-
$restrictions[strtolower($sniffName)] = true;
189-
}
190-
191-
$sniffFiles = [];
192-
$allSniffs = $ruleset->sniffCodes;
193-
foreach ($allSniffs as $sniffName) {
194-
$sniffFile = str_replace('\\', DIRECTORY_SEPARATOR, $sniffName);
195-
$sniffFile = __DIR__.DIRECTORY_SEPARATOR.$sniffFile.'.php';
196-
$sniffFiles[] = $sniffFile;
197-
}
198-
199-
$ruleset->registerSniffs($sniffFiles, $restrictions, []);
200-
$ruleset->populateTokenListeners();
177+
$config = new ConfigDouble($cliArgs);
178+
$ruleset = new Ruleset($config);
201179

202180
$this->expectOutputString('');
203181

@@ -215,38 +193,20 @@ public function testDeprecatedSniffsListDoesNotShowWhenSelectedSniffsAreNotDepre
215193
public function testDeprecatedSniffsListDoesNotShowWhenAllDeprecatedSniffsAreExcluded()
216194
{
217195
$standard = __DIR__.'/ShowSniffDeprecationsTest.xml';
218-
$config = new ConfigDouble(['.', "--standard=$standard"]);
219-
$ruleset = new Ruleset($config);
220-
221-
/*
222-
* Apply sniff restrictions.
223-
* For tests we need to manually trigger this if the standard is "installed", like with the fixtures these tests use.
224-
*/
225-
226-
$exclusions = [];
227-
$exclude = [
196+
$exclude = [
228197
'TestStandard.Deprecated.WithLongReplacement',
229198
'TestStandard.Deprecated.WithoutReplacement',
230199
'TestStandard.Deprecated.WithReplacement',
231200
'TestStandard.Deprecated.WithReplacementContainingLinuxNewlines',
232201
'TestStandard.Deprecated.WithReplacementContainingNewlines',
233202
];
234-
foreach ($exclude as $sniffCode) {
235-
$parts = explode('.', strtolower($sniffCode));
236-
$sniffName = $parts[0].'\\sniffs\\'.$parts[1].'\\'.$parts[2].'sniff';
237-
$exclusions[strtolower($sniffName)] = true;
238-
}
239-
240-
$sniffFiles = [];
241-
$allSniffs = $ruleset->sniffCodes;
242-
foreach ($allSniffs as $sniffName) {
243-
$sniffFile = str_replace('\\', DIRECTORY_SEPARATOR, $sniffName);
244-
$sniffFile = __DIR__.DIRECTORY_SEPARATOR.$sniffFile.'.php';
245-
$sniffFiles[] = $sniffFile;
246-
}
247-
248-
$ruleset->registerSniffs($sniffFiles, [], $exclusions);
249-
$ruleset->populateTokenListeners();
203+
$cliArgs = [
204+
'.',
205+
"--standard=$standard",
206+
'--exclude='.implode(',', $exclude),
207+
];
208+
$config = new ConfigDouble($cliArgs);
209+
$ruleset = new Ruleset($config);
250210

251211
$this->expectOutputString('');
252212

0 commit comments

Comments
 (0)