Skip to content

Commit 48c5e81

Browse files
committed
Ruleset::setSniffProperty(): add test for edge case / unused sniff
I haven't been able to come up with a _real_ situation in which [this condition](https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/15db7232015d4fc1e023ef1eff0e65777a906f2c/src/Ruleset.php#L1476-L1479) could result in an early return - either via reading an XML ruleset or via inline `phpcs:set` annotation. Having said that, the method is `public` and is regularly used in test frameworks for external standards, so this code should remain in place. This commit now safeguards the behaviour via a test.
1 parent ee7034b commit 48c5e81

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

tests/Core/Ruleset/SetSniffPropertyTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,39 @@ public function testSetPropertyDoesNotThrowErrorOnInvalidPropertyWhenSetForCateg
202202
}//end testSetPropertyDoesNotThrowErrorOnInvalidPropertyWhenSetForCategory()
203203

204204

205+
/**
206+
* Test that attempting to set a property for a sniff which isn't registered will be ignored.
207+
*
208+
* @return void
209+
*/
210+
public function testDirectCallIgnoredPropertyForUnusedSniff()
211+
{
212+
$sniffCode = 'Generic.Formatting.SpaceAfterCast';
213+
$sniffClass = 'PHP_CodeSniffer\\Standards\\Generic\\Sniffs\\Formatting\\SpaceAfterCastSniff';
214+
215+
// Set up the ruleset.
216+
$config = new ConfigDouble(['--standard=PSR1']);
217+
$ruleset = new Ruleset($config);
218+
219+
$ruleset->setSniffProperty(
220+
$sniffClass,
221+
'ignoreNewlines',
222+
[
223+
'scope' => 'sniff',
224+
'value' => true,
225+
]
226+
);
227+
228+
// Verify that there are sniffs registered.
229+
$this->assertGreaterThan(0, count($ruleset->sniffCodes), 'No sniff codes registered');
230+
231+
// Verify that our target sniff has NOT been registered after attempting to set the property.
232+
$this->assertArrayNotHasKey($sniffCode, $ruleset->sniffCodes, 'Unused sniff was registered in sniffCodes, but shouldn\'t have been');
233+
$this->assertArrayNotHasKey($sniffClass, $ruleset->sniffs, 'Unused sniff was registered in sniffs, but shouldn\'t have been');
234+
235+
}//end testDirectCallIgnoredPropertyForUnusedSniff()
236+
237+
205238
/**
206239
* Test that setting a property via a direct call to the Ruleset::setSniffProperty() method
207240
* sets the property correctly when using the new $settings array format.

0 commit comments

Comments
 (0)