| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
This extension provides following features:
It also contains this strict framework-specific rules (can be enabled separately):
If you need to configure the mock even after you assign it to a property or return it from a method, you should add \PHPUnit\Framework\MockObject\MockObject to the type:
private function createFooMock(): Foo&\PHPUnit\Framework\MockObject\MockObject
{
return $this->createMock(Foo::class);
}
public function testSomething(): void
{
$fooMock = $this->createFooMock();
$fooMock->method('doFoo')->will($this->returnValue('test'));
$fooMock->doFoo();
}If you cannot use native intersection types yet, you can use PHPDoc instead.
/**
* @return Foo&\PHPUnit\Framework\MockObject\MockObject
*/
private function createFooMock(): Foo
{
return $this->createMock(Foo::class);
}Please note that the correct syntax for intersection types is Foo&\PHPUnit\Framework\MockObject\MockObject. Foo|\PHPUnit\Framework\MockObject\MockObject is also supported, but only for ecosystem and legacy reasons.
If the mock is fully configured and only the methods of the mocked class are supposed to be called on the value, it's fine to typehint only the mocked class:
private Foo $foo;
protected function setUp(): void
{
$fooMock = $this->createMock(Foo::class);
$fooMock->method('doFoo')->will($this->returnValue('test'));
$this->foo = $fooMock;
}
public function testSomething(): void
{
$this->foo->doFoo();
// $this->foo->method() and expects() can no longer be called
}To use this extension, require it in Composer:
composer require --dev phpstan/phpstan-phpunit
If you also install phpstan/extension-installer then you're all set!
Manual installationIf you don't want to use phpstan/extension-installer, include extension.neon in your project's PHPStan config:
includes:
- vendor/phpstan/phpstan-phpunit/extension.neon
To perform framework-specific checks, include also this file:
- vendor/phpstan/phpstan-phpunit/rules.neon
| Back | FazBrowse Home | New Git URL |