Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 31 additions & 10 deletions PHPCSUtils/TestUtils/UtilityMethodTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

/*
Expand All @@ -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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down