When I wrote my first Sniff 4 years ago I wanted to test it. I expected testing class, that would register sniff, provide ugly code and compare it to fixed one. So I started to explore PHP_CodeSniffer looking for such feature. Found one class, second class, warnings, errors, uff and after 10th error, I closed it.
When I wrote my first Fixer, the story was a bit shorter but very similar. No wonder people don't test when the entry barrier is so huge.
Since I use both of them and I want to motivate people to write their own sniffs and fixers, I turned this barrier to just 5 short steps for both of them.
Imagine you have a LowerBoolConstantsFixer
that fixes all uppercase bool constants to lowercase ones:
-$value = TRUE;
+$value = true;
And nothing more. How do we take this test case to PHPUnit? That is what ECS Tester package will help us with.
composer require symplify/easy-coding-standard-tester --dev
# /tests/Fixer/LowerBoolConstantsFixer/config.yml
services:
Your\CodingStandard\LowerBoolConstantsFixer: ~
(Checker is a group name for sniff and fixer, nothing more.)
Create a test case that extends Symplify\EasyCodingStandardTester\Testing\AbstractCheckerTestCase
class.
<?php declare(strict_types=1);
namespace Your\CodingStandard\Tests\LowerBoolConstantsFixer;
use Symplify\EasyCodingStandardTester\Testing\AbstractCheckerTestCase;
final class LowerBoolConstantsFixerTest extends AbstractCheckerTestCase
{
// ...
}
And provide the config above in provideConfig()
method.
__DIR__ . '/config.yml'
.__DIR__ . '/configured-config.yml'
. <?php declare(strict_types=1);
namespace Your\CodingStandard\Tests\LowerBoolConstantsFixer;
use Symplify\EasyCodingStandardTester\Testing\AbstractCheckerTestCase;
final class LowerBoolConstantsFixerTest extends AbstractCheckerTestCase
{
// ...
+ protected function provideConfig(): string
+ {
+ return __DIR__ . '/config.yml';
+ }
}
You can make use of 3 testing methods:
doTestCorrectFile($correctFile)
- the file should not be affected by this checkerdoTestWrongToFixedFile($wrongFile, $fixedFile)
- classic before/after testingdoTestWrongFile($wrongFile)
- only for sniff - it doesn't fix, just reports<?php declare(strict_types=1);
namespace Your\CodingStandard\Tests\LowerBoolConstantsFixer;
use Symplify\EasyCodingStandardTester\Testing\AbstractCheckerTestCase;
final class LowerBoolConstantsFixerTest extends AbstractCheckerTestCase
{
public function test(): void
{
$this->doTestCorrectFile(__DIR__ . '/correct/correct.php.inc');
$this->doTestWrongToFixedFile(
__DIR__ . '/wrong/wrong.php.inc',
__DIR__ . '/fixed/fixed.php.inc'
);
}
protected function provideConfig(): string
{
return __DIR__ . '/config.yml';
}
}
This part you enjoy the most because your job is to break the checker... well, at least verify it behaves as you want it to behave.
What should it skip? Well, since NULL
/ null
is not a bool value...
// correct/correct.php.inc
$value = NULL;
$value = null;
I guess you already know the before/after part:
// wrong/wrong.php.inc
$value = TRUE;
$value = FALSE;
// fixed/fixed.php.inc
$value = true;
$value = false;
That's it!
Now you know all you need to be able to test any fixer or sniff. But if you want to know more, check the ECS Tester README.
Enjoy simple testing!
Do you learn from my contents or use open-souce packages like Rector every day?
Consider supporting it on GitHub Sponsors.
I'd really appreciate it!