PhpdocOrderFixerTest.php 18 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(): array
  336. {
  337. return [
  338. [['order' => ['param', 'throw', 'return']]],
  339. [['order' => ['param', 'return', 'throw']]],
  340. ];
  341. }
  342. /**
  343. * @dataProvider provideBasicCodeWithDifferentOrdersCases
  344. *
  345. * @param array<string, mixed> $config
  346. */
  347. public function testFixBasicCaseWithDifferentOrders(array $config, string $expected, ?string $input): void
  348. {
  349. $this->fixer->configure($config);
  350. $this->doTest($expected, $input);
  351. }
  352. /**
  353. * @return array<array<null|array<string, mixed>|string>>
  354. */
  355. public static function provideBasicCodeWithDifferentOrdersCases(): array
  356. {
  357. $input = <<<'EOF'
  358. <?php
  359. /**
  360. * @throws Exception
  361. * @return bool
  362. * @param string $foo
  363. */
  364. EOF;
  365. return [
  366. [
  367. ['order' => ['return', 'throws', 'param']],
  368. <<<'EOF'
  369. <?php
  370. /**
  371. * @return bool
  372. * @throws Exception
  373. * @param string $foo
  374. */
  375. EOF,
  376. $input,
  377. ],
  378. [
  379. ['order' => ['throws', 'return', 'param']],
  380. <<<'EOF'
  381. <?php
  382. /**
  383. * @throws Exception
  384. * @return bool
  385. * @param string $foo
  386. */
  387. EOF,
  388. null,
  389. ],
  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 provideCompleteCasesWithCustomOrdersCases
  453. *
  454. * @param array<string, mixed> $config
  455. */
  456. public function testFixCompleteCasesWithCustomOrders(array $config, string $expected, string $input): void
  457. {
  458. $this->fixer->configure($config);
  459. $this->doTest($expected, $input);
  460. }
  461. /**
  462. * @return array<string, array<int, string|string[][]>>
  463. */
  464. public static function provideCompleteCasesWithCustomOrdersCases(): array
  465. {
  466. return [
  467. 'intepacuthre' => [
  468. ['order' => ['internal', 'template', 'param', 'custom', 'throws', 'return']],
  469. <<<'EOF'
  470. <?php
  471. /**
  472. * Hello there
  473. *
  474. * Long description
  475. * goes here.
  476. *
  477. * @internal
  478. * @template T of Extension\Extension
  479. * @param string $foo
  480. * @param bool $bar Bar
  481. * @param class-string<T> $id
  482. * @custom Test!
  483. * asldnaksdkjasdasd
  484. * @throws Exception|RuntimeException dfsdf
  485. * jkaskdnaksdnkasndansdnansdajsdnkasd
  486. * @return bool Return false on failure
  487. * @return int Return the number of changes.
  488. **/
  489. EOF,
  490. <<<'EOF'
  491. <?php
  492. /**
  493. * Hello there
  494. *
  495. * Long description
  496. * goes here.
  497. *
  498. * @internal
  499. * @param string $foo
  500. * @param bool $bar Bar
  501. * @param class-string<T> $id
  502. * @custom Test!
  503. * asldnaksdkjasdasd
  504. * @template T of Extension\Extension
  505. * @return bool Return false on failure
  506. * @return int Return the number of changes.
  507. * @throws Exception|RuntimeException dfsdf
  508. * jkaskdnaksdnkasndansdnansdajsdnkasd
  509. **/
  510. EOF,
  511. ],
  512. 'pare' => [
  513. ['order' => ['param', 'return']],
  514. <<<'EOF'
  515. <?php
  516. /**
  517. * Hello there
  518. *
  519. * Long description
  520. * goes here.
  521. *
  522. * @internal
  523. * @param string $foo
  524. * @param bool $bar Bar
  525. * @param class-string<T> $id
  526. * @return bool Return false on failure
  527. * @return int Return the number of changes.
  528. * @custom Test!
  529. * asldnaksdkjasdasd
  530. * @template T of Extension\Extension
  531. * @throws Exception|RuntimeException dfsdf
  532. * jkaskdnaksdnkasndansdnansdajsdnkasd
  533. **/
  534. EOF,
  535. <<<'EOF'
  536. <?php
  537. /**
  538. * Hello there
  539. *
  540. * Long description
  541. * goes here.
  542. *
  543. * @internal
  544. * @return bool Return false on failure
  545. * @return int Return the number of changes.
  546. * @custom Test!
  547. * asldnaksdkjasdasd
  548. * @template T of Extension\Extension
  549. * @param string $foo
  550. * @param bool $bar Bar
  551. * @param class-string<T> $id
  552. * @throws Exception|RuntimeException dfsdf
  553. * jkaskdnaksdnkasndansdnansdajsdnkasd
  554. **/
  555. EOF,
  556. ],
  557. 'pareth' => [
  558. ['order' => ['param', 'return', 'throws']],
  559. <<<'EOF'
  560. <?php
  561. /**
  562. * Hello there
  563. *
  564. * Long description
  565. * goes here.
  566. *
  567. * @internal
  568. * @custom Test!
  569. * asldnaksdkjasdasd
  570. * @template T of Extension\Extension
  571. * @param string $foo
  572. * @param bool $bar Bar
  573. * @param class-string<T> $id
  574. * @return bool Return false on failure
  575. * @return int Return the number of changes.
  576. * @throws Exception|RuntimeException dfsdf
  577. * jkaskdnaksdnkasndansdnansdajsdnkasd
  578. **/
  579. EOF,
  580. <<<'EOF'
  581. <?php
  582. /**
  583. * Hello there
  584. *
  585. * Long description
  586. * goes here.
  587. *
  588. * @internal
  589. * @return bool Return false on failure
  590. * @return int Return the number of changes.
  591. * @custom Test!
  592. * asldnaksdkjasdasd
  593. * @template T of Extension\Extension
  594. * @param string $foo
  595. * @param bool $bar Bar
  596. * @param class-string<T> $id
  597. * @throws Exception|RuntimeException dfsdf
  598. * jkaskdnaksdnkasndansdnansdajsdnkasd
  599. **/
  600. EOF,
  601. ],
  602. 'pathre' => [
  603. ['order' => ['param', 'throws', 'return']],
  604. <<<'EOF'
  605. <?php
  606. /**
  607. * Hello there
  608. *
  609. * Long description
  610. * goes here.
  611. *
  612. * @internal
  613. * @custom Test!
  614. * asldnaksdkjasdasd
  615. * @template T of Extension\Extension
  616. * @param string $foo
  617. * @param bool $bar Bar
  618. * @param class-string<T> $id
  619. * @throws Exception|RuntimeException dfsdf
  620. * jkaskdnaksdnkasndansdnansdajsdnkasd
  621. * @return bool Return false on failure
  622. * @return int Return the number of changes.
  623. **/
  624. EOF,
  625. <<<'EOF'
  626. <?php
  627. /**
  628. * Hello there
  629. *
  630. * Long description
  631. * goes here.
  632. *
  633. * @internal
  634. * @return bool Return false on failure
  635. * @return int Return the number of changes.
  636. * @custom Test!
  637. * asldnaksdkjasdasd
  638. * @template T of Extension\Extension
  639. * @param string $foo
  640. * @param bool $bar Bar
  641. * @param class-string<T> $id
  642. * @throws Exception|RuntimeException dfsdf
  643. * jkaskdnaksdnkasndansdnansdajsdnkasd
  644. **/
  645. EOF,
  646. ],
  647. 'tepathre' => [
  648. ['order' => ['template', 'param', 'throws', 'return']],
  649. <<<'EOF'
  650. <?php
  651. /**
  652. * Hello there
  653. *
  654. * Long description
  655. * goes here.
  656. *
  657. * @internal
  658. * @template T of Extension\Extension
  659. * @param string $foo
  660. * @param bool $bar Bar
  661. * @param class-string<T> $id
  662. * @throws Exception|RuntimeException dfsdf
  663. * jkaskdnaksdnkasndansdnansdajsdnkasd
  664. * @return bool Return false on failure
  665. * @return int Return the number of changes.
  666. * @custom Test!
  667. * asldnaksdkjasdasd
  668. **/
  669. EOF,
  670. <<<'EOF'
  671. <?php
  672. /**
  673. * Hello there
  674. *
  675. * Long description
  676. * goes here.
  677. *
  678. * @internal
  679. * @return bool Return false on failure
  680. * @return int Return the number of changes.
  681. * @param string $foo
  682. * @param bool $bar Bar
  683. * @param class-string<T> $id
  684. * @template T of Extension\Extension
  685. * @custom Test!
  686. * asldnaksdkjasdasd
  687. * @throws Exception|RuntimeException dfsdf
  688. * jkaskdnaksdnkasndansdnansdajsdnkasd
  689. **/
  690. EOF,
  691. ],
  692. 'tepathre2' => [
  693. ['order' => ['template', 'param', 'throws', 'return']],
  694. <<<'EOF'
  695. <?php
  696. /**
  697. * Hello there
  698. *
  699. * Long description
  700. * goes here.
  701. *
  702. * @internal
  703. * @template T of Extension\Extension
  704. * @param string $foo
  705. * @param bool $bar Bar
  706. * @param class-string<T> $id
  707. * @throws Exception|RuntimeException dfsdf
  708. * jkaskdnaksdnkasndansdnansdajsdnkasd
  709. * @return bool Return false on failure
  710. * @return int Return the number of changes.
  711. * @custom Test!
  712. * asldnaksdkjasdasd
  713. **/
  714. EOF,
  715. <<<'EOF'
  716. <?php
  717. /**
  718. * Hello there
  719. *
  720. * Long description
  721. * goes here.
  722. *
  723. * @internal
  724. * @param string $foo
  725. * @param bool $bar Bar
  726. * @param class-string<T> $id
  727. * @return bool Return false on failure
  728. * @return int Return the number of changes.
  729. * @template T of Extension\Extension
  730. * @custom Test!
  731. * asldnaksdkjasdasd
  732. * @throws Exception|RuntimeException dfsdf
  733. * jkaskdnaksdnkasndansdnansdajsdnkasd
  734. **/
  735. EOF,
  736. ],
  737. ];
  738. }
  739. }