rector / mockstan
Static analysis for PHPUnit mocks
Fund package maintenance!
tomasvotruba
www.paypal.me/rectorphp
Installs: 1 738
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 0
Forks: 0
Open Issues: 0
Type:phpstan-extension
pkg:composer/rector/mockstan
Requires
- php: >=8.3
- phpstan/phpstan: ^2.1.38
Requires (Dev)
- nikic/php-parser: ^5.7
- phpstan/extension-installer: ^1.4
- phpunit/phpunit: ^12.5
- rector/jack: ^0.5
- rector/rector: ^2.3.6
- shipmonk/composer-dependency-analyser: ^1.8
- symplify/easy-coding-standard: ^13.0.4
- symplify/phpstan-extensions: ^12.0
- tomasvotruba/class-leak: ^2.1
- tomasvotruba/type-coverage: ^2.1
- tomasvotruba/unused-public: ^2.2
- tracy/tracy: ^2.11
This package is auto-updated.
Last update: 2026-02-08 21:53:30 UTC
README
- Do you use extensive mocking in your PHPUnit tests?
- Do you want to keep your tests clean, maintainable and avoid upgrade hell in the future?
- Do you want to have test that actually test somthing?
This set is for you!
Install
composer require rector/mockstan --dev
Note: Make sure you use phpstan/extension-installer to load necessary service configs.
AvoidAnyExpectsRule
Disallow usage of any() expectation in mocks to ensure that all mock interactions are explicitly defined and verified.
rules: - Rector\Mockstan\Rules\AvoidAnyExpectsRule
$someMock = $this->createMock(Service::class); $someMock->expects($this->any()) ->method('calculate') ->willReturn(10);
❌
$someMock = $this->createMock(Service::class); $someMock->expects($this->once()) ->method('calculate') ->willReturn(10);
👍
ExplicitExpectsMockMethodRule
Require explicit expects() usage when setting up mocks to avoid silent stubs. This is reuired since PHPUnit 12 to avoid silent stubs.
rules: - Rector\Mockstan\Rules\ExplicitExpectsMockMethodRule
$someMock = $this->createMock(Service::class); $someMock->method('calculate')->willReturn(10);
❌
$someMock = $this->createMock(Service::class); $someMock->expects($this->once())->method('calculate')->willReturn(10);
👍
ForbiddenClassToMockRule
Disallow mocking of forbidden/core classes (e.g. \DateTime, Symfony Request or RequestStack, Iterable, Symfony and Doctine event objects etc.).
rules: - Rector\Mockstan\Rules\ForbiddenClassToMockRule
$dateTimeMock = $this->createMock(\DateTime::class);
❌
$dateTime = new \DateTime();
👍
NoDocumentMockingRule
Prevent mocking of Doctrine ODM document classes. Use real instances instead.
rules: - Rector\Mockstan\Rules\NoDocumentMockingRule
$docMock = $this->createMock(App\Document\User::class);
❌
$user = new App\Document\User();
👍
NoDoubleConsecutiveTestMockRule
Avoid creating multiple consecutive methods mocks, one for params and other for return. Use single instead.
rules: - Rector\Mockstan\Rules\NoDoubleConsecutiveTestMockRule
$someMock = $this->createMock(SomeClass::class); $someMock->expects($this->once()) ->method('foo') ->willReturnOnConsecutiveCalls(...) ->willReturnCallback(...)
❌
$someMock = $this->createMock(SomeClass::class); $someMock->expects($this->once()) ->method('foo') // handle params in single call ->willReturnCallback(...)
👍
NoEntityMockingRule
Do not mock Doctrine entity classes. Use real object instance instead.
rules: - Rector\Mockstan\Rules\NoEntityMockingRule
$entityMock = $this->createMock(App\Entity\Product::class);
❌
$product = new App\Entity\Product();
👍
NoMockObjectAndRealObjectPropertyRule
Disallow assigning a mock to a property while another test uses the real object on the same property.
rules: - Rector\Mockstan\Rules\NoMockObjectAndRealObjectPropertyRule
$this->service = $this->createMock(Service::class); $this->service = new Service();
❌
$this->someMock = $this->createMock(AnotherService::class); $this->realService = new Service();
👍
NoMockOnlyTestRule
Avoid tests that only create mocks and never assert behavior. Require meaningful assertions with at least once real object to test.
rules: - Rector\Mockstan\Rules\NoMockOnlyTestRule
public function testNothing() { $someMock = $this->createMock(Dependency::class); $someMock->expects($this->once()) ->method('doSomething') ->willReturn(true); $this->assertSame($someMock->doSomething()); }
❌
public function testSomething() { $someMock = $this->createMock(Dependency::class); $realObject = new RealObject($someMock); $this->assertTrue($realObject->doSomething()); }
👍
Happy coding!