File tree Expand file tree Collapse file tree 4 files changed +153
-0
lines changed
rules-tests/PHPUnit120/Rector/Class_/AllowMockObjectsWithoutExpectationsAttributeRector
rules/PHPUnit120/Rector/Class_ Expand file tree Collapse file tree 4 files changed +153
-0
lines changed Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Rector \PHPUnit \Tests \PHPUnit120 \Rector \Class_ \AllowMockObjectsWithoutExpectationsAttributeRector \Fixture ;
4+
5+ use PHPUnit \Framework \TestCase ;
6+
7+ final class AddToSetupWhenNoExpects extends TestCase
8+ {
9+ private \PHPUnit \Framework \MockObject \MockObject $ someMock ;
10+
11+ protected function setUp (): void
12+ {
13+ $ this ->someMock = $ this ->createMock (\stdClass::class);
14+ $ this ->someMock ->method ('some ' )->willReturn (true );
15+ }
16+
17+ public function testOne ()
18+ {
19+ }
20+
21+ public function testTwo ()
22+ {
23+ }
24+ }
25+
26+ ?>
27+ -----
28+ <?php
29+
30+ namespace Rector \PHPUnit \Tests \PHPUnit120 \Rector \Class_ \AllowMockObjectsWithoutExpectationsAttributeRector \Fixture ;
31+
32+ use PHPUnit \Framework \TestCase ;
33+
34+ #[\PHPUnit \Framework \Attributes \AllowMockObjectsWithoutExpectations]
35+ final class AddToSetupWhenNoExpects extends TestCase
36+ {
37+ private \PHPUnit \Framework \MockObject \MockObject $ someMock ;
38+
39+ protected function setUp (): void
40+ {
41+ $ this ->someMock = $ this ->createMock (\stdClass::class);
42+ $ this ->someMock ->method ('some ' )->willReturn (true );
43+ }
44+
45+ public function testOne ()
46+ {
47+ }
48+
49+ public function testTwo ()
50+ {
51+ }
52+ }
53+
54+ ?>
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Rector \PHPUnit \Tests \PHPUnit120 \Rector \Class_ \AllowMockObjectsWithoutExpectationsAttributeRector \Fixture ;
4+
5+ use PHPUnit \Framework \TestCase ;
6+ use Rector \PHPUnit \Tests \PHPUnit120 \Rector \Class_ \AllowMockObjectsWithoutExpectationsAttributeRector \Source \SomeFinalClass ;
7+
8+ final class FinalPropertyAsWell extends TestCase
9+ {
10+ private \PHPUnit \Framework \MockObject \MockObject $ someMock ;
11+
12+ protected function setUp (): void
13+ {
14+ $ this ->someMock = $ this ->createMock (SomeFinalClass::class);
15+ $ this ->someMock ->method ('some ' )->willReturn (true );
16+ }
17+
18+ public function testOne ()
19+ {
20+ }
21+
22+ public function testTwo ()
23+ {
24+ }
25+ }
26+
27+ ?>
28+ -----
29+ <?php
30+
31+ namespace Rector \PHPUnit \Tests \PHPUnit120 \Rector \Class_ \AllowMockObjectsWithoutExpectationsAttributeRector \Fixture ;
32+
33+ use PHPUnit \Framework \TestCase ;
34+ use Rector \PHPUnit \Tests \PHPUnit120 \Rector \Class_ \AllowMockObjectsWithoutExpectationsAttributeRector \Source \SomeFinalClass ;
35+
36+ #[\PHPUnit \Framework \Attributes \AllowMockObjectsWithoutExpectations]
37+ final class FinalPropertyAsWell extends TestCase
38+ {
39+ private \PHPUnit \Framework \MockObject \MockObject $ someMock ;
40+
41+ protected function setUp (): void
42+ {
43+ $ this ->someMock = $ this ->createMock (SomeFinalClass::class);
44+ $ this ->someMock ->method ('some ' )->willReturn (true );
45+ }
46+
47+ public function testOne ()
48+ {
49+ }
50+
51+ public function testTwo ()
52+ {
53+ }
54+ }
55+
56+ ?>
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Rector \PHPUnit \Tests \PHPUnit120 \Rector \Class_ \AllowMockObjectsWithoutExpectationsAttributeRector \Source ;
4+
5+ final class SomeFinalClass
6+ {
7+
8+ }
Original file line number Diff line number Diff line change @@ -86,6 +86,16 @@ public function refactor(Node $node): ?Class_
8686 }
8787 }
8888
89+ // or find a ->method() calls on a setUp() mocked property
90+ $ hasAnyMethodInSetup = $ this ->isMissingExpectsOnMockObjectMethodCallInSetUp ($ node );
91+ if ($ hasAnyMethodInSetup && $ testMethodCount > 1 ) {
92+ $ node ->attrGroups [] = new AttributeGroup ([
93+ new Attribute (new FullyQualified (PHPUnitAttribute::ALLOW_MOCK_OBJECTS_WITHOUT_EXPECTATIONS )),
94+ ]);
95+
96+ return $ node ;
97+ }
98+
8999 if (! $ this ->shouldAddAttribute ($ missedTestMethodsByMockPropertyName )) {
90100 return null ;
91101 }
@@ -269,4 +279,29 @@ private function isAtLeastOneMockPropertyMockedOnce(array $usingTestMethodsByMoc
269279
270280 return false ;
271281 }
282+
283+ private function isMissingExpectsOnMockObjectMethodCallInSetUp (Class_ $ class ): bool
284+ {
285+ $ setupClassMethod = $ class ->getMethod (MethodName::SET_UP );
286+ if (! $ setupClassMethod instanceof ClassMethod) {
287+ return false ;
288+ }
289+
290+ /** @var MethodCall[] $methodCalls */
291+ $ methodCalls = $ this ->betterNodeFinder ->findInstancesOfScoped (
292+ (array ) $ setupClassMethod ->stmts ,
293+ MethodCall::class
294+ );
295+ foreach ($ methodCalls as $ methodCall ) {
296+ if (! $ this ->isName ($ methodCall ->name , 'method ' )) {
297+ continue ;
298+ }
299+
300+ if ($ methodCall ->var instanceof Node \Expr \Variable || $ methodCall ->var instanceof PropertyFetch) {
301+ return true ;
302+ }
303+ }
304+
305+ return false ;
306+ }
272307}
You can’t perform that action at this time.
0 commit comments