PhpUnitInternalClassFixerTest.php 8.6 KB

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