PhpUnitTestClassRequiresCoversFixerTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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\PhpUnit\PhpUnitTestClassRequiresCoversFixer
  21. */
  22. final class PhpUnitTestClassRequiresCoversFixerTest extends AbstractFixerTestCase
  23. {
  24. /**
  25. * @dataProvider provideFixCases
  26. */
  27. public function testFix(string $expected, ?string $input = null): void
  28. {
  29. $this->doTest($expected, $input);
  30. }
  31. public function provideFixCases(): array
  32. {
  33. return [
  34. 'already with annotation: @covers' => [
  35. '<?php
  36. /**
  37. * @covers Foo
  38. */
  39. class FooTest extends \PHPUnit_Framework_TestCase {}
  40. ',
  41. ],
  42. 'already with annotation: @coversDefaultClass' => [
  43. '<?php
  44. /**
  45. * @coversDefaultClass
  46. */
  47. class FooTest extends \PHPUnit_Framework_TestCase {}
  48. ',
  49. ],
  50. '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. '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. 'without docblock #2 (class is abstract)' => [
  73. '<?php
  74. abstract class FooTest extends \PHPUnit_Framework_TestCase {}
  75. ',
  76. ],
  77. '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. 'with one-line docblock but annotation is missing' => [
  97. '<?php
  98. /** Description. */
  99. final class FooTest extends \PHPUnit_Framework_TestCase {}
  100. ',
  101. ],
  102. 'with 2-lines docblock but annotation is missing #1' => [
  103. '<?php
  104. /** Description.
  105. * @coversNothing
  106. */
  107. final class FooTest extends \PHPUnit_Framework_TestCase {}
  108. ',
  109. '<?php
  110. /** Description.
  111. */
  112. final class FooTest extends \PHPUnit_Framework_TestCase {}
  113. ',
  114. ],
  115. 'with 2-lines docblock but annotation is missing #2' => [
  116. '<?php
  117. /**
  118. * @coversNothing
  119. * Description. */
  120. final class FooTest extends \PHPUnit_Framework_TestCase {}
  121. ',
  122. '<?php
  123. /**
  124. * Description. */
  125. final class FooTest extends \PHPUnit_Framework_TestCase {}
  126. ',
  127. ],
  128. 'with comment instead of docblock' => [
  129. '<?php
  130. /*
  131. * @covers Foo
  132. */
  133. /**
  134. * @coversNothing
  135. */
  136. class FooTest extends \PHPUnit_Framework_TestCase {}
  137. ',
  138. '<?php
  139. /*
  140. * @covers Foo
  141. */
  142. class FooTest extends \PHPUnit_Framework_TestCase {}
  143. ',
  144. ],
  145. 'not a test class' => [
  146. '<?php
  147. class Foo {}
  148. ',
  149. ],
  150. 'multiple classes in one file' => [
  151. '<?php /** */
  152. use \PHPUnit\Framework\TestCase;
  153. /**
  154. * Foo
  155. * @coversNothing
  156. */
  157. class FooTest extends \PHPUnit_Framework_TestCase {}
  158. class Bar {}
  159. /**
  160. * @coversNothing
  161. */
  162. class Baz1 extends PHPUnit_Framework_TestCase {}
  163. /**
  164. * @coversNothing
  165. */
  166. class Baz2 extends \PHPUnit_Framework_TestCase {}
  167. /**
  168. * @coversNothing
  169. */
  170. class Baz3 extends \PHPUnit\Framework\TestCase {}
  171. /**
  172. * @coversNothing
  173. */
  174. class Baz4 extends TestCase {}
  175. ',
  176. '<?php /** */
  177. use \PHPUnit\Framework\TestCase;
  178. /**
  179. * Foo
  180. */
  181. class FooTest extends \PHPUnit_Framework_TestCase {}
  182. class Bar {}
  183. class Baz1 extends PHPUnit_Framework_TestCase {}
  184. class Baz2 extends \PHPUnit_Framework_TestCase {}
  185. class Baz3 extends \PHPUnit\Framework\TestCase {}
  186. class Baz4 extends TestCase {}
  187. ',
  188. ],
  189. [
  190. '<?php /* comment */
  191. /**
  192. * @coversNothing
  193. */
  194. class FooTest extends \PHPUnit_Framework_TestCase {}
  195. ',
  196. '<?php /* comment */class FooTest extends \PHPUnit_Framework_TestCase {}
  197. ',
  198. ],
  199. ];
  200. }
  201. /**
  202. * @dataProvider provideMessyWhitespacesCases
  203. */
  204. public function testMessyWhitespaces(string $expected, ?string $input = null): void
  205. {
  206. $expected = str_replace([' ', "\n"], ["\t", "\r\n"], $expected);
  207. if (null !== $input) {
  208. $input = str_replace([' ', "\n"], ["\t", "\r\n"], $input);
  209. }
  210. $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n"));
  211. $this->doTest($expected, $input);
  212. }
  213. public function provideMessyWhitespacesCases(): array
  214. {
  215. return [
  216. [
  217. '<?php
  218. /**
  219. * @coversNothing
  220. */
  221. class FooTest extends \PHPUnit_Framework_TestCase {}
  222. ',
  223. '<?php
  224. class FooTest extends \PHPUnit_Framework_TestCase {}
  225. ',
  226. ],
  227. ];
  228. }
  229. }