PHPackages                             rector/mockstan - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. rector/mockstan

ActivePhpstan-extension[Testing &amp; Quality](/categories/testing)

rector/mockstan
===============

Static analysis for PHPUnit mocks

1.0.0(2w ago)495.0k↓38.7%MITPHPPHP &gt;=8.3CI passing

Since May 25Pushed 2w agoCompare

[ Source](https://github.com/rectorphp/mockstan)[ Packagist](https://packagist.org/packages/rector/mockstan)[ Fund](https://www.paypal.me/rectorphp)[ GitHub Sponsors](https://github.com/tomasvotruba)[ RSS](/packages/rector-mockstan/feed)WikiDiscussions main Synced 1w ago

READMEChangelogDependencies (13)Versions (4)Used By (0)

PHPUnit Mocks Rules
===================

[](#phpunit-mocks-rules)

[![Downloads](https://camo.githubusercontent.com/2c898b9b7532edcef4f0a1afb4c327f73c30f4b3c6b647ed03f787b0a79c6994/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f726563746f722f6d6f636b7374616e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/rector/mockstan/stats)

- 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
-------

[](#install)

```
composer require rector/mockstan --dev
```

*Note: Make sure you use [`phpstan/extension-installer`](https://github.com/phpstan/extension-installer#usage) to load necessary service configs.*

### AvoidAnyExpectsRule

[](#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

[](#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

[](#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();
```

👍

### NoWithOnStubRule

[](#nowithonstubrule)

Disallow `with()` on stubs (mocks without `expects()`). PHPUnit deprecates `with*()` on test stubs because they silently swallow argument mismatches.

```
rules:
    - Rector\Mockstan\Rules\NoWithOnStubRule
```

```
$someMock = $this->createMock(Service::class);
$someMock->method('calculate')
    ->with(10)
    ->willReturn(20);
```

❌

```
$someMock = $this->createMock(Service::class);
$someMock->expects($this->once())
    ->method('calculate')
    ->with(10)
    ->willReturn(20);
```

👍

### NoDocumentMockingRule

[](#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

[](#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

[](#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

[](#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();
```

👍

### RequireAtLeastOneRule

[](#requireatleastonerule)

Disallow `atLeast(0)` on mock expectations, as it matches any number of calls (including zero) and provides no real verification. Require a value of `1` or higher.

```
rules:
    - Rector\Mockstan\Rules\RequireAtLeastOneRule
```

```
$someMock = $this->createMock(Service::class);
$someMock->expects($this->atLeast(0))
    ->method('calculate')
    ->willReturn(10);
```

❌

```
$someMock = $this->createMock(Service::class);
$someMock->expects($this->atLeast(1))
    ->method('calculate')
    ->willReturn(10);
```

👍

### NoMockOnlyTestRule

[](#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!

###  Health Score

51

—

FairBetter than 95% of packages

Maintenance97

Actively maintained with recent releases

Popularity37

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity50

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

15d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/924196?v=4)[Tomas Votruba](/maintainers/TomasVotruba)[@TomasVotruba](https://github.com/TomasVotruba)

---

Top Contributors

[![TomasVotruba](https://avatars.githubusercontent.com/u/924196?v=4)](https://github.com/TomasVotruba "TomasVotruba (48 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisRector

Code StyleECS

### Embed Badge

![Health badge](/badges/rector-mockstan/health.svg)

```
[![Health](https://phpackages.com/badges/rector-mockstan/health.svg)](https://phpackages.com/packages/rector-mockstan)
```

###  Alternatives

[larastan/larastan

Larastan - Discover bugs in your code without running it. A phpstan/phpstan extension for Laravel

6.4k51.0M7.4k](/packages/larastan-larastan)[phpstan/phpstan-symfony

Symfony Framework extensions and rules for PHPStan

78973.3M2.0k](/packages/phpstan-phpstan-symfony)[phpstan/phpstan-doctrine

Doctrine extensions for PHPStan

66970.7M1.3k](/packages/phpstan-phpstan-doctrine)[shipmonk/dead-code-detector

Dead code detector to find unused PHP code via PHPStan extension. Can automatically remove dead PHP code. Supports libraries like Symfony, Doctrine, PHPUnit etc. Detects dead cycles. Can detect dead code that is tested.

4753.1M82](/packages/shipmonk-dead-code-detector)[spaze/phpstan-disallowed-calls

PHPStan rules to detect disallowed method &amp; function calls, constant, namespace, attribute, property &amp; superglobal usages, with powerful rules to re-allow a call or a usage in places where it should be allowed.

33321.8M504](/packages/spaze-phpstan-disallowed-calls)[mglaman/phpstan-drupal

Drupal extension and rules for PHPStan

20830.6M166](/packages/mglaman-phpstan-drupal)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
