PhpUnitSizeClassFixerTest.php 7.7 KB

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