PhpUnitMockFixerTest.php 4.4 KB

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