Skip to content

Commit ef411ee

Browse files
committed
Ruleset::expandRulesetReference(): add miscellaneous tests
Note: test coverage for the `Ruleset::expandRulesetReference()` method will still not be 100% even after the addition of these tests. * There are some conditions which are specific to running PHPCS via a PHAR file. At this time, the tests are not run with a PHAR file, which means these lines cannot be covered via the tests. * As for the other uncovered lines/branches: my fantasy ran out and I couldn't come up with a situation which would trigger that code. Code archaeology (`git blame`) to find out why the code was introduced unfortunately didn't help. This may indicate the code is redundant and can be removed, but this is not certain at this time and leaving the code in place is not harming anyone for now. If over time, nobody can come up with a real life situation which would allow us to test those lines, it could be considered to remove them in a future major release to see what breaks....
1 parent cef31ad commit ef411ee

16 files changed

+288
-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: 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>
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
<?php
2+
/**
3+
* Test the Ruleset::expandRulesetReference() method.
4+
*
5+
* @author Juliette Reinders Folmer <[email protected]>
6+
* @copyright 2024 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 various aspects of the Ruleset::expandRulesetReference() method not covered by other tests.
18+
*
19+
* @covers \PHP_CodeSniffer\Ruleset::expandRulesetReference
20+
*/
21+
final class ExpandRulesetReferenceTest extends AbstractRulesetTestCase
22+
{
23+
24+
25+
/**
26+
* Test handling of path references relative to the originally included ruleset.
27+
*
28+
* @return void
29+
*/
30+
public function testRulesetRelativePathReferences()
31+
{
32+
// Set up the ruleset.
33+
$standard = __DIR__.'/ExpandRulesetReferenceTest.xml';
34+
$config = new ConfigDouble(["--standard=$standard"]);
35+
$ruleset = new Ruleset($config);
36+
37+
$expected = [
38+
'ExternalA.CheckSomething.Valid' => 'Fixtures\\ExternalA\\Sniffs\\CheckSomething\\ValidSniff',
39+
'TestStandard.ValidSniffs.RegisterEmptyArray' => 'Fixtures\\TestStandard\\Sniffs\\ValidSniffs\\RegisterEmptyArraySniff',
40+
'ExternalB.CheckMore.Valid' => 'Fixtures\\ExternalB\\Sniffs\\CheckMore\\ValidSniff',
41+
];
42+
43+
$this->assertSame($expected, $ruleset->sniffCodes);
44+
45+
}//end testRulesetRelativePathReferences()
46+
47+
48+
/**
49+
* Test that an exception is thrown if a ruleset contains an unresolvable reference.
50+
*
51+
* @param string $standard The standard to use for the test.
52+
* @param string $replacement The reference which will be used in the exception message.
53+
*
54+
* @dataProvider dataUnresolvableReferenceThrowsException
55+
*
56+
* @return void
57+
*/
58+
public function testUnresolvableReferenceThrowsException($standard, $replacement)
59+
{
60+
// Set up the ruleset.
61+
$standard = __DIR__.'/'.$standard;
62+
$config = new ConfigDouble(["--standard=$standard"]);
63+
64+
$exceptionMessage = 'Referenced sniff "%s" does not exist';
65+
$this->expectRuntimeExceptionMessage(sprintf($exceptionMessage, $replacement));
66+
67+
new Ruleset($config);
68+
69+
}//end testUnresolvableReferenceThrowsException()
70+
71+
72+
/**
73+
* Data provider.
74+
*
75+
* @see testUnresolvableReferenceThrowsException()
76+
*
77+
* @return array<string, array<string, string>>
78+
*/
79+
public static function dataUnresolvableReferenceThrowsException()
80+
{
81+
$data = [
82+
'Referencing a non-existent XML file' => [
83+
'standard' => 'ExpandRulesetReferenceMissingFileTest.xml',
84+
'replacement' => './MissingFile.xml',
85+
],
86+
'Referencing an invalid directory starting with "~"' => [
87+
'standard' => 'ExpandRulesetReferenceInvalidHomePathRefTest.xml',
88+
'replacement' => '~/src/Standards/Squiz/Sniffs/Files/',
89+
],
90+
'Referencing an unknown standard' => [
91+
'standard' => 'ExpandRulesetReferenceUnknownStandardTest.xml',
92+
'replacement' => 'UnknownStandard',
93+
],
94+
'Referencing a non-existent category in a known standard' => [
95+
'standard' => 'ExpandRulesetReferenceUnknownCategoryTest.xml',
96+
'replacement' => 'TestStandard.UnknownCategory',
97+
],
98+
'Referencing a non-existent sniff in a known standard' => [
99+
'standard' => 'ExpandRulesetReferenceUnknownSniffTest.xml',
100+
'replacement' => 'TestStandard.InvalidSniffs.UnknownRule',
101+
],
102+
'Referencing an invalid error code - no standard name' => [
103+
'standard' => 'ExpandRulesetReferenceInvalidErrorCode1Test.xml',
104+
'replacement' => '.Invalid.Undetermined.Found',
105+
],
106+
'Referencing an invalid error code - no category name' => [
107+
'standard' => 'ExpandRulesetReferenceInvalidErrorCode2Test.xml',
108+
'replacement' => 'Standard..Undetermined.Found',
109+
],
110+
'Referencing an invalid error code - no sniff name' => [
111+
'standard' => 'ExpandRulesetReferenceInvalidErrorCode3Test.xml',
112+
'replacement' => 'Standard.Invalid..Found',
113+
],
114+
];
115+
116+
// Add tests which are only relevant for case-sensitive OSes.
117+
if (stripos(PHP_OS, 'WIN') === false) {
118+
$data['Referencing an existing sniff, but there is a case mismatch (OS-dependent) [1]'] = [
119+
'standard' => 'ExpandRulesetReferenceCaseMismatch1Test.xml',
120+
'replacement' => 'psr12.functions.nullabletypedeclaration',
121+
];
122+
$data['Referencing an existing sniff, but there is a case mismatch (OS-dependent) [2]'] = [
123+
'standard' => 'ExpandRulesetReferenceCaseMismatch2Test.xml',
124+
'replacement' => 'PSR12.Functions.ReturntypeDeclaration',
125+
];
126+
}
127+
128+
return $data;
129+
130+
}//end dataUnresolvableReferenceThrowsException()
131+
132+
133+
}//end class
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
<!-- Including a single sniff via a ruleset relative file ref. -->
5+
<rule ref="./Fixtures/ExternalA/Sniffs/CheckSomething/ValidSniff.php"/>
6+
7+
<!-- Including a category of sniffs via a ruleset relative directory ref. -->
8+
<rule ref="./Fixtures/TestStandard/Sniffs/ValidSniffs"/>
9+
10+
<!-- Including a complete standard via a ruleset relative directory ref. -->
11+
<rule ref="./Fixtures/ExternalB"/>
12+
13+
</ruleset>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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+
<config name="installed_paths" value="./tests/Core/Ruleset/Fixtures/TestStandard/"/>
5+
6+
<!-- This test deliberately includes a non-existent category in a known standard (which won't resolve).
7+
That is exactly what this test is about. -->
8+
<rule ref="TestStandard.UnknownCategory"/>
9+
10+
</ruleset>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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+
<config name="installed_paths" value="./tests/Core/Ruleset/Fixtures/TestStandard/"/>
5+
6+
<!-- This test deliberately includes a non-existent sniff in a known standard (which won't resolve).
7+
That is exactly what this test is about. -->
8+
<rule ref="TestStandard.InvalidSniffs.UnknownRule"/>
9+
10+
</ruleset>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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+
<config name="installed_paths" value="./tests/Core/Ruleset/Fixtures/TestStandard/"/>
5+
6+
<!-- This test deliberately includes an unknown standard which won't resolve.
7+
That is exactly what this test is about. -->
8+
<rule ref="UnknownStandard"/>
9+
10+
</ruleset>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
/**
3+
* Test fixture.
4+
*
5+
* @see \PHP_CodeSniffer\Tests\Core\Ruleset\ExpandRulesetReferenceTest
6+
*/
7+
8+
namespace Fixtures\ExternalA\Sniffs\CheckSomething;
9+
10+
use PHP_CodeSniffer\Files\File;
11+
use PHP_CodeSniffer\Sniffs\Sniff;
12+
13+
class ValidSniff implements Sniff
14+
{
15+
16+
public function register()
17+
{
18+
return [T_CLASS];
19+
}
20+
21+
public function process(File $phpcsFile, $stackPtr)
22+
{
23+
// Do something.
24+
}
25+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0"?>
2+
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="ExternalA" namespace="Fixtures\ExternalA" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/PHPCSStandards/PHP_CodeSniffer/master/phpcs.xsd">
3+
4+
</ruleset>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
/**
3+
* Test fixture.
4+
*
5+
* @see \PHP_CodeSniffer\Tests\Core\Ruleset\ExpandRulesetReferenceTest
6+
*/
7+
8+
namespace Fixtures\ExternalB\Sniffs\CheckMore;
9+
10+
use PHP_CodeSniffer\Files\File;
11+
use PHP_CodeSniffer\Sniffs\Sniff;
12+
13+
class ValidSniff implements Sniff
14+
{
15+
16+
public function register()
17+
{
18+
return [T_CLASS];
19+
}
20+
21+
public function process(File $phpcsFile, $stackPtr)
22+
{
23+
// Do something.
24+
}
25+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0"?>
2+
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="ExternalB" namespace="Fixtures\ExternalB" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/PHPCSStandards/PHP_CodeSniffer/master/phpcs.xsd">
3+
4+
</ruleset>

0 commit comments

Comments
 (0)