* @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 various aspects of the Ruleset::processRuleset() method not covered via other tests.
*
* @covers \PHP_CodeSniffer\Ruleset::processRuleset
*/
final class ProcessRulesetTest extends TestCase
{
/**
* Verify that a registered standard which doesn't have a "Sniffs" directory, but does have a file
* called "Sniffs" doesn't result in any errors being thrown.
*
* @return void
*/
public function testSniffsFileNotDirectory()
{
// Set up the ruleset.
$standard = __DIR__.'/ProcessRulesetInvalidNoSniffsDirTest.xml';
$config = new ConfigDouble(["--standard=$standard"]);
$ruleset = new Ruleset($config);
$expected = ['Generic.PHP.BacktickOperator' => 'PHP_CodeSniffer\Standards\Generic\Sniffs\PHP\BacktickOperatorSniff'];
$this->assertSame($expected, $ruleset->sniffCodes);
}//end testSniffsFileNotDirectory()
/**
* Verify that all sniffs in a registered standard included in a ruleset automatically get added.
*
* @return void
*/
public function testAutoExpandSniffsDirectory()
{
// Set up the ruleset.
$standard = __DIR__.'/ProcessRulesetAutoExpandSniffsDirectoryTest.xml';
$config = new ConfigDouble(["--standard=$standard"]);
$ruleset = new Ruleset($config);
$std = 'TestStandard';
$sniffDir = 'Fixtures\TestStandard\Sniffs';
$expected = [
"$std.Deprecated.WithLongReplacement" => "$sniffDir\Deprecated\WithLongReplacementSniff",
"$std.Deprecated.WithReplacement" => "$sniffDir\Deprecated\WithReplacementSniff",
"$std.Deprecated.WithReplacementContainingLinuxNewlines" => "$sniffDir\Deprecated\WithReplacementContainingLinuxNewlinesSniff",
"$std.Deprecated.WithReplacementContainingNewlines" => "$sniffDir\Deprecated\WithReplacementContainingNewlinesSniff",
"$std.Deprecated.WithoutReplacement" => "$sniffDir\Deprecated\WithoutReplacementSniff",
"$std.DeprecatedInvalid.EmptyDeprecationVersion" => "$sniffDir\DeprecatedInvalid\EmptyDeprecationVersionSniff",
"$std.DeprecatedInvalid.EmptyRemovalVersion" => "$sniffDir\DeprecatedInvalid\EmptyRemovalVersionSniff",
"$std.DeprecatedInvalid.InvalidDeprecationMessage" => "$sniffDir\DeprecatedInvalid\InvalidDeprecationMessageSniff",
"$std.DeprecatedInvalid.InvalidDeprecationVersion" => "$sniffDir\DeprecatedInvalid\InvalidDeprecationVersionSniff",
"$std.DeprecatedInvalid.InvalidRemovalVersion" => "$sniffDir\DeprecatedInvalid\InvalidRemovalVersionSniff",
"$std.MissingInterface.ValidImplements" => "$sniffDir\MissingInterface\ValidImplementsSniff",
"$std.MissingInterface.ValidImplementsViaAbstract" => "$sniffDir\MissingInterface\ValidImplementsViaAbstractSniff",
"$std.SetProperty.AllowedAsDeclared" => "$sniffDir\SetProperty\AllowedAsDeclaredSniff",
"$std.SetProperty.AllowedViaMagicMethod" => "$sniffDir\SetProperty\AllowedViaMagicMethodSniff",
"$std.SetProperty.AllowedViaStdClass" => "$sniffDir\SetProperty\AllowedViaStdClassSniff",
"$std.SetProperty.NotAllowedViaAttribute" => "$sniffDir\SetProperty\NotAllowedViaAttributeSniff",
"$std.SetProperty.PropertyTypeHandling" => "$sniffDir\SetProperty\PropertyTypeHandlingSniff",
"$std.ValidSniffs.RegisterEmptyArray" => "$sniffDir\ValidSniffs\RegisterEmptyArraySniff",
];
// 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.
$actual = $ruleset->sniffCodes;
ksort($actual);
$this->assertSame($expected, $actual);
}//end testAutoExpandSniffsDirectory()
/**
* Verify handling of exclusions of groups of sniffs after inclusion via an even larger "group".
*
* @return void
*/
public function testExcludeSniffGroup()
{
// Set up the ruleset.
$standard = __DIR__.'/ProcessRulesetExcludeSniffGroupTest.xml';
$config = new ConfigDouble(["--standard=$standard"]);
$ruleset = new Ruleset($config);
$expected = [
'PSR1.Classes.ClassDeclaration' => 'PHP_CodeSniffer\Standards\PSR1\Sniffs\Classes\ClassDeclarationSniff',
'PSR1.Methods.CamelCapsMethodName' => 'PHP_CodeSniffer\Standards\PSR1\Sniffs\Methods\CamelCapsMethodNameSniff',
];
// 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.
$actual = $ruleset->sniffCodes;
ksort($actual);
$this->assertSame($expected, $actual);
}//end testExcludeSniffGroup()
/*
* No test for