Skip to content

Commit db4740b

Browse files
authored
[PHPUnit100] Add willReturnOnConsecutiveCalls() variables to closure use() in WithConsecutiveRector (#821)
1 parent d32a17a commit db4740b

4 files changed

Lines changed: 65 additions & 3 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\PHPUnit100\Rector\StmtsAwareInterface\WithConsecutiveRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class CombineWithWillReturnOnConsecutiveCallsVariable extends TestCase
8+
{
9+
public function test()
10+
{
11+
$title = new \stdClass();
12+
13+
$this->manager->expects($this->exactly(2))
14+
->method('getFieldDefinitions')
15+
->withConsecutive(['first'], ['second'])
16+
->willReturnOnConsecutiveCalls(['title' => $title], []);
17+
}
18+
}
19+
20+
?>
21+
-----
22+
<?php
23+
24+
namespace Rector\PHPUnit\Tests\PHPUnit100\Rector\StmtsAwareInterface\WithConsecutiveRector\Fixture;
25+
26+
use PHPUnit\Framework\TestCase;
27+
28+
final class CombineWithWillReturnOnConsecutiveCallsVariable extends TestCase
29+
{
30+
public function test()
31+
{
32+
$title = new \stdClass();
33+
$matcher = $this->exactly(2);
34+
35+
$this->manager->expects($matcher)
36+
->method('getFieldDefinitions')->willReturnCallback(function (...$parameters) use ($matcher, $title) {
37+
if ($matcher->numberOfInvocations() === 1) {
38+
$this->assertSame('first', $parameters[0]);
39+
return ['title' => $title];
40+
}
41+
if ($matcher->numberOfInvocations() === 2) {
42+
$this->assertSame('second', $parameters[0]);
43+
return [];
44+
}
45+
});
46+
}
47+
}
48+
49+
?>

‎rules/PHPUnit100/NodeFactory/WillReturnCallbackFactory.php‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,14 @@ public function createClosure(
3636
MethodCall $withConsecutiveMethodCall,
3737
?Stmt $returnStmt,
3838
Variable|Expr|null $referenceVariable,
39+
?MethodCall $willReturnOnConsecutiveMethodCall = null,
3940
): Closure {
4041
$matcherVariable = new Variable(ConsecutiveVariable::MATCHER);
41-
$usedVariables = $this->usedVariablesResolver->resolveUsedVariables($withConsecutiveMethodCall, $returnStmt);
42+
$usedVariables = $this->usedVariablesResolver->resolveUsedVariables(
43+
$withConsecutiveMethodCall,
44+
$returnStmt,
45+
$willReturnOnConsecutiveMethodCall
46+
);
4247

4348
$closureStmts = $this->createParametersMatch($withConsecutiveMethodCall);
4449
if ($returnStmt instanceof Stmt) {

‎rules/PHPUnit100/Rector/StmtsAwareInterface/WithConsecutiveRector.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ private function refactorToWillReturnCallback(
242242
$withConsecutiveMethodCall,
243243
$returnStmt,
244244
$referenceVariable,
245+
$willReturnOnConsecutiveMethodCall,
245246
);
246247

247248
$withConsecutiveMethodCall->name = new Identifier(ConsecutiveMethodName::WILL_RETURN_CALLBACK);

‎src/NodeFactory/UsedVariablesResolver.php‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,17 @@ public function __construct(
2323
/**
2424
* @return Variable[]
2525
*/
26-
public function resolveUsedVariables(MethodCall $withConsecutiveMethodCall, ?Stmt $returnStmt): array
27-
{
26+
public function resolveUsedVariables(
27+
MethodCall $withConsecutiveMethodCall,
28+
?Stmt $returnStmt,
29+
?MethodCall $willReturnOnConsecutiveMethodCall = null
30+
): array {
2831
$consecutiveArgs = $withConsecutiveMethodCall->getArgs();
2932

33+
if ($willReturnOnConsecutiveMethodCall instanceof MethodCall) {
34+
$consecutiveArgs = array_merge($consecutiveArgs, $willReturnOnConsecutiveMethodCall->getArgs());
35+
}
36+
3037
$stmtVariables = $returnStmt instanceof Stmt ? $this->resolveUniqueVariables([$returnStmt]) : [];
3138

3239
return $this->resolveUniqueVariables(array_merge($consecutiveArgs, $stmtVariables));

0 commit comments

Comments
 (0)