PhpUnitTestClassRequiresCoversFixerTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <?php
  2. /*
  3. * This file is part of PHP CS Fixer.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. namespace PhpCsFixer\Tests\Fixer\PhpUnit;
  12. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  13. use PhpCsFixer\WhitespacesFixerConfig;
  14. /**
  15. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  16. *
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Fixer\PhpUnit\PhpUnitTestClassRequiresCoversFixer
  20. */
  21. final class PhpUnitTestClassRequiresCoversFixerTest extends AbstractFixerTestCase
  22. {
  23. /**
  24. * @param string $expected
  25. * @param null|string $input
  26. *
  27. * @dataProvider provideFixCases
  28. */
  29. public function testFix($expected, $input = null)
  30. {
  31. $this->doTest($expected, $input);
  32. }
  33. public function provideFixCases()
  34. {
  35. return [
  36. 'already with annotation: @covers' => [
  37. '<?php
  38. /**
  39. * @covers Foo
  40. */
  41. class FooTest extends \PHPUnit_Framework_TestCase {}
  42. ',
  43. ],
  44. 'already with annotation: @coversDefaultClass' => [
  45. '<?php
  46. /**
  47. * @coversDefaultClass
  48. */
  49. class FooTest extends \PHPUnit_Framework_TestCase {}
  50. ',
  51. ],
  52. 'without docblock #1' => [
  53. '<?php
  54. /**
  55. * @coversNothing
  56. */
  57. class FooTest extends \PHPUnit_Framework_TestCase {}
  58. ',
  59. '<?php
  60. class FooTest extends \PHPUnit_Framework_TestCase {}
  61. ',
  62. ],
  63. 'without docblock #2 (class is final)' => [
  64. '<?php
  65. /**
  66. * @coversNothing
  67. */
  68. final class FooTest extends \PHPUnit_Framework_TestCase {}
  69. ',
  70. '<?php
  71. final class FooTest extends \PHPUnit_Framework_TestCase {}
  72. ',
  73. ],
  74. 'without docblock #2 (class is abstract)' => [
  75. '<?php
  76. abstract class FooTest extends \PHPUnit_Framework_TestCase {}
  77. ',
  78. ],
  79. 'with docblock but annotation is missing' => [
  80. '<?php
  81. /**
  82. * Description.
  83. *
  84. * @since v2.2
  85. * @coversNothing
  86. */
  87. final class FooTest extends \PHPUnit_Framework_TestCase {}
  88. ',
  89. '<?php
  90. /**
  91. * Description.
  92. *
  93. * @since v2.2
  94. */
  95. final class FooTest extends \PHPUnit_Framework_TestCase {}
  96. ',
  97. ],
  98. 'with one-line docblock but annotation is missing' => [
  99. '<?php
  100. /** Description. */
  101. final class FooTest extends \PHPUnit_Framework_TestCase {}
  102. ',
  103. ],
  104. 'with 2-lines docblock but annotation is missing #1' => [
  105. '<?php
  106. /** Description.
  107. * @coversNothing
  108. */
  109. final class FooTest extends \PHPUnit_Framework_TestCase {}
  110. ',
  111. '<?php
  112. /** Description.
  113. */
  114. final class FooTest extends \PHPUnit_Framework_TestCase {}
  115. ',
  116. ],
  117. 'with 2-lines docblock but annotation is missing #2' => [
  118. '<?php
  119. /**
  120. * @coversNothing
  121. * Description. */
  122. final class FooTest extends \PHPUnit_Framework_TestCase {}
  123. ',
  124. '<?php
  125. /**
  126. * Description. */
  127. final class FooTest extends \PHPUnit_Framework_TestCase {}
  128. ',
  129. ],
  130. 'with comment instead of docblock' => [
  131. '<?php
  132. /*
  133. * @covers Foo
  134. */
  135. /**
  136. * @coversNothing
  137. */
  138. class FooTest extends \PHPUnit_Framework_TestCase {}
  139. ',
  140. '<?php
  141. /*
  142. * @covers Foo
  143. */
  144. class FooTest extends \PHPUnit_Framework_TestCase {}
  145. ',
  146. ],
  147. 'not a test class' => [
  148. '<?php
  149. class Foo {}
  150. ',
  151. ],
  152. 'multiple classes in one file' => [
  153. '<?php /** */
  154. use \PHPUnit\Framework\TestCase;
  155. /**
  156. * Foo
  157. * @coversNothing
  158. */
  159. class FooTest extends \PHPUnit_Framework_TestCase {}
  160. class Bar {}
  161. /**
  162. * @coversNothing
  163. */
  164. class Baz1 extends PHPUnit_Framework_TestCase {}
  165. /**
  166. * @coversNothing
  167. */
  168. class Baz2 extends \PHPUnit_Framework_TestCase {}
  169. /**
  170. * @coversNothing
  171. */
  172. class Baz3 extends \PHPUnit\Framework\TestCase {}
  173. /**
  174. * @coversNothing
  175. */
  176. class Baz4 extends TestCase {}
  177. ',
  178. '<?php /** */
  179. use \PHPUnit\Framework\TestCase;
  180. /**
  181. * Foo
  182. */
  183. class FooTest extends \PHPUnit_Framework_TestCase {}
  184. class Bar {}
  185. class Baz1 extends PHPUnit_Framework_TestCase {}
  186. class Baz2 extends \PHPUnit_Framework_TestCase {}
  187. class Baz3 extends \PHPUnit\Framework\TestCase {}
  188. class Baz4 extends TestCase {}
  189. ',
  190. ],
  191. ];
  192. }
  193. /**
  194. * @param string $expected
  195. * @param null|string $input
  196. *
  197. * @dataProvider provideMessyWhitespacesCases
  198. */
  199. public function testMessyWhitespaces($expected, $input = null)
  200. {
  201. $expected = str_replace([' ', "\n"], ["\t", "\r\n"], $expected);
  202. if (null !== $input) {
  203. $input = str_replace([' ', "\n"], ["\t", "\r\n"], $input);
  204. }
  205. $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n"));
  206. $this->doTest($expected, $input);
  207. }
  208. public function provideMessyWhitespacesCases()
  209. {
  210. return [
  211. [
  212. '<?php
  213. /**
  214. * @coversNothing
  215. */
  216. class FooTest extends \PHPUnit_Framework_TestCase {}
  217. ',
  218. '<?php
  219. class FooTest extends \PHPUnit_Framework_TestCase {}
  220. ',
  221. ],
  222. ];
  223. }
  224. }