|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Test the Ruleset::registerSniffs() method. |
| 4 | + * |
| 5 | + * @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl> |
| 6 | + * @copyright 2024 PHPCSStandards and contributors |
| 7 | + * @license http://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence |
| 8 | + */ |
| 9 | + |
| 10 | +namespace PHP_CodeSniffer\Tests\Core\Ruleset; |
| 11 | + |
| 12 | +use PHP_CodeSniffer\Ruleset; |
| 13 | +use PHP_CodeSniffer\Tests\ConfigDouble; |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | + |
| 16 | +/** |
| 17 | + * Test the Ruleset::registerSniffs() method. |
| 18 | + * |
| 19 | + * @covers \PHP_CodeSniffer\Ruleset::registerSniffs |
| 20 | + */ |
| 21 | +final class RegisterSniffsTest extends TestCase |
| 22 | +{ |
| 23 | + |
| 24 | + /** |
| 25 | + * The Ruleset object. |
| 26 | + * |
| 27 | + * @var \PHP_CodeSniffer\Ruleset |
| 28 | + */ |
| 29 | + private static $ruleset; |
| 30 | + |
| 31 | + /** |
| 32 | + * Original value of the $sniffs property on the Ruleset. |
| 33 | + * |
| 34 | + * @var array<string, \PHP_CodeSniffer\Sniffs\Sniff> |
| 35 | + */ |
| 36 | + private static $originalSniffs = []; |
| 37 | + |
| 38 | + /** |
| 39 | + * List of Standards dir relative sniff files loaded for the PSR1 standard. |
| 40 | + * |
| 41 | + * @var array<string> |
| 42 | + */ |
| 43 | + private static $psr1SniffFiles = [ |
| 44 | + 'Generic/Sniffs/Files/ByteOrderMarkSniff.php', |
| 45 | + 'Generic/Sniffs/NamingConventions/UpperCaseConstantNameSniff.php', |
| 46 | + 'Generic/Sniffs/PHP/DisallowAlternativePHPTagsSniff.php', |
| 47 | + 'Generic/Sniffs/PHP/DisallowShortOpenTagSniff.php', |
| 48 | + 'PSR1/Sniffs/Classes/ClassDeclarationSniff.php', |
| 49 | + 'PSR1/Sniffs/Files/SideEffectsSniff.php', |
| 50 | + 'PSR1/Sniffs/Methods/CamelCapsMethodNameSniff.php', |
| 51 | + 'Squiz/Sniffs/Classes/ValidClassNameSniff.php', |
| 52 | + ]; |
| 53 | + |
| 54 | + /** |
| 55 | + * Absolute paths to the sniff files loaded for the PSR1 standard. |
| 56 | + * |
| 57 | + * @var array<string> |
| 58 | + */ |
| 59 | + private static $psr1SniffAbsolutePaths = []; |
| 60 | + |
| 61 | + |
| 62 | + /** |
| 63 | + * Initialize the config and ruleset objects which will be used for some of these tests. |
| 64 | + * |
| 65 | + * @beforeClass |
| 66 | + * |
| 67 | + * @return void |
| 68 | + */ |
| 69 | + public static function initializeConfigAndRuleset() |
| 70 | + { |
| 71 | + // Set up the ruleset. |
| 72 | + $config = new ConfigDouble(['--standard=PSR1']); |
| 73 | + self::$ruleset = new Ruleset($config); |
| 74 | + |
| 75 | + // Remember the original value of the Ruleset::$sniff property as the tests adjust it. |
| 76 | + self::$originalSniffs = self::$ruleset->sniffs; |
| 77 | + |
| 78 | + // Sort the value to make the tests stable as different OSes will read directories |
| 79 | + // in a different order and the order is not relevant for these tests. Just the values. |
| 80 | + ksort(self::$originalSniffs); |
| 81 | + |
| 82 | + // Update the sniff file list. |
| 83 | + $standardsDir = dirname(dirname(dirname(__DIR__))).DIRECTORY_SEPARATOR; |
| 84 | + $standardsDir .= 'src'.DIRECTORY_SEPARATOR.'Standards'.DIRECTORY_SEPARATOR; |
| 85 | + |
| 86 | + self::$psr1SniffAbsolutePaths = self::relativeToAbsoluteSniffFiles($standardsDir, self::$psr1SniffFiles); |
| 87 | + |
| 88 | + }//end initializeConfigAndRuleset() |
| 89 | + |
| 90 | + |
| 91 | + /** |
| 92 | + * Convert relative paths to absolute paths and ensure the paths use the correct OS-specific directory separator. |
| 93 | + * |
| 94 | + * @param string $baseDir Directory to which these paths are relative to. Including trailing slash. |
| 95 | + * @param array<string> $relativePaths Relative paths. |
| 96 | + * |
| 97 | + * @return array<string> |
| 98 | + */ |
| 99 | + public static function relativeToAbsoluteSniffFiles($baseDir, $relativePaths) |
| 100 | + { |
| 101 | + $fileList = []; |
| 102 | + foreach ($relativePaths as $sniffName) { |
| 103 | + $sniffFile = str_replace('/', DIRECTORY_SEPARATOR, $sniffName); |
| 104 | + $sniffFile = $baseDir.$sniffFile; |
| 105 | + $fileList[] = $sniffFile; |
| 106 | + } |
| 107 | + |
| 108 | + return $fileList; |
| 109 | + |
| 110 | + }//end relativeToAbsoluteSniffFiles() |
| 111 | + |
| 112 | + |
| 113 | + /** |
| 114 | + * Clear out the Ruleset::$sniffs property. |
| 115 | + * |
| 116 | + * @before |
| 117 | + * |
| 118 | + * @return void |
| 119 | + */ |
| 120 | + protected function clearOutSniffs() |
| 121 | + { |
| 122 | + // Clear out the Ruleset::$sniffs property. |
| 123 | + self::$ruleset->sniffs = []; |
| 124 | + |
| 125 | + }//end clearOutSniffs() |
| 126 | + |
| 127 | + |
| 128 | + /** |
| 129 | + * Test that registering sniffs works as expected (simple base test case). |
| 130 | + * |
| 131 | + * @return void |
| 132 | + */ |
| 133 | + public function testRegisteredSniffsShouldBeTheSame() |
| 134 | + { |
| 135 | + self::$ruleset->registerSniffs(self::$psr1SniffAbsolutePaths, [], []); |
| 136 | + |
| 137 | + // Make sure the same sniff list was recreated (but without the objects having been created yet). |
| 138 | + $this->assertSame(array_keys(self::$originalSniffs), array_keys(self::$ruleset->sniffs)); |
| 139 | + $this->assertSame(array_keys(self::$originalSniffs), array_values(self::$ruleset->sniffs)); |
| 140 | + |
| 141 | + }//end testRegisteredSniffsShouldBeTheSame() |
| 142 | + |
| 143 | + |
| 144 | + /** |
| 145 | + * Test that if only specific sniffs are requested, only those are registered. |
| 146 | + * |
| 147 | + * {@internal Can't test this via the CLI arguments due to some code in the Ruleset class |
| 148 | + * related to sniff tests.} |
| 149 | + * |
| 150 | + * @return void |
| 151 | + */ |
| 152 | + public function testRegisteredSniffsWithRestrictions() |
| 153 | + { |
| 154 | + $restrictions = [ |
| 155 | + 'psr1\\sniffs\\classes\\classdeclarationsniff' => true, |
| 156 | + 'psr1\\sniffs\\files\\sideeffectssniff' => true, |
| 157 | + 'psr1\\sniffs\\methods\\camelcapsmethodnamesniff' => true, |
| 158 | + ]; |
| 159 | + |
| 160 | + $expected = [ |
| 161 | + 'PHP_CodeSniffer\\Standards\\PSR1\\Sniffs\\Classes\\ClassDeclarationSniff', |
| 162 | + 'PHP_CodeSniffer\\Standards\\PSR1\\Sniffs\\Files\\SideEffectsSniff', |
| 163 | + 'PHP_CodeSniffer\\Standards\\PSR1\\Sniffs\\Methods\\CamelCapsMethodNameSniff', |
| 164 | + ]; |
| 165 | + |
| 166 | + self::$ruleset->registerSniffs(self::$psr1SniffAbsolutePaths, $restrictions, []); |
| 167 | + |
| 168 | + $this->assertSame($expected, array_keys(self::$ruleset->sniffs)); |
| 169 | + |
| 170 | + }//end testRegisteredSniffsWithRestrictions() |
| 171 | + |
| 172 | + |
| 173 | + /** |
| 174 | + * Test that sniffs excluded via the CLI are not registered. |
| 175 | + * |
| 176 | + * @return void |
| 177 | + */ |
| 178 | + public function testRegisteredSniffsWithExclusions() |
| 179 | + { |
| 180 | + // Set up the ruleset. |
| 181 | + $args = [ |
| 182 | + '--standard=PSR1', |
| 183 | + '--exclude=PSR1.Classes.ClassDeclaration,PSR1.Files.SideEffects,PSR1.Methods.CamelCapsMethodName', |
| 184 | + ]; |
| 185 | + $config = new ConfigDouble($args); |
| 186 | + $ruleset = new Ruleset($config); |
| 187 | + |
| 188 | + $expected = [ |
| 189 | + 'PHP_CodeSniffer\\Standards\\Generic\\Sniffs\\Files\\ByteOrderMarkSniff', |
| 190 | + 'PHP_CodeSniffer\\Standards\\Generic\\Sniffs\\NamingConventions\\UpperCaseConstantNameSniff', |
| 191 | + 'PHP_CodeSniffer\\Standards\\Generic\\Sniffs\\PHP\\DisallowAlternativePHPTagsSniff', |
| 192 | + 'PHP_CodeSniffer\\Standards\\Generic\\Sniffs\\PHP\\DisallowShortOpenTagSniff', |
| 193 | + 'PHP_CodeSniffer\\Standards\\Squiz\\Sniffs\\Classes\\ValidClassNameSniff', |
| 194 | + ]; |
| 195 | + |
| 196 | + $actual = array_keys($ruleset->sniffs); |
| 197 | + sort($actual); |
| 198 | + |
| 199 | + $this->assertSame($expected, $actual); |
| 200 | + |
| 201 | + }//end testRegisteredSniffsWithExclusions() |
| 202 | + |
| 203 | + |
| 204 | + /** |
| 205 | + * Test combining requesting specific sniffs and excluding a subset of those. |
| 206 | + * |
| 207 | + * @return void |
| 208 | + */ |
| 209 | + public function testRegisteredSniffsBothRestrictionsAndExclusions() |
| 210 | + { |
| 211 | + $restrictions = [ |
| 212 | + 'generic\\sniffs\\namingconventions\\uppercaseconstantnamesniff' => true, |
| 213 | + 'generic\\sniffs\\php\\disallowalternativephptagssniff' => true, |
| 214 | + 'generic\\sniffs\\php\\disallowshortopentagsniff' => true, |
| 215 | + 'psr1\\sniffs\\classes\\classdeclarationsniff' => true, |
| 216 | + 'squiz\\sniffs\\classes\\validclassnamesniff' => true, |
| 217 | + ]; |
| 218 | + |
| 219 | + $exclusions = [ |
| 220 | + 'squiz\\sniffs\\classes\\validclassnamesniff' => true, |
| 221 | + 'generic\\sniffs\\php\\disallowalternativephptagssniff' => true, |
| 222 | + 'generic\\sniffs\\namingconventions\\uppercaseconstantnamesniff' => true, |
| 223 | + ]; |
| 224 | + |
| 225 | + $expected = [ |
| 226 | + 'PHP_CodeSniffer\\Standards\\Generic\\Sniffs\\PHP\\DisallowShortOpenTagSniff', |
| 227 | + 'PHP_CodeSniffer\\Standards\\PSR1\\Sniffs\\Classes\\ClassDeclarationSniff', |
| 228 | + ]; |
| 229 | + |
| 230 | + self::$ruleset->registerSniffs(self::$psr1SniffAbsolutePaths, $restrictions, $exclusions); |
| 231 | + |
| 232 | + $this->assertEquals($expected, array_keys(self::$ruleset->sniffs)); |
| 233 | + |
| 234 | + }//end testRegisteredSniffsBothRestrictionsAndExclusions() |
| 235 | + |
| 236 | + |
| 237 | + /** |
| 238 | + * Verify that abstract sniffs are filtered out and not registered. |
| 239 | + * |
| 240 | + * @return void |
| 241 | + */ |
| 242 | + public function testRegisterSniffsFiltersOutAbstractClasses() |
| 243 | + { |
| 244 | + $extraPathsBaseDir = __DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR; |
| 245 | + $extraPaths = [ |
| 246 | + 'DirectoryExpansion/.hiddenAbove/src/MyStandard/Sniffs/AbstractSniff.php', |
| 247 | + 'DirectoryExpansion/.hiddenAbove/src/MyStandard/Sniffs/CategoryB/AnotherAbstractSniff.php', |
| 248 | + ]; |
| 249 | + $extraPaths = self::relativeToAbsoluteSniffFiles($extraPathsBaseDir, $extraPaths); |
| 250 | + |
| 251 | + $fileList = self::$psr1SniffAbsolutePaths; |
| 252 | + foreach ($extraPaths as $path) { |
| 253 | + $fileList[] = $path; |
| 254 | + } |
| 255 | + |
| 256 | + self::$ruleset->registerSniffs($fileList, [], []); |
| 257 | + |
| 258 | + // Make sure the same sniff list was recreated (but without the objects having been created yet). |
| 259 | + $this->assertSame(array_keys(self::$originalSniffs), array_keys(self::$ruleset->sniffs)); |
| 260 | + $this->assertSame(array_keys(self::$originalSniffs), array_values(self::$ruleset->sniffs)); |
| 261 | + |
| 262 | + }//end testRegisterSniffsFiltersOutAbstractClasses() |
| 263 | + |
| 264 | + |
| 265 | + /** |
| 266 | + * Test that sniff files not in a "/Sniffs/" directory are filtered out and not registered. |
| 267 | + * |
| 268 | + * @return void |
| 269 | + */ |
| 270 | + public function testRegisteredSniffsFiltersOutFilePathsWithoutSniffsDir() |
| 271 | + { |
| 272 | + $extraPathsBaseDir = __DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR; |
| 273 | + $extraPaths = [ |
| 274 | + 'DirectoryExpansion/.hiddenAbove/src/MyStandard/Utils/NotInSniffsDirSniff.php', |
| 275 | + 'DirectoryExpansion/.hiddenAbove/src/MyStandard/Utils/SubDir/NotInSniffsDirSniff.php', |
| 276 | + ]; |
| 277 | + $extraPaths = self::relativeToAbsoluteSniffFiles($extraPathsBaseDir, $extraPaths); |
| 278 | + |
| 279 | + $fileList = self::$psr1SniffAbsolutePaths; |
| 280 | + foreach ($extraPaths as $path) { |
| 281 | + $fileList[] = $path; |
| 282 | + } |
| 283 | + |
| 284 | + self::$ruleset->registerSniffs($fileList, [], []); |
| 285 | + |
| 286 | + // Make sure the same sniff list was recreated (but without the objects having been created yet). |
| 287 | + $this->assertSame(array_keys(self::$originalSniffs), array_keys(self::$ruleset->sniffs)); |
| 288 | + $this->assertSame(array_keys(self::$originalSniffs), array_values(self::$ruleset->sniffs)); |
| 289 | + |
| 290 | + }//end testRegisteredSniffsFiltersOutFilePathsWithoutSniffsDir() |
| 291 | + |
| 292 | + |
| 293 | +}//end class |
0 commit comments