Skip to content

Commit 2839ff3

Browse files
authored
[PHPUnit120] Skip non-mock object on AllowMockObjectsWithoutExpectationsAttributeRector (#656)
* [PHPUnit120] Skip non-mock object on AllowMockObjectsWithoutExpectationsAttributeRector * Fix
1 parent 5eee71e commit 2839ff3

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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\ClassToStub;
7+
8+
final class SkipNotMockObject extends TestCase
9+
{
10+
private ClassToStub $foo;
11+
12+
public function setUp(): void
13+
{
14+
parent::setUp();
15+
16+
$foo = self::createStub(ClassToStub::class);
17+
$foo->method('foo')->willReturn('error');
18+
19+
$this->foo = $foo;
20+
}
21+
22+
public function test()
23+
{
24+
$this->foo->foo();
25+
}
26+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\Class_\AllowMockObjectsWithoutExpectationsAttributeRector\Source;
4+
5+
class ClassToStub
6+
{
7+
public function foo(): string
8+
{
9+
return 'error';
10+
}
11+
}

rules/PHPUnit120/Rector/Class_/AllowMockObjectsWithoutExpectationsAttributeRector.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
use PhpParser\Node\Stmt\Class_;
1616
use PhpParser\Node\Stmt\ClassMethod;
1717
use PHPStan\Reflection\ReflectionProvider;
18+
use PHPStan\Type\NeverType;
19+
use PHPStan\Type\ObjectType;
1820
use Rector\Doctrine\NodeAnalyzer\AttributeFinder;
1921
use Rector\PhpParser\Node\BetterNodeFinder;
2022
use Rector\PHPUnit\Enum\PHPUnitAttribute;
@@ -296,6 +298,14 @@ private function isMissingExpectsOnMockObjectMethodCallInSetUp(Class_ $class): b
296298
continue;
297299
}
298300

301+
$type = $this->getType($methodCall->var);
302+
if (
303+
// never type check is needed for fixture with mock final class, example use is possibly by dg/bypass-finals
304+
! $type instanceof NeverType &&
305+
! $this->isObjectType($methodCall->var, new ObjectType(PHPUnitClassName::MOCK_OBJECT))) {
306+
continue;
307+
}
308+
299309
if ($methodCall->var instanceof Variable || $methodCall->var instanceof PropertyFetch) {
300310
return true;
301311
}

0 commit comments

Comments
 (0)