PhpUnitTestClassRequiresCoversFixerTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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. use PhpCsFixer\WhitespacesFixerConfig;
  15. /**
  16. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  17. *
  18. * @internal
  19. *
  20. * @covers \PhpCsFixer\Fixer\AbstractPhpUnitFixer
  21. * @covers \PhpCsFixer\Fixer\PhpUnit\PhpUnitTestClassRequiresCoversFixer
  22. *
  23. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\PhpUnit\PhpUnitTestClassRequiresCoversFixer>
  24. */
  25. final class PhpUnitTestClassRequiresCoversFixerTest extends AbstractFixerTestCase
  26. {
  27. /**
  28. * @dataProvider provideFixCases
  29. */
  30. public function testFix(string $expected, ?string $input = null): void
  31. {
  32. $this->doTest($expected, $input);
  33. }
  34. /**
  35. * @return iterable<int|string, array{0: string, 1?: string}>
  36. */
  37. public static function provideFixCases(): iterable
  38. {
  39. yield 'already with annotation: @covers' => [
  40. '<?php
  41. /**
  42. * @covers Foo
  43. */
  44. class FooTest extends \PHPUnit_Framework_TestCase {}
  45. ',
  46. ];
  47. yield 'already with annotation: @coversDefaultClass' => [
  48. '<?php
  49. /**
  50. * @coversDefaultClass
  51. */
  52. class FooTest extends \PHPUnit_Framework_TestCase {}
  53. ',
  54. ];
  55. yield 'without docblock #1' => [
  56. '<?php
  57. /**
  58. * @coversNothing
  59. */
  60. class FooTest extends \PHPUnit_Framework_TestCase {}
  61. ',
  62. '<?php
  63. class FooTest extends \PHPUnit_Framework_TestCase {}
  64. ',
  65. ];
  66. yield 'without docblock #2 (class is final)' => [
  67. '<?php
  68. /**
  69. * @coversNothing
  70. */
  71. final class FooTest extends \PHPUnit_Framework_TestCase {}
  72. ',
  73. '<?php
  74. final class FooTest extends \PHPUnit_Framework_TestCase {}
  75. ',
  76. ];
  77. yield 'without docblock #2 (class is abstract)' => [
  78. '<?php
  79. abstract class FooTest extends \PHPUnit_Framework_TestCase {}
  80. ',
  81. ];
  82. yield 'with docblock but annotation is missing' => [
  83. '<?php
  84. /**
  85. * Description.
  86. *
  87. * @since v2.2
  88. * @coversNothing
  89. */
  90. final class FooTest extends \PHPUnit_Framework_TestCase {}
  91. ',
  92. '<?php
  93. /**
  94. * Description.
  95. *
  96. * @since v2.2
  97. */
  98. final class FooTest extends \PHPUnit_Framework_TestCase {}
  99. ',
  100. ];
  101. yield 'with one-line docblock but annotation is missing' => [
  102. '<?php
  103. /**
  104. * Description.
  105. * @coversNothing
  106. */
  107. final class FooTest extends \PHPUnit_Framework_TestCase {}
  108. ',
  109. '<?php
  110. /** Description. */
  111. final class FooTest extends \PHPUnit_Framework_TestCase {}
  112. ',
  113. ];
  114. yield 'with 2-lines docblock but annotation is missing #1' => [
  115. '<?php
  116. /** Description.
  117. * @coversNothing
  118. */
  119. final class FooTest extends \PHPUnit_Framework_TestCase {}
  120. ',
  121. '<?php
  122. /** Description.
  123. */
  124. final class FooTest extends \PHPUnit_Framework_TestCase {}
  125. ',
  126. ];
  127. yield 'with 2-lines docblock but annotation is missing #2' => [
  128. '<?php
  129. /**
  130. * @coversNothing
  131. * Description. */
  132. final class FooTest extends \PHPUnit_Framework_TestCase {}
  133. ',
  134. '<?php
  135. /**
  136. * Description. */
  137. final class FooTest extends \PHPUnit_Framework_TestCase {}
  138. ',
  139. ];
  140. yield 'with comment instead of docblock' => [
  141. '<?php
  142. /*
  143. * @covers Foo
  144. */
  145. /**
  146. * @coversNothing
  147. */
  148. class FooTest extends \PHPUnit_Framework_TestCase {}
  149. ',
  150. '<?php
  151. /*
  152. * @covers Foo
  153. */
  154. class FooTest extends \PHPUnit_Framework_TestCase {}
  155. ',
  156. ];
  157. yield 'not a test class' => [
  158. '<?php
  159. class Foo {}
  160. ',
  161. ];
  162. yield 'multiple classes in one file' => [
  163. '<?php /** */
  164. use \PHPUnit\Framework\TestCase;
  165. /**
  166. * Foo
  167. * @coversNothing
  168. */
  169. class FooTest extends \PHPUnit_Framework_TestCase {}
  170. class Bar {}
  171. /**
  172. * @coversNothing
  173. */
  174. class Baz1 extends PHPUnit_Framework_TestCase {}
  175. /**
  176. * @coversNothing
  177. */
  178. class Baz2 extends \PHPUnit_Framework_TestCase {}
  179. /**
  180. * @coversNothing
  181. */
  182. class Baz3 extends \PHPUnit\Framework\TestCase {}
  183. /**
  184. * @coversNothing
  185. */
  186. class Baz4 extends TestCase {}
  187. ',
  188. '<?php /** */
  189. use \PHPUnit\Framework\TestCase;
  190. /**
  191. * Foo
  192. */
  193. class FooTest extends \PHPUnit_Framework_TestCase {}
  194. class Bar {}
  195. class Baz1 extends PHPUnit_Framework_TestCase {}
  196. class Baz2 extends \PHPUnit_Framework_TestCase {}
  197. class Baz3 extends \PHPUnit\Framework\TestCase {}
  198. class Baz4 extends TestCase {}
  199. ',
  200. ];
  201. yield [
  202. '<?php /* comment */
  203. /**
  204. * @coversNothing
  205. */
  206. class FooTest extends \PHPUnit_Framework_TestCase {}
  207. ',
  208. '<?php /* comment */class FooTest extends \PHPUnit_Framework_TestCase {}
  209. ',
  210. ];
  211. }
  212. /**
  213. * @dataProvider provideWithWhitespacesConfigCases
  214. */
  215. public function testWithWhitespacesConfig(string $expected, ?string $input = null): void
  216. {
  217. $expected = str_replace([' ', "\n"], ["\t", "\r\n"], $expected);
  218. if (null !== $input) {
  219. $input = str_replace([' ', "\n"], ["\t", "\r\n"], $input);
  220. }
  221. $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n"));
  222. $this->doTest($expected, $input);
  223. }
  224. /**
  225. * @return iterable<array{string, string}>
  226. */
  227. public static function provideWithWhitespacesConfigCases(): iterable
  228. {
  229. yield [
  230. '<?php
  231. /**
  232. * @coversNothing
  233. */
  234. class FooTest extends \PHPUnit_Framework_TestCase {}
  235. ',
  236. '<?php
  237. class FooTest extends \PHPUnit_Framework_TestCase {}
  238. ',
  239. ];
  240. }
  241. /**
  242. * @dataProvider provideFix80Cases
  243. *
  244. * @requires PHP 8.0
  245. */
  246. public function testFix80(string $expected, ?string $input = null): void
  247. {
  248. $this->doTest($expected, $input);
  249. }
  250. /**
  251. * @return iterable<array{0: string, 1?: string}>
  252. */
  253. public static function provideFix80Cases(): iterable
  254. {
  255. yield 'already with attribute CoversClass' => [
  256. <<<'PHP'
  257. <?php
  258. #[PHPUnit\Framework\Attributes\CoversClass(Foo::class)]
  259. class FooTest extends \PHPUnit_Framework_TestCase {}
  260. PHP,
  261. ];
  262. yield 'already with attribute CoversNothing' => [
  263. <<<'PHP'
  264. <?php
  265. #[PHPUnit\Framework\Attributes\CoversNothing]
  266. class FooTest extends \PHPUnit_Framework_TestCase {}
  267. PHP,
  268. ];
  269. yield 'already with attribute CoversNothing with leading slash' => [
  270. <<<'PHP'
  271. <?php
  272. #[\PHPUnit\Framework\Attributes\CoversNothing]
  273. class FooTest extends \PHPUnit_Framework_TestCase {}
  274. PHP,
  275. ];
  276. yield 'already with imported attribute' => [
  277. <<<'PHP'
  278. <?php
  279. use PHPUnit\Framework\TestCase;
  280. use PHPUnit\Framework\Attributes\CoversClass;
  281. #[CoversClass(Foo::class)]
  282. class FooTest extends TestCase {}
  283. PHP,
  284. ];
  285. yield 'already with partially imported attribute' => [
  286. <<<'PHP'
  287. <?php
  288. use PHPUnit\Framework\Attributes;
  289. #[Attributes\CoversClass(Foo::class)]
  290. class FooTest extends \PHPUnit_Framework_TestCase {}
  291. PHP,
  292. ];
  293. yield 'already with aliased attribute' => [
  294. <<<'PHP'
  295. <?php
  296. use PHPUnit\Framework\Attributes\CoversClass as PHPUnitCoversClass;
  297. #[PHPUnitCoversClass(Foo::class)]
  298. class FooTest extends \PHPUnit_Framework_TestCase {}
  299. PHP,
  300. ];
  301. yield 'already with partially aliased attribute' => [
  302. <<<'PHP'
  303. <?php
  304. use PHPUnit\Framework\Attributes as PHPUnitAttributes;
  305. #[PHPUnitAttributes\CoversClass(Foo::class)]
  306. class FooTest extends \PHPUnit_Framework_TestCase {}
  307. PHP,
  308. ];
  309. yield 'with attribute from different namespace' => [
  310. <<<'PHP'
  311. <?php
  312. use Foo\CoversClass;
  313. use PHPUnit\Framework\Attributes\CoversClass as PHPUnitCoversClass;
  314. /**
  315. * @coversNothing
  316. */
  317. #[CoversClass(Foo::class)]
  318. class FooTest extends \PHPUnit_Framework_TestCase {}
  319. PHP,
  320. <<<'PHP'
  321. <?php
  322. use Foo\CoversClass;
  323. use PHPUnit\Framework\Attributes\CoversClass as PHPUnitCoversClass;
  324. #[CoversClass(Foo::class)]
  325. class FooTest extends \PHPUnit_Framework_TestCase {}
  326. PHP,
  327. ];
  328. yield 'with attribute on final class' => [
  329. <<<'PHP'
  330. <?php
  331. #[PHPUnit\Framework\Attributes\CoversNothing]
  332. final class FooTest extends \PHPUnit_Framework_TestCase {}
  333. PHP,
  334. ];
  335. yield 'already with attribute CoversMethod' => [
  336. <<<'PHP'
  337. <?php
  338. #[PHPUnit\Framework\Attributes\CoversMethod(Foo::class, 'bar')]
  339. class FooTest extends \PHPUnit_Framework_TestCase {}
  340. PHP,
  341. ];
  342. yield 'already with imported CoversMethod attribute' => [
  343. <<<'PHP'
  344. <?php
  345. use PHPUnit\Framework\TestCase;
  346. use PHPUnit\Framework\Attributes\CoversMethod;
  347. #[CoversMethod(Foo::class, 'bar')]
  348. class FooTest extends TestCase {}
  349. PHP,
  350. ];
  351. yield 'already with partially imported CoversMethod attribute' => [
  352. <<<'PHP'
  353. <?php
  354. use PHPUnit\Framework\Attributes;
  355. #[Attributes\CoversMethod(Foo::class, 'bar')]
  356. class FooTest extends \PHPUnit_Framework_TestCase {}
  357. PHP,
  358. ];
  359. yield 'already with aliased CoversMethod attribute' => [
  360. <<<'PHP'
  361. <?php
  362. use PHPUnit\Framework\Attributes\CoversMethod as PHPUnitCoversMethod;
  363. #[PHPUnitCoversMethod(Foo::class, 'bar')]
  364. class FooTest extends \PHPUnit_Framework_TestCase {}
  365. PHP,
  366. ];
  367. yield 'already with partially aliased CoversMethod attribute' => [
  368. <<<'PHP'
  369. <?php
  370. use PHPUnit\Framework\Attributes as PHPUnitAttributes;
  371. #[PHPUnitAttributes\CoversMethod(Foo::class, 'bar')]
  372. class FooTest extends \PHPUnit_Framework_TestCase {}
  373. PHP,
  374. ];
  375. yield 'already with attribute CoversFunction' => [
  376. <<<'PHP'
  377. <?php
  378. #[PHPUnit\Framework\Attributes\CoversFunction('bar')]
  379. class FooTest extends \PHPUnit_Framework_TestCase {}
  380. PHP,
  381. ];
  382. yield 'already with imported CoversFunction attribute' => [
  383. <<<'PHP'
  384. <?php
  385. use PHPUnit\Framework\TestCase;
  386. use PHPUnit\Framework\Attributes\CoversFunction;
  387. #[CoversFunction('bar')]
  388. class FooTest extends TestCase {}
  389. PHP,
  390. ];
  391. yield 'already with partially imported CoversFunction attribute' => [
  392. <<<'PHP'
  393. <?php
  394. use PHPUnit\Framework\Attributes;
  395. #[Attributes\CoversFunction('bar')]
  396. class FooTest extends \PHPUnit_Framework_TestCase {}
  397. PHP,
  398. ];
  399. yield 'already with aliased CoversFunction attribute' => [
  400. <<<'PHP'
  401. <?php
  402. use PHPUnit\Framework\Attributes\CoversFunction as PHPUnitCoversFunction;
  403. #[PHPUnitCoversFunction('bar')]
  404. class FooTest extends \PHPUnit_Framework_TestCase {}
  405. PHP,
  406. ];
  407. yield 'already with partially aliased CoversFunction attribute' => [
  408. <<<'PHP'
  409. <?php
  410. use PHPUnit\Framework\Attributes as PHPUnitAttributes;
  411. #[PHPUnitAttributes\CoversFunction('bar')]
  412. class FooTest extends \PHPUnit_Framework_TestCase {}
  413. PHP,
  414. ];
  415. }
  416. /**
  417. * @dataProvider provideFix82Cases
  418. *
  419. * @requires PHP 8.2
  420. */
  421. public function testFix82(string $expected, ?string $input = null): void
  422. {
  423. $this->doTest($expected, $input);
  424. }
  425. /**
  426. * @return iterable<string, array{0: string, 1?: null|string}>
  427. */
  428. public static function provideFix82Cases(): iterable
  429. {
  430. yield 'without docblock #2 (class is final)' => [
  431. '<?php
  432. /**
  433. * @coversNothing
  434. */
  435. readonly final class BarTest extends \PHPUnit_Framework_TestCase {}
  436. ',
  437. '<?php
  438. readonly final class BarTest extends \PHPUnit_Framework_TestCase {}
  439. ',
  440. ];
  441. yield 'without docblock #2 (class is abstract)' => [
  442. '<?php
  443. abstract readonly class FooTest extends \PHPUnit_Framework_TestCase {}
  444. ',
  445. null,
  446. ];
  447. yield 'with attribute on readonly class' => [
  448. <<<'PHP'
  449. <?php
  450. #[PHPUnit\Framework\Attributes\CoversNothing]
  451. readonly class FooTest extends \PHPUnit_Framework_TestCase {}
  452. PHP,
  453. ];
  454. yield 'with attribute on final readonly class' => [
  455. <<<'PHP'
  456. <?php
  457. #[PHPUnit\Framework\Attributes\CoversNothing]
  458. final readonly class FooTest extends \PHPUnit_Framework_TestCase {}
  459. PHP,
  460. ];
  461. }
  462. }