-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Documentation for testing custom sniffs? #1756
Comments
This is how I do it since 3.1.0, phpunit.xml
Verify PHP_Codesniffer works.
Check files match the PHPCS standard.
Set installed standard to MyStandard.
Verify the MyStandard standard is installed.
Run unit tests.
|
I've found it much easier and more flexible to write custom tests rather than using the use PHPUnit\Framework\TestCase;
use PHP_CodeSniffer\Files\LocalFile;
use PHP_CodeSniffer\Ruleset;
use PHP_CodeSniffer\Config;
class DisallowExtractSniffTest extends TestCase {
public function testDisallowExtractSniff() {
$fixtureFile = __DIR__ . '/fixture.php';
$sniffFiles = [__DIR__ . '/../../../StandardName/Sniffs/Extract/DisallowExtractSniff.php'];
$config = new Config();
$ruleset = new Ruleset($config);
$ruleset->registerSniffs($sniffFiles, [], []);
$ruleset->populateTokenListeners();
$phpcsFile = new LocalFile($fixtureFile, $ruleset, $config);
$phpcsFile->process();
$foundErrors = $phpcsFile->getErrors();
$lines = array_keys($foundErrors);
$this->assertEquals([7], $lines);
}
} (More details in my post on writing Sniffs.) |
By extracting the retrieval of the installed standards a third party ruleset/sniff developer, me, can easily create an extension of `AllSniffs` that only runs the sniffs of the ruleset in question. This simple extraction will prevent doing cli magic like `--filter` and allow full configurability in a `phpunit.xml` Adds to squizlabs#1756
From what I can see, the bootstrap and AllSniffs tests can be used for third-party custom sniffs, but there's no documentation on how to actually run these with custom standards (apart from a reference in the 3.1.0 release). Would be very useful to have a wiki page on how to set this up. :)
The text was updated successfully, but these errors were encountered: