From 0efda76e07f2ac708e076a214a3c7223281819ed Mon Sep 17 00:00:00 2001 From: jrfnl Date: Tue, 30 Apr 2024 12:45:04 +0200 Subject: [PATCH] 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. --- .../TestUtils/UtilityMethodTestCase.php | 41 ++++++++++++++----- .../FailedToTokenizeTest.php | 1 + .../MissingCaseFileTest.php | 1 + 3 files changed, 33 insertions(+), 10 deletions(-) diff --git a/PHPCSUtils/TestUtils/UtilityMethodTestCase.php b/PHPCSUtils/TestUtils/UtilityMethodTestCase.php index 2dd4351b..27bc4f07 100644 --- a/PHPCSUtils/TestUtils/UtilityMethodTestCase.php +++ b/PHPCSUtils/TestUtils/UtilityMethodTestCase.php @@ -206,12 +206,6 @@ public static function setUpTestFile() $caseFile = \substr($testFile, 0, -3) . static::$fileExtension; } - if (\is_readable($caseFile) === false) { - parent::fail("Test case file missing. Expected case file location: $caseFile"); - } - - $contents = \file_get_contents($caseFile); - $config = new ConfigDouble(); /* @@ -227,22 +221,49 @@ public static function setUpTestFile() $ruleset = new Ruleset($config); + self::$phpcsFile = self::parseFile($caseFile, $ruleset, $config); + } + + /** + * Create a File object. + * + * The file will only be parsed, not processed. + * + * This helper method can also be used to create a secondary file object using the same sniff objects + * as used for the original test case file. + * To do so, pass `self::$phpcsFile->ruleset` for the $ruleset and `self::$phpcsFile->config` for the $config. + * + * @param string $caseFile The absolute path to the file. + * @param \PHP_CodeSniffer\Ruleset $ruleset The ruleset for the run. + * @param \PHP_CodeSniffer\Config $config The config data for the run. + * + * @return \PHP_CodeSniffer\Files\File + */ + protected static function parseFile($caseFile, $ruleset, $config) + { + if (\is_readable($caseFile) === false) { + parent::fail("Test case file missing. Expected case file location: $caseFile"); + } + // Make sure the file gets parsed correctly based on the file type. + $contents = \file_get_contents($caseFile); $contents = 'phpcs_input_file: ' . $caseFile . \PHP_EOL . $contents; - self::$phpcsFile = new DummyFile($contents, $ruleset, $config); + $file = new DummyFile($contents, $ruleset, $config); // Only tokenize the file, do not process it. try { - self::$phpcsFile->parse(); + $file->parse(); } catch (TokenizerException $e) { // PHPCS 3.5.0 and higher. This is handled below. } - // Fail the test if the case file failed to tokenize. - if (self::$phpcsFile->numTokens === 0) { + // Fail the test if the file failed to tokenize. + if ($file->numTokens === 0) { parent::fail("Tokenizing of the test case file failed for case file: $caseFile"); } + + return $file; } /** diff --git a/Tests/TestUtils/UtilityMethodTestCase/FailedToTokenizeTest.php b/Tests/TestUtils/UtilityMethodTestCase/FailedToTokenizeTest.php index f6356f73..8ef79a21 100644 --- a/Tests/TestUtils/UtilityMethodTestCase/FailedToTokenizeTest.php +++ b/Tests/TestUtils/UtilityMethodTestCase/FailedToTokenizeTest.php @@ -16,6 +16,7 @@ * Tests for the \PHPCSUtils\TestUtils\UtilityMethodTestCase class. * * @covers \PHPCSUtils\TestUtils\UtilityMethodTestCase::setUpTestFile + * @covers \PHPCSUtils\TestUtils\UtilityMethodTestCase::parseFile * * @since 1.0.0 */ diff --git a/Tests/TestUtils/UtilityMethodTestCase/MissingCaseFileTest.php b/Tests/TestUtils/UtilityMethodTestCase/MissingCaseFileTest.php index 58b8cea1..f849d830 100644 --- a/Tests/TestUtils/UtilityMethodTestCase/MissingCaseFileTest.php +++ b/Tests/TestUtils/UtilityMethodTestCase/MissingCaseFileTest.php @@ -16,6 +16,7 @@ * Tests for the \PHPCSUtils\TestUtils\UtilityMethodTestCase class. * * @covers \PHPCSUtils\TestUtils\UtilityMethodTestCase::setUpTestFile + * @covers \PHPCSUtils\TestUtils\UtilityMethodTestCase::parseFile * * @since 1.0.0 */