phpstan / phpstan-phpunit
PHPUnit extensions and rules for PHPStan
Installs: 60 870 771
Dependents: 6 449
Suggesters: 7
Security: 0
Stars: 465
Watchers: 8
Forks: 46
Open Issues: 36
Type:phpstan-extension
- 2.0.x-dev
- 2.0.1
- 2.0.0
- 1.4.x-dev
- 1.4.1
- 1.4.0
- 1.3.x-dev
- 1.3.16
- 1.3.15
- 1.3.14
- 1.3.13
- 1.3.12
- 1.3.11
- 1.3.10
- 1.3.9
- 1.3.8
- 1.3.7
- 1.3.6
- 1.3.5
- 1.3.4
- 1.3.3
- 1.3.2
- 1.3.1
- 1.3.0
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.x-dev
- 1.1.3
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.0
- 0.12.22
- 0.12.21
- 0.12.20
- 0.12.19
- 0.12.18
- 0.12.17
- 0.12.16
- 0.12.15
- 0.12.14
- 0.12.13
- 0.12.12
- 0.12.11
- 0.12.10
- 0.12.9
- 0.12.8
- 0.12.7
- 0.12.6
- 0.12.5
- 0.12.4
- 0.12.3
- 0.12.2
- 0.12.1
- 0.12.0
- 0.11.2
- 0.11.1
- 0.11
- 0.10
- 0.9.4
- 0.9.3
- 0.9.2
- 0.9.1
- 0.9
- dev-renovate/major-root-composer
- dev-renovate/major-github-actions
This package is auto-updated.
Last update: 2024-11-12 12:48:17 UTC
README
This extension provides following features:
createMock()
,getMockForAbstractClass()
andgetMockFromWsdl()
methods return an intersection type (see the detailed explanation of intersection types) of the mock object and the mocked class so that both methods from the mock object (likeexpects
) and from the mocked class are available on the object.getMock()
called onMockBuilder
is also supported.- Interprets
Foo|PHPUnit_Framework_MockObject_MockObject
in phpDoc so that it results in an intersection type instead of a union type. - Defines early terminating method calls for the
PHPUnit\Framework\TestCase
class to prevent undefined variable errors. - Specifies types of expressions passed to various
assert
methods likeassertInstanceOf
,assertTrue
,assertInternalType
etc. - Combined with PHPStan's level 4, it points out always-true and always-false asserts like
assertTrue(true)
etc.
It also contains this strict framework-specific rules (can be enabled separately):
- Check that you are not using
assertSame()
withtrue
as expected value.assertTrue()
should be used instead. - Check that you are not using
assertSame()
withfalse
as expected value.assertFalse()
should be used instead. - Check that you are not using
assertSame()
withnull
as expected value.assertNull()
should be used instead. - Check that you are not using
assertSame()
withcount($variable)
as second parameter.assertCount($variable)
should be used instead.
How to document mock objects in phpDocs?
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 phpDoc:
/** * @return Foo&PHPUnit_Framework_MockObject_MockObject */ private function createFooMock() { return $this->createMock(Foo::class); } public function testSomething() { $fooMock = $this->createFooMock(); $fooMock->method('doFoo')->will($this->returnValue('test')); $fooMock->doFoo(); }
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:
/** @var Foo */ private $foo; protected function setUp() { $fooMock = $this->createMock(Foo::class); $fooMock->method('doFoo')->will($this->returnValue('test')); $this->foo = $fooMock; } public function testSomething() { $this->foo->doFoo(); // $this->foo->method() and expects() can no longer be called }
Installation
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 installation
If 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