Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\Class_\AllowMockObjectsWithoutExpectationsAttributeRector\Fixture;

use PHPUnit\Framework\TestCase;

final class AddToSetupWhenNoExpects extends TestCase
{
private \PHPUnit\Framework\MockObject\MockObject $someMock;

protected function setUp(): void
{
$this->someMock = $this->createMock(\stdClass::class);
$this->someMock->method('some')->willReturn(true);
}

public function testOne()
{
}

public function testTwo()
{
}
}

?>
-----
<?php

namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\Class_\AllowMockObjectsWithoutExpectationsAttributeRector\Fixture;

use PHPUnit\Framework\TestCase;

#[\PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations]
final class AddToSetupWhenNoExpects extends TestCase
{
private \PHPUnit\Framework\MockObject\MockObject $someMock;

protected function setUp(): void
{
$this->someMock = $this->createMock(\stdClass::class);
$this->someMock->method('some')->willReturn(true);
}

public function testOne()
{
}

public function testTwo()
{
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\ObjectType;
use Rector\Doctrine\NodeAnalyzer\AttributeFinder;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\PHPUnit\Enum\PHPUnitAttribute;
Expand Down Expand Up @@ -86,6 +87,16 @@ public function refactor(Node $node): ?Class_
}
}

// or find a ->method() calls on a setUp() mocked property
$hasAnyMethodInSetup = $this->isMissingExpectsOnMockObjectMethodCallInSetUp($node);
if ($hasAnyMethodInSetup && $testMethodCount > 1) {
$node->attrGroups[] = new AttributeGroup([
new Attribute(new FullyQualified(PHPUnitAttribute::ALLOW_MOCK_OBJECTS_WITHOUT_EXPECTATIONS)),
]);

return $node;
}

if (! $this->shouldAddAttribute($missedTestMethodsByMockPropertyName)) {
return null;
}
Expand Down Expand Up @@ -269,4 +280,31 @@ private function isAtLeastOneMockPropertyMockedOnce(array $usingTestMethodsByMoc

return false;
}

private function isMissingExpectsOnMockObjectMethodCallInSetUp(Class_ $class): bool
{
$setupClassMethod = $class->getMethod(MethodName::SET_UP);
if (! $setupClassMethod instanceof ClassMethod) {
return false;
}

/** @var MethodCall[] $methodCalls */
$methodCalls = $this->betterNodeFinder->findInstancesOfScoped(
(array) $setupClassMethod->stmts,
MethodCall::class
);
foreach ($methodCalls as $methodCall) {
if (! $this->isName($methodCall->name, 'method')) {
continue;
}

if (! $this->isObjectType($methodCall->var, new ObjectType(PHPUnitClassName::MOCK_OBJECT))) {
continue;
}

return true;
}

return false;
}
}