From a49d8fc98551f3718d635e6824df89aafebb2f2c Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Wed, 25 Sep 2024 20:21:49 +0200 Subject: [PATCH] add expects call --- ...combine_with_on_consecutive_without_expects.php.inc | 2 +- .../StmtsAwareInterface/WithConsecutiveRector.php | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/rules-tests/PHPUnit100/Rector/StmtsAwareInterface/WithConsecutiveRector/Fixture/combine_with_on_consecutive_without_expects.php.inc b/rules-tests/PHPUnit100/Rector/StmtsAwareInterface/WithConsecutiveRector/Fixture/combine_with_on_consecutive_without_expects.php.inc index febb395a..f2276db2 100644 --- a/rules-tests/PHPUnit100/Rector/StmtsAwareInterface/WithConsecutiveRector/Fixture/combine_with_on_consecutive_without_expects.php.inc +++ b/rules-tests/PHPUnit100/Rector/StmtsAwareInterface/WithConsecutiveRector/Fixture/combine_with_on_consecutive_without_expects.php.inc @@ -30,7 +30,7 @@ final class CombineWithOnConsecutiveWithoutExcepts extends TestCase public function test() { $matcher = $this->exactly(2); - $this->userServiceMock->expects($matcher)->method('prepare')->willReturnCallback(function ($parameters) use ($matcher) { + $this->userServiceMock->method('prepare')->expects($matcher)->willReturnCallback(function ($parameters) use ($matcher) { match ($matcher->numberOfInvocations()) { 1 => self::assertEquals([1, 2], $parameters), 2 => self::assertEquals([3, 4], $parameters), diff --git a/rules/PHPUnit100/Rector/StmtsAwareInterface/WithConsecutiveRector.php b/rules/PHPUnit100/Rector/StmtsAwareInterface/WithConsecutiveRector.php index 6136cfc6..9112f87f 100644 --- a/rules/PHPUnit100/Rector/StmtsAwareInterface/WithConsecutiveRector.php +++ b/rules/PHPUnit100/Rector/StmtsAwareInterface/WithConsecutiveRector.php @@ -386,6 +386,16 @@ private function refactorToWillReturnCallback( )), ]; + $hasExpects = $this->findMethodCall($expression, 'expects') instanceof MethodCall; + if ($hasExpects === false) { + /** @var MethodCall $mockMethodCall */ + $mockMethodCall = $expression->expr; + + $mockMethodCall->var = new MethodCall($mockMethodCall->var, 'expects', [ + new Arg(new Variable('matcher')) + ]); + } + $matcherAssign = new Assign(new Variable('matcher'), $expectsCall); return [new Expression($matcherAssign), $expression]; }