PhpUnitTestAnnotationFixerTest.php 26 KB

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