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

Documentation for testing custom sniffs? #1756

Open
rmccue opened this issue Nov 22, 2017 · 2 comments
Open

Documentation for testing custom sniffs? #1756

rmccue opened this issue Nov 22, 2017 · 2 comments

Comments

@rmccue
Copy link
Contributor

rmccue commented Nov 22, 2017

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. :)

@louisl
Copy link

louisl commented Dec 13, 2017

This is how I do it since 3.1.0,

phpunit.xml

<phpunit beStrictAboutTestsThatDoNotTestAnything="false" bootstrap="./vendor/squizlabs/php_codesniffer/tests/bootstrap.php">
    <testsuites>
        <testsuite name="Test Suite">
            <file>./vendor/squizlabs/php_codesniffer/tests/AllTests.php</file>
        </testsuite>
    </testsuites>
    <logging>
        <log type="coverage-clover" target="./build/logs/coverage/clover/clover.xml"/>
        <log type="coverage-html" target="./build/logs/coverage/html/"/>
    </logging>
    <filter>
        <whitelist>
            <directory suffix=".php">./MyStandard</directory>
        </whitelist>
    </filter>
</phpunit>

cd /Path/To/YourStandardProjectFolder

composer install squizlabs/php_codesniffer
composer install phpunit/phpunit
composer install satooshi/php-coveralls

Verify PHP_Codesniffer works.

./vendor/bin/phpcs -i

Check files match the PHPCS standard.

./vendor/bin/phpcs --ignore=*/Tests/* ./MyStandard/ --standard=./vendor/squizlabs/php_codesniffer/phpcs.xml.dist

Set installed standard to MyStandard.

./vendor/bin/phpcs --config-set installed_paths /Path/To/YourStandardProjectFolder/MyStandard

Verify the MyStandard standard is installed.

./vendor/bin/phpcs -i

Run unit tests.

./vendor/bin/phpunit --debug --filter MyStandard

@sirbrillig
Copy link

sirbrillig commented Dec 15, 2017

I've found it much easier and more flexible to write custom tests rather than using the AbstractSniffUnitTest. Something like the following (the boilerplate can be abstracted into a base test case or a helper).

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.)

timoschinkel added a commit to timoschinkel/PHP_CodeSniffer that referenced this issue Feb 5, 2018
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants