From 27d759aaf76ce4573e87e4f4f90bc7b57cd074a5 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Mon, 18 Nov 2024 01:09:11 +0100 Subject: [PATCH 1/3] Ruleset::getIgnorePatterns(): add tests --- tests/Core/Ruleset/GetIgnorePatternsTest.php | 111 +++++++++++++++++++ tests/Core/Ruleset/GetIgnorePatternsTest.xml | 19 ++++ 2 files changed, 130 insertions(+) create mode 100644 tests/Core/Ruleset/GetIgnorePatternsTest.php create mode 100644 tests/Core/Ruleset/GetIgnorePatternsTest.xml diff --git a/tests/Core/Ruleset/GetIgnorePatternsTest.php b/tests/Core/Ruleset/GetIgnorePatternsTest.php new file mode 100644 index 0000000000..a8f4db43b8 --- /dev/null +++ b/tests/Core/Ruleset/GetIgnorePatternsTest.php @@ -0,0 +1,111 @@ + + * @copyright 2024 PHPCSStandards and contributors + * @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence + */ + +namespace PHP_CodeSniffer\Tests\Core\Ruleset; + +use PHP_CodeSniffer\Ruleset; +use PHP_CodeSniffer\Tests\ConfigDouble; +use PHPUnit\Framework\TestCase; + +/** + * Test the Ruleset::getIgnorePatterns() method. + * + * @covers \PHP_CodeSniffer\Ruleset::getIgnorePatterns + */ +final class GetIgnorePatternsTest extends TestCase +{ + + /** + * The Ruleset object. + * + * @var \PHP_CodeSniffer\Ruleset + */ + private static $ruleset; + + + /** + * Initialize the config and ruleset objects for this test. + * + * @beforeClass + * + * @return void + */ + public static function initializeConfigAndRuleset() + { + // Set up the ruleset. + $standard = __DIR__."/GetIgnorePatternsTest.xml"; + $config = new ConfigDouble(["--standard=$standard"]); + self::$ruleset = new Ruleset($config); + + }//end initializeConfigAndRuleset() + + + /** + * Test retrieving ignore patterns. + * + * @param string|null $listener The listener to get patterns for or null for all patterns. + * @param array> $expected The expected function output. + * + * @dataProvider dataGetIgnorePatterns + * + * @return void + */ + public function testGetIgnorePatterns($listener, $expected) + { + $this->assertSame($expected, self::$ruleset->getIgnorePatterns($listener)); + + }//end testGetIgnorePatterns() + + + /** + * Data provider. + * + * @see self::testGetIgnorePatterns() + * + * @return array>|null>> + */ + public static function dataGetIgnorePatterns() + { + return [ + 'All ignore patterns' => [ + 'listener' => null, + 'expected' => [ + 'PSR1.Classes.ClassDeclaration' => [ + './src/*/file.php' => 'absolute', + './bin/' => 'absolute', + ], + 'Generic.Formatting.SpaceAfterCast' => [ + './src/*/test\\.php$' => 'absolute', + ], + './tests/' => 'absolute', + './vendor/*' => 'absolute', + '*/node-modules/*' => 'absolute', + ], + ], + 'Ignore patterns for PSR1.Classes.ClassDeclaration' => [ + 'listener' => 'PSR1.Classes.ClassDeclaration', + 'expected' => [ + './src/*/file.php' => 'absolute', + './bin/' => 'absolute', + ], + ], + 'Ignore patterns for Generic.Formatting.SpaceAfterCast' => [ + 'listener' => 'Generic.Formatting.SpaceAfterCast', + 'expected' => ['./src/*/test\\.php$' => 'absolute'], + ], + 'Ignore patterns for sniff without ignore patterns' => [ + 'listener' => 'PSR1.Files.SideEffects', + 'expected' => [], + ], + ]; + + }//end dataGetIgnorePatterns() + + +}//end class diff --git a/tests/Core/Ruleset/GetIgnorePatternsTest.xml b/tests/Core/Ruleset/GetIgnorePatternsTest.xml new file mode 100644 index 0000000000..ed2533ba81 --- /dev/null +++ b/tests/Core/Ruleset/GetIgnorePatternsTest.xml @@ -0,0 +1,19 @@ + + + + ./tests/ + ./vendor/* + */node-modules/* + + + + + ./src/*/file.php + ./bin/ + + + + ./src/*/test\.php$ + + + From 7c9314a3eb50aef5476120bd5c04ce96fbe0b3ce Mon Sep 17 00:00:00 2001 From: jrfnl Date: Fri, 22 Nov 2024 17:54:25 +0100 Subject: [PATCH 2/3] GetIgnorePatternsTest: update ruleset fixture to include `type` attributes --- tests/Core/Ruleset/GetIgnorePatternsTest.php | 4 ++-- tests/Core/Ruleset/GetIgnorePatternsTest.xml | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/Core/Ruleset/GetIgnorePatternsTest.php b/tests/Core/Ruleset/GetIgnorePatternsTest.php index a8f4db43b8..5ba78378a3 100644 --- a/tests/Core/Ruleset/GetIgnorePatternsTest.php +++ b/tests/Core/Ruleset/GetIgnorePatternsTest.php @@ -78,7 +78,7 @@ public static function dataGetIgnorePatterns() 'expected' => [ 'PSR1.Classes.ClassDeclaration' => [ './src/*/file.php' => 'absolute', - './bin/' => 'absolute', + './bin/' => 'relative', ], 'Generic.Formatting.SpaceAfterCast' => [ './src/*/test\\.php$' => 'absolute', @@ -92,7 +92,7 @@ public static function dataGetIgnorePatterns() 'listener' => 'PSR1.Classes.ClassDeclaration', 'expected' => [ './src/*/file.php' => 'absolute', - './bin/' => 'absolute', + './bin/' => 'relative', ], ], 'Ignore patterns for Generic.Formatting.SpaceAfterCast' => [ diff --git a/tests/Core/Ruleset/GetIgnorePatternsTest.xml b/tests/Core/Ruleset/GetIgnorePatternsTest.xml index ed2533ba81..e933d31705 100644 --- a/tests/Core/Ruleset/GetIgnorePatternsTest.xml +++ b/tests/Core/Ruleset/GetIgnorePatternsTest.xml @@ -8,8 +8,8 @@ - ./src/*/file.php - ./bin/ + ./src/*/file.php + ./bin/ From 7a8e2352d8ef2290c6735ed6734aa5bbfb1cb4db Mon Sep 17 00:00:00 2001 From: jrfnl Date: Fri, 22 Nov 2024 17:55:40 +0100 Subject: [PATCH 3/3] GetIgnorePatternsTest: update ruleset fixture to include `type` attributes [2] --- tests/Core/Ruleset/GetIgnorePatternsTest.php | 2 +- tests/Core/Ruleset/GetIgnorePatternsTest.xml | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Core/Ruleset/GetIgnorePatternsTest.php b/tests/Core/Ruleset/GetIgnorePatternsTest.php index 5ba78378a3..ae6b8bea50 100644 --- a/tests/Core/Ruleset/GetIgnorePatternsTest.php +++ b/tests/Core/Ruleset/GetIgnorePatternsTest.php @@ -85,7 +85,7 @@ public static function dataGetIgnorePatterns() ], './tests/' => 'absolute', './vendor/*' => 'absolute', - '*/node-modules/*' => 'absolute', + '*/node-modules/*' => 'relative', ], ], 'Ignore patterns for PSR1.Classes.ClassDeclaration' => [ diff --git a/tests/Core/Ruleset/GetIgnorePatternsTest.xml b/tests/Core/Ruleset/GetIgnorePatternsTest.xml index e933d31705..7fcfdc237d 100644 --- a/tests/Core/Ruleset/GetIgnorePatternsTest.xml +++ b/tests/Core/Ruleset/GetIgnorePatternsTest.xml @@ -2,8 +2,8 @@ ./tests/ - ./vendor/* - */node-modules/* + ./vendor/* + */node-modules/*