Skip to content

Commit d473ebd

Browse files
committed
Ruleset::processRuleset(): add tests for handling <autoload> directives
Add tests specifically aimed at verifying `<autoload>` elements are read and handled correctly. Note: as `autoload` directive are not stored in the ruleset, but directly lead to a file include using `include_once`, these tests need to run in a separate process to circumvent the possibility of test cross-contamination.
1 parent fa96dd3 commit d473ebd

6 files changed

+196
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
/**
3+
* Test fixture.
4+
*
5+
* This file is only used to check whether it has been `include`d.
6+
*
7+
* @see \PHP_CodeSniffer\Tests\Core\Ruleset\ProcessRulesetAutoloadTest
8+
*/
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
/**
3+
* Test fixture.
4+
*
5+
* This file is only used to check whether it has been `include`d.
6+
*
7+
* @see \PHP_CodeSniffer\Tests\Core\Ruleset\ProcessRulesetAutoloadTest
8+
*/
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
/**
3+
* Test fixture.
4+
*
5+
* This file is only used to check whether it has been `include`d.
6+
*
7+
* @see \PHP_CodeSniffer\Tests\Core\Ruleset\ProcessRulesetAutoloadTest
8+
*/
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="ProcessRulesetAutoloadTest" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/PHPCSStandards/PHP_CodeSniffer/master/phpcs.xsd">
3+
4+
<autoload>./tests/Core/Ruleset/Fixtures/ThisFileDoesNotExist.php</autoload>
5+
6+
<!-- Prevent a "no sniff were registered" error. -->
7+
<rule ref="Generic.PHP.BacktickOperator"/>
8+
</ruleset>
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
<?php
2+
/**
3+
* Test handling of <autoload> instructions.
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 handling of <autoload> instructions.
18+
*
19+
* Note: these tests need to run in separate processes as otherwise we cannot
20+
* reliably determine whether or not the correct files were loaded as the
21+
* underlying code uses `include_once`.
22+
*
23+
* @runTestsInSeparateProcesses
24+
* @preserveGlobalState disabled
25+
*
26+
* @covers \PHP_CodeSniffer\Ruleset::processRuleset
27+
*/
28+
final class ProcessRulesetAutoloadTest extends AbstractRulesetTestCase
29+
{
30+
31+
32+
/**
33+
* Verify that in CS mode, phpcs-only <autoload> directives are respected and phpcbf-only <autoload>
34+
* directives are ignored.
35+
*
36+
* @return void
37+
*/
38+
public function testShouldProcessAutoloadCsonly()
39+
{
40+
if (PHP_CODESNIFFER_CBF === true) {
41+
$this->markTestSkipped('This test needs CS mode to run');
42+
}
43+
44+
$originallyIncludes = get_included_files();
45+
46+
// Set up the ruleset.
47+
$standard = __DIR__.'/ProcessRulesetAutoloadTest.xml';
48+
$config = new ConfigDouble(["--standard=$standard"]);
49+
new Ruleset($config);
50+
51+
$finalIncludes = get_included_files();
52+
$diff = array_diff($finalIncludes, $originallyIncludes);
53+
54+
$this->assertContains(
55+
__DIR__.str_replace('/', DIRECTORY_SEPARATOR, '/Fixtures/ProcessRulesetAutoloadLoadAlways.php'),
56+
$diff,
57+
'ProcessRulesetAutoloadLoadAlways.php autoload file was not loaded'
58+
);
59+
$this->assertContains(
60+
__DIR__.str_replace('/', DIRECTORY_SEPARATOR, '/Fixtures/ProcessRulesetAutoloadLoadPhpcsOnly.php'),
61+
$diff,
62+
'ProcessRulesetAutoloadLoadPhpcsOnly.php autoload file was not loaded'
63+
);
64+
$this->assertNotContains(
65+
__DIR__.str_replace('/', DIRECTORY_SEPARATOR, '/Fixtures/ProcessRulesetAutoloadLoadPhpcbfOnly.php'),
66+
$diff,
67+
'ProcessRulesetAutoloadLoadPhpcbfOnly.php autoload file was loaded, while it shouldn\'t have been'
68+
);
69+
70+
}//end testShouldProcessAutoloadCsonly()
71+
72+
73+
/**
74+
* Verify that in CBF mode, phpcbf-only <autoload> directives are respected and phpcs-only <autoload>
75+
* directives are ignored.
76+
*
77+
* @group CBF
78+
*
79+
* @return void
80+
*/
81+
public function testShouldProcessAutoloadCbfonly()
82+
{
83+
if (PHP_CODESNIFFER_CBF === false) {
84+
$this->markTestSkipped('This test needs CBF mode to run');
85+
}
86+
87+
$originallyIncludes = get_included_files();
88+
89+
// Set up the ruleset.
90+
$standard = __DIR__.'/ProcessRulesetAutoloadTest.xml';
91+
$config = new ConfigDouble(["--standard=$standard"]);
92+
new Ruleset($config);
93+
94+
$finalIncludes = get_included_files();
95+
$diff = array_diff($finalIncludes, $originallyIncludes);
96+
97+
$this->assertContains(
98+
__DIR__.str_replace('/', DIRECTORY_SEPARATOR, '/Fixtures/ProcessRulesetAutoloadLoadAlways.php'),
99+
$diff,
100+
'ProcessRulesetAutoloadLoadAlways.php autoload file was not loaded'
101+
);
102+
$this->assertNotContains(
103+
__DIR__.str_replace('/', DIRECTORY_SEPARATOR, '/Fixtures/ProcessRulesetAutoloadLoadPhpcsOnly.php'),
104+
$diff,
105+
'ProcessRulesetAutoloadLoadPhpcsOnly.php autoload file was loaded, while it shouldn\'t have been'
106+
);
107+
$this->assertContains(
108+
__DIR__.str_replace('/', DIRECTORY_SEPARATOR, '/Fixtures/ProcessRulesetAutoloadLoadPhpcbfOnly.php'),
109+
$diff,
110+
'ProcessRulesetAutoloadLoadPhpcbfOnly.php autoload file was not loaded'
111+
);
112+
113+
}//end testShouldProcessAutoloadCbfonly()
114+
115+
116+
/**
117+
* Test an exception is thrown when the <autoload> directive points to a file which doesn't exist.
118+
*
119+
* @return void
120+
*/
121+
public function testFileNotFoundException()
122+
{
123+
$exceptionMsg = 'The specified autoload file "./tests/Core/Ruleset/Fixtures/ThisFileDoesNotExist.php" does not exist';
124+
$this->expectRuntimeExceptionMessage($exceptionMsg);
125+
126+
// Set up the ruleset.
127+
$standard = __DIR__.'/ProcessRulesetAutoloadFileNotFoundTest.xml';
128+
$config = new ConfigDouble(["--standard=$standard"]);
129+
new Ruleset($config);
130+
131+
}//end testFileNotFoundException()
132+
133+
134+
}//end class
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0"?>
2+
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="ProcessRulesetAutoloadTest" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/PHPCSStandards/PHP_CodeSniffer/master/phpcs.xsd">
3+
4+
<file>.</file>
5+
6+
<!--
7+
################
8+
# Neither set. #
9+
################
10+
-->
11+
<autoload>./tests/Core/Ruleset/Fixtures/ProcessRulesetAutoloadLoadAlways.php</autoload>
12+
13+
<!--
14+
###################
15+
# phpcs-only set. #
16+
###################
17+
-->
18+
<autoload phpcs-only="true">./tests/Core/Ruleset/Fixtures/ProcessRulesetAutoloadLoadPhpcsOnly.php</autoload>
19+
20+
<!--
21+
####################
22+
# phpcbf-only set. #
23+
####################
24+
-->
25+
<autoload phpcbf-only="true">./tests/Core/Ruleset/Fixtures/ProcessRulesetAutoloadLoadPhpcbfOnly.php</autoload>
26+
27+
28+
<!-- Prevent a "no sniff were registered" error. -->
29+
<rule ref="Generic.PHP.BacktickOperator"/>
30+
</ruleset>

0 commit comments

Comments
 (0)