Skip to content

Commit 3a72428

Browse files
committed
Merge branch 'issue-2391' of https://github.com/wvega/PHP_CodeSniffer
2 parents 100a81c + 1cad124 commit 3a72428

File tree

5 files changed

+160
-0
lines changed

5 files changed

+160
-0
lines changed

package.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,12 @@ http://pear.php.net/dtd/package-2.0.xsd">
107107
<file baseinstalldir="" name="IsReferenceTest.inc" role="test" />
108108
<file baseinstalldir="" name="IsReferenceTest.php" role="test" />
109109
</dir>
110+
<dir name="Filters">
111+
<dir name="Filter">
112+
<file baseinstalldir="" name="AcceptTest.xml" role="test" />
113+
<file baseinstalldir="" name="AcceptTest.php" role="test" />
114+
</dir>
115+
</dir>
110116
<file baseinstalldir="" name="AllTests.php" role="test" />
111117
<file baseinstalldir="" name="ErrorSuppressionTest.php" role="test" />
112118
<file baseinstalldir="" name="IsCamelCapsTest.php" role="test" />
@@ -1841,6 +1847,8 @@ http://pear.php.net/dtd/package-2.0.xsd">
18411847
<install as="CodeSniffer/Core/File/GetMethodPropertiesTest.inc" name="tests/Core/File/GetMethodPropertiesTest.inc" />
18421848
<install as="CodeSniffer/Core/File/IsReferenceTest.php" name="tests/Core/File/IsReferenceTest.php" />
18431849
<install as="CodeSniffer/Core/File/IsReferenceTest.inc" name="tests/Core/File/IsReferenceTest.inc" />
1850+
<install as="CodeSniffer/Core/Filters/Filter/AcceptTest.php" name="tests/Core/Filters/Filter/AcceptTest.php" />
1851+
<install as="CodeSniffer/Core/Filters/Filter/AcceptTest.xml" name="tests/Core/Filters/Filter/AcceptTest.xml" />
18441852
<install as="CodeSniffer/Standards/AllSniffs.php" name="tests/Standards/AllSniffs.php" />
18451853
<install as="CodeSniffer/Standards/AbstractSniffUnitTest.php" name="tests/Standards/AbstractSniffUnitTest.php" />
18461854
</filelist>
@@ -1874,6 +1882,8 @@ http://pear.php.net/dtd/package-2.0.xsd">
18741882
<install as="CodeSniffer/Core/File/GetMethodPropertiesTest.inc" name="tests/Core/File/GetMethodPropertiesTest.inc" />
18751883
<install as="CodeSniffer/Core/File/IsReferenceTest.php" name="tests/Core/File/IsReferenceTest.php" />
18761884
<install as="CodeSniffer/Core/File/IsReferenceTest.inc" name="tests/Core/File/IsReferenceTest.inc" />
1885+
<install as="CodeSniffer/Core/Filters/Filter/AcceptTest.php" name="tests/Core/Filters/Filter/AcceptTest.php" />
1886+
<install as="CodeSniffer/Core/Filters/Filter/AcceptTest.xml" name="tests/Core/Filters/Filter/AcceptTest.xml" />
18771887
<install as="CodeSniffer/Standards/AllSniffs.php" name="tests/Standards/AllSniffs.php" />
18781888
<install as="CodeSniffer/Standards/AbstractSniffUnitTest.php" name="tests/Standards/AbstractSniffUnitTest.php" />
18791889
<ignore name="bin/phpcs.bat" />

src/Filters/Filter.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,11 @@ protected function shouldIgnorePath($path)
229229
}
230230

