Skip to content

Commit 5306b47

Browse files
committed
Ruleset::getIgnorePatterns(): add tests (#705)
1 parent d1433ce commit 5306b47

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<?php
2+
/**
3+
* Test the Ruleset::getIgnorePatterns() method.
4+
*
5+
* @author Juliette Reinders Folmer <[email protected]>
6+
* @copyright 2024 PHPCSStandards and contributors
7+
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
8+
*/
9+
10+
namespace PHP_CodeSniffer\Tests\Core\Ruleset;
11+
12+
use PHP_CodeSniffer\Ruleset;
13+
use PHP_CodeSniffer\Tests\ConfigDouble;
14+
use PHPUnit\Framework\TestCase;
15+
16+
/**
17+
* Test the Ruleset::getIgnorePatterns() method.
18+
*
19+
* @covers \PHP_CodeSniffer\Ruleset::getIgnorePatterns
20+
*/
21+
final class GetIgnorePatternsTest extends TestCase
22+
{
23+
24+
/**
25+
* The Ruleset object.
26+
*
27+
* @var \PHP_CodeSniffer\Ruleset
28+
*/
29+
private static $ruleset;
30+
31+
32+
/**
33+
* Initialize the config and ruleset objects for this test.
34+
*
35+
* @beforeClass
36+
*
37+
* @return void
38+
*/
39+
public static function initializeConfigAndRuleset()
40+
{
41+
// Set up the ruleset.
42+
$standard = __DIR__.'/GetIgnorePatternsTest.xml';
43+
$config = new ConfigDouble(["--standard=$standard"]);
44+
self::$ruleset = new Ruleset($config);
45+
46+
}//end initializeConfigAndRuleset()
47+
48+
49+
/**
50+
* Test retrieving ignore patterns.
51+
*
52+
* @param string|null $listener The listener to get patterns for or null for all patterns.
53+
* @param array<string, string|array<string, string>> $expected The expected function output.
54+
*
55+
* @dataProvider dataGetIgnorePatterns
56+
*
57+
* @return void
58+
*/
59+
public function testGetIgnorePatterns($listener, $expected)
60+
{
61+
$this->assertSame($expected, self::$ruleset->getIgnorePatterns($listener));
62+
63+
}//end testGetIgnorePatterns()
64+
65+
66+
/**
67+
* Data provider.
68+
*
69+
* @see self::testGetIgnorePatterns()
70+
*
71+
* @return array<string, array<string, string|array<string, string|array<string, string>>|null>>
72+
*/
73+
public static function dataGetIgnorePatterns()
74+
{
75+
return [
76+
'All ignore patterns' => [
77+
'listener' => null,
78+
'expected' => [
79+
'PSR1.Classes.ClassDeclaration' => [
80+
'./src/*/file.php' => 'absolute',
81+
'./bin/' => 'relative',
82+
],
83+
'Generic.Formatting.SpaceAfterCast' => [
84+
'./src/*/test\\.php$' => 'absolute',
85+
],
86+
'./tests/' => 'absolute',
87+
'./vendor/*' => 'absolute',
88+
'*/node-modules/*' => 'relative',
89+
],
90+
],
91+
'Ignore patterns for PSR1.Classes.ClassDeclaration' => [
92+
'listener' => 'PSR1.Classes.ClassDeclaration',
93+
'expected' => [
94+
'./src/*/file.php' => 'absolute',
95+
'./bin/' => 'relative',
96+
],
97+
],
98+
'Ignore patterns for Generic.Formatting.SpaceAfterCast' => [
99+
'listener' => 'Generic.Formatting.SpaceAfterCast',
100+
'expected' => ['./src/*/test\\.php$' => 'absolute'],
101+
],
102+
'Ignore patterns for sniff without ignore patterns' => [
103+
'listener' => 'PSR1.Files.SideEffects',
104+
'expected' => [],
105+
],
106+
];
107+
108+
}//end dataGetIgnorePatterns()
109+
110+
111+
}//end class
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0"?>
2+
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="GetIgnorePatternsTest" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/PHPCSStandards/PHP_CodeSniffer/master/phpcs.xsd">
3+
4+
<exclude-pattern>./tests/</exclude-pattern>
5+
<exclude-pattern type="absolute">./vendor/*</exclude-pattern>
6+
<exclude-pattern type="relative">*/node-modules/*</exclude-pattern>
7+
8+
<rule ref="PSR1"/>
9+
10+
<rule ref="PSR1.Classes.ClassDeclaration">
11+
<exclude-pattern type="absolute">./src/*/file.php</exclude-pattern>
12+
<exclude-pattern type="relative">./bin/</exclude-pattern>
13+
</rule>
14+
15+
<rule ref="Generic.Formatting.SpaceAfterCast">
16+
<exclude-pattern>./src/*/test\.php$</exclude-pattern>
17+
</rule>
18+
19+
</ruleset>

0 commit comments

Comments
 (0)