PhpUnitSetUpTearDownVisibilityFixerTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. * @author Gert de Pagter
  16. *
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Fixer\PhpUnit\PhpUnitSetUpTearDownVisibilityFixer
  20. *
  21. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\PhpUnit\PhpUnitSetUpTearDownVisibilityFixer>
  22. */
  23. final class PhpUnitSetUpTearDownVisibilityFixerTest 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. /**
  33. * @return iterable<string, array{0: string, 1?: string}>
  34. */
  35. public static function provideFixCases(): iterable
  36. {
  37. yield 'setUp and tearDown are made protected if they are public' => [
  38. '<?php
  39. class FixerTest extends \PhpUnit\FrameWork\TestCase
  40. {
  41. protected function setUp() {}
  42. protected function tearDown() {}
  43. }
  44. ',
  45. '<?php
  46. class FixerTest extends \PhpUnit\FrameWork\TestCase
  47. {
  48. public function setUp() {}
  49. public function tearDown() {}
  50. }
  51. ',
  52. ];
  53. yield 'Other functions are ignored' => [
  54. '<?php
  55. class FixerTest extends \PhpUnit\FrameWork\TestCase
  56. {
  57. public function hello() {}
  58. protected function setUp() {}
  59. protected function tearDown() {}
  60. public function testWork() {}
  61. protected function testProtectedFunction() {}
  62. private function privateHelperFunction() {}
  63. }
  64. ',
  65. '<?php
  66. class FixerTest extends \PhpUnit\FrameWork\TestCase
  67. {
  68. public function hello() {}
  69. public function setUp() {}
  70. public function tearDown() {}
  71. public function testWork() {}
  72. protected function testProtectedFunction() {}
  73. private function privateHelperFunction() {}
  74. }
  75. ',
  76. ];
  77. yield 'It works when setUp and tearDown are final' => [
  78. '<?php
  79. class FixerTest extends \PhpUnit\FrameWork\TestCase
  80. {
  81. protected final function setUp() {}
  82. final protected function tearDown() {}
  83. }
  84. ',
  85. '<?php
  86. class FixerTest extends \PhpUnit\FrameWork\TestCase
  87. {
  88. public final function setUp() {}
  89. final public function tearDown() {}
  90. }
  91. ',
  92. ];
  93. yield 'It works when setUp and tearDown do not have visibility defined' => [
  94. '<?php
  95. class FixerTest extends \PhpUnit\FrameWork\TestCase
  96. {
  97. protected function setUp() {}
  98. protected function tearDown() {}
  99. }
  100. ',
  101. '<?php
  102. class FixerTest extends \PhpUnit\FrameWork\TestCase
  103. {
  104. function setUp() {}
  105. function tearDown() {}
  106. }
  107. ',
  108. ];
  109. yield 'It works when setUp and tearDown do not have visibility defined and are final' => [
  110. '<?php
  111. class FixerTest extends \PhpUnit\FrameWork\TestCase
  112. {
  113. final protected function setUp() {}
  114. final protected function tearDown() {}
  115. }
  116. ',
  117. '<?php
  118. class FixerTest extends \PhpUnit\FrameWork\TestCase
  119. {
  120. final function setUp() {}
  121. final function tearDown() {}
  122. }
  123. ',
  124. ];
  125. yield 'Functions outside a test class do not get changed' => [
  126. '<?php
  127. class Fixer extends OtherClass
  128. {
  129. public function hello() {}
  130. public function setUp() {}
  131. public function tearDown() {}
  132. public function testWork() {}
  133. protected function testProtectedFunction() {}
  134. private function privateHelperFunction() {}
  135. }
  136. ',
  137. ];
  138. yield 'It works even when setup and teardown have improper casing' => [
  139. '<?php
  140. class FixerTest extends \PhpUnit\FrameWork\TestCase
  141. {
  142. protected function sEtUp() {}
  143. protected function TeArDoWn() {}
  144. }
  145. ',
  146. '<?php
  147. class FixerTest extends \PhpUnit\FrameWork\TestCase
  148. {
  149. public function sEtUp() {}
  150. public function TeArDoWn() {}
  151. }
  152. ',
  153. ];
  154. yield 'It works even with messy comments' => [
  155. '<?php
  156. class FixerTest extends \PhpUnit\FrameWork\TestCase
  157. {
  158. protected /** foo */ function setUp() {}
  159. /** foo */protected function tearDown() {}
  160. }
  161. ',
  162. '<?php
  163. class FixerTest extends \PhpUnit\FrameWork\TestCase
  164. {
  165. public /** foo */ function setUp() {}
  166. /** foo */public function tearDown() {}
  167. }
  168. ',
  169. ];
  170. yield 'It works even with messy comments and no defined visibility' => [
  171. '<?php
  172. class FixerTest extends \PhpUnit\FrameWork\TestCase
  173. {
  174. /** foo */protected function setUp() {}
  175. /** bar */protected function tearDown() {}
  176. }
  177. ',
  178. '<?php
  179. class FixerTest extends \PhpUnit\FrameWork\TestCase
  180. {
  181. /** foo */function setUp() {}
  182. /** bar */function tearDown() {}
  183. }
  184. ',
  185. ];
  186. yield 'Nothing changes if setUp or tearDown are private' => [
  187. '<?php
  188. class FixerTest extends \PhpUnit\FrameWork\TestCase
  189. {
  190. private function setUp() {}
  191. private function tearDown() {}
  192. }
  193. ',
  194. ];
  195. yield 'It works when there are multiple classes in one file' => [
  196. '<?php
  197. class FixerTest extends \PhpUnit\FrameWork\TestCase
  198. {
  199. protected function setUp() {}
  200. protected function tearDown() {}
  201. }
  202. class OtherTest extends \PhpUnit\FrameWork\TestCase
  203. {
  204. protected function setUp() {}
  205. protected function tearDown() {}
  206. }
  207. ',
  208. '<?php
  209. class FixerTest extends \PhpUnit\FrameWork\TestCase
  210. {
  211. public function setUp() {}
  212. public function tearDown() {}
  213. }
  214. class OtherTest extends \PhpUnit\FrameWork\TestCase
  215. {
  216. public function setUp() {}
  217. public function tearDown() {}
  218. }
  219. ',
  220. ];
  221. }
  222. }