PhpUnitMockShortWillReturnFixerTest.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of PHP CS Fixer.
  5. *
  6. * (c) Fabien Potencier <fabien@symfony.com>
  7. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  8. *
  9. * This source file is subject to the MIT license that is bundled
  10. * with this source code in the file LICENSE.
  11. */
  12. namespace PhpCsFixer\Tests\Fixer\PhpUnit;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. /**
  15. * @author Michał Adamski <michal.adamski@gmail.com>
  16. *
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Fixer\PhpUnit\PhpUnitMockShortWillReturnFixer
  20. *
  21. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\PhpUnit\PhpUnitMockShortWillReturnFixer>
  22. */
  23. final class PhpUnitMockShortWillReturnFixerTest extends AbstractFixerTestCase
  24. {
  25. /**
  26. * @dataProvider provideFixCases
  27. */
  28. public function testFix(string $expected, ?string $input = null): void
  29. {
  30. $this->doTest($expected, $input);
  31. }
  32. /**
  33. * @return iterable<int|string, array{0: string, 1?: string}>
  34. */
  35. public static function provideFixCases(): iterable
  36. {
  37. yield 'do not fix' => [
  38. '<?php
  39. class FooTest extends TestCase {
  40. public function testFoo() {
  41. $someMock->method("someMethod")->will;
  42. $someMock->method("someMethod")->will("Smith");
  43. $someMock->method("someMethod")->will($this->returnSelf);
  44. $someMock->method("someMethod")->will($this->doSomething(7));
  45. }
  46. }',
  47. ];
  48. yield 'will return simple scenarios' => [
  49. '<?php
  50. class FooTest extends TestCase {
  51. public function testFoo() {
  52. $someMock->method("someMethod")->willReturn(10);
  53. $someMock->method("someMethod")->willReturn(20);
  54. $someMock->method("someMethod")->willReturn(30);
  55. $someMock->method("someMethod")->willReturn(40);
  56. $someMock->method("someMethod")->willReturn(50);
  57. $someMock->method("someMethod")->willReturn(60);
  58. $someMock->method("someMethod")->willReturn(-10);
  59. $someMock->method("someMethod")->willReturn(10.10);
  60. $someMock->method("someMethod")->willReturn(-10.10);
  61. $someMock->method("someMethod")->willReturn("myValue");
  62. $someMock->method("someMethod")->willReturn($myValue);
  63. $testMock->method("test_method")->willReturn(DEFAULT_VALUE);
  64. $testMock->method("test_method")->willReturn(self::DEFAULT_VALUE);
  65. $someMock->method("someMethod")->willReturn([]);
  66. $someMock->method("someMethod")->willReturn([[]]);
  67. $someMock->method("someMethod")->willReturn(array());
  68. $someMock->method("someMethod")->willReturn(new stdClass());
  69. $someMock->method("someMethod")->willReturn(new \DateTime());
  70. $someMock->method("someMethod")->willReturnSelf();
  71. $someMock->method("someMethod")->willReturnArgument(2);
  72. $someMock->method("someMethod")->willReturnCallback("str_rot13");
  73. $someMock->method("someMethod")->willReturnMap(["a", "b", "c", "d"]);
  74. $someMock->method("someMethod")->willReturn(1);
  75. $someMock->method("someMethod")->willReturn(2);
  76. }
  77. }',
  78. '<?php
  79. class FooTest extends TestCase {
  80. public function testFoo() {
  81. $someMock->method("someMethod")->will($this->returnValue(10));
  82. $someMock->method("someMethod")->will($THIS->returnValue(20));
  83. $someMock->method("someMethod")->will(self::returnValue(30));
  84. $someMock->method("someMethod")->will(Self::returnValue(40));
  85. $someMock->method("someMethod")->will(static::returnValue(50));
  86. $someMock->method("someMethod")->will(STATIC::returnValue(60));
  87. $someMock->method("someMethod")->will($this->returnValue(-10));
  88. $someMock->method("someMethod")->will($this->returnValue(10.10));
  89. $someMock->method("someMethod")->will($this->returnValue(-10.10));
  90. $someMock->method("someMethod")->will($this->returnValue("myValue"));
  91. $someMock->method("someMethod")->will($this->returnValue($myValue));
  92. $testMock->method("test_method")->will($this->returnValue(DEFAULT_VALUE));
  93. $testMock->method("test_method")->will($this->returnValue(self::DEFAULT_VALUE));
  94. $someMock->method("someMethod")->will($this->returnValue([]));
  95. $someMock->method("someMethod")->will($this->returnValue([[]]));
  96. $someMock->method("someMethod")->will($this->returnValue(array()));
  97. $someMock->method("someMethod")->will($this->returnValue(new stdClass()));
  98. $someMock->method("someMethod")->will($this->returnValue(new \DateTime()));
  99. $someMock->method("someMethod")->will($this->returnSelf());
  100. $someMock->method("someMethod")->will($this->returnArgument(2));
  101. $someMock->method("someMethod")->will($this->returnCallback("str_rot13"));
  102. $someMock->method("someMethod")->will($this->returnValueMap(["a", "b", "c", "d"]));
  103. $someMock->method("someMethod")->WILL($this->returnValue(1));
  104. $someMock->method("someMethod")->will($this->ReturnVALUE(2));
  105. }
  106. }',
  107. ];
  108. yield 'will return with multi lines and messy indents' => [
  109. '<?php
  110. class FooTest extends TestCase {
  111. public function testFoo() {
  112. $someMock
  113. ->method("someMethod")
  114. ->willReturn(
  115. 10
  116. );
  117. }
  118. }',
  119. '<?php
  120. class FooTest extends TestCase {
  121. public function testFoo() {
  122. $someMock
  123. ->method("someMethod")
  124. ->will(
  125. $this->returnValue(10)
  126. );
  127. }
  128. }',
  129. ];
  130. yield 'will return with multi lines, messy indents and comments inside' => [
  131. '<?php
  132. class FooTest extends TestCase {
  133. public function testFoo() {
  134. $someMock
  135. ->method("someMethod")
  136. ->willReturn(
  137. // foo
  138. 10
  139. // bar
  140. );
  141. }
  142. }',
  143. '<?php
  144. class FooTest extends TestCase {
  145. public function testFoo() {
  146. $someMock
  147. ->method("someMethod")
  148. ->will(
  149. // foo
  150. $this->returnValue(10)
  151. // bar
  152. );
  153. }
  154. }',
  155. ];
  156. yield 'will return with block comments in weird places' => [
  157. '<?php
  158. class FooTest extends TestCase {
  159. public function testFoo() {
  160. $someMock->method("someMethod")->/* a */willReturn/* b */(/* c */ 10 /* d */);
  161. }
  162. }',
  163. '<?php
  164. class FooTest extends TestCase {
  165. public function testFoo() {
  166. $someMock->method("someMethod")->/* a */will/* b */(/* c */ $this->returnValue(10) /* d */);
  167. }
  168. }',
  169. ];
  170. yield 'will return with comments persisted not touched even if put in unexpected places' => [
  171. '<?php
  172. class FooTest extends TestCase {
  173. public function testFoo() {
  174. $someMock->method("someMethod")// a
  175. ->/* b */willReturn/* c */(/* d */ /** e */
  176. // f
  177. '.'
  178. // g
  179. '.'
  180. /* h */
  181. 10 /* i */);
  182. }
  183. }',
  184. '<?php
  185. class FooTest extends TestCase {
  186. public function testFoo() {
  187. $someMock->method("someMethod")// a
  188. ->/* b */will/* c */(/* d */ $this/** e */
  189. -> // f
  190. returnValue
  191. // g
  192. (
  193. /* h */
  194. 10) /* i */);
  195. }
  196. }',
  197. ];
  198. yield 'will return with multi lines, messy indents and comments in weird places' => [
  199. '<?php
  200. class FooTest extends TestCase {
  201. public function testFoo() {
  202. $someMock
  203. ->method(
  204. "someMethod"
  205. )
  206. ->
  207. /* a */
  208. willReturn
  209. /*
  210. b
  211. c
  212. d
  213. e
  214. */ (
  215. // f g h i
  216. /* j */ '.'
  217. '.'
  218. 10
  219. /* k */
  220. /* l */);
  221. }
  222. }',
  223. '<?php
  224. class FooTest extends TestCase {
  225. public function testFoo() {
  226. $someMock
  227. ->method(
  228. "someMethod"
  229. )
  230. ->
  231. /* a */
  232. will
  233. /*
  234. b
  235. c
  236. d
  237. e
  238. */ (
  239. // f g h i
  240. /* j */ $this
  241. ->returnValue
  242. (10)
  243. /* k */
  244. /* l */);
  245. }
  246. }',
  247. ];
  248. yield [
  249. '<?php
  250. class FooTest extends TestCase {
  251. public function testFoo() {
  252. $someMock->method("someMethod")->willReturn( 10 , );
  253. }
  254. }',
  255. '<?php
  256. class FooTest extends TestCase {
  257. public function testFoo() {
  258. $someMock->method("someMethod")->will($this->returnValue( 10 , ));
  259. }
  260. }',
  261. ];
  262. yield 'with trailing commas' => [
  263. '<?php
  264. class FooTest extends TestCase {
  265. public function testFoo() {
  266. $someMock->method("someMethod")->willReturn( 10 , );
  267. }
  268. }',
  269. '<?php
  270. class FooTest extends TestCase {
  271. public function testFoo() {
  272. $someMock->method("someMethod")->will($this->returnValue( 10 , ) , );
  273. }
  274. }',
  275. ];
  276. }
  277. /**
  278. * @requires PHP 8.0
  279. */
  280. public function testFix80(): void
  281. {
  282. $this->doTest(
  283. '<?php
  284. class FooTest extends TestCase {
  285. public function testFoo() {
  286. $someMock?->method("someMethod")?->willReturn(10);
  287. }
  288. }',
  289. '<?php
  290. class FooTest extends TestCase {
  291. public function testFoo() {
  292. $someMock?->method("someMethod")?->will($this?->returnValue(10));
  293. }
  294. }'
  295. );
  296. }
  297. /**
  298. * @requires PHP 8.1
  299. */
  300. public function testFix81(): void
  301. {
  302. $this->doTest(
  303. '<?php
  304. class FooTest extends TestCase {
  305. public function testFoo() {
  306. $a = $someMock?->method("someMethod")->will($this?->returnValue(...));
  307. }
  308. }'
  309. );
  310. }
  311. }