PhpUnitMethodCasingFixerTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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. * @param _AutogeneratedInputConfiguration $configuration
  67. *
  68. * @dataProvider provideFix80Cases
  69. *
  70. * @requires PHP 8.0
  71. */
  72. public function testFix80(string $expected, string $input, array $configuration = []): void
  73. {
  74. $this->fixer->configure($configuration);
  75. $this->doTest($expected, $input);
  76. }
  77. /**
  78. * @return iterable<string, array{string, string}>
  79. */
  80. public static function provideFix80Cases(): iterable
  81. {
  82. yield '@depends annotation with class name in Snake_Case' => [
  83. '<?php class MyTest extends \PhpUnit\FrameWork\TestCase {
  84. public function testMyApp () {}
  85. /**
  86. * @depends Foo_Bar_Test::testMyApp
  87. */
  88. #[SimpleTest]
  89. public function testMyAppToo() {}
  90. }',
  91. '<?php class MyTest extends \PhpUnit\FrameWork\TestCase {
  92. public function test_my_app () {}
  93. /**
  94. * @depends Foo_Bar_Test::test_my_app
  95. */
  96. #[SimpleTest]
  97. public function test_my_app_too() {}
  98. }',
  99. ];
  100. yield '@depends annotation with class name in Snake_Case and attributes in between' => [
  101. '<?php class MyTest extends \PhpUnit\FrameWork\TestCase {
  102. public function testMyApp () {}
  103. /**
  104. * @depends Foo_Bar_Test::testMyApp
  105. */
  106. #[SimpleTest]
  107. #[Deprecated]
  108. public function testMyAppToo() {}
  109. }',
  110. '<?php class MyTest extends \PhpUnit\FrameWork\TestCase {
  111. public function test_my_app () {}
  112. /**
  113. * @depends Foo_Bar_Test::test_my_app
  114. */
  115. #[SimpleTest]
  116. #[Deprecated]
  117. public function test_my_app_too() {}
  118. }',
  119. ];
  120. yield 'test method with imported Test attribute' => [
  121. '<?php
  122. use PHPUnit\Framework\Attributes\Test;
  123. class MyTest extends \PHPUnit\Framework\TestCase {
  124. #[Test]
  125. public function myAppToo() {}
  126. }',
  127. '<?php
  128. use PHPUnit\Framework\Attributes\Test;
  129. class MyTest extends \PHPUnit\Framework\TestCase {
  130. #[Test]
  131. public function my_app_too() {}
  132. }',
  133. ];
  134. yield 'test method with fully qualified Test attribute' => [
  135. '<?php class MyTest extends \PHPUnit\Framework\TestCase {
  136. #[\PHPUnit\Framework\Attributes\Test]
  137. public function testMyApp() {}
  138. }',
  139. '<?php class MyTest extends \PHPUnit\Framework\TestCase {
  140. #[\PHPUnit\Framework\Attributes\Test]
  141. public function test_my_app() {}
  142. }',
  143. ];
  144. yield 'test method with multiple attributes' => [
  145. '<?php
  146. use PHPUnit\Framework\Attributes\Test;
  147. class MyTest extends \PHPUnit\Framework\TestCase {
  148. #[\PHPUnit\Framework\Attributes\After, Test]
  149. public function myAppToo() {}
  150. }',
  151. '<?php
  152. use PHPUnit\Framework\Attributes\Test;
  153. class MyTest extends \PHPUnit\Framework\TestCase {
  154. #[\PHPUnit\Framework\Attributes\After, Test]
  155. public function my_app_too() {}
  156. }',
  157. ];
  158. yield 'test method with multiple custom attributes' => [
  159. '<?php
  160. use PHPUnit\Framework\Attributes\Test;
  161. class MyTest extends \PHPUnit\Framework\TestCase {
  162. #[SimpleTest, Test]
  163. public function myAppToo() {}
  164. }',
  165. '<?php
  166. use PHPUnit\Framework\Attributes\Test;
  167. class MyTest extends \PHPUnit\Framework\TestCase {
  168. #[SimpleTest, Test]
  169. public function my_app_too() {}
  170. }',
  171. ];
  172. yield 'mixed test methods with and without attributes' => [
  173. '<?php
  174. use PHPUnit\Framework\Attributes\Test;
  175. class MyTest extends \PHPUnit\Framework\TestCase {
  176. public function testMyApp() {}
  177. #[Test]
  178. public function myAppToo() {}
  179. }',
  180. '<?php
  181. use PHPUnit\Framework\Attributes\Test;
  182. class MyTest extends \PHPUnit\Framework\TestCase {
  183. public function test_my_app() {}
  184. #[Test]
  185. public function my_app_too() {}
  186. }',
  187. ];
  188. yield 'snake_case conversion with attribute' => [
  189. '<?php
  190. use PHPUnit\Framework\Attributes\Test;
  191. class MyTest extends \PHPUnit\Framework\TestCase {
  192. #[Test]
  193. public function this_is_a_test_snake_case() {}
  194. }',
  195. '<?php
  196. use PHPUnit\Framework\Attributes\Test;
  197. class MyTest extends \PHPUnit\Framework\TestCase {
  198. #[Test]
  199. public function this_is_a_Test_Snake_Case() {}
  200. }',
  201. ['case' => 'snake_case'],
  202. ];
  203. yield 'camelCase to snake_case conversion with attribute' => [
  204. '<?php
  205. use \PHPUnit\Framework\Attributes\Test;
  206. class MyTest extends \PhpUnit\FrameWork\TestCase {
  207. #[Test]
  208. public function this_is_a_test_snake_case() {}
  209. }',
  210. '<?php
  211. use \PHPUnit\Framework\Attributes\Test;
  212. class MyTest extends \PhpUnit\FrameWork\TestCase {
  213. #[Test]
  214. public function this_is_a_TestSnakeCase() {}
  215. }',
  216. ['case' => 'snake_case'],
  217. ];
  218. yield 'method with attribute and docblock' => [
  219. '<?php
  220. use PHPUnit\Framework\Attributes\Test;
  221. class MyTest extends \PHPUnit\Framework\TestCase {
  222. #[Test]
  223. /** @return void */
  224. public function myAppToo() {}
  225. }',
  226. '<?php
  227. use PHPUnit\Framework\Attributes\Test;
  228. class MyTest extends \PHPUnit\Framework\TestCase {
  229. #[Test]
  230. /** @return void */
  231. public function my_app_too() {}
  232. }',
  233. ];
  234. yield 'method with attribute and docblock and multiple lines' => [
  235. '<?php
  236. use PHPUnit\Framework\Attributes\Test;
  237. class MyTest extends \PHPUnit\Framework\TestCase {
  238. #[Test]
  239. /** @return void */
  240. public function myAppToo() {}
  241. }',
  242. '<?php
  243. use PHPUnit\Framework\Attributes\Test;
  244. class MyTest extends \PHPUnit\Framework\TestCase {
  245. #[Test]
  246. /** @return void */
  247. public function my_app_too() {}
  248. }',
  249. ];
  250. yield 'method with multiple non-PHPUnit attributes' => [
  251. '<?php
  252. use PHPUnit\Framework\Attributes\Test;
  253. class MyTest extends \PHPUnit\Framework\TestCase {
  254. #[Test, Author("John"), Deprecated]
  255. public function myAppToo() {}
  256. }',
  257. '<?php
  258. use PHPUnit\Framework\Attributes\Test;
  259. class MyTest extends \PHPUnit\Framework\TestCase {
  260. #[Test, Author("John"), Deprecated]
  261. public function my_app_too() {}
  262. }',
  263. ];
  264. yield 'test attribute with fully qualified namespace without import' => [
  265. '<?php
  266. class MyTest extends \PHPUnit\Framework\TestCase {
  267. #[\PHPUnit\Framework\Attributes\Test]
  268. public function myAppToo() {}
  269. }',
  270. '<?php
  271. class MyTest extends \PHPUnit\Framework\TestCase {
  272. #[\PHPUnit\Framework\Attributes\Test]
  273. public function my_app_too() {}
  274. }',
  275. ];
  276. yield 'method with both attribute and @test annotation' => [
  277. '<?php
  278. use PHPUnit\Framework\Attributes\Test;
  279. class MyTest extends \PHPUnit\Framework\TestCase {
  280. #[Test]
  281. /** @test */
  282. public function myAppToo() {}
  283. }',
  284. '<?php
  285. use PHPUnit\Framework\Attributes\Test;
  286. class MyTest extends \PHPUnit\Framework\TestCase {
  287. #[Test]
  288. /** @test */
  289. public function my_app_too() {}
  290. }',
  291. ];
  292. yield 'method with parameters and attribute' => [
  293. '<?php
  294. use PHPUnit\Framework\Attributes\Test;
  295. class MyTest extends \PHPUnit\Framework\TestCase {
  296. #[Test]
  297. public function myAppToo(int $param): void {}
  298. }',
  299. '<?php
  300. use PHPUnit\Framework\Attributes\Test;
  301. class MyTest extends \PHPUnit\Framework\TestCase {
  302. #[Test]
  303. public function my_app_too(int $param): void {}
  304. }',
  305. ];
  306. yield 'test attribute in namespaced class' => [
  307. '<?php
  308. namespace App\Tests;
  309. use PHPUnit\Framework\Attributes\Test;
  310. class MyTest extends \PHPUnit\Framework\TestCase {
  311. #[Test]
  312. public function myAppToo() {}
  313. }',
  314. '<?php
  315. namespace App\Tests;
  316. use PHPUnit\Framework\Attributes\Test;
  317. class MyTest extends \PHPUnit\Framework\TestCase {
  318. #[Test]
  319. public function my_app_too() {}
  320. }',
  321. ];
  322. yield 'test attribute with alias' => [
  323. '<?php
  324. use PHPUnit\Framework\Attributes\Test as CheckTest;
  325. class MyTest extends \PHPUnit\Framework\TestCase {
  326. #[CheckTest]
  327. public function myAppToo() {}
  328. }',
  329. '<?php
  330. use PHPUnit\Framework\Attributes\Test as CheckTest;
  331. class MyTest extends \PHPUnit\Framework\TestCase {
  332. #[CheckTest]
  333. public function my_app_too() {}
  334. }',
  335. ];
  336. yield 'multiple attributes spanning multiple lines' => [
  337. '<?php
  338. use PHPUnit\Framework\Attributes\Test;
  339. class MyTest extends \PHPUnit\Framework\TestCase {
  340. #[
  341. Test,
  342. DataProvider("testData"),
  343. Group("testData")
  344. ]
  345. public function myAppToo() {}
  346. }',
  347. '<?php
  348. use PHPUnit\Framework\Attributes\Test;
  349. class MyTest extends \PHPUnit\Framework\TestCase {
  350. #[
  351. Test,
  352. DataProvider("testData"),
  353. Group("testData")
  354. ]
  355. public function my_app_too() {}
  356. }',
  357. ];
  358. yield 'test attribute with trailing comma' => [
  359. '<?php
  360. use PHPUnit\Framework\Attributes\Test;
  361. class MyTest extends \PHPUnit\Framework\TestCase {
  362. #[Test, ]
  363. public function myAppToo() {}
  364. }',
  365. '<?php
  366. use PHPUnit\Framework\Attributes\Test;
  367. class MyTest extends \PHPUnit\Framework\TestCase {
  368. #[Test, ]
  369. public function my_app_too() {}
  370. }',
  371. ];
  372. yield 'method with both name prefix and attribute' => [
  373. '<?php
  374. use PHPUnit\Framework\Attributes\Test;
  375. class MyTest extends \PHPUnit\Framework\TestCase {
  376. #[Test]
  377. public function testMyAppToo() {}
  378. }',
  379. '<?php
  380. use PHPUnit\Framework\Attributes\Test;
  381. class MyTest extends \PHPUnit\Framework\TestCase {
  382. #[Test]
  383. public function test_my_app_too() {}
  384. }',
  385. ];
  386. yield 'attribute with different casing' => [
  387. '<?php
  388. use PHPUnit\Framework\Attributes\TEST;
  389. class MyTest extends \PHPUnit\Framework\TestCase {
  390. #[TEST]
  391. public function myAppToo() {}
  392. }',
  393. '<?php
  394. use PHPUnit\Framework\Attributes\TEST;
  395. class MyTest extends \PHPUnit\Framework\TestCase {
  396. #[TEST]
  397. public function my_app_too() {}
  398. }',
  399. ];
  400. }
  401. /**
  402. * @return iterable<string, array{string, string}>
  403. */
  404. private static function pairs(): iterable
  405. {
  406. yield 'default sample' => [
  407. '<?php class MyTest extends \PhpUnit\FrameWork\TestCase { public function testMyApp() {} }',
  408. '<?php class MyTest extends \PhpUnit\FrameWork\TestCase { public function test_my_app() {} }',
  409. ];
  410. yield 'annotation' => [
  411. '<?php class MyTest extends \PhpUnit\FrameWork\TestCase { /** @test */ public function myApp() {} }',
  412. '<?php class MyTest extends \PhpUnit\FrameWork\TestCase { /** @test */ public function my_app() {} }',
  413. ];
  414. yield '@depends annotation' => [
  415. '<?php class MyTest extends \PhpUnit\FrameWork\TestCase {
  416. public function testMyApp () {}
  417. /**
  418. * @depends testMyApp
  419. */
  420. public function testMyAppToo() {}
  421. }',
  422. '<?php class MyTest extends \PhpUnit\FrameWork\TestCase {
  423. public function test_my_app () {}
  424. /**
  425. * @depends test_my_app
  426. */
  427. public function test_my_app_too() {}
  428. }',
  429. ];
  430. yield '@depends annotation with class name in PascalCase' => [
  431. '<?php class MyTest extends \PhpUnit\FrameWork\TestCase {
  432. public function testMyApp () {}
  433. /**
  434. * @depends FooBarTest::testMyApp
  435. */
  436. public function testMyAppToo() {}
  437. }',
  438. '<?php class MyTest extends \PhpUnit\FrameWork\TestCase {
  439. public function test_my_app () {}
  440. /**
  441. * @depends FooBarTest::test_my_app
  442. */
  443. public function test_my_app_too() {}
  444. }',
  445. ];
  446. yield '@depends annotation with class name in Snake_Case' => [
  447. '<?php class MyTest extends \PhpUnit\FrameWork\TestCase {
  448. public function testMyApp () {}
  449. /**
  450. * @depends Foo_Bar_Test::testMyApp
  451. */
  452. public function testMyAppToo() {}
  453. }',
  454. '<?php class MyTest extends \PhpUnit\FrameWork\TestCase {
  455. public function test_my_app () {}
  456. /**
  457. * @depends Foo_Bar_Test::test_my_app
  458. */
  459. public function test_my_app_too() {}
  460. }',
  461. ];
  462. yield '@depends and @test annotation' => [
  463. '<?php class MyTest extends \PhpUnit\FrameWork\TestCase {
  464. /**
  465. * @test
  466. */
  467. public function myApp () {}
  468. /**
  469. * @test
  470. * @depends myApp
  471. */
  472. public function myAppToo() {}
  473. /** not a test method */
  474. public function my_app_not() {}
  475. public function my_app_not_2() {}
  476. }',
  477. '<?php class MyTest extends \PhpUnit\FrameWork\TestCase {
  478. /**
  479. * @test
  480. */
  481. public function my_app () {}
  482. /**
  483. * @test
  484. * @depends my_app
  485. */
  486. public function my_app_too() {}
  487. /** not a test method */
  488. public function my_app_not() {}
  489. public function my_app_not_2() {}
  490. }',
  491. ];
  492. }
  493. }