Skip to content

Commit 78d3d3d

Browse files
committed
RuleInclusionTest: record code coverage
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.
1 parent 56784f1 commit 78d3d3d

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

tests/Core/Ruleset/RuleInclusionTest.php

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -47,35 +47,37 @@ final class RuleInclusionTest extends TestCase
4747
/**
4848
* Initialize the config and ruleset objects based on the `RuleInclusionTest.xml` ruleset file.
4949
*
50-
* @beforeClass
50+
* @before
5151
*
5252
* @return void
5353
*/
5454
public static function initializeConfigAndRuleset()
5555
{
56-
$standard = __DIR__.'/'.basename(__FILE__, '.php').'.xml';
57-
self::$standard = $standard;
56+
if (self::$standard === '') {
57+
$standard = __DIR__.'/'.basename(__FILE__, '.php').'.xml';
58+
self::$standard = $standard;
5859

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

64-
$repoRootDir = basename(dirname(dirname(dirname(__DIR__))));
65+
$repoRootDir = basename(dirname(dirname(dirname(__DIR__))));
6566

66-
$newPath = $repoRootDir;
67-
if (DIRECTORY_SEPARATOR === '\\') {
68-
$newPath = str_replace('\\', '/', $repoRootDir);
69-
}
67+
$newPath = $repoRootDir;
68+
if (DIRECTORY_SEPARATOR === '\\') {
69+
$newPath = str_replace('\\', '/', $repoRootDir);
70+
}
7071

71-
$adjusted = str_replace('%path_root_dir%', $newPath, $contents);
72+
$adjusted = str_replace('%path_root_dir%', $newPath, $contents);
7273

73-
if (file_put_contents($standard, $adjusted) === false) {
74-
self::markTestSkipped('On the fly ruleset adjustment failed');
75-
}
74+
if (file_put_contents($standard, $adjusted) === false) {
75+
self::markTestSkipped('On the fly ruleset adjustment failed');
76+
}
7677

77-
$config = new ConfigDouble(["--standard=$standard"]);
78-
self::$ruleset = new Ruleset($config);
78+
$config = new ConfigDouble(["--standard=$standard"]);
79+
self::$ruleset = new Ruleset($config);
80+
}//end if
7981

8082
}//end initializeConfigAndRuleset()
8183

0 commit comments

Comments
 (0)