PhpUnitMethodCasingFixerTest.php 7.9 KB

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