Skip to content
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

Ruleset::getIgnorePatterns(): add tests #705

Merged
merged 3 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
111 changes: 111 additions & 0 deletions tests/Core/Ruleset/GetIgnorePatternsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php
/**
* Test the Ruleset::getIgnorePatterns() 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::getIgnorePatterns() method.
*
* @covers \PHP_CodeSniffer\Ruleset::getIgnorePatterns
*/
final class GetIgnorePatternsTest extends TestCase
{

/**
* The Ruleset object.
*
* @var \PHP_CodeSniffer\Ruleset
*/
private static $ruleset;


/**
* Initialize the config and ruleset objects for this test.
*
* @beforeClass
*
* @return void
*/
public static function initializeConfigAndRuleset()
{
// Set up the ruleset.
$standard = __DIR__."/GetIgnorePatternsTest.xml";
$config = new ConfigDouble(["--standard=$standard"]);
self::$ruleset = new Ruleset($config);

}//end initializeConfigAndRuleset()


/**
* Test retrieving ignore patterns.
*
* @param string|null $listener The listener to get patterns for or null for all patterns.
* @param array<string, string|array<string, string>> $expected The expected function output.
*
* @dataProvider dataGetIgnorePatterns
*
* @return void
*/
public function testGetIgnorePatterns($listener, $expected)
{
$this->assertSame($expected, self::$ruleset->getIgnorePatterns($listener));

}//end testGetIgnorePatterns()


/**
* Data provider.
*
* @see self::testGetIgnorePatterns()
*
* @return array<string, array<string, string|array<string, string|array<string, string>>|null>>
*/
public static function dataGetIgnorePatterns()
{
return [
'All ignore patterns' => [
'listener' => null,
'expected' => [
'PSR1.Classes.ClassDeclaration' => [
'./src/*/file.php' => 'absolute',
'./bin/' => 'absolute',
],
'Generic.Formatting.SpaceAfterCast' => [
'./src/*/test\\.php$' => 'absolute',
],
'./tests/' => 'absolute',
'./vendor/*' => 'absolute',
'*/node-modules/*' => 'absolute',
],
],
'Ignore patterns for PSR1.Classes.ClassDeclaration' => [
'listener' => 'PSR1.Classes.ClassDeclaration',
'expected' => [
'./src/*/file.php' => 'absolute',
'./bin/' => 'absolute',
],
],
'Ignore patterns for Generic.Formatting.SpaceAfterCast' => [
'listener' => 'Generic.Formatting.SpaceAfterCast',
'expected' => ['./src/*/test\\.php$' => 'absolute'],
],
'Ignore patterns for sniff without ignore patterns' => [
'listener' => 'PSR1.Files.SideEffects',
'expected' => [],
],
];

}//end dataGetIgnorePatterns()


}//end class
19 changes: 19 additions & 0 deletions tests/Core/Ruleset/GetIgnorePatternsTest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0"?>
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="GetIgnorePatternsTest" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/PHPCSStandards/PHP_CodeSniffer/master/phpcs.xsd">

<exclude-pattern>./tests/</exclude-pattern>
<exclude-pattern>./vendor/*</exclude-pattern>
<exclude-pattern>*/node-modules/*</exclude-pattern>

<rule ref="PSR1"/>

<rule ref="PSR1.Classes.ClassDeclaration">
<exclude-pattern>./src/*/file.php</exclude-pattern>
<exclude-pattern>./bin/</exclude-pattern>
</rule>

<rule ref="Generic.Formatting.SpaceAfterCast">
<exclude-pattern>./src/*/test\.php$</exclude-pattern>
</rule>

</ruleset>