PhpUnitSetUpTearDownVisibilityFixerTest.php 5.5 KB

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