123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329 |
- <?php
- declare(strict_types=1);
- /*
- * This file is part of PHP CS Fixer.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- * Dariusz Rumiński <dariusz.ruminski@gmail.com>
- *
- * This source file is subject to the MIT license that is bundled
- * with this source code in the file LICENSE.
- */
- namespace PhpCsFixer\Tests\Fixer\PhpUnit;
- use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
- /**
- * @author Michał Adamski <michal.adamski@gmail.com>
- *
- * @internal
- *
- * @covers \PhpCsFixer\Fixer\PhpUnit\PhpUnitMockShortWillReturnFixer
- *
- * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\PhpUnit\PhpUnitMockShortWillReturnFixer>
- */
- final class PhpUnitMockShortWillReturnFixerTest extends AbstractFixerTestCase
- {
- /**
- * @dataProvider provideFixCases
- */
- public function testFix(string $expected, ?string $input = null): void
- {
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<int|string, array{0: string, 1?: string}>
- */
- public static function provideFixCases(): iterable
- {
- yield 'do not fix' => [
- '<?php
- class FooTest extends TestCase {
- public function testFoo() {
- $someMock->method("someMethod")->will;
- $someMock->method("someMethod")->will("Smith");
- $someMock->method("someMethod")->will($this->returnSelf);
- $someMock->method("someMethod")->will($this->doSomething(7));
- }
- }',
- ];
- yield 'will return simple scenarios' => [
- '<?php
- class FooTest extends TestCase {
- public function testFoo() {
- $someMock->method("someMethod")->willReturn(10);
- $someMock->method("someMethod")->willReturn(20);
- $someMock->method("someMethod")->willReturn(30);
- $someMock->method("someMethod")->willReturn(40);
- $someMock->method("someMethod")->willReturn(50);
- $someMock->method("someMethod")->willReturn(60);
- $someMock->method("someMethod")->willReturn(-10);
- $someMock->method("someMethod")->willReturn(10.10);
- $someMock->method("someMethod")->willReturn(-10.10);
- $someMock->method("someMethod")->willReturn("myValue");
- $someMock->method("someMethod")->willReturn($myValue);
- $testMock->method("test_method")->willReturn(DEFAULT_VALUE);
- $testMock->method("test_method")->willReturn(self::DEFAULT_VALUE);
- $someMock->method("someMethod")->willReturn([]);
- $someMock->method("someMethod")->willReturn([[]]);
- $someMock->method("someMethod")->willReturn(array());
- $someMock->method("someMethod")->willReturn(new stdClass());
- $someMock->method("someMethod")->willReturn(new \DateTime());
- $someMock->method("someMethod")->willReturnSelf();
- $someMock->method("someMethod")->willReturnArgument(2);
- $someMock->method("someMethod")->willReturnCallback("str_rot13");
- $someMock->method("someMethod")->willReturnMap(["a", "b", "c", "d"]);
- $someMock->method("someMethod")->willReturn(1);
- $someMock->method("someMethod")->willReturn(2);
- }
- }',
- '<?php
- class FooTest extends TestCase {
- public function testFoo() {
- $someMock->method("someMethod")->will($this->returnValue(10));
- $someMock->method("someMethod")->will($THIS->returnValue(20));
- $someMock->method("someMethod")->will(self::returnValue(30));
- $someMock->method("someMethod")->will(Self::returnValue(40));
- $someMock->method("someMethod")->will(static::returnValue(50));
- $someMock->method("someMethod")->will(STATIC::returnValue(60));
- $someMock->method("someMethod")->will($this->returnValue(-10));
- $someMock->method("someMethod")->will($this->returnValue(10.10));
- $someMock->method("someMethod")->will($this->returnValue(-10.10));
- $someMock->method("someMethod")->will($this->returnValue("myValue"));
- $someMock->method("someMethod")->will($this->returnValue($myValue));
- $testMock->method("test_method")->will($this->returnValue(DEFAULT_VALUE));
- $testMock->method("test_method")->will($this->returnValue(self::DEFAULT_VALUE));
- $someMock->method("someMethod")->will($this->returnValue([]));
- $someMock->method("someMethod")->will($this->returnValue([[]]));
- $someMock->method("someMethod")->will($this->returnValue(array()));
- $someMock->method("someMethod")->will($this->returnValue(new stdClass()));
- $someMock->method("someMethod")->will($this->returnValue(new \DateTime()));
- $someMock->method("someMethod")->will($this->returnSelf());
- $someMock->method("someMethod")->will($this->returnArgument(2));
- $someMock->method("someMethod")->will($this->returnCallback("str_rot13"));
- $someMock->method("someMethod")->will($this->returnValueMap(["a", "b", "c", "d"]));
- $someMock->method("someMethod")->WILL($this->returnValue(1));
- $someMock->method("someMethod")->will($this->ReturnVALUE(2));
- }
- }',
- ];
- yield 'will return with multi lines and messy indents' => [
- '<?php
- class FooTest extends TestCase {
- public function testFoo() {
- $someMock
- ->method("someMethod")
- ->willReturn(
- 10
- );
- }
- }',
- '<?php
- class FooTest extends TestCase {
- public function testFoo() {
- $someMock
- ->method("someMethod")
- ->will(
- $this->returnValue(10)
- );
- }
- }',
- ];
- yield 'will return with multi lines, messy indents and comments inside' => [
- '<?php
- class FooTest extends TestCase {
- public function testFoo() {
- $someMock
- ->method("someMethod")
- ->willReturn(
- // foo
- 10
- // bar
- );
- }
- }',
- '<?php
- class FooTest extends TestCase {
- public function testFoo() {
- $someMock
- ->method("someMethod")
- ->will(
- // foo
- $this->returnValue(10)
- // bar
- );
- }
- }',
- ];
- yield 'will return with block comments in weird places' => [
- '<?php
- class FooTest extends TestCase {
- public function testFoo() {
- $someMock->method("someMethod")->/* a */willReturn/* b */(/* c */ 10 /* d */);
- }
- }',
- '<?php
- class FooTest extends TestCase {
- public function testFoo() {
- $someMock->method("someMethod")->/* a */will/* b */(/* c */ $this->returnValue(10) /* d */);
- }
- }',
- ];
- yield 'will return with comments persisted not touched even if put in unexpected places' => [
- '<?php
- class FooTest extends TestCase {
- public function testFoo() {
- $someMock->method("someMethod")// a
- ->/* b */willReturn/* c */(/* d */ /** e */
- // f
- '.'
- // g
- '.'
- /* h */
- 10 /* i */);
- }
- }',
- '<?php
- class FooTest extends TestCase {
- public function testFoo() {
- $someMock->method("someMethod")// a
- ->/* b */will/* c */(/* d */ $this/** e */
- -> // f
- returnValue
- // g
- (
- /* h */
- 10) /* i */);
- }
- }',
- ];
- yield 'will return with multi lines, messy indents and comments in weird places' => [
- '<?php
- class FooTest extends TestCase {
- public function testFoo() {
- $someMock
- ->method(
- "someMethod"
- )
- ->
- /* a */
- willReturn
- /*
- b
- c
- d
- e
- */ (
- // f g h i
- /* j */ '.'
- '.'
- 10
- /* k */
- /* l */);
- }
- }',
- '<?php
- class FooTest extends TestCase {
- public function testFoo() {
- $someMock
- ->method(
- "someMethod"
- )
- ->
- /* a */
- will
- /*
- b
- c
- d
- e
- */ (
- // f g h i
- /* j */ $this
- ->returnValue
- (10)
- /* k */
- /* l */);
- }
- }',
- ];
- yield [
- '<?php
- class FooTest extends TestCase {
- public function testFoo() {
- $someMock->method("someMethod")->willReturn( 10 , );
- }
- }',
- '<?php
- class FooTest extends TestCase {
- public function testFoo() {
- $someMock->method("someMethod")->will($this->returnValue( 10 , ));
- }
- }',
- ];
- yield 'with trailing commas' => [
- '<?php
- class FooTest extends TestCase {
- public function testFoo() {
- $someMock->method("someMethod")->willReturn( 10 , );
- }
- }',
- '<?php
- class FooTest extends TestCase {
- public function testFoo() {
- $someMock->method("someMethod")->will($this->returnValue( 10 , ) , );
- }
- }',
- ];
- }
- /**
- * @requires PHP 8.0
- */
- public function testFix80(): void
- {
- $this->doTest(
- '<?php
- class FooTest extends TestCase {
- public function testFoo() {
- $someMock?->method("someMethod")?->willReturn(10);
- }
- }',
- '<?php
- class FooTest extends TestCase {
- public function testFoo() {
- $someMock?->method("someMethod")?->will($this?->returnValue(10));
- }
- }'
- );
- }
- /**
- * @requires PHP 8.1
- */
- public function testFix81(): void
- {
- $this->doTest(
- '<?php
- class FooTest extends TestCase {
- public function testFoo() {
- $a = $someMock?->method("someMethod")->will($this?->returnValue(...));
- }
- }'
- );
- }
- }
|