PhpdocOrderFixerTest.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  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\Phpdoc;
  13. use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
  14. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  15. /**
  16. * @author Graham Campbell <hello@gjcampbell.co.uk>
  17. * @author Jakub Kwaśniewski <jakub@zero-85.pl>
  18. *
  19. * @internal
  20. *
  21. * @covers \PhpCsFixer\Fixer\Phpdoc\PhpdocOrderFixer
  22. *
  23. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Phpdoc\PhpdocOrderFixer>
  24. *
  25. * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Phpdoc\PhpdocOrderFixer
  26. */
  27. final class PhpdocOrderFixerTest extends AbstractFixerTestCase
  28. {
  29. public function testEmptyOrderConfiguration(): void
  30. {
  31. $this->expectException(InvalidFixerConfigurationException::class);
  32. $this->expectExceptionMessage('The option "order" value is invalid. Minimum two tags are required.');
  33. $this->fixer->configure(['order' => []]);
  34. }
  35. public function testInvalidOrderConfiguration(): void
  36. {
  37. $this->expectException(InvalidFixerConfigurationException::class);
  38. $this->expectExceptionMessage('The option "order" value is invalid. Minimum two tags are required.');
  39. $this->fixer->configure(['order' => ['param']]);
  40. }
  41. public function testNoChanges(): void
  42. {
  43. $expected = <<<'EOF'
  44. <?php
  45. /**
  46. * Do some cool stuff.
  47. *
  48. * @param EngineInterface $templating
  49. * @param string $name
  50. *
  51. * @throws Exception
  52. *
  53. * @return void|bar
  54. */
  55. EOF;
  56. $this->doTest($expected);
  57. }
  58. /**
  59. * @dataProvider provideDifferentOrderCases
  60. *
  61. * @param _AutogeneratedInputConfiguration $config
  62. */
  63. public function testOnlyParams(array $config): void
  64. {
  65. $this->fixer->configure($config);
  66. $expected = <<<'EOF'
  67. <?php
  68. /**
  69. * @param EngineInterface $templating
  70. * @param string $name
  71. */
  72. EOF;
  73. $this->doTest($expected);
  74. }
  75. /**
  76. * @dataProvider provideDifferentOrderCases
  77. *
  78. * @param _AutogeneratedInputConfiguration $config
  79. */
  80. public function testOnlyReturns(array $config): void
  81. {
  82. $this->fixer->configure($config);
  83. $expected = <<<'EOF'
  84. <?php
  85. /**
  86. *
  87. * @return void|bar
  88. *
  89. */
  90. EOF;
  91. $this->doTest($expected);
  92. }
  93. /**
  94. * @dataProvider provideDifferentOrderCases
  95. *
  96. * @param _AutogeneratedInputConfiguration $config
  97. */
  98. public function testEmpty(array $config): void
  99. {
  100. $this->fixer->configure($config);
  101. $this->doTest('/***/');
  102. }
  103. /**
  104. * @dataProvider provideDifferentOrderCases
  105. *
  106. * @param _AutogeneratedInputConfiguration $config
  107. */
  108. public function testNoAnnotations(array $config): void
  109. {
  110. $this->fixer->configure($config);
  111. $expected = <<<'EOF'
  112. <?php
  113. /**
  114. *
  115. *
  116. *
  117. */
  118. EOF;
  119. $this->doTest($expected);
  120. }
  121. public function testFixBasicCase(): void
  122. {
  123. $expected = <<<'EOF'
  124. <?php
  125. /**
  126. * @param string $foo
  127. * @throws Exception
  128. * @return bool
  129. */
  130. EOF;
  131. $input = <<<'EOF'
  132. <?php
  133. /**
  134. * @throws Exception
  135. * @return bool
  136. * @param string $foo
  137. */
  138. EOF;
  139. $this->doTest($expected, $input);
  140. }
  141. public function testFixCompleteCase(): void
  142. {
  143. $expected = <<<'EOF'
  144. <?php
  145. /**
  146. * Hello there!
  147. *
  148. * Long description
  149. * goes here.
  150. *
  151. * @internal
  152. *
  153. *
  154. * @custom Test!
  155. * asldnaksdkjasdasd
  156. *
  157. *
  158. *
  159. * @param string $foo
  160. * @param bool $bar Bar
  161. * @throws Exception|RuntimeException dfsdf
  162. * jkaskdnaksdnkasndansdnansdajsdnkasd
  163. * @return bool Return false on failure.
  164. * @return int Return the number of changes.
  165. */
  166. EOF;
  167. $input = <<<'EOF'
  168. <?php
  169. /**
  170. * Hello there!
  171. *
  172. * Long description
  173. * goes here.
  174. *
  175. * @internal
  176. *
  177. * @throws Exception|RuntimeException dfsdf
  178. * jkaskdnaksdnkasndansdnansdajsdnkasd
  179. *
  180. * @custom Test!
  181. * asldnaksdkjasdasd
  182. *
  183. *
  184. * @return bool Return false on failure.
  185. * @return int Return the number of changes.
  186. *
  187. * @param string $foo
  188. * @param bool $bar Bar
  189. */
  190. EOF;
  191. $this->doTest($expected, $input);
  192. }
  193. public function testExampleFromSymfony(): void
  194. {
  195. $expected = <<<'EOF'
  196. <?php
  197. /**
  198. * Renders a template.
  199. *
  200. * @param mixed $name A template name
  201. * @param array $parameters An array of parameters to pass to the template
  202. *
  203. * @throws \InvalidArgumentException if the template does not exist
  204. * @throws \RuntimeException if the template cannot be rendered
  205. * @return string The evaluated template as a string
  206. *
  207. */
  208. EOF;
  209. $input = <<<'EOF'
  210. <?php
  211. /**
  212. * Renders a template.
  213. *
  214. * @param mixed $name A template name
  215. * @param array $parameters An array of parameters to pass to the template
  216. *
  217. * @return string The evaluated template as a string
  218. *
  219. * @throws \InvalidArgumentException if the template does not exist
  220. * @throws \RuntimeException if the template cannot be rendered
  221. */
  222. EOF;
  223. $this->doTest($expected, $input);
  224. }
  225. public function testNoChangesWithLaravelStyle(): void
  226. {
  227. $this->fixer->configure(['order' => ['param', 'return', 'throws']]);
  228. $expected = <<<'EOF'
  229. <?php
  230. /**
  231. * Do some cool stuff.
  232. *
  233. * @param EngineInterface $templating
  234. * @param string $name
  235. *
  236. * @return void|bar
  237. *
  238. * @throws Exception
  239. */
  240. EOF;
  241. $this->doTest($expected);
  242. }
  243. public function testFixBasicCaseWithLaravelStyle(): void
  244. {
  245. $this->fixer->configure(['order' => ['param', 'return', 'throws']]);
  246. $expected = <<<'EOF'
  247. <?php
  248. /**
  249. * @param string $foo
  250. * @return bool
  251. * @throws Exception
  252. */
  253. EOF;
  254. $input = <<<'EOF'
  255. <?php
  256. /**
  257. * @throws Exception
  258. * @return bool
  259. * @param string $foo
  260. */
  261. EOF;
  262. $this->doTest($expected, $input);
  263. }
  264. public function testFixCompleteCaseWithLaravelStyle(): void
  265. {
  266. $this->fixer->configure(['order' => ['param', 'return', 'throws']]);
  267. $expected = <<<'EOF'
  268. <?php
  269. /**
  270. * Hello there!
  271. *
  272. * Long description
  273. * goes here.
  274. *
  275. * @internal
  276. *
  277. *
  278. * @custom Test!
  279. * asldnaksdkjasdasd
  280. *
  281. *
  282. *
  283. * @param string $foo
  284. * @param bool $bar Bar
  285. * @return bool Return false on failure.
  286. * @return int Return the number of changes.
  287. * @throws Exception|RuntimeException dfsdf
  288. * jkaskdnaksdnkasndansdnansdajsdnkasd
  289. */
  290. EOF;
  291. $input = <<<'EOF'
  292. <?php
  293. /**
  294. * Hello there!
  295. *
  296. * Long description
  297. * goes here.
  298. *
  299. * @internal
  300. *
  301. * @throws Exception|RuntimeException dfsdf
  302. * jkaskdnaksdnkasndansdnansdajsdnkasd
  303. *
  304. * @custom Test!
  305. * asldnaksdkjasdasd
  306. *
  307. *
  308. * @return bool Return false on failure.
  309. * @return int Return the number of changes.
  310. *
  311. * @param string $foo
  312. * @param bool $bar Bar
  313. */
  314. EOF;
  315. $this->doTest($expected, $input);
  316. }
  317. public function testExampleFromSymfonyWithLaravelStyle(): void
  318. {
  319. $this->fixer->configure(['order' => ['param', 'return', 'throws']]);
  320. $input = <<<'EOF'
  321. <?php
  322. /**
  323. * Renders a template.
  324. *
  325. * @param mixed $name A template name
  326. * @param array $parameters An array of parameters to pass to the template
  327. *
  328. * @return string The evaluated template as a string
  329. *
  330. * @throws \InvalidArgumentException if the template does not exist
  331. * @throws \RuntimeException if the template cannot be rendered
  332. */
  333. EOF;
  334. $this->doTest($input);
  335. }
  336. /**
  337. * @return iterable<array{array<string, mixed>}>
  338. */
  339. public static function provideDifferentOrderCases(): iterable
  340. {
  341. yield [['order' => ['param', 'throw', 'return']]];
  342. yield [['order' => ['param', 'return', 'throw']]];
  343. }
  344. /**
  345. * @dataProvider provideFixBasicCaseWithDifferentOrdersCases
  346. *
  347. * @param _AutogeneratedInputConfiguration $config
  348. */
  349. public function testFixBasicCaseWithDifferentOrders(string $expected, ?string $input = null, ?array $config = null): void
  350. {
  351. $this->fixer->configure($config);
  352. $this->doTest($expected, $input);
  353. }
  354. /**
  355. * @return iterable<array{0: string, 1?: ?string, 2?: array<string, mixed>}>
  356. */
  357. public static function provideFixBasicCaseWithDifferentOrdersCases(): iterable
  358. {
  359. $input = <<<'EOF'
  360. <?php
  361. /**
  362. * @throws Exception
  363. * @return bool
  364. * @param string $foo
  365. */
  366. EOF;
  367. yield [
  368. <<<'EOF'
  369. <?php
  370. /**
  371. * @return bool
  372. * @throws Exception
  373. * @param string $foo
  374. */
  375. EOF,
  376. $input,
  377. ['order' => ['return', 'throws', 'param']],
  378. ];
  379. yield [
  380. <<<'EOF'
  381. <?php
  382. /**
  383. * @throws Exception
  384. * @return bool
  385. * @param string $foo
  386. */
  387. EOF,
  388. null,
  389. ['order' => ['throws', 'return', 'param']],
  390. ];
  391. }
  392. public function testFixCompleteCaseWithCustomOrder(): void
  393. {
  394. $this->fixer->configure(['order' => [
  395. 'throws',
  396. 'return',
  397. 'param',
  398. 'custom',
  399. 'internal',
  400. ]]);
  401. $expected = <<<'EOF'
  402. <?php
  403. /**
  404. * Hello there!
  405. *
  406. * Long description
  407. * goes here.
  408. *
  409. *
  410. * @throws Exception|RuntimeException dfsdf
  411. * jkaskdnaksdnkasndansdnansdajsdnkasd
  412. *
  413. *
  414. *
  415. * @return bool Return false on failure.
  416. * @return int Return the number of changes.
  417. *
  418. * @param string $foo
  419. * @param bool $bar Bar
  420. * @custom Test!
  421. * asldnaksdkjasdasd
  422. * @internal
  423. */
  424. EOF;
  425. $input = <<<'EOF'
  426. <?php
  427. /**
  428. * Hello there!
  429. *
  430. * Long description
  431. * goes here.
  432. *
  433. * @internal
  434. *
  435. * @throws Exception|RuntimeException dfsdf
  436. * jkaskdnaksdnkasndansdnansdajsdnkasd
  437. *
  438. * @custom Test!
  439. * asldnaksdkjasdasd
  440. *
  441. *
  442. * @return bool Return false on failure.
  443. * @return int Return the number of changes.
  444. *
  445. * @param string $foo
  446. * @param bool $bar Bar
  447. */
  448. EOF;
  449. $this->doTest($expected, $input);
  450. }
  451. /**
  452. * @dataProvider provideFixCompleteCasesWithCustomOrdersCases
  453. *
  454. * @param _AutogeneratedInputConfiguration $config
  455. */
  456. public function testFixCompleteCasesWithCustomOrders(string $expected, string $input, array $config): void
  457. {
  458. $this->fixer->configure($config);
  459. $this->doTest($expected, $input);
  460. }
  461. /**
  462. * @return iterable<string, array{0: string, 1?: ?string, 2?: array<string, mixed>}>
  463. */
  464. public static function provideFixCompleteCasesWithCustomOrdersCases(): iterable
  465. {
  466. yield 'intepacuthre' => [
  467. <<<'EOF'
  468. <?php
  469. /**
  470. * Hello there
  471. *
  472. * Long description
  473. * goes here.
  474. *
  475. * @internal
  476. * @template T of Extension\Extension
  477. * @param string $foo
  478. * @param bool $bar Bar
  479. * @param class-string<T> $id
  480. * @custom Test!
  481. * asldnaksdkjasdasd
  482. * @throws Exception|RuntimeException dfsdf
  483. * jkaskdnaksdnkasndansdnansdajsdnkasd
  484. * @return bool Return false on failure
  485. * @return int Return the number of changes.
  486. **/
  487. EOF,
  488. <<<'EOF'
  489. <?php
  490. /**
  491. * Hello there
  492. *
  493. * Long description
  494. * goes here.
  495. *
  496. * @internal
  497. * @param string $foo
  498. * @param bool $bar Bar
  499. * @param class-string<T> $id
  500. * @custom Test!
  501. * asldnaksdkjasdasd
  502. * @template T of Extension\Extension
  503. * @return bool Return false on failure
  504. * @return int Return the number of changes.
  505. * @throws Exception|RuntimeException dfsdf
  506. * jkaskdnaksdnkasndansdnansdajsdnkasd
  507. **/
  508. EOF,
  509. ['order' => ['internal', 'template', 'param', 'custom', 'throws', 'return']],
  510. ];
  511. yield 'pare' => [
  512. <<<'EOF'
  513. <?php
  514. /**
  515. * Hello there
  516. *
  517. * Long description
  518. * goes here.
  519. *
  520. * @internal
  521. * @param string $foo
  522. * @param bool $bar Bar
  523. * @param class-string<T> $id
  524. * @return bool Return false on failure
  525. * @return int Return the number of changes.
  526. * @custom Test!
  527. * asldnaksdkjasdasd
  528. * @template T of Extension\Extension
  529. * @throws Exception|RuntimeException dfsdf
  530. * jkaskdnaksdnkasndansdnansdajsdnkasd
  531. **/
  532. EOF,
  533. <<<'EOF'
  534. <?php
  535. /**
  536. * Hello there
  537. *
  538. * Long description
  539. * goes here.
  540. *
  541. * @internal
  542. * @return bool Return false on failure
  543. * @return int Return the number of changes.
  544. * @custom Test!
  545. * asldnaksdkjasdasd
  546. * @template T of Extension\Extension
  547. * @param string $foo
  548. * @param bool $bar Bar
  549. * @param class-string<T> $id
  550. * @throws Exception|RuntimeException dfsdf
  551. * jkaskdnaksdnkasndansdnansdajsdnkasd
  552. **/
  553. EOF,
  554. ['order' => ['param', 'return']],
  555. ];
  556. yield 'pareth' => [
  557. <<<'EOF'
  558. <?php
  559. /**
  560. * Hello there
  561. *
  562. * Long description
  563. * goes here.
  564. *
  565. * @internal
  566. * @custom Test!
  567. * asldnaksdkjasdasd
  568. * @template T of Extension\Extension
  569. * @param string $foo
  570. * @param bool $bar Bar
  571. * @param class-string<T> $id
  572. * @return bool Return false on failure
  573. * @return int Return the number of changes.
  574. * @throws Exception|RuntimeException dfsdf
  575. * jkaskdnaksdnkasndansdnansdajsdnkasd
  576. **/
  577. EOF,
  578. <<<'EOF'
  579. <?php
  580. /**
  581. * Hello there
  582. *
  583. * Long description
  584. * goes here.
  585. *
  586. * @internal
  587. * @return bool Return false on failure
  588. * @return int Return the number of changes.
  589. * @custom Test!
  590. * asldnaksdkjasdasd
  591. * @template T of Extension\Extension
  592. * @param string $foo
  593. * @param bool $bar Bar
  594. * @param class-string<T> $id
  595. * @throws Exception|RuntimeException dfsdf
  596. * jkaskdnaksdnkasndansdnansdajsdnkasd
  597. **/
  598. EOF,
  599. ['order' => ['param', 'return', 'throws']],
  600. ];
  601. yield 'pathre' => [
  602. <<<'EOF'
  603. <?php
  604. /**
  605. * Hello there
  606. *
  607. * Long description
  608. * goes here.
  609. *
  610. * @internal
  611. * @custom Test!
  612. * asldnaksdkjasdasd
  613. * @template T of Extension\Extension
  614. * @param string $foo
  615. * @param bool $bar Bar
  616. * @param class-string<T> $id
  617. * @throws Exception|RuntimeException dfsdf
  618. * jkaskdnaksdnkasndansdnansdajsdnkasd
  619. * @return bool Return false on failure
  620. * @return int Return the number of changes.
  621. **/
  622. EOF,
  623. <<<'EOF'
  624. <?php
  625. /**
  626. * Hello there
  627. *
  628. * Long description
  629. * goes here.
  630. *
  631. * @internal
  632. * @return bool Return false on failure
  633. * @return int Return the number of changes.
  634. * @custom Test!
  635. * asldnaksdkjasdasd
  636. * @template T of Extension\Extension
  637. * @param string $foo
  638. * @param bool $bar Bar
  639. * @param class-string<T> $id
  640. * @throws Exception|RuntimeException dfsdf
  641. * jkaskdnaksdnkasndansdnansdajsdnkasd
  642. **/
  643. EOF,
  644. ['order' => ['param', 'throws', 'return']],
  645. ];
  646. yield 'tepathre' => [
  647. <<<'EOF'
  648. <?php
  649. /**
  650. * Hello there
  651. *
  652. * Long description
  653. * goes here.
  654. *
  655. * @internal
  656. * @template T of Extension\Extension
  657. * @param string $foo
  658. * @param bool $bar Bar
  659. * @param class-string<T> $id
  660. * @throws Exception|RuntimeException dfsdf
  661. * jkaskdnaksdnkasndansdnansdajsdnkasd
  662. * @return bool Return false on failure
  663. * @return int Return the number of changes.
  664. * @custom Test!
  665. * asldnaksdkjasdasd
  666. **/
  667. EOF,
  668. <<<'EOF'
  669. <?php
  670. /**
  671. * Hello there
  672. *
  673. * Long description
  674. * goes here.
  675. *
  676. * @internal
  677. * @return bool Return false on failure
  678. * @return int Return the number of changes.
  679. * @param string $foo
  680. * @param bool $bar Bar
  681. * @param class-string<T> $id
  682. * @template T of Extension\Extension
  683. * @custom Test!
  684. * asldnaksdkjasdasd
  685. * @throws Exception|RuntimeException dfsdf
  686. * jkaskdnaksdnkasndansdnansdajsdnkasd
  687. **/
  688. EOF,
  689. ['order' => ['template', 'param', 'throws', 'return']],
  690. ];
  691. yield 'tepathre2' => [
  692. <<<'EOF'
  693. <?php
  694. /**
  695. * Hello there
  696. *
  697. * Long description
  698. * goes here.
  699. *
  700. * @internal
  701. * @template T of Extension\Extension
  702. * @param string $foo
  703. * @param bool $bar Bar
  704. * @param class-string<T> $id
  705. * @throws Exception|RuntimeException dfsdf
  706. * jkaskdnaksdnkasndansdnansdajsdnkasd
  707. * @return bool Return false on failure
  708. * @return int Return the number of changes.
  709. * @custom Test!
  710. * asldnaksdkjasdasd
  711. **/
  712. EOF,
  713. <<<'EOF'
  714. <?php
  715. /**
  716. * Hello there
  717. *
  718. * Long description
  719. * goes here.
  720. *
  721. * @internal
  722. * @param string $foo
  723. * @param bool $bar Bar
  724. * @param class-string<T> $id
  725. * @return bool Return false on failure
  726. * @return int Return the number of changes.
  727. * @template T of Extension\Extension
  728. * @custom Test!
  729. * asldnaksdkjasdasd
  730. * @throws Exception|RuntimeException dfsdf
  731. * jkaskdnaksdnkasndansdnansdajsdnkasd
  732. **/
  733. EOF,
  734. ['order' => ['template', 'param', 'throws', 'return']],
  735. ];
  736. }
  737. }