From 78d3d3d9db029209ad96c8ec2c7a86c32ddf65b5 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Fri, 15 Nov 2024 16:15:31 +0100 Subject: [PATCH 1/2] 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. --- tests/Core/Ruleset/RuleInclusionTest.php | 38 +++++++++++++----------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/tests/Core/Ruleset/RuleInclusionTest.php b/tests/Core/Ruleset/RuleInclusionTest.php index ef3ad83cac..175febf30a 100644 --- a/tests/Core/Ruleset/RuleInclusionTest.php +++ b/tests/Core/Ruleset/RuleInclusionTest.php @@ -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() From 7e29324efefc6db1efe3ea0b2e733254f22345d8 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Fri, 15 Nov 2024 16:04:43 +0100 Subject: [PATCH 2/2] RuleInclusionTest: add test with directory include --- tests/Core/Ruleset/RuleInclusionTest.php | 10 +++++++++- tests/Core/Ruleset/RuleInclusionTest.xml | 9 +++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/tests/Core/Ruleset/RuleInclusionTest.php b/tests/Core/Ruleset/RuleInclusionTest.php index 175febf30a..3f6a7180d6 100644 --- a/tests/Core/Ruleset/RuleInclusionTest.php +++ b/tests/Core/Ruleset/RuleInclusionTest.php @@ -103,7 +103,7 @@ public function resetRuleset() */ public function testHasSniffCodes() { - $this->assertCount(48, self::$ruleset->sniffCodes); + $this->assertCount(49, self::$ruleset->sniffCodes); }//end testHasSniffCodes() @@ -320,6 +320,10 @@ public static function dataRegisteredSniffCodes() 'Generic.Metrics.CyclomaticComplexity', 'PHP_CodeSniffer\Standards\Generic\Sniffs\Metrics\CyclomaticComplexitySniff', ], + [ + 'Squiz.Files.FileExtension', + 'PHP_CodeSniffer\Standards\Squiz\Sniffs\Files\FileExtensionSniff', + ], [ 'Generic.NamingConventions.CamelCapsFunctionName', 'PHP_CodeSniffer\Standards\Generic\Sniffs\NamingConventions\CamelCapsFunctionNameSniff', @@ -470,6 +474,10 @@ public static function dataSettingInvalidPropertiesOnStandardsAndCategoriesSilen 'sniffClass' => 'PHP_CodeSniffer\Standards\PSR12\Sniffs\Operators\OperatorSpacingSniff', 'propertyName' => 'setforallincategory', ], + 'Set property for all sniffs in included category directory' => [ + 'sniffClass' => 'PHP_CodeSniffer\Standards\Squiz\Sniffs\Files\FileExtensionSniff', + 'propertyName' => 'setforsquizfilessniffs', + ], ]; }//end dataSettingInvalidPropertiesOnStandardsAndCategoriesSilentlyFails() diff --git a/tests/Core/Ruleset/RuleInclusionTest.xml b/tests/Core/Ruleset/RuleInclusionTest.xml index 8275fe0111..6b5c0a970b 100644 --- a/tests/Core/Ruleset/RuleInclusionTest.xml +++ b/tests/Core/Ruleset/RuleInclusionTest.xml @@ -27,6 +27,14 @@ + + + + + + + + @@ -39,6 +47,7 @@ +