@@ -206,12 +206,6 @@ public static function setUpTestFile()
206
206
$ caseFile = \substr ($ testFile , 0 , -3 ) . static ::$ fileExtension ;
207
207
}
208
208
209
- if (\is_readable ($ caseFile ) === false ) {
210
- parent ::fail ("Test case file missing. Expected case file location: $ caseFile " );
211
- }
212
-
213
- $ contents = \file_get_contents ($ caseFile );
214
-
215
209
$ config = new ConfigDouble ();
216
210
217
211
/*
@@ -227,22 +221,49 @@ public static function setUpTestFile()
227
221
228
222
$ ruleset = new Ruleset ($ config );
229
223
224
+ self ::$ phpcsFile = self ::parseFile ($ caseFile , $ ruleset , $ config );
225
+ }
226
+
227
+ /**
228
+ * Create a File object.
229
+ *
230
+ * The file will only be parsed, not processed.
231
+ *
232
+ * This helper method can also be used to create a secondary file object using the same sniff objects
233
+ * as used for the original test case file.
234
+ * To do so, pass `self::$phpcsFile->ruleset` for the $ruleset and `self::$phpcsFile->config` for the $config.
235
+ *
236
+ * @param string $caseFile The absolute path to the file.
237
+ * @param \PHP_CodeSniffer\Ruleset $ruleset The ruleset for the run.
238
+ * @param \PHP_CodeSniffer\Config $config The config data for the run.
239
+ *
240
+ * @return \PHP_CodeSniffer\Files\File
241
+ */
242
+ protected static function parseFile ($ caseFile , $ ruleset , $ config )
243
+ {
244
+ if (\is_readable ($ caseFile ) === false ) {
245
+ parent ::fail ("Test case file missing. Expected case file location: $ caseFile " );
246
+ }
247
+
230
248
// Make sure the file gets parsed correctly based on the file type.
249
+ $ contents = \file_get_contents ($ caseFile );
231
250
$ contents = 'phpcs_input_file: ' . $ caseFile . \PHP_EOL . $ contents ;
232
251
233
- self :: $ phpcsFile = new DummyFile ($ contents , $ ruleset , $ config );
252
+ $ file = new DummyFile ($ contents , $ ruleset , $ config );
234
253
235
254
// Only tokenize the file, do not process it.
236
255
try {
237
- self :: $ phpcsFile ->parse ();
256
+ $ file ->parse ();
238
257
} catch (TokenizerException $ e ) {
239
258
// PHPCS 3.5.0 and higher. This is handled below.
240
259
}
241
260
242
- // Fail the test if the case file failed to tokenize.
243
- if (self :: $ phpcsFile ->numTokens === 0 ) {
261
+ // Fail the test if the file failed to tokenize.
262
+ if ($ file ->numTokens === 0 ) {
244
263
parent ::fail ("Tokenizing of the test case file failed for case file: $ caseFile " );
245
264
}
265
+
266
+ return $ file ;
246
267
}
247
268
248
269
/**
0 commit comments