PhpUnitTestAnnotationFixerTest.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149
  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. use PhpCsFixer\WhitespacesFixerConfig;
  15. /**
  16. * @author Gert de Pagter
  17. *
  18. * @internal
  19. *
  20. * @covers \PhpCsFixer\Fixer\PhpUnit\PhpUnitTestAnnotationFixer
  21. */
  22. final class PhpUnitTestAnnotationFixerTest extends AbstractFixerTestCase
  23. {
  24. /**
  25. * @param array<string, mixed> $config
  26. *
  27. * @dataProvider provideFixCases
  28. */
  29. public function testFix(string $expected, ?string $input = null, array $config = []): void
  30. {
  31. $this->fixer->configure($config);
  32. $this->doTest($expected, $input);
  33. }
  34. public static function provideFixCases(): iterable
  35. {
  36. yield 'Annotation is used, and it should not be' => [
  37. '<?php
  38. class Test extends \PhpUnit\FrameWork\TestCase
  39. {
  40. /**
  41. *
  42. */
  43. public function testItDoesSomething() {}
  44. }',
  45. '<?php
  46. class Test extends \PhpUnit\FrameWork\TestCase
  47. {
  48. /**
  49. * @test
  50. */
  51. public function itDoesSomething() {}
  52. }',
  53. ['style' => 'prefix'],
  54. ];
  55. yield 'Annotation is not used, but should be' => [
  56. '<?php
  57. class Test extends \PhpUnit\FrameWork\TestCase
  58. {
  59. /**
  60. * @test
  61. */
  62. public function itDoesSomething() {}
  63. }',
  64. '<?php
  65. class Test extends \PhpUnit\FrameWork\TestCase
  66. {
  67. public function testItDoesSomething() {}
  68. }',
  69. ['style' => 'annotation'],
  70. ];
  71. yield 'Annotation is not used, but should be, class is extra indented' => [
  72. '<?php
  73. if (1) {
  74. class Test extends \PhpUnit\FrameWork\TestCase
  75. {
  76. /**
  77. * @test
  78. */
  79. public function itDoesSomething() {}
  80. }
  81. }',
  82. '<?php
  83. if (1) {
  84. class Test extends \PhpUnit\FrameWork\TestCase
  85. {
  86. public function testItDoesSomething() {}
  87. }
  88. }',
  89. ['style' => 'annotation'],
  90. ];
  91. yield 'Annotation is not used, but should be, and there is already a docBlock' => [
  92. '<?php
  93. class Test extends \PhpUnit\FrameWork\TestCase
  94. {
  95. /**
  96. * @dataProvider blabla
  97. *
  98. * @test
  99. */
  100. public function itDoesSomething() {}
  101. }',
  102. '<?php
  103. class Test extends \PhpUnit\FrameWork\TestCase
  104. {
  105. /**
  106. * @dataProvider blabla
  107. */
  108. public function testItDoesSomething() {}
  109. }',
  110. ['style' => 'annotation'],
  111. ];
  112. yield 'Annotation is used, but should not be, and it depends on other tests' => [
  113. '<?php
  114. class Test extends \PhpUnit\FrameWork\TestCase
  115. {
  116. /**
  117. *
  118. */
  119. public function testAaa () {}
  120. public function helperFunction() {}
  121. /**
  122. * @depends testAaa
  123. *
  124. *
  125. */
  126. public function testBbb () {}
  127. }',
  128. '<?php
  129. class Test extends \PhpUnit\FrameWork\TestCase
  130. {
  131. /**
  132. * @test
  133. */
  134. public function aaa () {}
  135. public function helperFunction() {}
  136. /**
  137. * @depends aaa
  138. *
  139. * @test
  140. */
  141. public function bbb () {}
  142. }',
  143. ['style' => 'prefix'],
  144. ];
  145. yield 'Annotation is not used, but should be, and it depends on other tests' => [
  146. '<?php
  147. class Test extends \PhpUnit\FrameWork\TestCase
  148. {
  149. /**
  150. * @test
  151. */
  152. public function aaa () {}
  153. /**
  154. * @depends aaa
  155. *
  156. * @test
  157. */
  158. public function bbb () {}
  159. }',
  160. '<?php
  161. class Test extends \PhpUnit\FrameWork\TestCase
  162. {
  163. public function testAaa () {}
  164. /**
  165. * @depends testAaa
  166. */
  167. public function testBbb () {}
  168. }',
  169. ['style' => 'annotation'],
  170. ];
  171. yield 'Annotation is removed, the function is one word and we want it to use camel case' => [
  172. '<?php
  173. class Test extends \PhpUnit\FrameWork\TestCase
  174. {
  175. /**
  176. *
  177. */
  178. public function testWorks() {}
  179. }',
  180. '<?php
  181. class Test extends \PhpUnit\FrameWork\TestCase
  182. {
  183. /**
  184. * @test
  185. */
  186. public function works() {}
  187. }',
  188. ];
  189. yield 'Annotation is added, and it is snake case' => [
  190. '<?php
  191. class Test extends \PhpUnit\FrameWork\TestCase
  192. {
  193. /**
  194. * @test
  195. */
  196. public function it_has_snake_case() {}
  197. }',
  198. '<?php
  199. class Test extends \PhpUnit\FrameWork\TestCase
  200. {
  201. public function test_it_has_snake_case() {}
  202. }',
  203. ['style' => 'annotation'],
  204. ];
  205. yield 'Annotation gets added, it has an @depends, and we use snake case' => [
  206. '<?php
  207. class Test extends \PhpUnit\FrameWork\TestCase
  208. {
  209. /**
  210. * @test
  211. */
  212. public function works_fine () {}
  213. /**
  214. * @depends works_fine
  215. *
  216. * @test
  217. */
  218. public function works_fine_too() {}
  219. }',
  220. '<?php
  221. class Test extends \PhpUnit\FrameWork\TestCase
  222. {
  223. public function test_works_fine () {}
  224. /**
  225. * @depends test_works_fine
  226. */
  227. public function test_works_fine_too() {}
  228. }',
  229. ['style' => 'annotation'],
  230. ];
  231. yield 'Class has both camel and snake case, annotated functions and not, and wants to add annotations' => [
  232. '<?php
  233. class Test extends \PhpUnit\FrameWork\TestCase
  234. {
  235. /**
  236. * @test
  237. */
  238. public function snake_cased () {}
  239. /**
  240. * @test
  241. */
  242. public function camelCased () {}
  243. /**
  244. * Description.
  245. *
  246. * @depends camelCased
  247. *
  248. * @test
  249. */
  250. public function depends_on_someone () {}
  251. //It even has a comment
  252. public function a_helper_function () {}
  253. /**
  254. * @depends depends_on_someone
  255. *
  256. * @test
  257. */
  258. public function moreDepends() {}
  259. /**
  260. * @depends depends_on_someone
  261. *
  262. * @test
  263. */
  264. public function alreadyAnnotated() {}
  265. }',
  266. '<?php
  267. class Test extends \PhpUnit\FrameWork\TestCase
  268. {
  269. public function test_snake_cased () {}
  270. public function testCamelCased () {}
  271. /**
  272. * Description.
  273. *
  274. * @depends testCamelCased
  275. */
  276. public function test_depends_on_someone () {}
  277. //It even has a comment
  278. public function a_helper_function () {}
  279. /**
  280. * @depends test_depends_on_someone
  281. */
  282. public function testMoreDepends() {}
  283. /**
  284. * @depends test_depends_on_someone
  285. *
  286. * @test
  287. */
  288. public function alreadyAnnotated() {}
  289. }',
  290. ['style' => 'annotation'],
  291. ];
  292. yield 'Annotation has to be added to multiple functions' => [
  293. '<?php
  294. class Test extends \PhpUnit\FrameWork\TestCase
  295. {
  296. /**
  297. * @test
  298. */
  299. public function itWorks() {}
  300. /**
  301. * @test
  302. */
  303. public function itDoesSomething() {}
  304. }',
  305. '<?php
  306. class Test extends \PhpUnit\FrameWork\TestCase
  307. {
  308. public function testItWorks() {}
  309. public function testItDoesSomething() {}
  310. }',
  311. ['style' => 'annotation'],
  312. ];
  313. yield 'Class with big doc blocks and multiple functions has to remove annotations' => [
  314. '<?php
  315. class Test extends \PhpUnit\FrameWork\TestCase
  316. {
  317. /**
  318. * This test is part of the database group and has a provider.
  319. *
  320. * @param int $paramOne
  321. * @param bool $paramTwo
  322. *
  323. *
  324. * @dataProvider provides
  325. * @group Database
  326. */
  327. public function testDatabase ($paramOne, $paramTwo) {}
  328. /**
  329. * Provider for the database test function
  330. *
  331. * @return array
  332. */
  333. public function provides() {}
  334. /**
  335. * I am just a helper function but I have test in my name.
  336. * I also have a doc Block
  337. *
  338. * @return Foo\Bar
  339. */
  340. public function help_test() {}
  341. protected function setUp() {}
  342. /**
  343. * I depend on the database function, but I already
  344. * had test in my name and a docblock
  345. *
  346. * @depends testDatabase
  347. */
  348. public function testDepends () {}
  349. }',
  350. '<?php
  351. class Test extends \PhpUnit\FrameWork\TestCase
  352. {
  353. /**
  354. * This test is part of the database group and has a provider.
  355. *
  356. * @param int $paramOne
  357. * @param bool $paramTwo
  358. *
  359. * @test
  360. * @dataProvider provides
  361. * @group Database
  362. */
  363. public function database ($paramOne, $paramTwo) {}
  364. /**
  365. * Provider for the database test function
  366. *
  367. * @return array
  368. */
  369. public function provides() {}
  370. /**
  371. * I am just a helper function but I have test in my name.
  372. * I also have a doc Block
  373. *
  374. * @return Foo\Bar
  375. */
  376. public function help_test() {}
  377. protected function setUp() {}
  378. /**
  379. * I depend on the database function, but I already
  380. * had test in my name and a docblock
  381. *
  382. * @depends database
  383. */
  384. public function testDepends () {}
  385. }',
  386. ];
  387. yield 'Test Annotation has to be removed, but its just one line' => [
  388. '<?php
  389. class Test extends \PhpUnit\FrameWork\TestCase
  390. {
  391. /** */
  392. public function testItWorks() {}
  393. }',
  394. '<?php
  395. class Test extends \PhpUnit\FrameWork\TestCase
  396. {
  397. /** @test */
  398. public function itWorks() {}
  399. }',
  400. ];
  401. yield 'Test annotation has to be added, but there is already a one line doc block' => [
  402. '<?php
  403. class Test extends \PhpUnit\FrameWork\TestCase
  404. {
  405. /**
  406. * @group Database
  407. *
  408. * @test
  409. */
  410. public function itTestsDatabase() {}
  411. }',
  412. '<?php
  413. class Test extends \PhpUnit\FrameWork\TestCase
  414. {
  415. /** @group Database */
  416. public function testItTestsDatabase() {}
  417. }',
  418. ['style' => 'annotation'],
  419. ];
  420. yield 'Test annotation has to be added, but there is already a one line doc block which is a sentence' => [
  421. '<?php
  422. class Test extends \PhpUnit\FrameWork\TestCase
  423. {
  424. /**
  425. * I really like this test, it helps a lot
  426. *
  427. * @test
  428. */
  429. public function itTestsDatabase() {}
  430. }',
  431. '<?php
  432. class Test extends \PhpUnit\FrameWork\TestCase
  433. {
  434. /** I really like this test, it helps a lot */
  435. public function testItTestsDatabase() {}
  436. }',
  437. ['style' => 'annotation'],
  438. ];
  439. yield 'Test annotation has to be added, but there is already a one line comment present' => [
  440. '<?php
  441. class Test extends \PhpUnit\FrameWork\TestCase
  442. {
  443. //I really like this test, it helps a lot
  444. /**
  445. * @test
  446. */
  447. public function itTestsDatabase() {}
  448. }',
  449. '<?php
  450. class Test extends \PhpUnit\FrameWork\TestCase
  451. {
  452. //I really like this test, it helps a lot
  453. public function testItTestsDatabase() {}
  454. }',
  455. ['style' => 'annotation'],
  456. ];
  457. yield 'Test annotation has to be added, there is a one line doc block which is an @depends tag' => [
  458. '<?php
  459. class Test extends \PhpUnit\FrameWork\TestCase
  460. {
  461. /**
  462. * @test
  463. */
  464. public function itTestsDatabase() {}
  465. /**
  466. * @depends itTestsDatabase
  467. *
  468. * @test
  469. */
  470. public function itDepends() {}
  471. }',
  472. '<?php
  473. class Test extends \PhpUnit\FrameWork\TestCase
  474. {
  475. public function testItTestsDatabase() {}
  476. /** @depends testItTestsDatabase */
  477. public function testItDepends() {}
  478. }',
  479. ['style' => 'annotation'],
  480. ];
  481. yield 'Annotation gets removed, but the function has a @testWith' => [
  482. '<?php
  483. final class ProcessLinterProcessBuilderTest extends TestCase
  484. {
  485. /**
  486. *
  487. * @param string $executable
  488. * @param string $file
  489. * @param string $expected
  490. *
  491. * @testWith ["php", "foo.php", "\"php\" -l \"foo.php\""]
  492. * ["C:\\Program Files\\php\\php.exe", "foo bar\\baz.php", "\"C:\\Program Files\\php\\php.exe\" -l \"foo bar\\baz.php\""]
  493. * @requires OS Linux|Darwin
  494. */
  495. public function testPrepareCommandOnPhpOnLinuxOrMac($executable, $file, $expected)
  496. {
  497. $builder = new ProcessLinterProcessBuilder($executable);
  498. $this->assertSame(
  499. $expected,
  500. $builder->build($file)->getCommandLine()
  501. );
  502. }
  503. }',
  504. '<?php
  505. final class ProcessLinterProcessBuilderTest extends TestCase
  506. {
  507. /**
  508. * @test
  509. * @param string $executable
  510. * @param string $file
  511. * @param string $expected
  512. *
  513. * @testWith ["php", "foo.php", "\"php\" -l \"foo.php\""]
  514. * ["C:\\Program Files\\php\\php.exe", "foo bar\\baz.php", "\"C:\\Program Files\\php\\php.exe\" -l \"foo bar\\baz.php\""]
  515. * @requires OS Linux|Darwin
  516. */
  517. public function prepareCommandOnPhpOnLinuxOrMac($executable, $file, $expected)
  518. {
  519. $builder = new ProcessLinterProcessBuilder($executable);
  520. $this->assertSame(
  521. $expected,
  522. $builder->build($file)->getCommandLine()
  523. );
  524. }
  525. }',
  526. ];
  527. yield 'Annotation gets added, but there is already an @testWith in the doc block' => [
  528. '<?php
  529. final class ProcessLinterProcessBuilderTest extends TestCase
  530. {
  531. /**
  532. * @param string $executable
  533. * @param string $file
  534. * @param string $expected
  535. *
  536. * @testWith ["php", "foo.php", "\"php\" -l \"foo.php\""]
  537. * ["C:\\Program Files\\php\\php.exe", "foo bar\\baz.php", "\"C:\\Program Files\\php\\php.exe\" -l \"foo bar\\baz.php\""]
  538. * @requires OS Linux|Darwin
  539. *
  540. * @test
  541. */
  542. public function prepareCommandOnPhpOnLinuxOrMac($executable, $file, $expected)
  543. {
  544. $builder = new ProcessLinterProcessBuilder($executable);
  545. $this->assertSame(
  546. $expected,
  547. $builder->build($file)->getCommandLine()
  548. );
  549. }
  550. }',
  551. '<?php
  552. final class ProcessLinterProcessBuilderTest extends TestCase
  553. {
  554. /**
  555. * @param string $executable
  556. * @param string $file
  557. * @param string $expected
  558. *
  559. * @testWith ["php", "foo.php", "\"php\" -l \"foo.php\""]
  560. * ["C:\\Program Files\\php\\php.exe", "foo bar\\baz.php", "\"C:\\Program Files\\php\\php.exe\" -l \"foo bar\\baz.php\""]
  561. * @requires OS Linux|Darwin
  562. */
  563. public function testPrepareCommandOnPhpOnLinuxOrMac($executable, $file, $expected)
  564. {
  565. $builder = new ProcessLinterProcessBuilder($executable);
  566. $this->assertSame(
  567. $expected,
  568. $builder->build($file)->getCommandLine()
  569. );
  570. }
  571. }',
  572. ['style' => 'annotation'],
  573. ];
  574. yield 'Annotation gets properly removed, even when it is in a weird place' => [
  575. '<?php
  576. class Test extends \PhpUnit\FrameWork\TestCase
  577. {
  578. /**
  579. * I am a comment about the function
  580. */
  581. public function testIHateMyTestSuite() {}
  582. /**
  583. * I am another comment about a function
  584. */
  585. public function testThisMakesNoSense() {}
  586. /**
  587. * This comment has more issues
  588. */
  589. public function testItUsesTabs() {}
  590. /**
  591. * @depends testItUsesTabs
  592. */
  593. public function testItDependsReally() {}
  594. /**
  595. * @depends testItUsesTabs
  596. */
  597. public function testItDependsSomeMore() {}
  598. }',
  599. '<?php
  600. class Test extends \PhpUnit\FrameWork\TestCase
  601. {
  602. /**
  603. * I am a comment @test about the function
  604. */
  605. public function iHateMyTestSuite() {}
  606. /**
  607. * I am another comment about a function @test
  608. */
  609. public function thisMakesNoSense() {}
  610. /**
  611. * This comment has @test more issues
  612. */
  613. public function itUsesTabs() {}
  614. /**
  615. * @depends itUsesTabs @test
  616. */
  617. public function itDependsReally() {}
  618. /**
  619. * @test @depends itUsesTabs
  620. */
  621. public function itDependsSomeMore() {}
  622. }',
  623. ];
  624. yield 'Annotation gets added when a single line has doc block has multiple tags already' => [
  625. '<?php
  626. class Test extends \PhpUnit\FrameWork\TestCase
  627. {
  628. /**
  629. * There is some text here @group Database @group Integration
  630. *
  631. * @test
  632. */
  633. public function whyDoThis() {}
  634. }',
  635. '<?php
  636. class Test extends \PhpUnit\FrameWork\TestCase
  637. {
  638. /** There is some text here @group Database @group Integration */
  639. public function testWhyDoThis() {}
  640. }',
  641. ['style' => 'annotation'],
  642. ];
  643. yield 'Annotation gets removed when a single line doc block has the tag, but there are other things as well' => [
  644. '<?php
  645. class Test extends \PhpUnit\FrameWork\TestCase
  646. {
  647. /** There is some text here @group Database @group Integration */
  648. public function testWhyDoThis() {}
  649. }',
  650. '<?php
  651. class Test extends \PhpUnit\FrameWork\TestCase
  652. {
  653. /** There is some @test text here @group Database @group Integration */
  654. public function testWhyDoThis() {}
  655. }',
  656. ];
  657. yield 'Annotation is used, and should be' => [
  658. '<?php
  659. class Test extends \PhpUnit\FrameWork\TestCase
  660. {
  661. /**
  662. * @test
  663. */
  664. public function itDoesSomething() {}
  665. }',
  666. null,
  667. ['style' => 'annotation'],
  668. ];
  669. yield 'Annotation is not used, and should not be' => [
  670. '<?php
  671. class Test extends \PhpUnit\FrameWork\TestCase
  672. {
  673. public function testItDoesSomethingWithoutPhpDoc() {}
  674. /**
  675. * No annotation, just text
  676. */
  677. public function testItDoesSomethingWithPhpDoc() {}
  678. public function testingItDoesSomethingWithoutPhpDoc() {}
  679. /**
  680. * No annotation, just text
  681. */
  682. public function testingItDoesSomethingWithPhpDoc() {}
  683. }',
  684. ];
  685. yield 'Annotation is added when it is already present in a weird place' => [
  686. '<?php
  687. class Test extends \PhpUnit\FrameWork\TestCase
  688. {
  689. /**
  690. * I am a comment @test about the function
  691. *
  692. * @test
  693. */
  694. public function iHateMyTestSuite() {}
  695. }',
  696. '<?php
  697. class Test extends \PhpUnit\FrameWork\TestCase
  698. {
  699. /**
  700. * I am a comment @test about the function
  701. */
  702. public function iHateMyTestSuite() {}
  703. }',
  704. ['style' => 'annotation'],
  705. ];
  706. yield 'Docblock does not get converted to a multi line doc block if it already has @test annotation' => [
  707. '<?php
  708. class Test extends \PhpUnit\FrameWork\TestCase
  709. {
  710. /** @test */
  711. public function doesSomeThings() {}
  712. }',
  713. null,
  714. ['style' => 'annotation'],
  715. ];
  716. yield 'Annotation does not get added if class is not a test' => [
  717. '<?php
  718. class Waterloo
  719. {
  720. public function testDoesSomeThings() {}
  721. }',
  722. null,
  723. ['style' => 'annotation'],
  724. ];
  725. yield 'Annotation does not get removed if class is not a test' => [
  726. '<?php
  727. class Waterloo
  728. {
  729. /**
  730. * @test
  731. */
  732. public function doesSomeThings() {}
  733. }',
  734. ];
  735. yield 'Annotation does not get added if there are no tests in the test class' => [
  736. '<?php
  737. class Test extends \PhpUnit\FrameWork\TestCase
  738. {
  739. public function setUp() {}
  740. public function itHelpsSomeTests() {}
  741. public function someMoreChanges() {}
  742. }',
  743. null,
  744. ['style' => 'annotation'],
  745. ];
  746. yield 'Abstract test gets annotation removed' => [
  747. '<?php
  748. abstract class Test extends \PhpUnit\FrameWork\TestCase
  749. {
  750. /**
  751. *
  752. */
  753. abstract function testFooBar();
  754. }',
  755. '<?php
  756. abstract class Test extends \PhpUnit\FrameWork\TestCase
  757. {
  758. /**
  759. * @test
  760. */
  761. abstract function fooBar();
  762. }',
  763. ['style' => 'prefix'],
  764. ];
  765. yield 'Annotation present, but method already have test prefix' => [
  766. '<?php
  767. class Test extends \PhpUnit\FrameWork\TestCase
  768. {
  769. /**
  770. *
  771. */
  772. public function testarossaIsFromItaly() {}
  773. }',
  774. '<?php
  775. class Test extends \PhpUnit\FrameWork\TestCase
  776. {
  777. /**
  778. * @test
  779. */
  780. public function testarossaIsFromItaly() {}
  781. }',
  782. ['style' => 'prefix'],
  783. ];
  784. yield 'Annotation present, but method is test prefix' => [
  785. '<?php
  786. class Test extends \PhpUnit\FrameWork\TestCase
  787. {
  788. /**
  789. *
  790. */
  791. public function test() {}
  792. }',
  793. '<?php
  794. class Test extends \PhpUnit\FrameWork\TestCase
  795. {
  796. /**
  797. * @test
  798. */
  799. public function test() {}
  800. }',
  801. ['style' => 'prefix'],
  802. ];
  803. yield 'Abstract test gets annotation added' => [
  804. '<?php
  805. abstract class Test extends \PhpUnit\FrameWork\TestCase
  806. {
  807. /**
  808. * @test
  809. */
  810. abstract function fooBar();
  811. }',
  812. '<?php
  813. abstract class Test extends \PhpUnit\FrameWork\TestCase
  814. {
  815. abstract function testFooBar();
  816. }',
  817. ['style' => 'annotation'],
  818. ];
  819. yield 'Annotation gets added, but there is a number after the testprefix so it keeps the prefix' => [
  820. '<?php
  821. class Test extends \PhpUnit\FrameWork\TestCase
  822. {
  823. /**
  824. * @test
  825. */
  826. public function test123fooBar() {}
  827. }',
  828. '<?php
  829. class Test extends \PhpUnit\FrameWork\TestCase
  830. {
  831. public function test123fooBar() {}
  832. }',
  833. ['style' => 'annotation'],
  834. ];
  835. yield 'Annotation missing, but there is a lowercase character after the test prefix so it keeps the prefix' => [
  836. '<?php
  837. class Test extends \PhpUnit\FrameWork\TestCase
  838. {
  839. /**
  840. * @test
  841. */
  842. public function testarossaIsFromItaly() {}
  843. }',
  844. '<?php
  845. class Test extends \PhpUnit\FrameWork\TestCase
  846. {
  847. public function testarossaIsFromItaly() {}
  848. }',
  849. ['style' => 'annotation'],
  850. ];
  851. yield 'Annotation present, but there is a lowercase character after the test prefix so it keeps the prefix' => [
  852. '<?php
  853. class Test extends \PhpUnit\FrameWork\TestCase
  854. {
  855. /**
  856. * @test
  857. */
  858. public function testarossaIsFromItaly() {}
  859. }',
  860. null,
  861. ['style' => 'annotation'],
  862. ];
  863. yield 'Annotation missing, method qualifies as test, but test prefix cannot be removed' => [
  864. '<?php
  865. class Test extends \PhpUnit\FrameWork\TestCase
  866. {
  867. /**
  868. * @test
  869. */
  870. public function test() {}
  871. }',
  872. '<?php
  873. class Test extends \PhpUnit\FrameWork\TestCase
  874. {
  875. public function test() {}
  876. }',
  877. ['style' => 'annotation'],
  878. ];
  879. yield 'Annotation missing, method qualifies as test, but test_ prefix cannot be removed' => [
  880. '<?php
  881. class Test extends \PhpUnit\FrameWork\TestCase
  882. {
  883. /**
  884. * @test
  885. */
  886. public function test_() {}
  887. }',
  888. '<?php
  889. class Test extends \PhpUnit\FrameWork\TestCase
  890. {
  891. public function test_() {}
  892. }',
  893. ['style' => 'annotation'],
  894. ];
  895. yield 'Annotation present, method qualifies as test, but test_ prefix cannot be removed' => [
  896. '<?php
  897. class Test extends \PhpUnit\FrameWork\TestCase
  898. {
  899. /**
  900. * @test
  901. */
  902. public function test_() {}
  903. }',
  904. null,
  905. ['style' => 'annotation'],
  906. ];
  907. yield 'Annotation missing, method after fix still has "test" prefix' => [
  908. '<?php
  909. class Test extends \PhpUnit\FrameWork\TestCase
  910. {
  911. /**
  912. * @test
  913. */
  914. public function test_foo() {}
  915. }',
  916. '<?php
  917. class Test extends \PhpUnit\FrameWork\TestCase
  918. {
  919. public function test_test_foo() {}
  920. }',
  921. ['style' => 'annotation'],
  922. ];
  923. yield 'do not touch single line @depends annotation when already correct' => [
  924. '<?php class FooTest extends \PHPUnit\Framework\TestCase
  925. {
  926. public function testOne() {}
  927. /** @depends testOne */
  928. public function testTwo() {}
  929. /** @depends testTwo */
  930. public function testThree() {}
  931. }',
  932. ];
  933. }
  934. /**
  935. * @param array<string, mixed> $config
  936. *
  937. * @dataProvider provideMessyWhitespacesCases
  938. */
  939. public function testMessyWhitespaces(string $expected, ?string $input = null, array $config = []): void
  940. {
  941. $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n"));
  942. $this->fixer->configure($config);
  943. $this->doTest($expected, $input);
  944. }
  945. public static function provideMessyWhitespacesCases(): iterable
  946. {
  947. yield [
  948. '<?php
  949. class FooTest extends \PHPUnit_Framework_TestCase {
  950. /**
  951. *
  952. */
  953. public function testFooTest() {}
  954. }
  955. ',
  956. '<?php
  957. class FooTest extends \PHPUnit_Framework_TestCase {
  958. /**
  959. * @test
  960. */
  961. public function fooTest() {}
  962. }
  963. ',
  964. ];
  965. }
  966. /**
  967. * @dataProvider provideFix80Cases
  968. *
  969. * @param array<string, string> $config
  970. *
  971. * @requires PHP 8.0
  972. */
  973. public function testFix80(string $expected, string $input, array $config): void
  974. {
  975. $this->fixer->configure($config);
  976. $this->doTest($expected, $input);
  977. }
  978. /**
  979. * @return iterable<(array<string, string>|string)[]>
  980. */
  981. public static function provideFix80Cases(): iterable
  982. {
  983. yield [
  984. '<?php
  985. class Test extends \PhpUnit\FrameWork\TestCase
  986. {
  987. /**
  988. * @test
  989. */
  990. #[OneTest]
  991. public function itWorks() {}
  992. /**
  993. * @test
  994. */
  995. #[TwoTest]
  996. public function itDoesSomething() {}
  997. }',
  998. '<?php
  999. class Test extends \PhpUnit\FrameWork\TestCase
  1000. {
  1001. #[OneTest]
  1002. public function testItWorks() {}
  1003. #[TwoTest]
  1004. public function testItDoesSomething() {}
  1005. }',
  1006. ['style' => 'annotation'],
  1007. ];
  1008. yield [
  1009. '<?php
  1010. class Test extends \PhpUnit\FrameWork\TestCase
  1011. {
  1012. /**
  1013. * @test
  1014. */
  1015. #[OneTest]
  1016. #[Internal]
  1017. public function itWorks() {}
  1018. /**
  1019. * @test
  1020. */
  1021. #[TwoTest]
  1022. #[Internal]
  1023. public function itDoesSomething() {}
  1024. }',
  1025. '<?php
  1026. class Test extends \PhpUnit\FrameWork\TestCase
  1027. {
  1028. #[OneTest]
  1029. #[Internal]
  1030. public function testItWorks() {}
  1031. #[TwoTest]
  1032. #[Internal]
  1033. public function testItDoesSomething() {}
  1034. }',
  1035. ['style' => 'annotation'],
  1036. ];
  1037. }
  1038. }