@@ -26,18 +26,43 @@ abstract class AbstractSniffUnitTest extends TestCase
2626{
2727
2828 /**
29- * Cache for the Config object .
29+ * Ruleset template with placeholders .
3030 *
31- * @var \PHP_CodeSniffer\Tests\ConfigDouble
31+ * @var string
3232 */
33- private static $ config ;
33+ private const RULESET_TEMPLATE = <<<'TEMPLATE'
34+ <?xml version="1.0"?>
35+ <ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="[STANDARDNAME]" xsi:noNamespaceSchemaLocation="../../phpcs.xsd">
36+ <description>Temporary ruleset used by the AbstractSniffUnitTest class.</description>
37+
38+ <rule ref="[SNIFFFILEREF]"/>
39+
40+ </ruleset>
41+ TEMPLATE;
42+
43+ /**
44+ * Placeholders used in the ruleset template which need to be replaced.
45+ *
46+ * @var array<string>
47+ */
48+ private const SEARCH_FOR = [
49+ '[STANDARDNAME] ' ,
50+ '[SNIFFFILEREF] ' ,
51+ ];
52+
53+ /**
54+ * Location where the temporary ruleset file will be saved.
55+ *
56+ * @var string
57+ */
58+ private const RULESET_FILENAME = __DIR__ .'/sniffStnd.xml ' ;
3459
3560 /**
36- * Cache for Ruleset objects .
61+ * Cache for the Config object .
3762 *
38- * @var array<string, \PHP_CodeSniffer\Ruleset>
63+ * @var \PHP_CodeSniffer\Tests\ConfigDouble
3964 */
40- private static $ rulesets = [] ;
65+ private static $ config ;
4166
4267 /**
4368 * Extensions to disregard when gathering the test files.
@@ -52,6 +77,18 @@ abstract class AbstractSniffUnitTest extends TestCase
5277 ];
5378
5479
80+ /**
81+ * Clean up temporary ruleset file.
82+ *
83+ * @return void
84+ */
85+ public static function tearDownAfterClass (): void
86+ {
87+ @unlink (self ::RULESET_FILENAME );
88+
89+ }//end tearDownAfterClass()
90+
91+
5592 /**
5693 * Get a list of all test files to check.
5794 *
@@ -103,7 +140,8 @@ protected function shouldSkipTest()
103140 * Tests the extending classes Sniff class.
104141 *
105142 * @return void
106- * @throws \PHPUnit\Framework\Exception
143+ *
144+ * @throws \PHP_CodeSniffer\Exceptions\RuntimeException
107145 */
108146 final public function testSniff ()
109147 {
@@ -122,6 +160,23 @@ final public function testSniff()
122160 // Get a list of all test files to check.
123161 $ testFiles = $ this ->getTestFiles ($ testFileBase );
124162
163+ $ sniffFile = preg_replace ('`[/ \\\\]Tests[/ \\\\]` ' , DIRECTORY_SEPARATOR .'Sniffs ' .DIRECTORY_SEPARATOR , $ testFileBase );
164+ $ sniffFile = str_replace ('UnitTest. ' , 'Sniff.php ' , $ sniffFile );
165+
166+ if (file_exists ($ sniffFile ) === false ) {
167+ $ this ->fail (sprintf ('ERROR: Sniff file %s for test %s does not appear to exist ' , $ sniffFile , static ::class));
168+ }
169+
170+ $ replacements = [
171+ $ standardName ,
172+ $ sniffFile ,
173+ ];
174+ $ rulesetContents = str_replace (self ::SEARCH_FOR , $ replacements , self ::RULESET_TEMPLATE );
175+
176+ if (file_put_contents (self ::RULESET_FILENAME , $ rulesetContents ) === false ) {
177+ throw new RuntimeException ('Failed to write custom ruleset file ' );
178+ }
179+
125180 if (isset (self ::$ config ) === true ) {
126181 $ config = self ::$ config ;
127182 } else {
@@ -130,31 +185,11 @@ final public function testSniff()
130185 self ::$ config = $ config ;
131186 }
132187
133- $ config ->standards = [$ standardName ];
188+ $ config ->standards = [self :: RULESET_FILENAME ];
134189 $ config ->sniffs = [$ sniffCode ];
135190 $ config ->ignored = [];
136191
137- if (isset (self ::$ rulesets [$ standardName ]) === false ) {
138- $ ruleset = new Ruleset ($ config );
139- self ::$ rulesets [$ standardName ] = $ ruleset ;
140- }
141-
142- $ ruleset = self ::$ rulesets [$ standardName ];
143-
144- $ sniffFile = preg_replace ('`[/ \\\\]Tests[/ \\\\]` ' , DIRECTORY_SEPARATOR .'Sniffs ' .DIRECTORY_SEPARATOR , $ testFileBase );
145- $ sniffFile = str_replace ('UnitTest. ' , 'Sniff.php ' , $ sniffFile );
146-
147- if (file_exists ($ sniffFile ) === false ) {
148- $ this ->fail (sprintf ('ERROR: Sniff file %s for test %s does not appear to exist ' , $ sniffFile , static ::class));
149- }
150-
151- $ sniffClassName = substr (get_class ($ this ), 0 , -8 ).'Sniff ' ;
152- $ sniffClassName = str_replace ('\Tests \\' , '\Sniffs \\' , $ sniffClassName );
153- $ sniffClassName = Common::cleanSniffClass ($ sniffClassName );
154-
155- $ restrictions = [strtolower ($ sniffClassName ) => true ];
156- $ ruleset ->registerSniffs ([$ sniffFile ], $ restrictions , []);
157- $ ruleset ->populateTokenListeners ();
192+ $ ruleset = new Ruleset ($ config );
158193
159194 $ failureMessages = [];
160195 foreach ($ testFiles as $ testFile ) {
0 commit comments