PhpUnitSetUpTearDownVisibilityFixerTest.php 5.4 KB

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