PhpUnitNamespacedFixerTest.php 8.5 KB

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