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,26 @@
<?php

declare(strict_types=1);

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

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

final class SkipStubOnly extends TestCase
{
#[DataProvider('someDataProvider')]
public function test(int $value)
{
$someStub = $this->createStub(\Countable::class);
$someStub->method('count')->willReturn($value);

$this->assertSame($value, $someStub->count());
}

public static function someDataProvider(): iterable
{
yield [1];
yield [2];
}
}
21 changes: 21 additions & 0 deletions rules/CodeQuality/NodeAnalyser/MockObjectExprDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\ObjectType;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\PHPUnit\CodeQuality\NodeFinder\VariableFinder;
use Rector\PHPUnit\Enum\PHPUnitClassName;
Expand All @@ -27,6 +28,7 @@ public function __construct(
private NodeNameResolver $nodeNameResolver,
private VariableFinder $variableFinder,
private ReflectionResolver $reflectionResolver,
private NodeTypeResolver $nodeTypeResolver,
) {
}

Expand All @@ -49,6 +51,11 @@ public function hasMethodCallWithoutExpects(ClassMethod $classMethod): bool
continue;
}

// stubs, e.g. from createStub(), do not need expectations
if ($this->isStubOnly($methodCall->var)) {
continue;
}

return true;
}

Expand Down Expand Up @@ -165,4 +172,18 @@ public function isPropertyUsedForMocking(Class_ $class, string $propertyName): b

return false;
}

private function isStubOnly(Expr $expr): bool
{
$exprType = $this->nodeTypeResolver->getType($expr);

$stubObjectType = new ObjectType(PHPUnitClassName::STUB);
if (! $stubObjectType->isSuperTypeOf($exprType)->yes()) {
return false;
}

$mockObjectType = new ObjectType(PHPUnitClassName::MOCK_OBJECT);
return ! $mockObjectType->isSuperTypeOf($exprType)
->yes();
}
}
Loading