Skip to content

Commit

Permalink
RuleInclusionTest: record code coverage
Browse files Browse the repository at this point in the history
Code executed during "before class" methods is not recorded for code coverage, while code executed in "before" methods is, but the "before" method is executed before _every_ test in the class, not just once before the tests in the class run.

So, to record code coverage, while still maintaining the performance benefits of only creating the Config and Ruleset objects once, the code still sets a static property and will only run if that static property has not been filled yet.
  • Loading branch information
jrfnl committed Nov 15, 2024
1 parent 56784f1 commit 78d3d3d
Showing 1 changed file with 20 additions and 18 deletions.
38 changes: 20 additions & 18 deletions tests/Core/Ruleset/RuleInclusionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,35 +47,37 @@ final class RuleInclusionTest extends TestCase
/**
* Initialize the config and ruleset objects based on the `RuleInclusionTest.xml` ruleset file.
*
* @beforeClass
* @before
*
* @return void
*/
public static function initializeConfigAndRuleset()
{
$standard = __DIR__.'/'.basename(__FILE__, '.php').'.xml';
self::$standard = $standard;
if (self::$standard === '') {
$standard = __DIR__.'/'.basename(__FILE__, '.php').'.xml';
self::$standard = $standard;

// On-the-fly adjust the ruleset test file to be able to test
// sniffs included with relative paths.
$contents = file_get_contents($standard);
self::$contents = $contents;
// On-the-fly adjust the ruleset test file to be able to test
// sniffs included with relative paths.
$contents = file_get_contents($standard);
self::$contents = $contents;

$repoRootDir = basename(dirname(dirname(dirname(__DIR__))));
$repoRootDir = basename(dirname(dirname(dirname(__DIR__))));

$newPath = $repoRootDir;
if (DIRECTORY_SEPARATOR === '\\') {
$newPath = str_replace('\\', '/', $repoRootDir);
}
$newPath = $repoRootDir;
if (DIRECTORY_SEPARATOR === '\\') {
$newPath = str_replace('\\', '/', $repoRootDir);
}

$adjusted = str_replace('%path_root_dir%', $newPath, $contents);
$adjusted = str_replace('%path_root_dir%', $newPath, $contents);

if (file_put_contents($standard, $adjusted) === false) {
self::markTestSkipped('On the fly ruleset adjustment failed');
}
if (file_put_contents($standard, $adjusted) === false) {
self::markTestSkipped('On the fly ruleset adjustment failed');
}

$config = new ConfigDouble(["--standard=$standard"]);
self::$ruleset = new Ruleset($config);
$config = new ConfigDouble(["--standard=$standard"]);
self::$ruleset = new Ruleset($config);
}//end if

}//end initializeConfigAndRuleset()

Expand Down

0 comments on commit 78d3d3d

Please sign in to comment.