-
-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ruleset::expandSniffDirectory(): add test
- Loading branch information
Showing
22 changed files
with
328 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
/** | ||
* Test the Ruleset::expandSniffDirectory() method. | ||
* | ||
* @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl> | ||
* @copyright 2024 PHPCSStandards and contributors | ||
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence | ||
*/ | ||
|
||
namespace PHP_CodeSniffer\Tests\Core\Ruleset; | ||
|
||
use PHP_CodeSniffer\Ruleset; | ||
use PHP_CodeSniffer\Tests\ConfigDouble; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* Test the Ruleset::expandSniffDirectory() method. | ||
* | ||
* @covers \PHP_CodeSniffer\Ruleset::expandSniffDirectory | ||
*/ | ||
final class ExpandSniffDirectoryTest extends TestCase | ||
{ | ||
|
||
|
||
/** | ||
* Test finding sniff files based on a given directory. | ||
* | ||
* This test verifies that: | ||
* - Hidden (sub)directories are ignored, but the given directory is allowed to be within a hidden directory. | ||
* - Hidden files are ignored. | ||
* - Files without a "php" extension are ignored. | ||
* - Files without a "Sniff" suffix in the file name are ignored. | ||
* | ||
* Note: the "[Another]AbstractSniff" files will be found and included in the return value | ||
* from `Ruleset::expandSniffDirectory()`. | ||
* Those are filtered out later in the `Ruleset::registerSniffs()` method. | ||
* | ||
* @return void | ||
*/ | ||
public function testExpandSniffDirectory() | ||
{ | ||
// Set up the ruleset. | ||
$standard = __DIR__.'/ExpandSniffDirectoryTest.xml'; | ||
$config = new ConfigDouble(["--standard=$standard"]); | ||
$ruleset = new Ruleset($config); | ||
|
||
$expectedPathToRuleset = __DIR__.'/Fixtures/DirectoryExpansion/.hiddenAbove/src/MyStandard/ruleset.xml'; | ||
$expectedPathToRuleset = realpath($expectedPathToRuleset); | ||
$this->assertNotFalse($expectedPathToRuleset, 'Ruleset file could not be found'); | ||
$this->assertContains($expectedPathToRuleset, $ruleset->paths, 'Ruleset file not included in the "seen ruleset paths"'); | ||
|
||
/* | ||
* Take note: the expectation includes some "undesirables" related to the convoluted directory structure | ||
* in the "standard" used as a test fixture. | ||
* | ||
* That is okay as (for now) non-standard directory layouts are supported. | ||
* | ||
* This test is not about the standard directory layout. | ||
*/ | ||
|
||
$expectedSniffCodes = [ | ||
'.Sniffs.IncorrectLevelShouldStillBeFound' => 'MyStandard\\Sniffs\\IncorrectLevelShouldStillBeFoundSniff', | ||
'MyStandard.CategoryA.FindMe' => 'MyStandard\\Sniffs\\CategoryA\\FindMeSniff', | ||
'MyStandard.CategoryB.FindMe' => 'MyStandard\\Sniffs\\CategoryB\\FindMeSniff', | ||
'Sniffs.SubDir.IncorrectLevelShouldStillBeFound' => 'MyStandard\\Sniffs\\CategoryA\\SubDir\\IncorrectLevelShouldStillBeFoundSniff', | ||
]; | ||
|
||
// Sort the value to make the tests stable as different OSes will read directories | ||
// in a different order and the order is not relevant for these tests. Just the values. | ||
$actual = $ruleset->sniffCodes; | ||
ksort($actual); | ||
|
||
$this->assertSame($expectedSniffCodes, $actual, 'Registered sniffs do not match expectation'); | ||
|
||
}//end testExpandSniffDirectory() | ||
|
||
|
||
}//end class |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?xml version="1.0"?> | ||
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="ExpandSniffDirectoryTest" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/PHPCSStandards/PHP_CodeSniffer/master/phpcs.xsd"> | ||
|
||
<config name="installed_paths" value="./tests/Core/Ruleset/Fixtures/DirectoryExpansion/.hiddenAbove/src/"/> | ||
|
||
<rule ref="MyStandard"/> | ||
|
||
</ruleset> |
15 changes: 15 additions & 0 deletions
15
.../DirectoryExpansion/.hiddenAbove/src/MyStandard/.hidden/HiddenDirShouldBeIgnoredSniff.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
/** | ||
* Test fixture. | ||
* | ||
* Note: yes, there is a parse error in this file (namespace name is invalid). | ||
* No, that's not a problem. | ||
* | ||
* @see \PHP_CodeSniffer\Tests\Core\Ruleset\ExpandSniffDirectoryTest | ||
*/ | ||
|
||
namespace MyStandard\.hidden; | ||
|
||
use MyStandard\DummySniff; | ||
|
||
final class HiddenDirShouldBeIgnoredSniff extends DummySniff {} |
12 changes: 12 additions & 0 deletions
12
tests/Core/Ruleset/Fixtures/DirectoryExpansion/.hiddenAbove/src/MyStandard/AbstractSniff.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
/** | ||
* Test fixture. | ||
* | ||
* @see \PHP_CodeSniffer\Tests\Core\Ruleset\ExpandSniffDirectoryTest | ||
*/ | ||
|
||
namespace MyStandard\Sniffs; | ||
|
||
use PHP_CodeSniffer\Sniffs\Sniff; | ||
|
||
abstract class AbstractSniff implements Sniff {} |
25 changes: 25 additions & 0 deletions
25
tests/Core/Ruleset/Fixtures/DirectoryExpansion/.hiddenAbove/src/MyStandard/DummySniff.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
/** | ||
* Test fixture. | ||
* | ||
* @see \PHP_CodeSniffer\Tests\Core\Ruleset\ExpandSniffDirectoryTest | ||
*/ | ||
|
||
namespace MyStandard; | ||
|
||
use PHP_CodeSniffer\Files\File; | ||
use PHP_CodeSniffer\Sniffs\Sniff; | ||
|
||
abstract class DummySniff implements Sniff | ||
{ | ||
|
||
public function register() | ||
{ | ||
return [T_WHITESPACE]; | ||
} | ||
|
||
public function process(File $phpcsFile, $stackPtr) | ||
{ | ||
// Do something. | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...oryExpansion/.hiddenAbove/src/MyStandard/Sniffs/.hidden/HiddenDirShouldBeIgnoredSniff.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
/** | ||
* Test fixture. | ||
* | ||
* Note: yes, there is a parse error in this file (namespace name is invalid). | ||
* No, that's not a problem. | ||
* | ||
* @see \PHP_CodeSniffer\Tests\Core\Ruleset\ExpandSniffDirectoryTest | ||
*/ | ||
|
||
namespace MyStandard\Sniffs\.hidden; | ||
|
||
use MyStandard\DummySniff; | ||
|
||
final class HiddenDirShouldBeIgnoredSniff extends DummySniff {} |
12 changes: 12 additions & 0 deletions
12
.../Ruleset/Fixtures/DirectoryExpansion/.hiddenAbove/src/MyStandard/Sniffs/AbstractSniff.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
/** | ||
* Test fixture. | ||
* | ||
* @see \PHP_CodeSniffer\Tests\Core\Ruleset\ExpandSniffDirectoryTest | ||
*/ | ||
|
||
namespace MyStandard\Sniffs; | ||
|
||
use PHP_CodeSniffer\Sniffs\Sniff; | ||
|
||
abstract class AbstractSniff implements Sniff {} |
12 changes: 12 additions & 0 deletions
12
...ures/DirectoryExpansion/.hiddenAbove/src/MyStandard/Sniffs/CategoryA/.HiddenFileSniff.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
/** | ||
* Test fixture. | ||
* | ||
* @see \PHP_CodeSniffer\Tests\Core\Ruleset\ExpandSniffDirectoryTest | ||
*/ | ||
|
||
namespace MyStandard\Sniffs\CategoryA; | ||
|
||
use MyStandard\DummySniff; | ||
|
||
final class HiddenFileSniff extends DummySniff {} |
Empty file.
15 changes: 15 additions & 0 deletions
15
...on/.hiddenAbove/src/MyStandard/Sniffs/CategoryA/.hidden/HiddenDirShouldBeIgnoredSniff.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
/** | ||
* Test fixture. | ||
* | ||
* Note: yes, there is a parse error in this file (namespace name is invalid). | ||
* No, that's not a problem. | ||
* | ||
* @see \PHP_CodeSniffer\Tests\Core\Ruleset\ExpandSniffDirectoryTest | ||
*/ | ||
|
||
namespace MyStandard\Sniffs\CategoryA\.hidden; | ||
|
||
use MyStandard\DummySniff; | ||
|
||
final class HiddenDirShouldBeIgnoredSniff extends DummySniff {} |
12 changes: 12 additions & 0 deletions
12
...ures/DirectoryExpansion/.hiddenAbove/src/MyStandard/Sniffs/CategoryA/DoNotFindMeSniff.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
/** | ||
* Test fixture. | ||
* | ||
* @see \PHP_CodeSniffer\Tests\Core\Ruleset\ExpandSniffDirectoryTest | ||
*/ | ||
|
||
namespace MyStandard\Sniffs\CategoryA; | ||
|
||
use MyStandard\DummySniff; | ||
|
||
final class DoNotFindMeSniff extends DummySniff {} |
12 changes: 12 additions & 0 deletions
12
.../Fixtures/DirectoryExpansion/.hiddenAbove/src/MyStandard/Sniffs/CategoryA/FindMeSniff.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
/** | ||
* Test fixture. | ||
* | ||
* @see \PHP_CodeSniffer\Tests\Core\Ruleset\ExpandSniffDirectoryTest | ||
*/ | ||
|
||
namespace MyStandard\Sniffs\CategoryA; | ||
|
||
use MyStandard\DummySniff; | ||
|
||
final class FindMeSniff extends DummySniff {} |
12 changes: 12 additions & 0 deletions
12
...oryExpansion/.hiddenAbove/src/MyStandard/Sniffs/CategoryA/IncorrectFileExtensionSniff.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
/** | ||
* Test fixture. | ||
* | ||
* @see \PHP_CodeSniffer\Tests\Core\Ruleset\ExpandSniffDirectoryTest | ||
*/ | ||
|
||
namespace MyStandard\Sniffs\CategoryA; | ||
|
||
use MyStandard\DummySniff; | ||
|
||
final class IncorrectFileExtensionSniff extends DummySniff {} |
12 changes: 12 additions & 0 deletions
12
...es/DirectoryExpansion/.hiddenAbove/src/MyStandard/Sniffs/CategoryA/MissingSniffSuffix.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
/** | ||
* Test fixture. | ||
* | ||
* @see \PHP_CodeSniffer\Tests\Core\Ruleset\ExpandSniffDirectoryTest | ||
*/ | ||
|
||
namespace MyStandard\Sniffs\CategoryA; | ||
|
||
use MyStandard\DummySniff; | ||
|
||
final class MissingSniffSuffix extends DummySniff {} |
12 changes: 12 additions & 0 deletions
12
...denAbove/src/MyStandard/Sniffs/CategoryA/Subdir/IncorrectLevelShouldStillBeFoundSniff.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
/** | ||
* Test fixture. | ||
* | ||
* @see \PHP_CodeSniffer\Tests\Core\Ruleset\ExpandSniffDirectoryTest | ||
*/ | ||
|
||
namespace MyStandard\Sniffs\CategoryA\SubDir; | ||
|
||
use MyStandard\DummySniff; | ||
|
||
final class IncorrectLevelShouldStillBeFoundSniff extends DummySniff {} |
12 changes: 12 additions & 0 deletions
12
.../DirectoryExpansion/.hiddenAbove/src/MyStandard/Sniffs/CategoryB/AnotherAbstractSniff.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
/** | ||
* Test fixture. | ||
* | ||
* @see \PHP_CodeSniffer\Tests\Core\Ruleset\ExpandSniffDirectoryTest | ||
*/ | ||
|
||
namespace MyStandard\Sniffs\CategoryB; | ||
|
||
use PHP_CodeSniffer\Sniffs\Sniff; | ||
|
||
abstract class AnotherAbstractSniff implements Sniff {} |
12 changes: 12 additions & 0 deletions
12
.../Fixtures/DirectoryExpansion/.hiddenAbove/src/MyStandard/Sniffs/CategoryB/FindMeSniff.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
/** | ||
* Test fixture. | ||
* | ||
* @see \PHP_CodeSniffer\Tests\Core\Ruleset\ExpandSniffDirectoryTest | ||
*/ | ||
|
||
namespace MyStandard\Sniffs\CategoryB; | ||
|
||
use MyStandard\DummySniff; | ||
|
||
final class FindMeSniff extends DummySniff {} |
12 changes: 12 additions & 0 deletions
12
...ryExpansion/.hiddenAbove/src/MyStandard/Sniffs/CategoryB/IncorrectFileExtensionSniff.php3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
/** | ||
* Test fixture. | ||
* | ||
* @see \PHP_CodeSniffer\Tests\Core\Ruleset\ExpandSniffDirectoryTest | ||
*/ | ||
|
||
namespace MyStandard\Sniffs\CategoryB; | ||
|
||
use MyStandard\DummySniff; | ||
|
||
final class IncorrectFileExtensionSniff extends DummySniff {} |
12 changes: 12 additions & 0 deletions
12
...oryExpansion/.hiddenAbove/src/MyStandard/Sniffs/IncorrectLevelShouldStillBeFoundSniff.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
/** | ||
* Test fixture. | ||
* | ||
* @see \PHP_CodeSniffer\Tests\Core\Ruleset\ExpandSniffDirectoryTest | ||
*/ | ||
|
||
namespace MyStandard\Sniffs; | ||
|
||
use MyStandard\DummySniff; | ||
|
||
final class IncorrectLevelShouldStillBeFoundSniff extends DummySniff {} |
12 changes: 12 additions & 0 deletions
12
...set/Fixtures/DirectoryExpansion/.hiddenAbove/src/MyStandard/Utils/NotInSniffsDirSniff.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
/** | ||
* Test fixture. | ||
* | ||
* @see \PHP_CodeSniffer\Tests\Core\Ruleset\ExpandSniffDirectoryTest | ||
*/ | ||
|
||
namespace MyStandard\Utils; | ||
|
||
use MyStandard\DummySniff; | ||
|
||
final class NotInSniffsDirSniff extends DummySniff {} |
12 changes: 12 additions & 0 deletions
12
...tures/DirectoryExpansion/.hiddenAbove/src/MyStandard/Utils/SubDir/NotInSniffsDirSniff.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
/** | ||
* Test fixture. | ||
* | ||
* @see \PHP_CodeSniffer\Tests\Core\Ruleset\ExpandSniffDirectoryTest | ||
*/ | ||
|
||
namespace MyStandard\Utils\SubDir; | ||
|
||
use MyStandard\DummySniff; | ||
|
||
final class NotInSniffsDirSniff extends DummySniff {} |
4 changes: 4 additions & 0 deletions
4
tests/Core/Ruleset/Fixtures/DirectoryExpansion/.hiddenAbove/src/MyStandard/ruleset.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?xml version="1.0"?> | ||
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="MyStandard" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/PHPCSStandards/PHP_CodeSniffer/master/phpcs.xsd"> | ||
|
||
</ruleset> |