From c5e942849009e00357b8ea876e01639306ab8106 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Mon, 31 Dec 2018 21:59:41 +0100 Subject: [PATCH] [PHPUnit] Add UseSpecificWillMethodRector --- config/level/phpunit/phpunit-mock.yml | 2 + docs/AllRectorsOverview.md | 24 ++++ .../UseSpecificWillMethodRector.php | 129 ++++++++++++++++++ .../Fixture/will.php.inc | 74 ++++++++++ .../Fixture/with.php.inc | 35 +++++ .../UseSpecificWillMethodRectorTest.php | 19 +++ 6 files changed, 283 insertions(+) create mode 100644 config/level/phpunit/phpunit-mock.yml create mode 100644 packages/PHPUnit/src/Rector/MethodCall/UseSpecificWillMethodRector.php create mode 100644 packages/PHPUnit/tests/Rector/MethodCall/UseSpecificWillMethodRector/Fixture/will.php.inc create mode 100644 packages/PHPUnit/tests/Rector/MethodCall/UseSpecificWillMethodRector/Fixture/with.php.inc create mode 100644 packages/PHPUnit/tests/Rector/MethodCall/UseSpecificWillMethodRector/UseSpecificWillMethodRectorTest.php diff --git a/config/level/phpunit/phpunit-mock.yml b/config/level/phpunit/phpunit-mock.yml new file mode 100644 index 00000000000..5ffed8b4990 --- /dev/null +++ b/config/level/phpunit/phpunit-mock.yml @@ -0,0 +1,2 @@ +services: + Rector\PHPUnit\Rector\MethodCall\UseSpecificWillMethodRector: ~ diff --git a/docs/AllRectorsOverview.md b/docs/AllRectorsOverview.md index 85af4060d7d..da37fb5c2a1 100644 --- a/docs/AllRectorsOverview.md +++ b/docs/AllRectorsOverview.md @@ -1048,6 +1048,30 @@ Change assertContains()/assertNotContains() method to new string and iterable al
+### `UseSpecificWillMethodRector` + +- class: `Rector\PHPUnit\Rector\MethodCall\UseSpecificWillMethodRector` + +Changes ->will($this->xxx()) to one specific method + +```diff + class SomeClass extends PHPUnit\Framework\TestCase + { + public function test() + { + $translator = $this->getMockBuilder('Symfony\Component\Translation\TranslatorInterface')->getMock(); + $translator->expects($this->any()) + ->method('trans') +- ->with($this->equalTo('old max {{ max }}!')) +- ->will($this->returnValue('translated max {{ max }}!')); ++ ->with('old max {{ max }}!') ++ ->willReturnValue('translated max {{ max }}!'); + } + } +``` + +
+ ### `SpecificAssertInternalTypeRector` - class: `Rector\PHPUnit\Rector\MethodCall\SpecificAssertInternalTypeRector` diff --git a/packages/PHPUnit/src/Rector/MethodCall/UseSpecificWillMethodRector.php b/packages/PHPUnit/src/Rector/MethodCall/UseSpecificWillMethodRector.php new file mode 100644 index 00000000000..717ab514791 --- /dev/null +++ b/packages/PHPUnit/src/Rector/MethodCall/UseSpecificWillMethodRector.php @@ -0,0 +1,129 @@ + 'willReturnArgument', + 'returnCallback' => 'willReturnCallback', + 'returnSelf' => 'willReturnSelf', + 'returnValue' => 'willReturn', + 'returnValueMap' => 'willReturnMap', + 'throwException' => 'willThrowException', + ]; + + public function getDefinition(): RectorDefinition + { + return new RectorDefinition('Changes ->will($this->xxx()) to one specific method', [ + new CodeSample( + <<<'CODE_SAMPLE' +class SomeClass extends PHPUnit\Framework\TestCase +{ + public function test() + { + $translator = $this->getMockBuilder('Symfony\Component\Translation\TranslatorInterface')->getMock(); + $translator->expects($this->any()) + ->method('trans') + ->with($this->equalTo('old max {{ max }}!')) + ->will($this->returnValue('translated max {{ max }}!')); + } +} +CODE_SAMPLE + , + <<<'CODE_SAMPLE' +class SomeClass extends PHPUnit\Framework\TestCase +{ + public function test() + { + $translator = $this->getMockBuilder('Symfony\Component\Translation\TranslatorInterface')->getMock(); + $translator->expects($this->any()) + ->method('trans') + ->with('old max {{ max }}!') + ->willReturnValue('translated max {{ max }}!'); + } +} +CODE_SAMPLE + ), + ]); + } + + /** + * @return string[] + */ + public function getNodeTypes(): array + { + return [MethodCall::class]; + } + + /** + * @param MethodCall $node + */ + public function refactor(Node $node): ?Node + { + if (! $this->isInTestClass($node)) { + return null; + } + + if (! $this->isType($node, 'PHPUnit\Framework\MockObject\Builder\InvocationMocker')) { + return null; + } + + if ($this->isNameInsensitive($node, 'with')) { + return $this->processWithCall($node); + } + + if ($this->isNameInsensitive($node, 'will')) { + return $this->processWillCall($node); + } + + return null; + } + + private function processWithCall(MethodCall $methodCallNode): ?MethodCall + { + foreach ($methodCallNode->args as $i => $argNode) { + if ($argNode->value instanceof MethodCall && $this->isName($argNode->value, 'equalTo')) { + $methodCallNode->args[$i] = $argNode->value->args[0]; + } + } + + return $methodCallNode; + } + + private function processWillCall(MethodCall $methodCallNode): ?MethodCall + { + if (! $methodCallNode->args[0]->value instanceof MethodCall) { + return null; + } + + $nestedMethodCall = $methodCallNode->args[0]->value; + + foreach ($this->nestedMethodToRenameMap as $oldMethodName => $newParentMethodName) { + if ($this->isNameInsensitive($nestedMethodCall, $oldMethodName)) { + $methodCallNode->name = new Identifier($newParentMethodName); + + // move args up + $methodCallNode->args = $nestedMethodCall->args; + + return $methodCallNode; + } + } + + return null; + } +} diff --git a/packages/PHPUnit/tests/Rector/MethodCall/UseSpecificWillMethodRector/Fixture/will.php.inc b/packages/PHPUnit/tests/Rector/MethodCall/UseSpecificWillMethodRector/Fixture/will.php.inc new file mode 100644 index 00000000000..1b052ac2917 --- /dev/null +++ b/packages/PHPUnit/tests/Rector/MethodCall/UseSpecificWillMethodRector/Fixture/will.php.inc @@ -0,0 +1,74 @@ +getMockBuilder('Symfony\Component\Translation\TranslatorInterface')->getMock(); + $translator->expects($this->any()) + ->method('trans') + ->will($this->returnValue('translated max {{ max }}!')); + + $translator->expects($this->any()) + ->will($this->returnSelf()); + + $translator->expects($this->any()) + ->will($this->returnArgument(2)); + + $translator->expects($this->any()) + ->will($this->returnCallback("str_rot13")); + + $translator->expects($this->any()) + ->will($this->returnValueMap(["a", "b", "c", "d"])); + + $translator->expects($this->any()) + ->WILL($this->returnValue(1)); + + $translator->expects($this->any()) + ->will($this->ReturnVALUE(2)); + + $translator->expects($this->any()) + ->will($this->throwException(new \InvalidArgumentException())); + } +} + +?> +----- +getMockBuilder('Symfony\Component\Translation\TranslatorInterface')->getMock(); + $translator->expects($this->any()) + ->method('trans') + ->willReturn('translated max {{ max }}!'); + + $translator->expects($this->any())->willReturnSelf(); + + $translator->expects($this->any()) + ->willReturnArgument(2); + + $translator->expects($this->any()) + ->willReturnCallback("str_rot13"); + + $translator->expects($this->any()) + ->willReturnMap(["a", "b", "c", "d"]); + + $translator->expects($this->any()) + ->willReturn(1); + + $translator->expects($this->any()) + ->willReturn(2); + + $translator->expects($this->any()) + ->willThrowException(new \InvalidArgumentException()); + } +} + +?> diff --git a/packages/PHPUnit/tests/Rector/MethodCall/UseSpecificWillMethodRector/Fixture/with.php.inc b/packages/PHPUnit/tests/Rector/MethodCall/UseSpecificWillMethodRector/Fixture/with.php.inc new file mode 100644 index 00000000000..cde45e52da3 --- /dev/null +++ b/packages/PHPUnit/tests/Rector/MethodCall/UseSpecificWillMethodRector/Fixture/with.php.inc @@ -0,0 +1,35 @@ +getMockBuilder('Symfony\Component\Translation\TranslatorInterface')->getMock(); + $translator->expects($this->any()) + ->method('trans') + ->with($this->equalTo('old max {{ max }}!')) + ->with($this->equalTo($log), $this->equalTo($parameters)); + } +} + +?> +----- +getMockBuilder('Symfony\Component\Translation\TranslatorInterface')->getMock(); + $translator->expects($this->any()) + ->method('trans') + ->with('old max {{ max }}!') + ->with($log, $parameters); + } +} + +?> diff --git a/packages/PHPUnit/tests/Rector/MethodCall/UseSpecificWillMethodRector/UseSpecificWillMethodRectorTest.php b/packages/PHPUnit/tests/Rector/MethodCall/UseSpecificWillMethodRector/UseSpecificWillMethodRectorTest.php new file mode 100644 index 00000000000..443936dd10c --- /dev/null +++ b/packages/PHPUnit/tests/Rector/MethodCall/UseSpecificWillMethodRector/UseSpecificWillMethodRectorTest.php @@ -0,0 +1,19 @@ +doTestFiles([__DIR__ . '/Fixture/will.php.inc', __DIR__ . '/Fixture/with.php.inc']); + } + + protected function getRectorClass(): string + { + return UseSpecificWillMethodRector::class; + } +}