Skip to content

Commit d11ed56

Browse files
authored
Merge pull request #801 from PHPCSStandards/feature/ruleset-expandrulesetreference-add-tests
Ruleset::expandRulesetReference(): add tests
2 parents 589e826 + ef411ee commit d11ed56

26 files changed

+577
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0"?>
2+
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="ExpandRulesetReferenceTest" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/PHPCSStandards/PHP_CodeSniffer/master/phpcs.xsd">
3+
4+
<!-- Error handling: Case mismatch, will only error on case sensitive OSes.
5+
Correct case would be: PSR12.Functions.NullableTypeDeclaration,
6+
i.e. matching the case of the standard, category and sniff class name exactly. -->
7+
<rule ref="psr12.functions.nullabletypedeclaration"/>
8+
9+
</ruleset>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0"?>
2+
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="ExpandRulesetReferenceTest" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/PHPCSStandards/PHP_CodeSniffer/master/phpcs.xsd">
3+
4+
<!-- Error handling: Case mismatch, will only error on case sensitive OSes.
5+
Correct case would be: PSR12.Functions.ReturnTypeDeclaration - note the capital T in the sniff name,
6+
i.e. matching the case of the standard, category and sniff class name exactly. -->
7+
<rule ref="PSR12.Functions.ReturntypeDeclaration"/>
8+
9+
</ruleset>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0"?>
2+
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="ExpandRulesetReferenceHomePathTest" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/PHPCSStandards/PHP_CodeSniffer/master/phpcs.xsd">
3+
4+
<!-- With a mocked "home" path to an existing directory, this reference should still fail
5+
as the underlying subdirectory doesn't exist. -->
6+
<rule ref="~/src/MyStandard/Sniffs/DoesntExist/"/>
7+
8+
</ruleset>
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<?php
2+
/**
3+
* Test the Ruleset::expandRulesetReference() method.
4+
*
5+
* @author Juliette Reinders Folmer <[email protected]>
6+
* @copyright 2025 PHPCSStandards and contributors
7+
* @license https://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 PHP_CodeSniffer\Tests\Core\Ruleset\AbstractRulesetTestCase;
15+
16+
/**
17+
* Test home path handling in the Ruleset::expandRulesetReference() method.
18+
*
19+
* @covers \PHP_CodeSniffer\Ruleset::expandRulesetReference
20+
*/
21+
final class ExpandRulesetReferenceHomePathTest extends AbstractRulesetTestCase
22+
{
23+
24+
/**
25+
* Original value of the user's home path environment variable.
26+
*
27+
* @var string|false Path or false is the `HOME` environment variable is not available.
28+
*/
29+
private static $homepath = false;
30+
31+
32+
/**
33+
* Store the user's home path.
34+
*
35+
* @beforeClass
36+
*
37+
* @return void
38+
*/
39+
protected function storeHomePath()
40+
{
41+
$this->homepath = getenv('HOME');
42+
43+
}//end storeHomePath()
44+
45+
46+
/**
47+
* Restore the user's home path environment variable in case the test changed it or created it.
48+
*
49+
* @afterClass
50+
*
51+
* @return void
52+
*/
53+
protected function restoreHomePath()
54+
{
55+
if (is_string($this->homepath) === true) {
56+
putenv('HOME='.$this->homepath);
57+
} else {
58+
// Remove the environment variable as it didn't exist before.
59+
putenv('HOME');
60+
}
61+
62+
}//end restoreHomePath()
63+
64+
65+
/**
66+
* Set the home path to an alternative location.
67+
*
68+
* @before
69+
*
70+
* @return void
71+
*/
72+
protected function setHomePath()
73+
{
74+
$fakeHomePath = __DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'FakeHomePath';
75+
putenv("HOME=$fakeHomePath");
76+
77+
}//end setHomePath()
78+
79+
80+
/**
81+
* Verify that a sniff reference with the magic "home path" placeholder gets expanded correctly
82+
* and finds sniffs if the path exists underneath the "home path".
83+
*
84+
* @return void
85+
*/
86+
public function testHomePathRefGetsExpandedAndFindsSniff()
87+
{
88+
// Set up the ruleset.
89+
$standard = __DIR__.'/ExpandRulesetReferenceHomePathTest.xml';
90+
$config = new ConfigDouble(["--standard=$standard"]);
91+
$ruleset = new Ruleset($config);
92+
93+
$expected = ['MyStandard.Category.Valid' => 'FakeHomePath\\MyStandard\\Sniffs\\Category\\ValidSniff'];
94+
95+
$this->assertSame($expected, $ruleset->sniffCodes);
96+
97+
}//end testHomePathRefGetsExpandedAndFindsSniff()
98+
99+
100+
/**
101+
* Verify that a sniff reference with the magic "home path" placeholder gets expanded correctly
102+
* and still fails to find sniffs if the path doesn't exists underneath the "home path".
103+
*
104+
* @return void
105+
*/
106+
public function testHomePathRefGetsExpandedAndThrowsExceptionWhenPathIsInvalid()
107+
{
108+
// Set up the ruleset.
109+
$standard = __DIR__.'/ExpandRulesetReferenceHomePathFailTest.xml';
110+
$config = new ConfigDouble(["--standard=$standard"]);
111+
112+
$exceptionMessage = 'Referenced sniff "~/src/MyStandard/Sniffs/DoesntExist/" does not exist';
113+
$this->expectRuntimeExceptionMessage($exceptionMessage);
114+
115+
new Ruleset($config);
116+
117+
}//end testHomePathRefGetsExpandedAndThrowsExceptionWhenPathIsInvalid()
118+
119+
120+
}//end class
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0"?>
2+
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="ExpandRulesetReferenceHomePathTest" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/PHPCSStandards/PHP_CodeSniffer/master/phpcs.xsd">
3+
4+
<!-- With a mocked "home" path, this reference should work. -->
5+
<rule ref="~/src/MyStandard/Sniffs/Category/"/>
6+
7+
</ruleset>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0"?>
2+
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="ExpandRulesetReferenceInternalTest" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/PHPCSStandards/PHP_CodeSniffer/master/phpcs.xsd">
3+
4+
<config name="installed_paths" value="./tests/Core/Ruleset/Fixtures/Internal/"/>
5+
6+
<rule ref="Internal.NoCodeFound">
7+
<severity>0</severity>
8+
</rule>
9+
10+
<!-- While this references a valid sniff category, it will be ignored anyway as the ref starts with "Internal". -->
11+
<rule ref="Internal.Valid"/>
12+
13+
<!-- Prevent a "no sniff were registered" error. -->
14+
<rule ref="Generic.PHP.BacktickOperator"/>
15+
</ruleset>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0"?>
2+
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="ExpandRulesetReferenceInternalTest" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/PHPCSStandards/PHP_CodeSniffer/master/phpcs.xsd">
3+
4+
<config name="installed_paths" value="./tests/Core/Ruleset/Fixtures/Internal/"/>
5+
6+
<rule ref="Internal"/>
7+
8+
</ruleset>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
/**
3+
* Test the Ruleset::expandRulesetReference() method.
4+
*
5+
* @author Juliette Reinders Folmer <[email protected]>
6+
* @copyright 2025 PHPCSStandards and contributors
7+
* @license https://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 PHP_CodeSniffer\Tests\Core\Ruleset\AbstractRulesetTestCase;
15+
16+
/**
17+
* Test handling of "internal" references in the Ruleset::expandRulesetReference() method.
18+
*
19+
* @covers \PHP_CodeSniffer\Ruleset::expandRulesetReference
20+
*/
21+
final class ExpandRulesetReferenceInternalTest extends AbstractRulesetTestCase
22+
{
23+
24+
25+
/**
26+
* Verify that a ruleset reference starting with "Internal." (including the dot) doesn't cause any sniffs to be registered.
27+
*
28+
* @return void
29+
*/
30+
public function testInternalRefDoesNotGetExpanded()
31+
{
32+
// Set up the ruleset.
33+
$standard = __DIR__.'/ExpandRulesetReferenceInternalIgnoreTest.xml';
34+
$config = new ConfigDouble(["--standard=$standard"]);
35+
$ruleset = new Ruleset($config);
36+
37+
$expected = ['Generic.PHP.BacktickOperator' => 'PHP_CodeSniffer\\Standards\\Generic\\Sniffs\\PHP\\BacktickOperatorSniff'];
38+
39+
$this->assertSame($expected, $ruleset->sniffCodes);
40+
41+
}//end testInternalRefDoesNotGetExpanded()
42+
43+
44+
/**
45+
* While definitely not recommended, including a standard named "Internal", _does_ allow for sniffs to be registered.
46+
*
47+
* Note: customizations (exclusions/property setting etc) for individual sniffs may not always be handled correctly,
48+
* which is why naming a standard "Internal" is definitely not recommended.
49+
*
50+
* @return void
51+
*/
52+
public function testInternalStandardDoesGetExpanded()
53+
{
54+
// Set up the ruleset.
55+
$standard = __DIR__.'/ExpandRulesetReferenceInternalStandardTest.xml';
56+
$config = new ConfigDouble(["--standard=$standard"]);
57+
$ruleset = new Ruleset($config);
58+
59+
$expected = ['Internal.Valid.Valid' => 'Fixtures\\Internal\\Sniffs\\Valid\\ValidSniff'];
60+
61+
$this->assertSame($expected, $ruleset->sniffCodes);
62+
63+
}//end testInternalStandardDoesGetExpanded()
64+
65+
66+
}//end class
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0"?>
2+
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="ExpandRulesetReferenceTest" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/PHPCSStandards/PHP_CodeSniffer/master/phpcs.xsd">
3+
4+
<!-- Error handling: Invalid error code ref - missing standard name. -->
5+
<rule ref=".Invalid.Undetermined.Found"/>
6+
7+
</ruleset>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0"?>
2+
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="ExpandRulesetReferenceTest" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/PHPCSStandards/PHP_CodeSniffer/master/phpcs.xsd">
3+
4+
<!-- Error handling: Invalid error code ref - missing category name. -->
5+
<rule ref="Standard..Undetermined.Found"/>
6+
7+
</ruleset>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0"?>
2+
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="ExpandRulesetReferenceTest" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/PHPCSStandards/PHP_CodeSniffer/master/phpcs.xsd">
3+
4+
<!-- Error handling: Invalid error code ref - missing sniff name. -->
5+
<rule ref="Standard.Invalid..Found"/>
6+
7+
</ruleset>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0"?>
2+
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="ExpandRulesetReferenceTest" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/PHPCSStandards/PHP_CodeSniffer/master/phpcs.xsd">
3+
4+
<!-- This "home path" reference is not going to work, but that's not the point of this test.
5+
The point is for the code to, at least, _try_ to resolve it. -->
6+
<rule ref="~/src/Standards/Squiz/Sniffs/Files/"/>
7+
8+
</ruleset>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0"?>
2+
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="ExpandRulesetReferenceTest" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/PHPCSStandards/PHP_CodeSniffer/master/phpcs.xsd">
3+
4+
<!-- This test deliberately includes an XML file which doesn't exist. That is exactly what this test is about. -->
5+
<rule ref="./MissingFile.xml"/>
6+
7+
</ruleset>

0 commit comments

Comments
 (0)