Skip to content

Commit 9f983d9

Browse files
committed
add to setUp with only variables
1 parent 03d2f60 commit 9f983d9

File tree

2 files changed

+54
-6
lines changed

2 files changed

+54
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\Class_\AllowMockObjectsWithoutExpectationsAttributeRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class AddToSetupWhenNoExpectsVariable extends TestCase
8+
{
9+
protected function setUp(): void
10+
{
11+
$someMock = $this->createMock(\stdClass::class);
12+
$someMock->method('some')->willReturn(true);
13+
}
14+
15+
public function testOne()
16+
{
17+
}
18+
19+
public function testTwo()
20+
{
21+
}
22+
}
23+
24+
?>
25+
-----
26+
<?php
27+
28+
namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\Class_\AllowMockObjectsWithoutExpectationsAttributeRector\Fixture;
29+
30+
use PHPUnit\Framework\TestCase;
31+
32+
#[\PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations]
33+
final class AddToSetupWhenNoExpectsVariable extends TestCase
34+
{
35+
protected function setUp(): void
36+
{
37+
$someMock = $this->createMock(\stdClass::class);
38+
$someMock->method('some')->willReturn(true);
39+
}
40+
41+
public function testOne()
42+
{
43+
}
44+
45+
public function testTwo()
46+
{
47+
}
48+
}
49+
50+
?>

rules/PHPUnit120/Rector/Class_/AllowMockObjectsWithoutExpectationsAttributeRector.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,9 @@ public function refactor(Node $node): ?Class_
5454
return null;
5555
}
5656

57+
// even for 0 mocked properties, the variable in setUp() can be mocked
5758
$mockObjectPropertyNames = $this->matchMockObjectPropertyNames($node);
5859

59-
// there are no mock object properties
60-
if ($mockObjectPropertyNames === []) {
61-
return null;
62-
}
63-
6460
$missedTestMethodsByMockPropertyName = [];
6561
$usingTestMethodsByMockPropertyName = [];
6662
$testMethodCount = 0;
@@ -89,7 +85,7 @@ public function refactor(Node $node): ?Class_
8985

9086
// or find a ->method() calls on a setUp() mocked property
9187
$hasAnyMethodInSetup = $this->isMissingExpectsOnMockObjectMethodCallInSetUp($node);
92-
if ($hasAnyMethodInSetup && $testMethodCount > 1) {
88+
if ($hasAnyMethodInSetup) {
9389
$node->attrGroups[] = new AttributeGroup([
9490
new Attribute(new FullyQualified(PHPUnitAttribute::ALLOW_MOCK_OBJECTS_WITHOUT_EXPECTATIONS)),
9591
]);
@@ -284,6 +280,7 @@ private function isAtLeastOneMockPropertyMockedOnce(array $usingTestMethodsByMoc
284280
private function isMissingExpectsOnMockObjectMethodCallInSetUp(Class_ $class): bool
285281
{
286282
$setupClassMethod = $class->getMethod(MethodName::SET_UP);
283+
287284
if (! $setupClassMethod instanceof ClassMethod) {
288285
return false;
289286
}
@@ -293,6 +290,7 @@ private function isMissingExpectsOnMockObjectMethodCallInSetUp(Class_ $class): b
293290
(array) $setupClassMethod->stmts,
294291
MethodCall::class
295292
);
293+
296294
foreach ($methodCalls as $methodCall) {
297295
if (! $this->isName($methodCall->name, 'method')) {
298296
continue;

0 commit comments

Comments
 (0)