PATH:
opt
/
cpanel
/
ea-wappspector
/
vendor
/
squizlabs
/
php_codesniffer
/
tests
/
Core
/
Ruleset
<?php /** * Test the Ruleset::registerSniffs() method. * * @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl> * @copyright 2024 PHPCSStandards and contributors * @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence */ namespace PHP_CodeSniffer\Tests\Core\Ruleset; use PHP_CodeSniffer\Ruleset; use PHP_CodeSniffer\Tests\ConfigDouble; use PHPUnit\Framework\TestCase; /** * Test the Ruleset::registerSniffs() method. * * @covers \PHP_CodeSniffer\Ruleset::registerSniffs */ final class RegisterSniffsTest extends TestCase { /** * The Ruleset object. * * @var \PHP_CodeSniffer\Ruleset */ private static $ruleset; /** * Original value of the $sniffs property on the Ruleset. * * @var array<string, \PHP_CodeSniffer\Sniffs\Sniff> */ private static $originalSniffs = []; /** * List of Standards dir relative sniff files loaded for the PSR1 standard. * * @var array<string> */ private static $psr1SniffFiles = [ 'Generic/Sniffs/Files/ByteOrderMarkSniff.php', 'Generic/Sniffs/NamingConventions/UpperCaseConstantNameSniff.php', 'Generic/Sniffs/PHP/DisallowAlternativePHPTagsSniff.php', 'Generic/Sniffs/PHP/DisallowShortOpenTagSniff.php', 'PSR1/Sniffs/Classes/ClassDeclarationSniff.php', 'PSR1/Sniffs/Files/SideEffectsSniff.php', 'PSR1/Sniffs/Methods/CamelCapsMethodNameSniff.php', 'Squiz/Sniffs/Classes/ValidClassNameSniff.php', ]; /** * Absolute paths to the sniff files loaded for the PSR1 standard. * * @var array<string> */ private static $psr1SniffAbsolutePaths = []; /** * Initialize the config and ruleset objects which will be used for some of these tests. * * @beforeClass * * @return void */ public static function initializeConfigAndRuleset() { // Set up the ruleset. $config = new ConfigDouble(['--standard=PSR1']); self::$ruleset = new Ruleset($config); // Remember the original value of the Ruleset::$sniff property as the tests adjust it. self::$originalSniffs = self::$ruleset->sniffs; // Sort the value to make the tests stable as different OSes will read directories // in a different order and the order is not relevant for these tests. Just the values. ksort(self::$originalSniffs); // Update the sniff file list. $standardsDir = dirname(dirname(dirname(__DIR__))).DIRECTORY_SEPARATOR; $standardsDir .= 'src'.DIRECTORY_SEPARATOR.'Standards'.DIRECTORY_SEPARATOR; self::$psr1SniffAbsolutePaths = self::relativeToAbsoluteSniffFiles($standardsDir, self::$psr1SniffFiles); }//end initializeConfigAndRuleset() /** * Convert relative paths to absolute paths and ensure the paths use the correct OS-specific directory separator. * * @param string $baseDir Directory to which these paths are relative to. Including trailing slash. * @param array<string> $relativePaths Relative paths. * * @return array<string> */ public static function relativeToAbsoluteSniffFiles($baseDir, $relativePaths) { $fileList = []; foreach ($relativePaths as $sniffName) { $sniffFile = str_replace('/', DIRECTORY_SEPARATOR, $sniffName); $sniffFile = $baseDir.$sniffFile; $fileList[] = $sniffFile; } return $fileList; }//end relativeToAbsoluteSniffFiles() /** * Clear out the Ruleset::$sniffs property. * * @before * * @return void */ protected function clearOutSniffs() { // Clear out the Ruleset::$sniffs property. self::$ruleset->sniffs = []; }//end clearOutSniffs() /** * Test that registering sniffs works as expected (simple base test case). * * @return void */ public function testRegisteredSniffsShouldBeTheSame() { self::$ruleset->registerSniffs(self::$psr1SniffAbsolutePaths, [], []); // Make sure the same sniff list was recreated (but without the objects having been created yet). $this->assertSame(array_keys(self::$originalSniffs), array_keys(self::$ruleset->sniffs)); $this->assertSame(array_keys(self::$originalSniffs), array_values(self::$ruleset->sniffs)); }//end testRegisteredSniffsShouldBeTheSame() /** * Test that if only specific sniffs are requested, only those are registered. * * {@internal Can't test this via the CLI arguments due to some code in the Ruleset class * related to sniff tests.} * * @return void */ public function testRegisteredSniffsWithRestrictions() { $restrictions = [ 'psr1\\sniffs\\classes\\classdeclarationsniff' => true, 'psr1\\sniffs\\files\\sideeffectssniff' => true, 'psr1\\sniffs\\methods\\camelcapsmethodnamesniff' => true, ]; $expected = [ 'PHP_CodeSniffer\\Standards\\PSR1\\Sniffs\\Classes\\ClassDeclarationSniff', 'PHP_CodeSniffer\\Standards\\PSR1\\Sniffs\\Files\\SideEffectsSniff', 'PHP_CodeSniffer\\Standards\\PSR1\\Sniffs\\Methods\\CamelCapsMethodNameSniff', ]; self::$ruleset->registerSniffs(self::$psr1SniffAbsolutePaths, $restrictions, []); $this->assertSame($expected, array_keys(self::$ruleset->sniffs)); }//end testRegisteredSniffsWithRestrictions() /** * Test that sniffs excluded via the CLI are not registered. * * @return void */ public function testRegisteredSniffsWithExclusions() { // Set up the ruleset. $args = [ '--standard=PSR1', '--exclude=PSR1.Classes.ClassDeclaration,PSR1.Files.SideEffects,PSR1.Methods.CamelCapsMethodName', ]; $config = new ConfigDouble($args); $ruleset = new Ruleset($config); $expected = [ 'PHP_CodeSniffer\\Standards\\Generic\\Sniffs\\Files\\ByteOrderMarkSniff', 'PHP_CodeSniffer\\Standards\\Generic\\Sniffs\\NamingConventions\\UpperCaseConstantNameSniff', 'PHP_CodeSniffer\\Standards\\Generic\\Sniffs\\PHP\\DisallowAlternativePHPTagsSniff', 'PHP_CodeSniffer\\Standards\\Generic\\Sniffs\\PHP\\DisallowShortOpenTagSniff', 'PHP_CodeSniffer\\Standards\\Squiz\\Sniffs\\Classes\\ValidClassNameSniff', ]; $actual = array_keys($ruleset->sniffs); sort($actual); $this->assertSame($expected, $actual); }//end testRegisteredSniffsWithExclusions() /** * Test combining requesting specific sniffs and excluding a subset of those. * * @return void */ public function testRegisteredSniffsBothRestrictionsAndExclusions() { $restrictions = [ 'generic\\sniffs\\namingconventions\\uppercaseconstantnamesniff' => true, 'generic\\sniffs\\php\\disallowalternativephptagssniff' => true, 'generic\\sniffs\\php\\disallowshortopentagsniff' => true, 'psr1\\sniffs\\classes\\classdeclarationsniff' => true, 'squiz\\sniffs\\classes\\validclassnamesniff' => true, ]; $exclusions = [ 'squiz\\sniffs\\classes\\validclassnamesniff' => true, 'generic\\sniffs\\php\\disallowalternativephptagssniff' => true, 'generic\\sniffs\\namingconventions\\uppercaseconstantnamesniff' => true, ]; $expected = [ 'PHP_CodeSniffer\\Standards\\Generic\\Sniffs\\PHP\\DisallowShortOpenTagSniff', 'PHP_CodeSniffer\\Standards\\PSR1\\Sniffs\\Classes\\ClassDeclarationSniff', ]; self::$ruleset->registerSniffs(self::$psr1SniffAbsolutePaths, $restrictions, $exclusions); $this->assertSame($expected, array_keys(self::$ruleset->sniffs)); }//end testRegisteredSniffsBothRestrictionsAndExclusions() /** * Verify that abstract sniffs are filtered out and not registered. * * @return void */ public function testRegisterSniffsFiltersOutAbstractClasses() { $extraPathsBaseDir = __DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR; $extraPaths = [ 'DirectoryExpansion/.hiddenAbove/src/MyStandard/Sniffs/AbstractSniff.php', 'DirectoryExpansion/.hiddenAbove/src/MyStandard/Sniffs/CategoryB/AnotherAbstractSniff.php', ]; $extraPaths = self::relativeToAbsoluteSniffFiles($extraPathsBaseDir, $extraPaths); $fileList = self::$psr1SniffAbsolutePaths; foreach ($extraPaths as $path) { $fileList[] = $path; } self::$ruleset->registerSniffs($fileList, [], []); // Make sure the same sniff list was recreated (but without the objects having been created yet). $this->assertSame(array_keys(self::$originalSniffs), array_keys(self::$ruleset->sniffs)); $this->assertSame(array_keys(self::$originalSniffs), array_values(self::$ruleset->sniffs)); }//end testRegisterSniffsFiltersOutAbstractClasses() /** * Test that sniff files not in a "/Sniffs/" directory are filtered out and not registered. * * @return void */ public function testRegisteredSniffsFiltersOutFilePathsWithoutSniffsDir() { $extraPathsBaseDir = __DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR; $extraPaths = [ 'DirectoryExpansion/.hiddenAbove/src/MyStandard/Utils/NotInSniffsDirSniff.php', 'DirectoryExpansion/.hiddenAbove/src/MyStandard/Utils/SubDir/NotInSniffsDirSniff.php', ]; $extraPaths = self::relativeToAbsoluteSniffFiles($extraPathsBaseDir, $extraPaths); $fileList = self::$psr1SniffAbsolutePaths; foreach ($extraPaths as $path) { $fileList[] = $path; } self::$ruleset->registerSniffs($fileList, [], []); // Make sure the same sniff list was recreated (but without the objects having been created yet). $this->assertSame(array_keys(self::$originalSniffs), array_keys(self::$ruleset->sniffs)); $this->assertSame(array_keys(self::$originalSniffs), array_values(self::$ruleset->sniffs)); }//end testRegisteredSniffsFiltersOutFilePathsWithoutSniffsDir() }//end class
[+]
..
[-] ProcessRulesetShouldProcessElementTest.xml
[edit]
[-] ProcessRulesetAutoExpandSniffsDirectoryTest.xml
[edit]
[-] ProcessRulesetExcludeSniffGroupTest.xml
[edit]
[-] PropertyTypeHandlingTest.xml
[edit]
[-] ExplainCustomRulesetTest.xml
[edit]
[-] ShowSniffDeprecationsInvalidDeprecationVersionTest.xml
[edit]
[-] RegisterSniffsMissingInterfaceTest.php
[edit]
[-] ExpandRulesetReferenceCaseMismatch2Test.xml
[edit]
[-] PopulateTokenListenersTest.php
[edit]
[-] ExpandSniffDirectoryTest.xml
[edit]
[-] ExpandRulesetReferenceInvalidErrorCode1Test.xml
[edit]
[-] SetPropertyDoesNotThrowErrorOnInvalidPropertyWhenSetForStandardTest.xml
[edit]
[-] ExpandRulesetReferenceTest.php
[edit]
[-] RegisterSniffsMissingInterfaceValidTest.xml
[edit]
[-] ExpandRulesetReferenceHomePathTest.php
[edit]
[-] ExpandRulesetReferenceUnknownCategoryTest.xml
[edit]
[-] ExpandSniffDirectoryTest.php
[edit]
[-] RuleInclusionAbsoluteWindowsTest.php
[edit]
[-] AbstractRulesetTestCase.php
[edit]
[-] ProcessRulesetTest.php
[edit]
[-] ShowSniffDeprecationsEmptyRemovalVersionTest.xml
[edit]
[-] GetIncludePatternsTest.xml
[edit]
[-] RegisterSniffsRejectsInvalidSniffNoImplementsNoRegisterOrProcessTest.xml
[edit]
[-] RegisterSniffsTest.php
[edit]
[+]
Fixtures
[-] SetPropertyAppliesPropertyToMultipleSniffsInCategoryTest.xml
[edit]
[-] ShowSniffDeprecationsTest.xml
[edit]
[-] ExpandRulesetReferenceMissingFileTest.xml
[edit]
[-] ProcessRulesetBrokenRulesetEmptyFileTest.xml
[edit]
[-] ExpandRulesetReferenceInternalIgnoreTest.xml
[edit]
[-] ExpandRulesetReferenceInternalTest.php
[edit]
[-] ProcessRulesetMiscTest.xml
[edit]
[-] SetPropertyAllowedViaStdClassTest.xml
[edit]
[-] PopulateTokenListenersRegisterNoArrayTest.xml
[edit]
[-] RegisterSniffsRejectsInvalidSniffNoImplementsNoProcessTest.xml
[edit]
[-] ExpandRulesetReferenceUnknownSniffTest.xml
[edit]
[-] ProcessRulesetBrokenRulesetSingleErrorTest.xml
[edit]
[-] ExpandRulesetReferenceCaseMismatch1Test.xml
[edit]
[-] SetPropertyNotAllowedViaAttributeTest.xml
[edit]
[-] GetIgnorePatternsTest.xml
[edit]
[-] RuleInclusionAbsoluteLinuxTest.xml
[edit]
[-] ShowSniffDeprecationsEmptyDeprecationVersionTest.xml
[edit]
[-] PopulateTokenListenersSupportedTokenizersTest.php
[edit]
[-] ShowSniffDeprecationsInvalidRemovalVersionTest.xml
[edit]
[-] ShowSniffDeprecationsTest.php
[edit]
[-] ProcessRuleInvalidTypeTest.php
[edit]
[-] RegisterSniffsRejectsInvalidSniffTest.php
[edit]
[-] SetPropertyAllowedAsDeclaredTest.xml
[edit]
[-] ShowSniffDeprecationsReportWidthTest.xml
[edit]
[-] ProcessRuleInvalidTypeTest.xml
[edit]
[-] ExpandRulesetReferenceInternalStandardTest.xml
[edit]
[-] ExpandRulesetReferenceInvalidErrorCode2Test.xml
[edit]
[-] PopulateTokenListenersNamingConventionsTest.xml
[edit]
[-] SetSniffPropertyTest.php
[edit]
[-] ProcessRuleShouldProcessElementTest.xml
[edit]
[-] RuleInclusionTest-include.xml
[edit]
[-] ShowSniffDeprecationsInvalidDeprecationMessageTest.xml
[edit]
[-] PopulateTokenListenersSupportedTokenizersTest.xml
[edit]
[-] RuleInclusionTest.xml
[edit]
[-] ProcessRulesetAutoloadFileNotFoundTest.xml
[edit]
[-] RuleInclusionTest.php
[edit]
[-] PopulateTokenListenersNamingConventionsTest.php
[edit]
[-] ExplainSingleSniffTest.xml
[edit]
[-] GetIncludePatternsTest.php
[edit]
[-] GetIgnorePatternsTest.php
[edit]
[-] RegisterSniffsRejectsInvalidSniffNoImplementsNoRegisterTest.xml
[edit]
[-] ExpandRulesetReferenceHomePathFailTest.xml
[edit]
[-] ProcessRuleShouldProcessElementTest.php
[edit]
[-] ExpandRulesetReferenceUnknownStandardTest.xml
[edit]
[-] ExplainTest.php
[edit]
[-] PopulateTokenListenersTest.xml
[edit]
[-] ExpandRulesetReferenceHomePathTest.xml
[edit]
[-] ConstructorTest.php
[edit]
[-] PropertyTypeHandlingTest.php
[edit]
[-] ProcessRulesetAutoloadTest.php
[edit]
[-] PropertyTypeHandlingInlineTest.xml
[edit]
[-] SetPropertyDoesNotThrowErrorOnInvalidPropertyWhenSetForCategoryTest.xml
[edit]
[-] RegisterSniffsMissingInterfaceInvalidTest.xml
[edit]
[-] ProcessRulesetAutoloadTest.xml
[edit]
[-] SetPropertyAllowedViaMagicMethodTest.xml
[edit]
[-] ProcessRulesetInvalidNoSniffsDirTest.xml
[edit]
[-] ShowSniffDeprecationsOrderTest.xml
[edit]
[-] ExpandRulesetReferenceInvalidErrorCode3Test.xml
[edit]
[-] RuleInclusionAbsoluteLinuxTest.php
[edit]
[-] SetPropertyThrowsErrorOnInvalidPropertyTest.xml
[edit]
[-] ProcessRulesetBrokenRulesetTest.php
[edit]
[-] ConstructorNoSniffsTest.xml
[edit]
[-] ExpandRulesetReferenceInvalidHomePathRefTest.xml
[edit]
[-] ExpandRulesetReferenceTest.xml
[edit]
[-] ProcessRulesetBrokenRulesetMultiErrorTest.xml
[edit]
[-] RuleInclusionAbsoluteWindowsTest.xml
[edit]
[-] ProcessRulesetShouldProcessElementTest.php
[edit]
[-] DisplayCachedMessagesTest.php
[edit]