-
-
Notifications
You must be signed in to change notification settings - Fork 82
Increase test coverage for --sniffs
and --exclude
options
#474
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
Merged
jrfnl
merged 27 commits into
PHPCSStandards:master
from
fredden:feature/tests/config.sniffs-exclude
May 14, 2024
Merged
Changes from 19 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
6bba777
Increase test coverage for --sniffs and --exclude
fredden 46f27de
Merge remote-tracking branch 'upstream/master' into feature/tests/con…
fredden 2829da7
Fix tyop
fredden 61c7bf9
Use capital letters in comments
fredden c73d0b3
Quote fixed strings to avoid confustion with words
fredden 8e2603f
Add full-stops
fredden 3391c09
Rename variable
fredden f14581f
Sort 'use' statements
fredden 7ee42d1
Unwrap foreach; reduce test cases
fredden 13edab3
Name tests
fredden bd314c1
Test an empty string
fredden 1ffe83a
Rename testscase
fredden db76be8
Add another test case
fredden 389f112
Correct indentation
fredden bd249fb
Mark test as not-risky
fredden e92830d
Use more specific return type shape in comment
fredden b6df259
Assert configuration value is set on success
fredden b18272d
Add test case for setting multiple times
fredden 6400c61
Rename class again
fredden 35d8a65
Clarify shape of return array
fredden f7aa3ef
Clarify shape of return array
fredden fd5b8c7
Clarify shape of return array
fredden 801aefa
Correct comment
fredden 81bea03
Set 'position' argument
fredden 9d964ee
Avoid using internal method
fredden a0b31d9
Mark internal method as internal
fredden 70358b0
Revert "Mark internal method as internal"
fredden File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,199 @@ | ||
<?php | ||
/** | ||
* Tests for the \PHP_CodeSniffer\Config --sniffs and --exclude arguments. | ||
* | ||
* @author Dan Wallis <[email protected]> | ||
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence | ||
*/ | ||
|
||
namespace PHP_CodeSniffer\Tests\Core\Config; | ||
|
||
use PHP_CodeSniffer\Tests\ConfigDouble; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* Tests for the \PHP_CodeSniffer\Config --sniffs and --exclude arguments. | ||
* | ||
* @covers \PHP_CodeSniffer\Config::processLongArgument | ||
*/ | ||
final class SniffsExcludeArgsTest 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() | ||
|
||
|
||
/** | ||
* Data provider for testInvalid(). | ||
* | ||
* @see self::testInvalid() | ||
* @return array<array<string>> | ||
*/ | ||
public static function dataInvalidSniffs() | ||
{ | ||
$arguments = [ | ||
'sniffs', | ||
'exclude', | ||
]; | ||
$data = []; | ||
|
||
$messageTemplate = 'ERROR: The specified sniff code "%s" is invalid'.PHP_EOL.PHP_EOL; | ||
|
||
foreach ($arguments as $argument) { | ||
// A standard is not a valid sniff. | ||
fredden marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$data[$argument.'; empty string'] = [ | ||
'argument' => $argument, | ||
'value' => '', | ||
'message' => sprintf($messageTemplate, ''), | ||
]; | ||
|
||
// A standard is not a valid sniff. | ||
$data[$argument.'; standard'] = [ | ||
'argument' => $argument, | ||
'value' => 'Standard', | ||
'message' => sprintf($messageTemplate, 'Standard'), | ||
]; | ||
|
||
// A category is not a valid sniff. | ||
$data[$argument.'; category'] = [ | ||
'argument' => $argument, | ||
'value' => 'Standard.Category', | ||
'message' => sprintf($messageTemplate, 'Standard.Category'), | ||
]; | ||
|
||
// An error-code is not a valid sniff. | ||
$data[$argument.'; error-code'] = [ | ||
'argument' => $argument, | ||
'value' => 'Standard.Category', | ||
'message' => sprintf($messageTemplate, 'Standard.Category'), | ||
]; | ||
|
||
// Only the first error is reported. | ||
$data[$argument.'; two errors'] = [ | ||
'argument' => $argument, | ||
'value' => 'StandardOne,StandardTwo', | ||
'message' => sprintf($messageTemplate, 'StandardOne'), | ||
]; | ||
$data[$argument.'; valid followed by invalid'] = [ | ||
'argument' => $argument, | ||
'value' => 'StandardOne.Category.Sniff,StandardTwo.Category', | ||
'message' => sprintf($messageTemplate, 'StandardTwo.Category'), | ||
]; | ||
}//end foreach | ||
|
||
return $data; | ||
|
||
}//end dataInvalidSniffs() | ||
|
||
|
||
/** | ||
* Ensure that the valid data does not throw an exception, and the value is stored. | ||
* | ||
* @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); | ||
|
||
$this->assertSame(explode(',', $value), $config->$argument); | ||
|
||
}//end testValid() | ||
|
||
|
||
/** | ||
* Data provider for testValid(). | ||
* | ||
* @see self::testValid() | ||
* @return array<array<string>> | ||
fredden marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
public static function dataValidSniffs() | ||
{ | ||
$arguments = [ | ||
'sniffs', | ||
'exclude', | ||
]; | ||
$data = []; | ||
|
||
foreach ($arguments as $argument) { | ||
$data[$argument.'; one valid sniff'] = [ | ||
'argument' => $argument, | ||
'value' => 'Standard.Category.Sniff', | ||
]; | ||
$data[$argument.'; two valid sniffs'] = [ | ||
'argument' => $argument, | ||
'value' => 'StandardOne.Category.Sniff,StandardTwo.Category.Sniff', | ||
]; | ||
} | ||
|
||
return $data; | ||
|
||
}//end dataValidSniffs() | ||
|
||
|
||
/** | ||
* Ensure that only the first argument is processed and others are ignored. | ||
* | ||
* @param string $argument 'sniffs' or 'exclude'. | ||
* | ||
* @return void | ||
* @dataProvider dataOnlySetOnce | ||
*/ | ||
public function testOnlySetOnce($argument) | ||
{ | ||
$config = new ConfigDouble(); | ||
$config->processLongArgument($argument.'=StandardOne.Category.Sniff', 0); | ||
$config->processLongArgument($argument.'=StandardTwo.Category.Sniff', 0); | ||
$config->processLongArgument($argument.'=Standard.AnotherCategory.Sniff', 0); | ||
jrfnl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
$this->assertSame(['StandardOne.Category.Sniff'], $config->$argument); | ||
|
||
}//end testOnlySetOnce() | ||
|
||
|
||
/** | ||
* Data provider for testOnlySetOnce(). | ||
* | ||
* @return string[] | ||
fredden marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
public static function dataOnlySetOnce() | ||
{ | ||
return [ | ||
'sniffs' => ['sniffs'], | ||
'exclude' => ['exclude'], | ||
]; | ||
|
||
}//end dataOnlySetOnce() | ||
|
||
|
||
}//end class |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.