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. /**
  122. * @return iterable<array{array<string, mixed>}>
  123. */
  124. public static function provideDifferentOrderCases(): iterable
  125. {
  126. yield [['order' => ['param', 'throw', 'return']]];
  127. yield [['order' => ['param', 'return', 'throw']]];
  128. }
  129. public function testFixBasicCase(): void
  130. {
  131. $expected = <<<'EOF'
  132. <?php
  133. /**
  134. * @param string $foo
  135. * @throws Exception
  136. * @return bool
  137. */
  138. EOF;
  139. $input = <<<'EOF'
  140. <?php
  141. /**
  142. * @throws Exception
  143. * @return bool
  144. * @param string $foo
  145. */
  146. EOF;
  147. $this->doTest($expected, $input);
  148. }
  149. public function testFixCompleteCase(): void
  150. {
  151. $expected = <<<'EOF'
  152. <?php
  153. /**
  154. * Hello there!
  155. *
  156. * Long description
  157. * goes here.
  158. *
  159. * @internal
  160. *
  161. *
  162. * @custom Test!
  163. * asldnaksdkjasdasd
  164. *
  165. *
  166. *
  167. * @param string $foo
  168. * @param bool $bar Bar
  169. * @throws Exception|RuntimeException dfsdf
  170. * jkaskdnaksdnkasndansdnansdajsdnkasd
  171. * @return bool Return false on failure.
  172. * @return int Return the number of changes.
  173. */
  174. EOF;
  175. $input = <<<'EOF'
  176. <?php
  177. /**
  178. * Hello there!
  179. *
  180. * Long description
  181. * goes here.
  182. *
  183. * @internal
  184. *
  185. * @throws Exception|RuntimeException dfsdf
  186. * jkaskdnaksdnkasndansdnansdajsdnkasd
  187. *
  188. * @custom Test!
  189. * asldnaksdkjasdasd
  190. *
  191. *
  192. * @return bool Return false on failure.
  193. * @return int Return the number of changes.
  194. *
  195. * @param string $foo
  196. * @param bool $bar Bar
  197. */
  198. EOF;
  199. $this->doTest($expected, $input);
  200. }
  201. public function testExampleFromSymfony(): void
  202. {
  203. $expected = <<<'EOF'
  204. <?php
  205. /**
  206. * Renders a template.
  207. *
  208. * @param mixed $name A template name
  209. * @param array $parameters An array of parameters to pass to the template
  210. *
  211. * @throws \InvalidArgumentException if the template does not exist
  212. * @throws \RuntimeException if the template cannot be rendered
  213. * @return string The evaluated template as a string
  214. *
  215. */
  216. EOF;
  217. $input = <<<'EOF'
  218. <?php
  219. /**
  220. * Renders a template.
  221. *
  222. * @param mixed $name A template name
  223. * @param array $parameters An array of parameters to pass to the template
  224. *
  225. * @return string The evaluated template as a string
  226. *
  227. * @throws \InvalidArgumentException if the template does not exist
  228. * @throws \RuntimeException if the template cannot be rendered
  229. */
  230. EOF;
  231. $this->doTest($expected, $input);
  232. }
  233. public function testNoChangesWithLaravelStyle(): void
  234. {
  235. $this->fixer->configure(['order' => ['param', 'return', 'throws']]);
  236. $expected = <<<'EOF'
  237. <?php
  238. /**
  239. * Do some cool stuff.
  240. *
  241. * @param EngineInterface $templating
  242. * @param string $name
  243. *
  244. * @return void|bar
  245. *
  246. * @throws Exception
  247. */
  248. EOF;
  249. $this->doTest($expected);
  250. }
  251. public function testFixBasicCaseWithLaravelStyle(): void
  252. {
  253. $this->fixer->configure(['order' => ['param', 'return', 'throws']]);
  254. $expected = <<<'EOF'
  255. <?php
  256. /**
  257. * @param string $foo
  258. * @return bool
  259. * @throws Exception
  260. */
  261. EOF;
  262. $input = <<<'EOF'
  263. <?php
  264. /**
  265. * @throws Exception
  266. * @return bool
  267. * @param string $foo
  268. */
  269. EOF;
  270. $this->doTest($expected, $input);
  271. }
  272. public function testFixCompleteCaseWithLaravelStyle(): void
  273. {
  274. $this->fixer->configure(['order' => ['param', 'return', 'throws']]);
  275. $expected = <<<'EOF'
  276. <?php
  277. /**
  278. * Hello there!
  279. *
  280. * Long description
  281. * goes here.
  282. *
  283. * @internal
  284. *
  285. *
  286. * @custom Test!
  287. * asldnaksdkjasdasd
  288. *
  289. *
  290. *
  291. * @param string $foo
  292. * @param bool $bar Bar
  293. * @return bool Return false on failure.
  294. * @return int Return the number of changes.
  295. * @throws Exception|RuntimeException dfsdf
  296. * jkaskdnaksdnkasndansdnansdajsdnkasd
  297. */
  298. EOF;
  299. $input = <<<'EOF'
  300. <?php
  301. /**
  302. * Hello there!
  303. *
  304. * Long description
  305. * goes here.
  306. *
  307. * @internal
  308. *
  309. * @throws Exception|RuntimeException dfsdf
  310. * jkaskdnaksdnkasndansdnansdajsdnkasd
  311. *
  312. * @custom Test!
  313. * asldnaksdkjasdasd
  314. *
  315. *
  316. * @return bool Return false on failure.
  317. * @return int Return the number of changes.
  318. *
  319. * @param string $foo
  320. * @param bool $bar Bar
  321. */
  322. EOF;
  323. $this->doTest($expected, $input);
  324. }
  325. public function testExampleFromSymfonyWithLaravelStyle(): void
  326. {
  327. $this->fixer->configure(['order' => ['param', 'return', 'throws']]);
  328. $input = <<<'EOF'
  329. <?php
  330. /**
  331. * Renders a template.
  332. *
  333. * @param mixed $name A template name
  334. * @param array $parameters An array of parameters to pass to the template
  335. *
  336. * @return string The evaluated template as a string
  337. *
  338. * @throws \InvalidArgumentException if the template does not exist
  339. * @throws \RuntimeException if the template cannot be rendered
  340. */
  341. EOF;
  342. $this->doTest($input);
  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. }