Skip to content

Commit fd17538

Browse files
committed
Filters/Filter: expand test coverage for the Filter::accept() method
This commit expands the Filter::accept() tests to cover cases that were not covered before: - Run the tests on Windows as there is dedicated code when the directory separator is "\". - Real directories trigger code that relies on is_dir() returning `true`. - Sets Config::$local to `true` and ensure in this case directories are not accepted. - Files without an extension, with an invalid one or starting with a dot are ignored. - Duplicated files are ignored. - Relative exclude patterns are handled correctly.
1 parent 30fdf53 commit fd17538

File tree

2 files changed

+68
-2
lines changed

2 files changed

+68
-2
lines changed

tests/Core/Filters/Filter/AcceptTest.php

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* Tests for the \PHP_CodeSniffer\Filters\Filter::accept method.
2121
*
2222
* @covers \PHP_CodeSniffer\Filters\Filter
23+
* @group Windows
2324
*/
2425
final class AcceptTest extends AbstractFilterTestCase
2526
{
@@ -46,16 +47,23 @@ public static function initializeConfigAndRuleset()
4647
*
4748
* @param array<string> $inputPaths List of file paths to be filtered.
4849
* @param array<string> $expectedOutput Expected filtering result.
50+
* @param bool $localFiles Value of the Config class "local" setting (default: false).
4951
*
5052
* @dataProvider dataExcludePatterns
5153
*
5254
* @return void
5355
*/
54-
public function testExcludePatterns($inputPaths, $expectedOutput)
56+
public function testExcludePatterns($inputPaths, $expectedOutput, $localFiles=false)
5557
{
5658
$fakeDI = new RecursiveArrayIterator($inputPaths);
5759
$filter = new Filter($fakeDI, '/', self::$config, self::$ruleset);
5860

61+
self::$config->local = false;
62+
63+
if ($localFiles === true) {
64+
self::$config->local = true;
65+
}
66+
5967
$this->assertSame($expectedOutput, $this->getFilteredResultsAsArray($filter));
6068

6169
}//end testExcludePatterns()
@@ -99,6 +107,60 @@ public static function dataExcludePatterns()
99107
'/path/to/src/anything-generic/Main.php',
100108
],
101109
],
110+
'Filter should exclude files without an extension, using unsupported extension and starting with a dot' => [
111+
'inputPaths' => [
112+
'/path/to/src/Main.php',
113+
'/path/to/src/.hiddenfile',
114+
'/path/to/src/NoExtension',
115+
'/path/to/src/UnsupportedExtension.txt',
116+
'/path/to/src/UnsupportedExtension.php.bak',
117+
],
118+
'expectedOutput' => [
119+
'/path/to/src/Main.php',
120+
],
121+
],
122+
'Filter should ignore duplicate files' => [
123+
'inputPaths' => [
124+
__FILE__,
125+
__FILE__,
126+
'/path/to/src/Main.php',
127+
],
128+
'expectedOutput' => [
129+
__FILE__,
130+
'/path/to/src/Main.php',
131+
],
132+
],
133+
'Filter should work for relative exclude patterns' => [
134+
'inputPaths' => [
135+
'src/Main.php',
136+
'src/AnotherDir/File.php',
137+
],
138+
'expectedOutput' => [
139+
'src/Main.php',
140+
],
141+
],
142+
143+
// Uses real directories to test code that calls is_dir().
144+
'Filter should handle directories' => [
145+
'inputPaths' => [
146+
self::getBaseDir().'/src/Generators',
147+
self::getBaseDir().'/src/Standards',
148+
],
149+
'expectedOutput' => [
150+
self::getBaseDir().'/src/Standards',
151+
],
152+
],
153+
'Filter should ignore directories when --local is used' => [
154+
'inputPaths' => [
155+
'src/Main.php',
156+
self::getBaseDir().'/src/Generators',
157+
self::getBaseDir().'/src/Standards',
158+
],
159+
'expectedOutput' => [
160+
'src/Main.php',
161+
],
162+
'localFiles' => true,
163+
],
102164
];
103165

104166
// Allow these tests to work on Windows as well.

tests/Core/Filters/Filter/AcceptTest.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@
22
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="AcceptTest" xsi:noNamespaceSchemaLocation="phpcs.xsd">
33
<description>Ruleset to test the filtering based on exclude patterns.</description>
44

5-
<!-- Directory pattern. -->
5+
<!-- Directory patterns. -->
66
<exclude-pattern>*/something/*</exclude-pattern>
7+
<exclude-pattern>*/src/Generators/*</exclude-pattern>
78
<!-- File pattern. -->
89
<exclude-pattern>*/Other/Main\.php$</exclude-pattern>
910

11+
<!-- Relative directory pattern. -->
12+
<exclude-pattern type="relative">/AnotherDir/*</exclude-pattern>
13+
1014
<rule ref="Generic">
1115
<exclude name="Generic.Debug"/>
1216

0 commit comments

Comments
 (0)