PhpUnitTestAnnotationFixerTest.php 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  1. <?php
  2. /*
  3. * This file is part of PHP CS Fixer.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. namespace PhpCsFixer\Tests\Fixer\PhpUnit;
  12. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  13. use PhpCsFixer\WhitespacesFixerConfig;
  14. /**
  15. * @author Gert de Pagter
  16. *
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Fixer\PhpUnit\PhpUnitTestAnnotationFixer
  20. */
  21. final class PhpUnitTestAnnotationFixerTest extends AbstractFixerTestCase
  22. {
  23. /**
  24. * @dataProvider provideFixCases
  25. *
  26. * @param string $expected
  27. * @param null|string $input
  28. */
  29. public function testFix($expected, $input = null, array $config = [])
  30. {
  31. $this->fixer->configure($config);
  32. $this->doTest($expected, $input);
  33. }
  34. /**
  35. * @return array
  36. */
  37. public function provideFixCases()
  38. {
  39. return [
  40. '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. '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. '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. '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. '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. '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. '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. '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. '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. '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. '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. '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. * Im 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. * Im 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. '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. '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. '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. '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. '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. '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. '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. 'Annotation gets properly removed, even when it is in a weird place' => [
  579. '<?php
  580. class Test extends \PhpUnit\FrameWork\TestCase
  581. {
  582. /**
  583. * Im a comment about the function
  584. */
  585. public function testIHateMyTestSuite() {}
  586. /**
  587. * Im 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. * Im a comment @test about the function
  608. */
  609. public function iHateMyTestSuite() {}
  610. /**
  611. * Im 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. '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. '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. '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. '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. 'Annotation is added when it is already present in a weird place' => [
  690. '<?php
  691. class Test extends \PhpUnit\FrameWork\TestCase
  692. {
  693. /**
  694. * Im 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. * Im a comment @test about the function
  705. */
  706. public function iHateMyTestSuite() {}
  707. }',
  708. ['style' => 'annotation'],
  709. ],
  710. '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. '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. '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. '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. '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. '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. '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. '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. '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. '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. '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. '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. '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. '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. '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. ];
  928. }
  929. /**
  930. * @param string $expected
  931. * @param null|string $input
  932. *
  933. * @dataProvider provideMessyWhitespacesCases
  934. */
  935. public function testMessyWhitespaces($expected, $input = null, array $config = [])
  936. {
  937. $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n"));
  938. $this->fixer->configure($config);
  939. $this->doTest($expected, $input);
  940. }
  941. public function provideMessyWhitespacesCases()
  942. {
  943. return [
  944. [
  945. '<?php
  946. class FooTest extends \PHPUnit_Framework_TestCase {
  947. /**
  948. *
  949. */
  950. public function testFooTest() {}
  951. }
  952. ',
  953. '<?php
  954. class FooTest extends \PHPUnit_Framework_TestCase {
  955. /**
  956. * @test
  957. */
  958. public function fooTest() {}
  959. }
  960. ',
  961. ],
  962. ];
  963. }
  964. }