Skip to content
Merged
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
78 changes: 78 additions & 0 deletions tests/Core/Ruleset/ExpandSniffDirectoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php
/**
* Test the Ruleset::expandSniffDirectory() method.
*
* @author Juliette Reinders Folmer <[email protected]>
* @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
8 changes: 8 additions & 0 deletions tests/Core/Ruleset/ExpandSniffDirectoryTest.xml
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>
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 {}
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 {}
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.
}
}
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 {}
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 {}
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 {}
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 {}
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 {}
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 {}
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 {}
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 {}
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 {}
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 {}
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 {}
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 {}
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 {}
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 {}
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 {}
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>
Loading