Skip to content

Commit ee43f7c

Browse files
committed
add fixture
1 parent d99e5c8 commit ee43f7c

File tree

4 files changed

+153
-0
lines changed

4 files changed

+153
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
?>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\Class_\AllowMockObjectsWithoutExpectationsAttributeRector\Source;
4+
5+
final class SomeFinalClass
6+
{
7+
8+
}

rules/PHPUnit120/Rector/Class_/AllowMockObjectsWithoutExpectationsAttributeRector.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff 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
}

0 commit comments

Comments
 (0)