Skip to content

Filters/Filter: expand test coverage for the Filter::accept() method #1127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 63 additions & 1 deletion tests/Core/Filters/Filter/AcceptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* Tests for the \PHP_CodeSniffer\Filters\Filter::accept method.
*
* @covers \PHP_CodeSniffer\Filters\Filter
* @group Windows
*/
final class AcceptTest extends AbstractFilterTestCase
{
Expand All @@ -46,16 +47,23 @@ public static function initializeConfigAndRuleset()
*
* @param array<string> $inputPaths List of file paths to be filtered.
* @param array<string> $expectedOutput Expected filtering result.
* @param bool $localFiles Value of the Config class "local" setting (default: false).
*
* @dataProvider dataExcludePatterns
*
* @return void
*/
public function testExcludePatterns($inputPaths, $expectedOutput)
public function testExcludePatterns($inputPaths, $expectedOutput, $localFiles=false)
{
$fakeDI = new RecursiveArrayIterator($inputPaths);
$filter = new Filter($fakeDI, '/', self::$config, self::$ruleset);

self::$config->local = false;

if ($localFiles === true) {
self::$config->local = true;
}

$this->assertSame($expectedOutput, $this->getFilteredResultsAsArray($filter));

}//end testExcludePatterns()
Expand Down Expand Up @@ -99,6 +107,60 @@ public static function dataExcludePatterns()
'/path/to/src/anything-generic/Main.php',
],
],
'Filter should exclude files without an extension, using unsupported extension and starting with a dot' => [
'inputPaths' => [
'/path/to/src/Main.php',
'/path/to/src/.hiddenfile',
'/path/to/src/NoExtension',
'/path/to/src/UnsupportedExtension.txt',
'/path/to/src/UnsupportedExtension.php.bak',
],
'expectedOutput' => [
'/path/to/src/Main.php',
],
],
'Filter should ignore duplicate files' => [
'inputPaths' => [
__FILE__,
__FILE__,
'/path/to/src/Main.php',
],
'expectedOutput' => [
__FILE__,
'/path/to/src/Main.php',
],
],
'Filter should work for relative exclude patterns' => [
'inputPaths' => [
'src/Main.php',
'src/AnotherDir/File.php',
],
'expectedOutput' => [
'src/Main.php',
],
],

// Uses real directories to test code that calls is_dir().
'Filter should handle directories' => [
'inputPaths' => [
self::getBaseDir().'/src/Generators',
self::getBaseDir().'/src/Standards',
],
'expectedOutput' => [
self::getBaseDir().'/src/Standards',
],
],
'Filter should ignore directories when --local is used' => [
'inputPaths' => [
'src/Main.php',
self::getBaseDir().'/src/Generators',
self::getBaseDir().'/src/Standards',
],
'expectedOutput' => [
'src/Main.php',
],
'localFiles' => true,
],
];

// Allow these tests to work on Windows as well.
Expand Down
6 changes: 5 additions & 1 deletion tests/Core/Filters/Filter/AcceptTest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="AcceptTest" xsi:noNamespaceSchemaLocation="phpcs.xsd">
<description>Ruleset to test the filtering based on exclude patterns.</description>

<!-- Directory pattern. -->
<!-- Directory patterns. -->
<exclude-pattern>*/something/*</exclude-pattern>
<exclude-pattern>*/src/Generators/*</exclude-pattern>
<!-- File pattern. -->
<exclude-pattern>*/Other/Main\.php$</exclude-pattern>

<!-- Relative directory pattern. -->
<exclude-pattern type="relative">/AnotherDir/*</exclude-pattern>

<rule ref="Generic">
<exclude name="Generic.Debug"/>

Expand Down