PhpUnitTestClassRequiresCoversFixerTest.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. final class PhpUnitTestClassRequiresCoversFixerTest extends AbstractFixerTestCase
  24. {
  25. /**
  26. * @dataProvider provideFixCases
  27. */
  28. public function testFix(string $expected, ?string $input = null): void
  29. {
  30. $this->doTest($expected, $input);
  31. }
  32. public static function provideFixCases(): iterable
  33. {
  34. yield 'already with annotation: @covers' => [
  35. '<?php
  36. /**
  37. * @covers Foo
  38. */
  39. class FooTest extends \PHPUnit_Framework_TestCase {}
  40. ',
  41. ];
  42. yield 'already with annotation: @coversDefaultClass' => [
  43. '<?php
  44. /**
  45. * @coversDefaultClass
  46. */
  47. class FooTest extends \PHPUnit_Framework_TestCase {}
  48. ',
  49. ];
  50. yield 'without docblock #1' => [
  51. '<?php
  52. /**
  53. * @coversNothing
  54. */
  55. class FooTest extends \PHPUnit_Framework_TestCase {}
  56. ',
  57. '<?php
  58. class FooTest extends \PHPUnit_Framework_TestCase {}
  59. ',
  60. ];
  61. yield 'without docblock #2 (class is final)' => [
  62. '<?php
  63. /**
  64. * @coversNothing
  65. */
  66. final class FooTest extends \PHPUnit_Framework_TestCase {}
  67. ',
  68. '<?php
  69. final class FooTest extends \PHPUnit_Framework_TestCase {}
  70. ',
  71. ];
  72. yield 'without docblock #2 (class is abstract)' => [
  73. '<?php
  74. abstract class FooTest extends \PHPUnit_Framework_TestCase {}
  75. ',
  76. ];
  77. yield 'with docblock but annotation is missing' => [
  78. '<?php
  79. /**
  80. * Description.
  81. *
  82. * @since v2.2
  83. * @coversNothing
  84. */
  85. final class FooTest extends \PHPUnit_Framework_TestCase {}
  86. ',
  87. '<?php
  88. /**
  89. * Description.
  90. *
  91. * @since v2.2
  92. */
  93. final class FooTest extends \PHPUnit_Framework_TestCase {}
  94. ',
  95. ];
  96. yield 'with one-line docblock but annotation is missing' => [
  97. '<?php
  98. /**
  99. * Description.
  100. * @coversNothing
  101. */
  102. final class FooTest extends \PHPUnit_Framework_TestCase {}
  103. ',
  104. '<?php
  105. /** Description. */
  106. final class FooTest extends \PHPUnit_Framework_TestCase {}
  107. ',
  108. ];
  109. yield 'with 2-lines docblock but annotation is missing #1' => [
  110. '<?php
  111. /** Description.
  112. * @coversNothing
  113. */
  114. final class FooTest extends \PHPUnit_Framework_TestCase {}
  115. ',
  116. '<?php
  117. /** Description.
  118. */
  119. final class FooTest extends \PHPUnit_Framework_TestCase {}
  120. ',
  121. ];
  122. yield 'with 2-lines docblock but annotation is missing #2' => [
  123. '<?php
  124. /**
  125. * @coversNothing
  126. * Description. */
  127. final class FooTest extends \PHPUnit_Framework_TestCase {}
  128. ',
  129. '<?php
  130. /**
  131. * Description. */
  132. final class FooTest extends \PHPUnit_Framework_TestCase {}
  133. ',
  134. ];
  135. yield 'with comment instead of docblock' => [
  136. '<?php
  137. /*
  138. * @covers Foo
  139. */
  140. /**
  141. * @coversNothing
  142. */
  143. class FooTest extends \PHPUnit_Framework_TestCase {}
  144. ',
  145. '<?php
  146. /*
  147. * @covers Foo
  148. */
  149. class FooTest extends \PHPUnit_Framework_TestCase {}
  150. ',
  151. ];
  152. yield 'not a test class' => [
  153. '<?php
  154. class Foo {}
  155. ',
  156. ];
  157. yield 'multiple classes in one file' => [
  158. '<?php /** */
  159. use \PHPUnit\Framework\TestCase;
  160. /**
  161. * Foo
  162. * @coversNothing
  163. */
  164. class FooTest extends \PHPUnit_Framework_TestCase {}
  165. class Bar {}
  166. /**
  167. * @coversNothing
  168. */
  169. class Baz1 extends PHPUnit_Framework_TestCase {}
  170. /**
  171. * @coversNothing
  172. */
  173. class Baz2 extends \PHPUnit_Framework_TestCase {}
  174. /**
  175. * @coversNothing
  176. */
  177. class Baz3 extends \PHPUnit\Framework\TestCase {}
  178. /**
  179. * @coversNothing
  180. */
  181. class Baz4 extends TestCase {}
  182. ',
  183. '<?php /** */
  184. use \PHPUnit\Framework\TestCase;
  185. /**
  186. * Foo
  187. */
  188. class FooTest extends \PHPUnit_Framework_TestCase {}
  189. class Bar {}
  190. class Baz1 extends PHPUnit_Framework_TestCase {}
  191. class Baz2 extends \PHPUnit_Framework_TestCase {}
  192. class Baz3 extends \PHPUnit\Framework\TestCase {}
  193. class Baz4 extends TestCase {}
  194. ',
  195. ];
  196. yield [
  197. '<?php /* comment */
  198. /**
  199. * @coversNothing
  200. */
  201. class FooTest extends \PHPUnit_Framework_TestCase {}
  202. ',
  203. '<?php /* comment */class FooTest extends \PHPUnit_Framework_TestCase {}
  204. ',
  205. ];
  206. }
  207. /**
  208. * @dataProvider provideMessyWhitespacesCases
  209. */
  210. public function testMessyWhitespaces(string $expected, ?string $input = null): void
  211. {
  212. $expected = str_replace([' ', "\n"], ["\t", "\r\n"], $expected);
  213. if (null !== $input) {
  214. $input = str_replace([' ', "\n"], ["\t", "\r\n"], $input);
  215. }
  216. $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n"));
  217. $this->doTest($expected, $input);
  218. }
  219. public static function provideMessyWhitespacesCases(): iterable
  220. {
  221. yield [
  222. '<?php
  223. /**
  224. * @coversNothing
  225. */
  226. class FooTest extends \PHPUnit_Framework_TestCase {}
  227. ',
  228. '<?php
  229. class FooTest extends \PHPUnit_Framework_TestCase {}
  230. ',
  231. ];
  232. }
  233. /**
  234. * @dataProvider provideFix82Cases
  235. *
  236. * @requires PHP 8.2
  237. */
  238. public function testFix82(string $expected, ?string $input): void
  239. {
  240. $this->doTest($expected, $input);
  241. }
  242. public static function provideFix82Cases(): iterable
  243. {
  244. yield 'without docblock #2 (class is final)' => [
  245. '<?php
  246. /**
  247. * @coversNothing
  248. */
  249. readonly final class BarTest extends \PHPUnit_Framework_TestCase {}
  250. ',
  251. '<?php
  252. readonly final class BarTest extends \PHPUnit_Framework_TestCase {}
  253. ',
  254. ];
  255. yield 'without docblock #2 (class is abstract)' => [
  256. '<?php
  257. abstract readonly class FooTest extends \PHPUnit_Framework_TestCase {}
  258. ',
  259. null,
  260. ];
  261. }
  262. }