@@ -20,124 +20,131 @@ composer require rector/mockstan --dev
2020
2121<br >
2222
23- ``` markdown
2423### ExplicitExpectsMockMethodRule
2524
26- Require explicit ` expects() ` usage when setting up mocks to avoid silent stubs.
25+ Require explicit ` expects() ` usage when setting up mocks to avoid silent stubs. This is reuired since PHPUnit 12 to avoid silent stubs.
2726
2827``` yaml
2928rules :
3029 - Rector\Mockstan\Rules\ExplicitExpectsMockMethodRule
3130` ` `
3231
3332` ` ` php
34- // Bad (implicit stubbing)
35- $mock = $this->createMock(Service::class);
36- $mock->method('calculate')->willReturn(10);
33+ $someMock = $this->createMock(Service::class);
34+ $someMock->method('calculate')->willReturn(10);
3735```
3836
3937:x :
4038
4139``` php
42- // Good (explicit expects)
43- $mock = $this->createMock(Service::class);
44- $mock->expects($this->any())->method('calculate')->willReturn(10);
40+ $someMock = $this->createMock(Service::class);
41+ $someMock->expects($this->once())->method('calculate')->willReturn(10);
4542```
4643
44+ :+1 :
45+
4746<br >
4847
4948### ForbiddenClassToMockRule
5049
51- Disallow mocking of forbidden/core classes (e.g. ` \DateTime ` , framework internals ).
50+ Disallow mocking of forbidden/core classes (e.g. ` \DateTime ` , Symfony ` Request ` or ` RequestStack ` , ` Iterable ` , Symfony and Doctine event objects etc. ).
5251
5352``` yaml
5453rules :
5554 - Rector\Mockstan\Rules\ForbiddenClassToMockRule
5655` ` `
5756
5857` ` ` php
59- // Bad
60- $dtMock = $this->createMock(\DateTime::class);
58+ $dateTimeMock = $this->createMock(\DateTime::class);
6159```
6260
6361:x :
6462
6563``` php
66- // Good
67- $dt = new \DateTime();
64+ $dateTime = new \DateTime();
6865```
6966
67+ :+1 :
68+
7069<br >
7170
7271### NoDocumentMockingRule
7372
74- Prevent mocking of document classes (persisted models) — use real instances or factories .
73+ Prevent mocking of Doctrine ODM document classes. Use real instances instead .
7574
7675``` yaml
7776rules :
7877 - Rector\Mockstan\Rules\NoDocumentMockingRule
7978` ` `
8079
8180` ` ` php
82- // Bad
8381$docMock = $this->createMock(App\Document\User::class);
8482```
8583
8684:x :
8785
8886``` php
89- // Good
9087$user = new App\Document\User();
9188```
9289
90+ :+1 :
91+
9392<br >
9493
9594### NoDoubleConsecutiveTestMockRule
9695
97- Avoid creating multiple consecutive mocks in test body that indicate poor test design .
96+ Avoid creating multiple consecutive methods mocks, one for params and other for return. Use single instead .
9897
9998``` yaml
10099rules :
101100 - Rector\Mockstan\Rules\NoDoubleConsecutiveTestMockRule
102101` ` `
103102
104103` ` ` php
105- // Bad
106- $a = $this->createMock(A::class);
107- $b = $this->createMock(B::class);
104+ $someMock = $this->createMock(SomeClass::class);
105+
106+ $someMock->expects($this->once())
107+ ->method('foo')
108+ ->willReturnOnConsecutiveCalls(...)
109+ ->willReturnCallback(...)
108110```
109111
110112:x :
111113
112114``` php
113- // Good — combine setup or use single test-specific fixture
114- $a = $this->createMock(A::class);
115- // configure $a as needed, or refactor test
115+ $someMock = $this->createMock(SomeClass::class);
116+
117+ $someMock->expects($this->once())
118+ ->method('foo')
119+ // handle params in single call
120+ ->willReturnCallback(...)
116121```
117122
123+ :+1 :
124+
118125<br >
119126
120127### NoEntityMockingRule
121128
122- Do not mock entity classes (Doctrine entities); use real entity instances .
129+ Do not mock Doctrine entity classes. Use real object instance instead .
123130
124131``` yaml
125132rules :
126133 - Rector\Mockstan\Rules\NoEntityMockingRule
127134` ` `
128135
129136` ` ` php
130- // Bad
131137$entityMock = $this->createMock(App\Entity\Product::class);
132138```
133139
134140:x :
135141
136142``` php
137- // Good
138143$product = new App\Entity\Product();
139144```
140145
146+ :+1 :
147+
141148<br >
142149
143150### NoMockObjectAndRealObjectPropertyRule
@@ -157,11 +164,9 @@ $this->service = new Service();
157164:x :
158165
159166``` php
160- // Good — keep property consistent or isolate tests
161- $this->service = $this->createMock(Service::class);
167+ $this->someMock = $this->createMock(AnotherService::class);
162168
163- // or
164- $this->service = new Service();
169+ $this->realService = new Service();
165170```
166171
167172:+1 :
@@ -171,62 +176,35 @@ $this->service = new Service();
171176
172177### NoMockOnlyTestRule
173178
174- Detect tests that only create mocks and never assert behavior — require meaningful assertions.
179+ Avoid tests that only create mocks and never assert behavior. Require meaningful assertions with at least once real object to test .
175180
176181``` yaml
177182rules :
178183 - Rector\Mockstan\Rules\NoMockOnlyTestRule
179184` ` `
180185
181186` ` ` php
182- // Bad
183- public function testSomething()
187+ public function testNothing()
184188{
185- $this->createMock(Dependency::class);
186- }
187- ```
189+ $someMock = $this->createMock(Dependency::class);
188190
189- :x :
191+ $someMock->expects($this->once())
192+ ->method('doSomething')
193+ ->willReturn(true);
190194
191- ``` php
192- public function testSomething()
193- {
194- $dep = $this->createMock(Dependency::class);
195- $this->assertInstanceOf(Dependency::class, $dep);
196- }
197- ```
198-
199- :+1 :
200-
201-
202-
203-
204- ### ParamNameToTypeConventionRule
205-
206- Interface must be located in "Contract" or "Contracts" namespace
207-
208- ``` yaml
209- rules :
210- - Rector\Mockstan\Rules\CheckRequiredInterfaceInContractNamespaceRule
211- ` ` `
212-
213- ` ` ` php
214- namespace App\Repository;
215-
216- interface ProductRepositoryInterface
217- {
195+ $this->assertSame($someMock->doSomething());
218196}
219197```
220198
221199:x :
222200
223- <br >
224-
225201``` php
226- namespace App\Contract\Repository;
227-
228- interface ProductRepositoryInterface
202+ public function testSomething()
229203{
204+ $someMock = $this->createMock(Dependency::class);
205+ $realObject = new RealObject($someMock);
206+
207+ $this->assertTrue($realObject->doSomething());
230208}
231209```
232210
0 commit comments