PhpUnitMockFixerTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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\Fixer\PhpUnit\PhpUnitTargetVersion;
  14. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  15. /**
  16. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  17. *
  18. * @internal
  19. *
  20. * @covers \PhpCsFixer\Fixer\PhpUnit\PhpUnitMockFixer
  21. *
  22. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\PhpUnit\PhpUnitMockFixer>
  23. *
  24. * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\PhpUnit\PhpUnitMockFixer
  25. */
  26. final class PhpUnitMockFixerTest extends AbstractFixerTestCase
  27. {
  28. /**
  29. * @param _AutogeneratedInputConfiguration $config
  30. *
  31. * @dataProvider provideFixCases
  32. */
  33. public function testFix(string $expected, ?string $input = null, array $config = []): void
  34. {
  35. $this->fixer->configure($config);
  36. $this->doTest($expected, $input);
  37. }
  38. public static function provideFixCases(): iterable
  39. {
  40. yield [
  41. '<?php
  42. final class MyTest extends \PHPUnit_Framework_TestCase
  43. {
  44. public function testFoo()
  45. {
  46. $this->createMock("Foo");
  47. }
  48. }',
  49. '<?php
  50. final class MyTest extends \PHPUnit_Framework_TestCase
  51. {
  52. public function testFoo()
  53. {
  54. $this->getMockWithoutInvokingTheOriginalConstructor("Foo");
  55. }
  56. }',
  57. ];
  58. yield [
  59. '<?php
  60. final class MyTest extends \PHPUnit_Framework_TestCase
  61. {
  62. public function testFoo()
  63. {
  64. $this->createMock("Foo");
  65. $this->createMock($foo(1, 2));
  66. $this->getMock("Foo", ["aaa"]);
  67. }
  68. }',
  69. '<?php
  70. final class MyTest extends \PHPUnit_Framework_TestCase
  71. {
  72. public function testFoo()
  73. {
  74. $this->getMock("Foo");
  75. $this->getMock($foo(1, 2));
  76. $this->getMock("Foo", ["aaa"]);
  77. }
  78. }',
  79. ['target' => PhpUnitTargetVersion::VERSION_5_4],
  80. ];
  81. yield [
  82. '<?php
  83. final class MyTest extends \PHPUnit_Framework_TestCase
  84. {
  85. public function testFoo()
  86. {
  87. $this->createMock("Foo");
  88. $this->createMock($foo(1, 2));
  89. $this->createPartialMock("Foo", ["aaa"]);
  90. $this->getMock("Foo", ["aaa"], ["argument"]);
  91. }
  92. }',
  93. '<?php
  94. final class MyTest extends \PHPUnit_Framework_TestCase
  95. {
  96. public function testFoo()
  97. {
  98. $this->getMock("Foo");
  99. $this->getMock($foo(1, 2));
  100. $this->getMock("Foo", ["aaa"]);
  101. $this->getMock("Foo", ["aaa"], ["argument"]);
  102. }
  103. }',
  104. ];
  105. yield [
  106. '<?php
  107. class FooTest extends TestCase
  108. {
  109. public function testFoo()
  110. {
  111. $this->createMock("Foo",);
  112. $this->createMock("Bar" ,);
  113. $this->createMock("Baz" , );
  114. $this->createMock($foo(1, 2), );
  115. $this->createMock($foo(3, 4, ));
  116. $this->createMock($foo(5, 6, ), );
  117. $this->createPartialMock("Foo", ["aaa"], );
  118. $this->createPartialMock("Foo", ["bbb", ], );
  119. $this->getMock("Foo", ["aaa"], ["argument"], );
  120. $this->getMock("Foo", ["bbb", ], ["argument", ], );
  121. }
  122. }',
  123. '<?php
  124. class FooTest extends TestCase
  125. {
  126. public function testFoo()
  127. {
  128. $this->getMock("Foo",);
  129. $this->getMock("Bar" ,);
  130. $this->getMock("Baz" , );
  131. $this->getMock($foo(1, 2), );
  132. $this->getMock($foo(3, 4, ));
  133. $this->getMock($foo(5, 6, ), );
  134. $this->getMock("Foo", ["aaa"], );
  135. $this->getMock("Foo", ["bbb", ], );
  136. $this->getMock("Foo", ["aaa"], ["argument"], );
  137. $this->getMock("Foo", ["bbb", ], ["argument", ], );
  138. }
  139. }',
  140. ];
  141. }
  142. /**
  143. * @requires PHP 8.0
  144. */
  145. public function testFix80(): void
  146. {
  147. $this->doTest(
  148. '<?php
  149. class FooTest extends TestCase
  150. {
  151. public function testFoo()
  152. {
  153. $this?->createMock("Foo");
  154. }
  155. }',
  156. '<?php
  157. class FooTest extends TestCase
  158. {
  159. public function testFoo()
  160. {
  161. $this?->getMock("Foo");
  162. }
  163. }'
  164. );
  165. }
  166. }