Skip to content

Commit 0efda76

Browse files
committed
UtilityMethodTestCase: new parseFile() method
This abstracts the parsing of a file out from the `setUpTestFile()` method, without changing the functionality. Having the parsing of a file as a separate method allows for tests to parse a secondary test case file for use in a test. As the new method doesn't create any functional changes, it is already covered by existing tests.
1 parent da8827f commit 0efda76

File tree

3 files changed

+33
-10
lines changed

3 files changed

+33
-10
lines changed

PHPCSUtils/TestUtils/UtilityMethodTestCase.php

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -206,12 +206,6 @@ public static function setUpTestFile()
206206
$caseFile = \substr($testFile, 0, -3) . static::$fileExtension;
207207
}
208208

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-
215209
$config = new ConfigDouble();
216210

217211
/*
@@ -227,22 +221,49 @@ public static function setUpTestFile()
227221

228222
$ruleset = new Ruleset($config);
229223

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+
230248
// Make sure the file gets parsed correctly based on the file type.
249+
$contents = \file_get_contents($caseFile);
231250
$contents = 'phpcs_input_file: ' . $caseFile . \PHP_EOL . $contents;
232251

233-
self::$phpcsFile = new DummyFile($contents, $ruleset, $config);
252+
$file = new DummyFile($contents, $ruleset, $config);
234253

235254
// Only tokenize the file, do not process it.
236255
try {
237-
self::$phpcsFile->parse();
256+
$file->parse();
238257
} catch (TokenizerException $e) {
239258
// PHPCS 3.5.0 and higher. This is handled below.
240259
}
241260

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) {
244263
parent::fail("Tokenizing of the test case file failed for case file: $caseFile");
245264
}
265+
266+
return $file;
246267
}
247268

248269
/**

Tests/TestUtils/UtilityMethodTestCase/FailedToTokenizeTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* Tests for the \PHPCSUtils\TestUtils\UtilityMethodTestCase class.
1717
*
1818
* @covers \PHPCSUtils\TestUtils\UtilityMethodTestCase::setUpTestFile
19+
* @covers \PHPCSUtils\TestUtils\UtilityMethodTestCase::parseFile
1920
*
2021
* @since 1.0.0
2122
*/

Tests/TestUtils/UtilityMethodTestCase/MissingCaseFileTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* Tests for the \PHPCSUtils\TestUtils\UtilityMethodTestCase class.
1717
*
1818
* @covers \PHPCSUtils\TestUtils\UtilityMethodTestCase::setUpTestFile
19+
* @covers \PHPCSUtils\TestUtils\UtilityMethodTestCase::parseFile
1920
*
2021
* @since 1.0.0
2122
*/

0 commit comments

Comments
 (0)