PhpUnitMethodCasingFixerTest.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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\Fixer\PhpUnit\PhpUnitMethodCasingFixer;
  14. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  15. /**
  16. * @author Filippo Tessarotto <zoeslam@gmail.com>
  17. *
  18. * @internal
  19. *
  20. * @covers \PhpCsFixer\Fixer\PhpUnit\PhpUnitMethodCasingFixer
  21. */
  22. final class PhpUnitMethodCasingFixerTest extends AbstractFixerTestCase
  23. {
  24. /**
  25. * @dataProvider provideFixCases
  26. */
  27. public function testFixToCamelCase(string $expected, ?string $input = null): void
  28. {
  29. $this->doTest($expected, $input);
  30. }
  31. /**
  32. * @dataProvider provideFixCases
  33. */
  34. public function testFixToSnakeCase(string $camelExpected, ?string $camelInput = null): void
  35. {
  36. if (null === $camelInput) {
  37. $expected = $camelExpected;
  38. $input = $camelInput;
  39. } else {
  40. $expected = $camelInput;
  41. $input = $camelExpected;
  42. }
  43. $this->fixer->configure(['case' => PhpUnitMethodCasingFixer::SNAKE_CASE]);
  44. $this->doTest($expected, $input);
  45. }
  46. public static function provideFixCases(): iterable
  47. {
  48. yield 'skip non phpunit methods' => [
  49. '<?php class MyClass {
  50. public function testMyApp() {}
  51. public function test_my_app() {}
  52. }',
  53. ];
  54. yield 'skip non test methods' => [
  55. '<?php class MyTest extends \PhpUnit\FrameWork\TestCase {
  56. public function not_a_test() {}
  57. public function notATestEither() {}
  58. }',
  59. ];
  60. yield 'default sample' => [
  61. '<?php class MyTest extends \PhpUnit\FrameWork\TestCase { public function testMyApp() {} }',
  62. '<?php class MyTest extends \PhpUnit\FrameWork\TestCase { public function test_my_app() {} }',
  63. ];
  64. yield 'annotation' => [
  65. '<?php class MyTest extends \PhpUnit\FrameWork\TestCase { /** @test */ public function myApp() {} }',
  66. '<?php class MyTest extends \PhpUnit\FrameWork\TestCase { /** @test */ public function my_app() {} }',
  67. ];
  68. yield '@depends annotation' => [
  69. '<?php class MyTest extends \PhpUnit\FrameWork\TestCase {
  70. public function testMyApp () {}
  71. /**
  72. * @depends testMyApp
  73. */
  74. public function testMyAppToo() {}
  75. }',
  76. '<?php class MyTest extends \PhpUnit\FrameWork\TestCase {
  77. public function test_my_app () {}
  78. /**
  79. * @depends test_my_app
  80. */
  81. public function test_my_app_too() {}
  82. }',
  83. ];
  84. yield '@depends annotation with class name in PascalCase' => [
  85. '<?php class MyTest extends \PhpUnit\FrameWork\TestCase {
  86. public function testMyApp () {}
  87. /**
  88. * @depends FooBarTest::testMyApp
  89. */
  90. public function testMyAppToo() {}
  91. }',
  92. '<?php class MyTest extends \PhpUnit\FrameWork\TestCase {
  93. public function test_my_app () {}
  94. /**
  95. * @depends FooBarTest::test_my_app
  96. */
  97. public function test_my_app_too() {}
  98. }',
  99. ];
  100. yield '@depends annotation with class name in Snake_Case' => [
  101. '<?php class MyTest extends \PhpUnit\FrameWork\TestCase {
  102. public function testMyApp () {}
  103. /**
  104. * @depends Foo_Bar_Test::testMyApp
  105. */
  106. public function testMyAppToo() {}
  107. }',
  108. '<?php class MyTest extends \PhpUnit\FrameWork\TestCase {
  109. public function test_my_app () {}
  110. /**
  111. * @depends Foo_Bar_Test::test_my_app
  112. */
  113. public function test_my_app_too() {}
  114. }',
  115. ];
  116. yield '@depends and @test annotation' => [
  117. '<?php class MyTest extends \PhpUnit\FrameWork\TestCase {
  118. /**
  119. * @test
  120. */
  121. public function myApp () {}
  122. /**
  123. * @test
  124. * @depends myApp
  125. */
  126. public function myAppToo() {}
  127. /** not a test method */
  128. public function my_app_not() {}
  129. public function my_app_not_2() {}
  130. }',
  131. '<?php class MyTest extends \PhpUnit\FrameWork\TestCase {
  132. /**
  133. * @test
  134. */
  135. public function my_app () {}
  136. /**
  137. * @test
  138. * @depends my_app
  139. */
  140. public function my_app_too() {}
  141. /** not a test method */
  142. public function my_app_not() {}
  143. public function my_app_not_2() {}
  144. }',
  145. ];
  146. }
  147. /**
  148. * @dataProvider provideFix80ToCamelCaseCases
  149. *
  150. * @requires PHP 8.0
  151. */
  152. public function testFix80ToCamelCase(string $expected, string $input): void
  153. {
  154. $this->doTest($expected, $input);
  155. }
  156. /**
  157. * @return iterable<string[]>
  158. */
  159. public static function provideFix80ToCamelCaseCases(): iterable
  160. {
  161. yield '@depends annotation with class name in Snake_Case' => [
  162. '<?php class MyTest extends \PhpUnit\FrameWork\TestCase {
  163. public function testMyApp () {}
  164. /**
  165. * @depends Foo_Bar_Test::testMyApp
  166. */
  167. #[SimpleTest]
  168. public function testMyAppToo() {}
  169. }',
  170. '<?php class MyTest extends \PhpUnit\FrameWork\TestCase {
  171. public function test_my_app () {}
  172. /**
  173. * @depends Foo_Bar_Test::test_my_app
  174. */
  175. #[SimpleTest]
  176. public function test_my_app_too() {}
  177. }',
  178. ];
  179. yield '@depends annotation with class name in Snake_Case and attributes in between' => [
  180. '<?php class MyTest extends \PhpUnit\FrameWork\TestCase {
  181. public function testMyApp () {}
  182. /**
  183. * @depends Foo_Bar_Test::testMyApp
  184. */
  185. #[SimpleTest]
  186. #[Deprecated]
  187. public function testMyAppToo() {}
  188. }',
  189. '<?php class MyTest extends \PhpUnit\FrameWork\TestCase {
  190. public function test_my_app () {}
  191. /**
  192. * @depends Foo_Bar_Test::test_my_app
  193. */
  194. #[SimpleTest]
  195. #[Deprecated]
  196. public function test_my_app_too() {}
  197. }',
  198. ];
  199. }
  200. }