PhpdocOrderFixerTest.php 25 KB

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