forknetwork / strict-phpunit
A simple PHPUnit extension that disallows unexpected method calls.
Installs: 29 469
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 2
Forks: 0
Open Issues: 0
README
A simple PHPUnit extension that disallows unexpected method calls.
For PHPUnit 7.1 and up.
Installation
composer require forknetwork/strict-phpunit ^2.0
How to use
The StrictTestTrait
can be used by any class that extends from TestCase
of PHPUnit. This trait can be added automatically using the PHP Code Sniffer.
Before
use PHPUnit\Framework\TestCase; class YourTest extends TestCase { // ... }
After
use ForkNetwork\StrictPHPUnit\StrictTestTrait; use PHPUnit\Framework\TestCase; class YourTest extends TestCase { use StrictTestTrait; // ... }
Overwrite strict mock generation
By default all mocks will be strict, but in some cases that won't work. For example, PHPUnit doesn't handle the destruct method for mocks well. In those cases you might want to enable the value generation again.
Before
private function createFooMock() { return $this->createMock('\Bar\Foo'); }
After
private function createFooMock() { return $this->getMockBuilder('\Bar\Foo') ->enableAutoReturnValueGeneration(); ->getMock(); }
Another option would to remove the trait from the class and manually add ->disableAutoReturnValueGeneration()
onto the mocks your want strict.
Optional: PHP code sniffer and fixer
This library comes with a code sniff with auto-fixer. Just include the reference in your phpcs.xml(.dist)
file and run the code sniffer as usual.
<rule ref="./vendor/forknetwork/strict-phpunit/src/Standards/ForkNetwork/ruleset.xml"/>
By default it will sniff all classes that extend from TestCase
(PHPUnit). You can change which extended class it should sniff by adding this to your configuration.
<rule ref="ForkNetwork.PHPUnit.StrictUnitTest"> <properties> <property name="extendedClasses" type="array" value="TestCase,ExtendedTestCase,AnotherCustomTestCase"/> </properties> </rule>
Please note that this sniff only ensures the trait and use statements are added. It does not sort or pay attention to the rest of your code style.
Known limitations
The current implementation of the sniff doesn't support the following situations:
- Comma separated use trait statements (e.g.
use StrictUnitTrait, FooTrait;
) - FQCN or partial use trait statements (e.g.
use Traits\StrictUnitTest;
) - Running the sniff unit tests in PHPUnit 8 or higher is not supported.