|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Test the Ruleset::expandSniffDirectory() method. |
| 4 | + * |
| 5 | + * @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl> |
| 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::expandSniffDirectory() method. |
| 18 | + * |
| 19 | + * @covers \PHP_CodeSniffer\Ruleset::expandSniffDirectory |
| 20 | + */ |
| 21 | +final class ExpandSniffDirectoryTest extends TestCase |
| 22 | +{ |
| 23 | + |
| 24 | + |
| 25 | + /** |
| 26 | + * Test finding sniff files based on a given directory. |
| 27 | + * |
| 28 | + * This test verifies that: |
| 29 | + * - Hidden (sub)directories are ignored, but the given directory is allowed to be within a hidden directory. |
| 30 | + * - Hidden files are ignored. |
| 31 | + * - Files without a "php" extension are ignored. |
| 32 | + * - Files without a "Sniff" suffix in the file name are ignored. |
| 33 | + * |
| 34 | + * Note: the "[Another]AbstractSniff" files will be found and included in the return value |
| 35 | + * from `Ruleset::expandSniffDirectory()`. |
| 36 | + * Those are filtered out later in the `Ruleset::registerSniffs()` method. |
| 37 | + * |
| 38 | + * @return void |
| 39 | + */ |
| 40 | + public function testExpandSniffDirectory() |
| 41 | + { |
| 42 | + // Set up the ruleset. |
| 43 | + $standard = __DIR__.'/ExpandSniffDirectoryTest.xml'; |
| 44 | + $config = new ConfigDouble(["--standard=$standard"]); |
| 45 | + $ruleset = new Ruleset($config); |
| 46 | + |
| 47 | + $expectedPathToRuleset = __DIR__.'/Fixtures/DirectoryExpansion/.hiddenAbove/src/MyStandard/ruleset.xml'; |
| 48 | + $expectedPathToRuleset = realpath($expectedPathToRuleset); |
| 49 | + $this->assertNotFalse($expectedPathToRuleset, 'Ruleset file could not be found'); |
| 50 | + $this->assertContains($expectedPathToRuleset, $ruleset->paths, 'Ruleset file not included in the "seen ruleset paths"'); |
| 51 | + |
| 52 | + /* |
| 53 | + * Take note: the expectation includes some "undesirables" related to the convoluted directory structure |
| 54 | + * in the "standard" used as a test fixture. |
| 55 | + * |
| 56 | + * That is okay as (for now) non-standard directory layouts are supported. |
| 57 | + * |
| 58 | + * This test is not about the standard directory layout. |
| 59 | + */ |
| 60 | + |
| 61 | + $expectedSniffCodes = [ |
| 62 | + '.Sniffs.IncorrectLevelShouldStillBeFound' => 'MyStandard\\Sniffs\\IncorrectLevelShouldStillBeFoundSniff', |
| 63 | + 'MyStandard.CategoryA.FindMe' => 'MyStandard\\Sniffs\\CategoryA\\FindMeSniff', |
| 64 | + 'MyStandard.CategoryB.FindMe' => 'MyStandard\\Sniffs\\CategoryB\\FindMeSniff', |
| 65 | + 'Sniffs.SubDir.IncorrectLevelShouldStillBeFound' => 'MyStandard\\Sniffs\\CategoryA\\SubDir\\IncorrectLevelShouldStillBeFoundSniff', |
| 66 | + ]; |
| 67 | + |
| 68 | + // Sort the value to make the tests stable as different OSes will read directories |
| 69 | + // in a different order and the order is not relevant for these tests. Just the values. |
| 70 | + $actual = $ruleset->sniffCodes; |
| 71 | + ksort($actual); |
| 72 | + |
| 73 | + $this->assertSame($expectedSniffCodes, $actual, 'Registered sniffs do not match expectation'); |
| 74 | + |
| 75 | + }//end testExpandSniffDirectory() |
| 76 | + |
| 77 | + |
| 78 | +}//end class |
0 commit comments