PhpUnitMockFixerTest.php 4.3 KB

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