Skip to content

Commit

Permalink
Increase test coverage for --sniffs and --exclude
Browse files Browse the repository at this point in the history
  • Loading branch information
fredden committed Apr 30, 2024
1 parent 6119828 commit 6bba777
Showing 1 changed file with 140 additions and 0 deletions.
140 changes: 140 additions & 0 deletions tests/Core/Config/SniffListTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<?php
/**
* Tests for the \PHP_CodeSniffer\Config --sniffs and --exclude arguments.
*
* @author Dan Wallis <dan@wallis.nz>
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/

namespace PHP_CodeSniffer\Tests\Core\Config;

use PHPUnit\Framework\TestCase;
use PHP_CodeSniffer\Tests\ConfigDouble;

/**
* Tests for the \PHP_CodeSniffer\Config --sniffs and --exclude arguments.
*
* @covers \PHP_CodeSniffer\Config::processLongArgument
*/
final class SniffListTest extends TestCase
{


/**
* Ensure that the expected error message is returned for invalid arguments
*
* @param string $argument sniffs or exclude
* @param string $value list of sniffs to include / exclude
* @param string $message expected error message text
*
* @return void
* @dataProvider dataInvalidSniffs
*/
public function testInvalid($argument, $value, $message)
{
$config = new ConfigDouble();
$exception = 'PHP_CodeSniffer\Exceptions\DeepExitException';

if (method_exists($this, 'expectException') === true) {
// PHPUnit 5+.
$this->expectException($exception);
$this->expectExceptionMessage($message);
} else {
// PHPUnit 4.
$this->setExpectedException($exception, $message);
}

$config->processLongArgument($argument.'='.$value, 0);

}//end testInvalid()


/**
* Date provider for testInvalid()
*
* @see self::testInvalid()
* @return array
*/
public static function dataInvalidSniffs()
{
$arguments = [
'sniffs',
'exclude',
];
$result = [];

$sniffs = [];

$types = [
'Standard',
'Standard.Category',
'Standard.Category.Sniff.Code',
];
foreach ($types as $value) {
$sniffs[$value] = $value;
$sniffs['Standard.Category.Sniff,B'.$value] = 'B'.$value;
foreach ($types as $extra) {
$sniffs['A'.$value.',B'.$extra] = 'A'.$value;
}
}

$messageTemplate = 'ERROR: The specified sniff code "%s" is invalid'.PHP_EOL.PHP_EOL;
foreach ($arguments as $argument) {
foreach ($sniffs as $input => $output) {
$result[] = [
'argument' => $argument,
'value' => $input,
'message' => sprintf($messageTemplate, $output),
];
}
}

return $result;

}//end dataInvalidSniffs()


/**
* Ensure that the valid data does not throw an exception
*
* @param string $argument sniffs or exclude
* @param string $value list of sniffs to include or exclude
*
* @return void
* @dataProvider dataValidSniffs
*/
public function testValid($argument, $value)
{
$config = new ConfigDouble();
$config->processLongArgument($argument.'='.$value, 0);

}//end testValid()


/**
* Data provider for testValid()
*
* @see self::testValid()
* @return array
*/
public static function dataValidSniffs()
{
$arguments = [
'sniffs',
'exclude',
];
$result = [];

foreach ($arguments as $argument) {
$result[] = [
'argument' => $argument,
'value' => 'Standard.Category.Sniff',
];
}

return $result;

}//end dataValidSniffs()


}//end class

0 comments on commit 6bba777

Please sign in to comment.