PhpUnitSizeClassFixerTest.php 6.6 KB

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