231231
foreach ($ignorePatterns as $pattern => $type) {
232+
// Ignore standard/sniff specific exclude rules.
233+
if (is_array($type) === true) {
234+
continue;
235+
}
236+
232237
// Maintains backwards compatibility in case the ignore pattern does
233238
// not have a relative/absolute value.
234239
if (is_int($pattern) === true) {

tests/Core/AllTests.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
require_once 'File/GetMethodParametersTest.php';
2222
require_once 'File/GetMethodPropertiesTest.php';
2323
require_once 'File/IsReferenceTest.php';
24+
require_once 'Filters/Filter/AcceptTest.php';
2425

2526
class AllTests
2627
{
@@ -55,6 +56,7 @@ public static function suite()
5556
$suite->addTestSuite('PHP_CodeSniffer\Tests\Core\File\GetMethodParametersTest');
5657
$suite->addTestSuite('PHP_CodeSniffer\Tests\Core\File\GetMethodPropertiesTest');
5758
$suite->addTestSuite('PHP_CodeSniffer\Tests\Core\File\IsReferenceTest');
59+
$suite->addTestSuite('PHP_CodeSniffer\Tests\Core\Filters\Filter\AcceptTest');
5860
return $suite;
5961

6062
}//end suite()
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<?php
2+
/**
3+
* Tests for the \PHP_CodeSniffer\Filters\Filter::accept method.
4+
*
5+
* @author Willington Vega <wvega@wvega.com>
6+
* @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl>
7+
* @copyright 2019 Squiz Pty Ltd (ABN 77 084 670 600)
8+
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
9+
*/
10+
11+
namespace PHP_CodeSniffer\Tests\Core\Filters\Filter;
12+
13+
use PHP_CodeSniffer\Config;
14+
use PHP_CodeSniffer\Filters\Filter;
15+
use PHP_CodeSniffer\Ruleset;
16+
use PHPUnit\Framework\TestCase;
17+
18+
class AcceptTest extends TestCase
19+
{
20+
21+
/**
22+
* The Config object.
23+
*
24+
* @var \PHP_CodeSniffer\Config
25+
*/
26+
protected static $config;
27+
28+
/**
29+
* The Ruleset object.
30+
*
31+
* @var \PHP_CodeSniffer\Ruleset
32+
*/
33+
protected static $ruleset;
34+
35+
36+
/**
37+
* Initialize the config and ruleset objects based on the `AcceptTest.xml` ruleset file.
38+
*
39+
* @return void
40+
*/
41+
public static function setUpBeforeClass()
42+
{
43+
$standard = __DIR__.'/'.basename(__FILE__, '.php').'.xml';
44+
self::$config = new Config(["--standard=$standard"]);
45+
self::$ruleset = new Ruleset(self::$config);
46+
47+
}//end setUpBeforeClass()
48+
49+
50+
/**
51+
* Test filtering a file list for excluded paths.
52+
*
53+
* @param array $inputPaths List of file paths to be filtered.
54+
* @param array $expectedOutput Expected filtering result.
55+
*
56+
* @dataProvider dataExcludePatterns
57+
*
58+
* @return void
59+
*/
60+
public function testExcludePatterns($inputPaths, $expectedOutput)
61+
{
62+
$fakeDI = new \RecursiveArrayIterator($inputPaths);
63+
$filter = new Filter($fakeDI, '/', self::$config, self::$ruleset);
64+
$iterator = new \RecursiveIteratorIterator($filter);
65+
$files = [];
66+
67+
foreach ($iterator as $file) {
68+
$files[] = $file;
69+
}
70+
71+
$this->assertEquals($expectedOutput, $files);
72+
73+
}//end testExcludePatterns()
74+
75+
76+
/**
77+
* Data provider.
78+
*
79+
* @see testExcludePatterns
80+
*
81+
* @return array
82+
*/
83+
public function dataExcludePatterns()
84+
{
85+
$testCases = [
86+
// Test top-level exclude patterns.
87+
[
88+
[
89+
'/path/to/src/Main.php',
90+
'/path/to/src/Something/Main.php',
91+
'/path/to/src/Other/Main.php',
92+
],
93+
['/path/to/src/Main.php'],
94+
],
95+
96+
// Test ignoring standard/sniff specific exclude patterns.
97+
[
98+
[
99+
'/path/to/src/generic-project/Main.php',
100+
'/path/to/src/generic/Main.php',
101+
'/path/to/src/anything-generic/Main.php',
102+
],
103+
[
104+
'/path/to/src/generic-project/Main.php',
105+
'/path/to/src/generic/Main.php',
106+
'/path/to/src/anything-generic/Main.php',
107+
],
108+
],
109+
];
110+
111+
// Allow these tests to work on Windows as well.
112+
if (DIRECTORY_SEPARATOR === '\\') {
113+
foreach ($testCases as $key => $case) {
114+
foreach ($case as $nr => $param) {
115+
foreach ($param as $file => $value) {
116+
$testCases[$key][$nr][$file] = strtr($value, '/', '\\');
117+
}
118+
}
119+
}
120+
}
121+
122+
return $testCases;
123+
124+
}//end dataExcludePatterns()
125+
126+
127+
}//end class
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0"?>
2+
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="AcceptTest" xsi:noNamespaceSchemaLocation="phpcs.xsd">
3+
<description>Ruleset to test the filtering based on exclude patterns.</description>
4+
5+
<!-- Directory pattern. -->
6+
<exclude-pattern>*/something/*</exclude-pattern>
7+
<!-- File pattern. -->
8+
<exclude-pattern>*/Other/Main\.php$</exclude-pattern>
9+
10+
<rule ref="Generic">
11+
<!-- Standard specific directory pattern. -->
12+
<exclude-pattern>/anything/*</exclude-pattern>
13+
<!-- Standard specific file pattern. -->
14+
<exclude-pattern>/YetAnother/Main\.php</exclude-pattern>
15+
</rule>
16+
</ruleset>

0 commit comments

Comments
 (0)