PhpUnitInternalClassFixerTest.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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\Tests\Test\AbstractFixerTestCase;
  14. /**
  15. * @internal
  16. *
  17. * @author Gert de Pagter <BackEndTea@gmail.com>
  18. *
  19. * @covers \PhpCsFixer\Fixer\PhpUnit\PhpUnitInternalClassFixer
  20. */
  21. final class PhpUnitInternalClassFixerTest extends AbstractFixerTestCase
  22. {
  23. /**
  24. * @param array<string, mixed> $config
  25. *
  26. * @dataProvider provideFixCases
  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 static function provideFixCases(): iterable
  34. {
  35. yield 'It does not change normal classes' => [
  36. '<?php
  37. class Hello
  38. {
  39. }
  40. ',
  41. ];
  42. yield 'It marks a test class as internal' => [
  43. '<?php
  44. /**
  45. * @internal
  46. */
  47. class Test extends TestCase
  48. {
  49. }
  50. ',
  51. '<?php
  52. class Test extends TestCase
  53. {
  54. }
  55. ',
  56. ];
  57. yield 'It adds an internal tag to a class that already has a doc block' => [
  58. '<?php
  59. /**
  60. * @coversNothing
  61. * @internal
  62. */
  63. class Test extends TestCase
  64. {
  65. }
  66. ',
  67. '<?php
  68. /**
  69. * @coversNothing
  70. */
  71. class Test extends TestCase
  72. {
  73. }
  74. ',
  75. ];
  76. yield 'It does not change a class that is already internal' => [
  77. '<?php
  78. /**
  79. * @internal
  80. */
  81. class Test extends TestCase
  82. {
  83. }
  84. ',
  85. ];
  86. yield 'It does not change a class that is already internal and has other annotations' => [
  87. '<?php
  88. /**
  89. * @author me
  90. * @coversNothing
  91. * @internal
  92. * @group large
  93. */
  94. class Test extends TestCase
  95. {
  96. }
  97. ',
  98. ];
  99. yield 'It works on other indentation levels' => [
  100. '<?php
  101. if (class_exists("Foo\Bar")) {
  102. /**
  103. * @internal
  104. */
  105. class Test Extends TestCase
  106. {
  107. }
  108. }
  109. ',
  110. '<?php
  111. if (class_exists("Foo\Bar")) {
  112. class Test Extends TestCase
  113. {
  114. }
  115. }
  116. ',
  117. ];
  118. yield 'It works on other indentation levels when the class has other annotations' => [
  119. '<?php
  120. if (class_exists("Foo\Bar")) {
  121. /**
  122. * @author me again
  123. *
  124. *
  125. * @covers \Other\Class
  126. * @internal
  127. */
  128. class Test Extends TestCase
  129. {
  130. }
  131. }
  132. ',
  133. '<?php
  134. if (class_exists("Foo\Bar")) {
  135. /**
  136. * @author me again
  137. *
  138. *
  139. * @covers \Other\Class
  140. */
  141. class Test Extends TestCase
  142. {
  143. }
  144. }
  145. ',
  146. ];
  147. yield 'It works for tab ident' => [
  148. '<?php
  149. if (class_exists("Foo\Bar")) {
  150. /**
  151. * @author me again
  152. *
  153. *
  154. * @covers \Other\Class
  155. * @internal
  156. */
  157. class Test Extends TestCase
  158. {
  159. }
  160. }
  161. ',
  162. '<?php
  163. if (class_exists("Foo\Bar")) {
  164. /**
  165. * @author me again
  166. *
  167. *
  168. * @covers \Other\Class
  169. */
  170. class Test Extends TestCase
  171. {
  172. }
  173. }
  174. ',
  175. ];
  176. yield 'It always adds @internal to the bottom of the doc block' => [
  177. '<?php
  178. /**
  179. * @coversNothing
  180. *
  181. *
  182. *
  183. *
  184. *
  185. *
  186. *
  187. *
  188. *
  189. *
  190. *
  191. *
  192. *
  193. *
  194. * @internal
  195. */
  196. class Test extends TestCase
  197. {
  198. }
  199. ',
  200. '<?php
  201. /**
  202. * @coversNothing
  203. *
  204. *
  205. *
  206. *
  207. *
  208. *
  209. *
  210. *
  211. *
  212. *
  213. *
  214. *
  215. *
  216. *
  217. */
  218. class Test extends TestCase
  219. {
  220. }
  221. ',
  222. ];
  223. yield 'It does not change a class with a single line internal doc block' => [
  224. '<?php
  225. /** @internal */
  226. class Test extends TestCase
  227. {
  228. }
  229. ',
  230. ];
  231. yield 'It adds an internal tag to a class that already has a one linedoc block' => [
  232. '<?php
  233. /**
  234. * @coversNothing
  235. * @internal
  236. */
  237. class Test extends TestCase
  238. {
  239. }
  240. ',
  241. '<?php
  242. /** @coversNothing */
  243. class Test extends TestCase
  244. {
  245. }
  246. ',
  247. ];
  248. yield 'By default it will not mark an abstract class as internal' => [
  249. '<?php
  250. abstract class Test extends TestCase
  251. {
  252. }
  253. ',
  254. ];
  255. yield 'If abstract is added as an option, abstract classes will be marked internal' => [
  256. '<?php
  257. /**
  258. * @internal
  259. */
  260. abstract class Test extends TestCase
  261. {
  262. }
  263. ',
  264. '<?php
  265. abstract class Test extends TestCase
  266. {
  267. }
  268. ',
  269. [
  270. 'types' => ['abstract'],
  271. ],
  272. ];
  273. yield 'If final is not added as an option, final classes will not be marked internal' => [
  274. '<?php
  275. final class Test extends TestCase
  276. {
  277. }
  278. ',
  279. null,
  280. [
  281. 'types' => ['abstract'],
  282. ],
  283. ];
  284. yield 'If normal is not added as an option, normal classes will not be marked internal' => [
  285. '<?php
  286. class Test extends TestCase
  287. {
  288. }
  289. ',
  290. null,
  291. [
  292. 'types' => ['abstract'],
  293. ],
  294. ];
  295. yield 'It works correctly with multiple classes in one file, even when one of them is not allowed' => [
  296. '<?php
  297. /**
  298. * @internal
  299. */
  300. class Test extends TestCase
  301. {
  302. }
  303. abstract class Test2 extends TestCase
  304. {
  305. }
  306. class FooBar
  307. {
  308. }
  309. /**
  310. * @internal
  311. */
  312. class Test extends TestCase
  313. {
  314. }
  315. ',
  316. '<?php
  317. class Test extends TestCase
  318. {
  319. }
  320. abstract class Test2 extends TestCase
  321. {
  322. }
  323. class FooBar
  324. {
  325. }
  326. class Test extends TestCase
  327. {
  328. }
  329. ',
  330. ];
  331. }
  332. /**
  333. * @dataProvider provideFix80Cases
  334. *
  335. * @requires PHP 8.0
  336. */
  337. public function testFix80(string $expected, string $input): void
  338. {
  339. $this->doTest($expected, $input);
  340. }
  341. /**
  342. * @return iterable<string[]>
  343. */
  344. public static function provideFix80Cases(): iterable
  345. {
  346. yield 'it adds a docblock above when there is an attribute' => [
  347. '<?php
  348. /**
  349. * @internal
  350. */
  351. #[SimpleTest]
  352. class Test extends TestCase
  353. {
  354. }
  355. ',
  356. '<?php
  357. #[SimpleTest]
  358. class Test extends TestCase
  359. {
  360. }
  361. ',
  362. ];
  363. yield 'it adds the internal tag along other tags when there is an attribute' => [
  364. '<?php
  365. /**
  366. * @coversNothing
  367. * @internal
  368. */
  369. #[SimpleTest]
  370. class Test extends TestCase
  371. {
  372. }
  373. ',
  374. '<?php
  375. /**
  376. * @coversNothing
  377. */
  378. #[SimpleTest]
  379. class Test extends TestCase
  380. {
  381. }
  382. ',
  383. ];
  384. yield 'it adds a docblock above when there are attributes' => [
  385. '<?php
  386. /**
  387. * @internal
  388. */
  389. #[SimpleTest]
  390. #[Annotated]
  391. class Test extends TestCase
  392. {
  393. }
  394. ',
  395. '<?php
  396. #[SimpleTest]
  397. #[Annotated]
  398. class Test extends TestCase
  399. {
  400. }
  401. ',
  402. ];
  403. yield 'it adds the internal tag along other tags when there are attributes' => [
  404. '<?php
  405. /**
  406. * @coversNothing
  407. * @internal
  408. */
  409. #[SimpleTest]
  410. #[Annotated]
  411. class Test extends TestCase
  412. {
  413. }
  414. ',
  415. '<?php
  416. /**
  417. * @coversNothing
  418. */
  419. #[SimpleTest]
  420. #[Annotated]
  421. class Test extends TestCase
  422. {
  423. }
  424. ',
  425. ];
  426. }
  427. /**
  428. * @param array<string, mixed> $config
  429. *
  430. * @dataProvider provideFix82Cases
  431. *
  432. * @requires PHP 8.2
  433. */
  434. public function testFix82(string $expected, ?string $input = null, array $config = []): void
  435. {
  436. $this->fixer->configure($config);
  437. $this->doTest($expected, $input);
  438. }
  439. public static function provideFix82Cases(): iterable
  440. {
  441. yield 'If final is not added as an option, final classes will not be marked internal' => [
  442. '<?php
  443. final readonly class Test extends TestCase
  444. {}
  445. ',
  446. null,
  447. [
  448. 'types' => ['normal'],
  449. ],
  450. ];
  451. yield [
  452. '<?php
  453. /**
  454. * @internal
  455. */
  456. readonly final class Test extends TestCase {}',
  457. '<?php
  458. readonly final class Test extends TestCase {}',
  459. ];
  460. }
  461. }