Skip to content

Commit c942e6a

Browse files
authored
Ruleset::processRuleset(): add tests for handling <autoload> directives (#715)
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 585602f commit c942e6a

9 files changed

+257
-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+
<?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: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
<?php
2+
/**
3+
* Test handling of <autoload> instructions.
4+
*
5+
* @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl>
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.1.php'),
56+
$diff,
57+
'ProcessRulesetAutoloadLoadAlways.1.php autoload file was not loaded'
58+
);
59+
$this->assertContains(
60+
__DIR__.str_replace('/', DIRECTORY_SEPARATOR, '/Fixtures/ProcessRulesetAutoloadLoadAlways.2.php'),
61+
$diff,
62+
'ProcessRulesetAutoloadLoadAlways.2.php autoload file was not loaded'
63+
);
64+
$this->assertContains(
65+
__DIR__.str_replace('/', DIRECTORY_SEPARATOR, '/Fixtures/ProcessRulesetAutoloadLoadAlways.3.php'),
66+
$diff,
67+
'ProcessRulesetAutoloadLoadAlways.3.php autoload file was not loaded'
68+
);
69+
$this->assertContains(
70+
__DIR__.str_replace('/', DIRECTORY_SEPARATOR, '/Fixtures/ProcessRulesetAutoloadLoadAlways.4.php'),
71+
$diff,
72+
'ProcessRulesetAutoloadLoadAlways.4.php autoload file was not loaded'
73+
);
74+
$this->assertContains(
75+
__DIR__.str_replace('/', DIRECTORY_SEPARATOR, '/Fixtures/ProcessRulesetAutoloadLoadPhpcsOnly.php'),
76+
$diff,
77+
'ProcessRulesetAutoloadLoadPhpcsOnly.php autoload file was not loaded'
78+
);
79+
$this->assertNotContains(
80+
__DIR__.str_replace('/', DIRECTORY_SEPARATOR, '/Fixtures/ProcessRulesetAutoloadLoadPhpcbfOnly.php'),
81+
$diff,
82+
'ProcessRulesetAutoloadLoadPhpcbfOnly.php autoload file was loaded, while it shouldn\'t have been'
83+
);
84+
85+
}//end testShouldProcessAutoloadCsonly()
86+
87+
88+
/**
89+
* Verify that in CBF mode, phpcbf-only <autoload> directives are respected and phpcs-only <autoload>
90+
* directives are ignored.
91+
*
92+
* @group CBF
93+
*
94+
* @return void
95+
*/
96+
public function testShouldProcessAutoloadCbfonly()
97+
{
98+
if (PHP_CODESNIFFER_CBF === false) {
99+
$this->markTestSkipped('This test needs CBF mode to run');
100+
}
101+
102+
$originallyIncludes = get_included_files();
103+
104+
// Set up the ruleset.
105+
$standard = __DIR__.'/ProcessRulesetAutoloadTest.xml';
106+
$config = new ConfigDouble(["--standard=$standard"]);
107+
new Ruleset($config);
108+
109+
$finalIncludes = get_included_files();
110+
$diff = array_diff($finalIncludes, $originallyIncludes);
111+
112+
$this->assertContains(
113+
__DIR__.str_replace('/', DIRECTORY_SEPARATOR, '/Fixtures/ProcessRulesetAutoloadLoadAlways.1.php'),
114+
$diff,
115+
'ProcessRulesetAutoloadLoadAlways.1.php autoload file was not loaded'
116+
);
117+
$this->assertContains(
118+
__DIR__.str_replace('/', DIRECTORY_SEPARATOR, '/Fixtures/ProcessRulesetAutoloadLoadAlways.2.php'),
119+
$diff,
120+
'ProcessRulesetAutoloadLoadAlways.2.php autoload file was not loaded'
121+
);
122+
$this->assertContains(
123+
__DIR__.str_replace('/', DIRECTORY_SEPARATOR, '/Fixtures/ProcessRulesetAutoloadLoadAlways.3.php'),
124+
$diff,
125+
'ProcessRulesetAutoloadLoadAlways.3.php autoload file was not loaded'
126+
);
127+
$this->assertContains(
128+
__DIR__.str_replace('/', DIRECTORY_SEPARATOR, '/Fixtures/ProcessRulesetAutoloadLoadAlways.4.php'),
129+
$diff,
130+
'ProcessRulesetAutoloadLoadAlways.4.php autoload file was not loaded'
131+
);
132+
$this->assertNotContains(
133+
__DIR__.str_replace('/', DIRECTORY_SEPARATOR, '/Fixtures/ProcessRulesetAutoloadLoadPhpcsOnly.php'),
134+
$diff,
135+
'ProcessRulesetAutoloadLoadPhpcsOnly.php autoload file was loaded, while it shouldn\'t have been'
136+
);
137+
$this->assertContains(
138+
__DIR__.str_replace('/', DIRECTORY_SEPARATOR, '/Fixtures/ProcessRulesetAutoloadLoadPhpcbfOnly.php'),
139+
$diff,
140+
'ProcessRulesetAutoloadLoadPhpcbfOnly.php autoload file was not loaded'
141+
);
142+
143+
}//end testShouldProcessAutoloadCbfonly()
144+
145+
146+
/**
147+
* Test an exception is thrown when the <autoload> directive points to a file which doesn't exist.
148+
*
149+
* @return void
150+
*/
151+
public function testFileNotFoundException()
152+
{
153+
$exceptionMsg = 'The specified autoload file "./tests/Core/Ruleset/Fixtures/ThisFileDoesNotExist.php" does not exist';
154+
$this->expectRuntimeExceptionMessage($exceptionMsg);
155+
156+
// Set up the ruleset.
157+
$standard = __DIR__.'/ProcessRulesetAutoloadFileNotFoundTest.xml';
158+
$config = new ConfigDouble(["--standard=$standard"]);
159+
new Ruleset($config);
160+
161+
}//end testFileNotFoundException()
162+
163+
164+
}//end class
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
# Testing various ways paths can be provided. #
10+
###############################################
11+
-->
12+
<!-- Path relative to directory containing the ruleset. -->
13+
<autoload>./Fixtures/ProcessRulesetAutoloadLoadAlways.1.php</autoload>
14+
<autoload>Fixtures/ProcessRulesetAutoloadLoadAlways.2.php</autoload>
15+
16+
<!-- Path relative to command directory. -->
17+
<autoload>./tests/Core/Ruleset/Fixtures/ProcessRulesetAutoloadLoadAlways.3.php</autoload>
18+
<autoload>tests/Core/Ruleset/Fixtures/ProcessRulesetAutoloadLoadAlways.4.php</autoload>
19+
20+
<!--
21+
###################
22+
# phpcs-only set. #
23+
###################
24+
-->
25+
<autoload phpcs-only="true">./tests/Core/Ruleset/Fixtures/ProcessRulesetAutoloadLoadPhpcsOnly.php</autoload>
26+
27+
<!--
28+
####################
29+
# phpcbf-only set. #
30+
####################
31+
-->
32+
<autoload phpcbf-only="true">./tests/Core/Ruleset/Fixtures/ProcessRulesetAutoloadLoadPhpcbfOnly.php</autoload>
33+
34+
35+
<!-- Prevent a "no sniff were registered" error. -->
36+
<rule ref="Generic.PHP.BacktickOperator"/>
37+
</ruleset>

0 commit comments

Comments
 (0)