Skip to content

Commit ceb0d4c

Browse files
authored
[PHPUnit120] Skip PropertyCreateMockToCreateStubRector for property with expects() nested in closure (#698)
1 parent e436bad commit ceb0d4c

2 files changed

Lines changed: 26 additions & 2 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\Class_\PropertyCreateMockToCreateStubRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class SkipPropertyUsedInClosure 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+
}
15+
16+
public function testThis()
17+
{
18+
$builder = $this->createMock(\stdClass::class);
19+
$builder->method('addEventListener')->willReturnCallback(function () {
20+
$this->someMock->expects($this->once())->method('something');
21+
});
22+
}
23+
}

‎rules/CodeQuality/NodeAnalyser/MockObjectExprDetector.php‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,10 @@ public function isPropertyUsedForMocking(Class_ $class, string $propertyName): b
141141
return true;
142142
}
143143

144-
// find out, how many are used in call likes as args
144+
// find out, how many are used in call likes as args;
145+
// not scoped on purpose, as expects() can be nested in closures, e.g. willReturnCallback()
145146
/** @var array<Expr\MethodCall> $methodCalls */
146-
$methodCalls = $this->betterNodeFinder->findInstancesOfScoped($class->getMethods(), [MethodCall::class]);
147+
$methodCalls = $this->betterNodeFinder->findInstancesOf($class->getMethods(), [MethodCall::class]);
147148

148149
foreach ($methodCalls as $methodCall) {
149150
if (! $methodCall->var instanceof PropertyFetch) {

0 commit comments

Comments
 (0)