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

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

use PHPUnit\Framework\TestCase;
use Rector\PHPUnit\Tests\PHPUnit120\Rector\Class_\AllowMockObjectsWithoutExpectationsAttributeRector\Source\ClassToStub;

final class SkipNotMockObject extends TestCase
{
private ClassToStub $foo;

public function setUp(): void
{
parent::setUp();

$foo = self::createStub(ClassToStub::class);
$foo->method('foo')->willReturn('error');

$this->foo = $foo;
}

public function test()
{
$this->foo->foo();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

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

class ClassToStub
{
public function foo(): string
{
return 'error';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\NeverType;
use PHPStan\Type\ObjectType;
use Rector\Doctrine\NodeAnalyzer\AttributeFinder;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\PHPUnit\Enum\PHPUnitAttribute;
Expand Down Expand Up @@ -296,6 +298,14 @@ private function isMissingExpectsOnMockObjectMethodCallInSetUp(Class_ $class): b
continue;
}

$type = $this->getType($methodCall->var);
if (
// never type check is needed for fixture with mock final class, example use is possibly by dg/bypass-finals
! $type instanceof NeverType &&
! $this->isObjectType($methodCall->var, new ObjectType(PHPUnitClassName::MOCK_OBJECT))) {
continue;
}

if ($methodCall->var instanceof Variable || $methodCall->var instanceof PropertyFetch) {
return true;
}
Expand Down