Skip to content

Commit a480170

Browse files
authored
Ruleset::getIncludePatterns(): add tests (#706)
1 parent b5a81c6 commit a480170

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
/**
3+
* Test the Ruleset::getIncludePatterns() 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::getIncludePatterns() method.
18+
*
19+
* @covers \PHP_CodeSniffer\Ruleset::getIncludePatterns
20+
*/
21+
final class GetIncludePatternsTest 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__."/GetIncludePatternsTest.xml";
43+
$config = new ConfigDouble(["--standard=$standard"]);
44+
self::$ruleset = new Ruleset($config);
45+
46+
}//end initializeConfigAndRuleset()
47+
48+
49+
/**
50+
* Test retrieving include 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 dataGetIncludePatterns
56+
*
57+
* @return void
58+
*/
59+
public function testGetIncludePatterns($listener, $expected)
60+
{
61+
$this->assertSame($expected, self::$ruleset->getIncludePatterns($listener));
62+
63+
}//end testGetIncludePatterns()
64+
65+
66+
/**
67+
* Data provider.
68+
*
69+
* @see self::testGetIncludePatterns()
70+
*
71+
* @return array<string, array<string, string|array<string, string|array<string, string>>|null>>
72+
*/
73+
public static function dataGetIncludePatterns()
74+
{
75+
return [
76+
'All include 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+
],
87+
],
88+
'Include patterns for PSR1.Classes.ClassDeclaration' => [
89+
'listener' => 'PSR1.Classes.ClassDeclaration',
90+
'expected' => [
91+
'./src/*/file.php' => 'absolute',
92+
'./bin/' => 'relative',
93+
],
94+
],
95+
'Include patterns for Generic.Formatting.SpaceAfterCast' => [
96+
'listener' => 'Generic.Formatting.SpaceAfterCast',
97+
'expected' => ['./src/*/test\\.php$' => 'absolute'],
98+
],
99+
'Include patterns for sniff without include patterns' => [
100+
'listener' => 'PSR1.Files.SideEffects',
101+
'expected' => [],
102+
],
103+
];
104+
105+
}//end dataGetIncludePatterns()
106+
107+
108+
}//end class
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0"?>
2+
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="GetIncludePatternsTest" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/PHPCSStandards/PHP_CodeSniffer/master/phpcs.xsd">
3+
4+
<rule ref="PSR1"/>
5+
6+
<rule ref="PSR1.Classes.ClassDeclaration">
7+
<include-pattern type="absolute">./src/*/file.php</include-pattern>
8+
<include-pattern type="relative">./bin/</include-pattern>
9+
</rule>
10+
11+
<rule ref="Generic.Formatting.SpaceAfterCast">
12+
<include-pattern>./src/*/test\.php$</include-pattern>
13+
</rule>
14+
15+
</ruleset>

0 commit comments

Comments
 (0)