PhpUnitNamespacedFixerTest.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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\PhpUnitNamespacedFixer
  21. *
  22. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\PhpUnit\PhpUnitNamespacedFixer>
  23. *
  24. * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\PhpUnit\PhpUnitNamespacedFixer
  25. */
  26. final class PhpUnitNamespacedFixerTest 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 'class_mapping' => [
  41. '<?php new PHPUnit\Framework\Error\Error();',
  42. '<?php new PHPUnit_Framework_Error();',
  43. ];
  44. yield 'class_mapping_bogus_fqcn' => [
  45. '<?php new \PHPUnit\Framework\MockObject\Stub\ReturnStub();',
  46. '<?php new \PHPUnit_Framework_MockObject_Stub_Return();',
  47. ];
  48. yield 'class_mapping_bogus_fqcn_lowercase' => [
  49. '<?php new \PHPUnit\Framework\MockObject\Stub\ReturnStub();',
  50. '<?php new \phpunit_framework_mockobject_stub_return();',
  51. ];
  52. yield 'class_mapping_bogus_fqcn_uppercase' => [
  53. '<?php new \PHPUnit\Framework\MockObject\Stub\ReturnStub();',
  54. '<?php new \PHPUNIT_FRAMEWORK_MOCKOBJECT_STUB_RETURN();',
  55. ];
  56. yield [
  57. '<?php
  58. final class MyTest extends \PHPUnit\Framework\TestCase
  59. {
  60. }',
  61. '<?php
  62. final class MyTest extends \PHPUnit_Framework_TestCase
  63. {
  64. }',
  65. ];
  66. yield [
  67. '<?php
  68. final class TextDiffTest extends PHPUnit\Framework\TestCase
  69. {
  70. }',
  71. '<?php
  72. final class TextDiffTest extends PHPUnit_Framework_TestCase
  73. {
  74. }',
  75. ];
  76. yield [
  77. '<?php
  78. use \PHPUnit\Framework\TestCase;
  79. final class TextDiffTest extends TestCase
  80. {
  81. }',
  82. '<?php
  83. use \PHPUnit_Framework_TestCase;
  84. final class TextDiffTest extends PHPUnit_Framework_TestCase
  85. {
  86. }',
  87. ];
  88. yield [
  89. '<?php
  90. use \PHPUnit\FRAMEWORK\TestCase as TestAlias;
  91. final class TextDiffTest extends TestAlias
  92. {
  93. }',
  94. '<?php
  95. use \PHPUnit_FRAMEWORK_TestCase as TestAlias;
  96. final class TextDiffTest extends TestAlias
  97. {
  98. }',
  99. ];
  100. yield [
  101. '<?php
  102. namespace Foo;
  103. use PHPUnit\Framework\TestCase;
  104. final class TextDiffTest extends TestCase
  105. {
  106. }',
  107. '<?php
  108. namespace Foo;
  109. use PHPUnit_Framework_TestCase;
  110. final class TextDiffTest extends PHPUnit_Framework_TestCase
  111. {
  112. }',
  113. ];
  114. yield [
  115. '<?php
  116. namespace Foo;
  117. use PHPUnit\Framework\TestCase as TestAlias;
  118. final class TextDiffTest extends TestAlias
  119. {
  120. }',
  121. '<?php
  122. namespace Foo;
  123. use PHPUnit_Framework_TestCase as TestAlias;
  124. final class TextDiffTest extends TestAlias
  125. {
  126. }',
  127. ];
  128. yield [
  129. '<?php
  130. final class MyTest extends \PHPUnit\Framework\TestCase
  131. {
  132. public function aaa()
  133. {
  134. $a = new PHPUnit_Framework_Assert();
  135. $b = new PHPUnit_Framework_BaseTestListener();
  136. $c = new PHPUnit_Framework_TestListener();
  137. $d1 = new PHPUnit_Aaa();
  138. $d2 = new PHPUnit_Aaa_Bbb();
  139. $d3 = new PHPUnit_Aaa_Bbb_Ccc();
  140. $d4 = new PHPUnit_Aaa_Bbb_Ccc_Ddd();
  141. $d5 = new PHPUnit_Aaa_Bbb_Ccc_Ddd_Eee();
  142. }
  143. }',
  144. '<?php
  145. final class MyTest extends \PHPUnit_Framework_TestCase
  146. {
  147. public function aaa()
  148. {
  149. $a = new PHPUnit_Framework_Assert();
  150. $b = new PHPUnit_Framework_BaseTestListener();
  151. $c = new PHPUnit_Framework_TestListener();
  152. $d1 = new PHPUnit_Aaa();
  153. $d2 = new PHPUnit_Aaa_Bbb();
  154. $d3 = new PHPUnit_Aaa_Bbb_Ccc();
  155. $d4 = new PHPUnit_Aaa_Bbb_Ccc_Ddd();
  156. $d5 = new PHPUnit_Aaa_Bbb_Ccc_Ddd_Eee();
  157. }
  158. }',
  159. ['target' => PhpUnitTargetVersion::VERSION_4_8],
  160. ];
  161. yield [
  162. '<?php
  163. final class MyTest extends \PHPUnit\Framework\TestCase
  164. {
  165. public function aaa()
  166. {
  167. $a = new PHPUnit\Framework\Assert();
  168. $b = new PHPUnit\Framework\BaseTestListener();
  169. $c = new PHPUnit\Framework\TestListener();
  170. $d1 = new PHPUnit_Aaa();
  171. $d2 = new PHPUnit_Aaa_Bbb();
  172. $d3 = new PHPUnit_Aaa_Bbb_Ccc();
  173. $d4 = new PHPUnit_Aaa_Bbb_Ccc_Ddd();
  174. $d5 = new PHPUnit_Aaa_Bbb_Ccc_Ddd_Eee();
  175. }
  176. }',
  177. '<?php
  178. final class MyTest extends \PHPUnit_Framework_TestCase
  179. {
  180. public function aaa()
  181. {
  182. $a = new PHPUnit_Framework_Assert();
  183. $b = new PHPUnit_Framework_BaseTestListener();
  184. $c = new PHPUnit_Framework_TestListener();
  185. $d1 = new PHPUnit_Aaa();
  186. $d2 = new PHPUnit_Aaa_Bbb();
  187. $d3 = new PHPUnit_Aaa_Bbb_Ccc();
  188. $d4 = new PHPUnit_Aaa_Bbb_Ccc_Ddd();
  189. $d5 = new PHPUnit_Aaa_Bbb_Ccc_Ddd_Eee();
  190. }
  191. }',
  192. ['target' => PhpUnitTargetVersion::VERSION_5_7],
  193. ];
  194. yield [
  195. '<?php
  196. final class MyTest extends \PHPUnit\Framework\TestCase
  197. {
  198. public function aaa()
  199. {
  200. $a = new PHPUnit\Framework\Assert();
  201. $b = new PHPUnit\Framework\BaseTestListener();
  202. $c = new PHPUnit\Framework\TestListener();
  203. $d1 = new PHPUnit\Aaa();
  204. $d2 = new PHPUnit\Aaa\Bbb();
  205. $d3 = new PHPUnit\Aaa\Bbb\Ccc();
  206. $d4 = new PHPUnit\Aaa\Bbb\Ccc\Ddd();
  207. $d5 = new PHPUnit\Aaa\Bbb\Ccc\Ddd\Eee();
  208. }
  209. }',
  210. '<?php
  211. final class MyTest extends \PHPUnit_Framework_TestCase
  212. {
  213. public function aaa()
  214. {
  215. $a = new PHPUnit_Framework_Assert();
  216. $b = new PHPUnit_Framework_BaseTestListener();
  217. $c = new PHPUnit_Framework_TestListener();
  218. $d1 = new PHPUnit_Aaa();
  219. $d2 = new PHPUnit_Aaa_Bbb();
  220. $d3 = new PHPUnit_Aaa_Bbb_Ccc();
  221. $d4 = new PHPUnit_Aaa_Bbb_Ccc_Ddd();
  222. $d5 = new PHPUnit_Aaa_Bbb_Ccc_Ddd_Eee();
  223. }
  224. }',
  225. ['target' => PhpUnitTargetVersion::VERSION_6_0],
  226. ];
  227. yield [
  228. '<?php
  229. echo \PHPUnit\Runner\Version::id();
  230. echo \PHPUnit\Runner\Version::id();
  231. ',
  232. '<?php
  233. echo \PHPUnit_Runner_Version::id();
  234. echo \PHPUnit_Runner_Version::id();
  235. ',
  236. ];
  237. yield [
  238. '<?php
  239. final class MyTest extends TestCase
  240. {
  241. const PHPUNIT_FOO = "foo";
  242. }',
  243. ];
  244. yield [
  245. '<?php
  246. final class MyTest extends TestCase
  247. {
  248. const FOO = Bar::PHPUNIT_FOO;
  249. }',
  250. ];
  251. yield [
  252. '<?php
  253. define(\'PHPUNIT_COMPOSER_INSTALL\', dirname(__DIR__).\'/vendor/autoload.php\');
  254. require PHPUNIT_COMPOSER_INSTALL;',
  255. ];
  256. }
  257. /**
  258. * @dataProvider provideFix81Cases
  259. *
  260. * @requires PHP 8.1
  261. */
  262. public function testFix81(string $expected, ?string $input = null): void
  263. {
  264. $this->doTest($expected, $input);
  265. }
  266. /**
  267. * @return iterable<array{string}>
  268. */
  269. public static function provideFix81Cases(): iterable
  270. {
  271. yield [
  272. '<?php
  273. final class MyTest extends TestCase
  274. {
  275. final public const PHPUNIT_FOO_A = "foo";
  276. final public const PHPUNIT_FOO_B = Bar::PHPUNIT_FOO;
  277. }',
  278. ];
  279. }
  280. }