HeaderCommentFixerTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  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\Comment;
  13. use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
  14. use PhpCsFixer\ConfigurationException\RequiredFixerConfigurationException;
  15. use PhpCsFixer\Fixer\Comment\HeaderCommentFixer;
  16. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  17. use PhpCsFixer\WhitespacesFixerConfig;
  18. /**
  19. * @internal
  20. *
  21. * @covers \PhpCsFixer\Fixer\Comment\HeaderCommentFixer
  22. *
  23. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Comment\HeaderCommentFixer>
  24. *
  25. * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Comment\HeaderCommentFixer
  26. */
  27. final class HeaderCommentFixerTest extends AbstractFixerTestCase
  28. {
  29. /**
  30. * @param _AutogeneratedInputConfiguration $configuration
  31. *
  32. * @dataProvider provideFixCases
  33. */
  34. public function testFix(array $configuration, string $expected, ?string $input = null): void
  35. {
  36. $this->fixer->configure($configuration);
  37. $this->doTest($expected, $input);
  38. }
  39. public static function provideFixCases(): iterable
  40. {
  41. yield [
  42. ['header' => ''],
  43. '<?php
  44. $a;',
  45. '<?php
  46. /**
  47. * new
  48. */
  49. $a;',
  50. ];
  51. yield [
  52. [
  53. 'header' => 'tmp',
  54. 'location' => 'after_declare_strict',
  55. ],
  56. '<?php
  57. declare(strict_types=1);
  58. /*
  59. * tmp
  60. */
  61. namespace A\B;
  62. echo 1;',
  63. '<?php
  64. declare(strict_types=1);namespace A\B;
  65. echo 1;',
  66. ];
  67. yield [
  68. [
  69. 'header' => 'tmp',
  70. 'location' => 'after_declare_strict',
  71. 'separate' => 'bottom',
  72. 'comment_type' => HeaderCommentFixer::HEADER_PHPDOC,
  73. ],
  74. '<?php
  75. declare(strict_types=1);
  76. /**
  77. * tmp
  78. */
  79. namespace A\B;
  80. echo 1;',
  81. '<?php
  82. declare(strict_types=1);
  83. namespace A\B;
  84. echo 1;',
  85. ];
  86. yield [
  87. [
  88. 'header' => 'tmp',
  89. 'location' => 'after_open',
  90. ],
  91. '<?php
  92. /*
  93. * tmp
  94. */
  95. declare(strict_types=1);
  96. namespace A\B;
  97. echo 1;',
  98. '<?php
  99. declare(strict_types=1);
  100. namespace A\B;
  101. echo 1;',
  102. ];
  103. yield [
  104. [
  105. 'header' => 'new',
  106. 'comment_type' => HeaderCommentFixer::HEADER_COMMENT,
  107. ],
  108. '<?php
  109. /*
  110. * new
  111. */
  112. ',
  113. '<?php
  114. /** test */
  115. ',
  116. ];
  117. yield [
  118. [
  119. 'header' => 'new',
  120. 'comment_type' => HeaderCommentFixer::HEADER_PHPDOC,
  121. ],
  122. '<?php
  123. /**
  124. * new
  125. */
  126. ',
  127. '<?php
  128. /* test */
  129. ',
  130. ];
  131. yield [
  132. [
  133. 'header' => 'def',
  134. 'comment_type' => HeaderCommentFixer::HEADER_PHPDOC,
  135. ],
  136. '<?php
  137. /**
  138. * def
  139. */
  140. ',
  141. '<?php
  142. ',
  143. ];
  144. yield [
  145. ['header' => 'xyz'],
  146. '<?php
  147. /*
  148. * xyz
  149. */
  150. $b;',
  151. '<?php
  152. $b;',
  153. ];
  154. yield [
  155. [
  156. 'header' => 'xyz123',
  157. 'separate' => 'none',
  158. ],
  159. '<?php
  160. /*
  161. * xyz123
  162. */
  163. $a;',
  164. '<?php
  165. $a;',
  166. ];
  167. yield [
  168. [
  169. 'header' => 'abc',
  170. 'comment_type' => HeaderCommentFixer::HEADER_PHPDOC,
  171. ],
  172. '<?php
  173. /**
  174. * abc
  175. */
  176. $c;',
  177. '<?php
  178. $c;',
  179. ];
  180. yield [
  181. [
  182. 'header' => 'ghi',
  183. 'separate' => 'both',
  184. ],
  185. '<?php
  186. /*
  187. * ghi
  188. */
  189. $d;',
  190. '<?php
  191. $d;',
  192. ];
  193. yield [
  194. [
  195. 'header' => 'ghi',
  196. 'separate' => 'top',
  197. ],
  198. '<?php
  199. /*
  200. * ghi
  201. */
  202. $d;',
  203. '<?php
  204. $d;',
  205. ];
  206. yield [
  207. [
  208. 'header' => 'tmp',
  209. 'location' => 'after_declare_strict',
  210. ],
  211. '<?php
  212. /*
  213. * tmp
  214. */
  215. declare(ticks=1);
  216. echo 1;',
  217. '<?php
  218. declare(ticks=1);
  219. echo 1;',
  220. ];
  221. yield [
  222. ['header' => 'Foo'],
  223. '<?php
  224. /*
  225. * Foo
  226. */
  227. echo \'bar\';',
  228. '<?php echo \'bar\';',
  229. ];
  230. yield [
  231. ['header' => 'x'],
  232. '<?php
  233. /*
  234. * x
  235. */
  236. echo \'a\';',
  237. '<?php
  238. /*
  239. * y
  240. * z
  241. */
  242. echo \'a\';',
  243. ];
  244. yield [
  245. ['header' => "a\na"],
  246. '<?php
  247. /*
  248. * a
  249. * a
  250. */
  251. echo \'x\';',
  252. '<?php
  253. /*
  254. * b
  255. * c
  256. */
  257. echo \'x\';',
  258. ];
  259. yield [
  260. [
  261. 'header' => 'foo',
  262. 'location' => 'after_open',
  263. 'separate' => 'bottom',
  264. 'comment_type' => HeaderCommentFixer::HEADER_PHPDOC,
  265. ],
  266. '<?php
  267. /**
  268. * foo
  269. */
  270. declare(strict_types=1);
  271. namespace A;
  272. echo 1;',
  273. '<?php
  274. declare(strict_types=1);
  275. /**
  276. * foo
  277. */
  278. namespace A;
  279. echo 1;',
  280. ];
  281. yield [
  282. [
  283. 'header' => 'foo',
  284. 'location' => 'after_open',
  285. 'separate' => 'bottom',
  286. 'comment_type' => HeaderCommentFixer::HEADER_PHPDOC,
  287. ],
  288. '<?php
  289. /**
  290. * foo
  291. */
  292. declare(strict_types=1);
  293. /**
  294. * bar
  295. */
  296. namespace A;
  297. echo 1;',
  298. '<?php
  299. declare(strict_types=1);
  300. /**
  301. * bar
  302. */
  303. namespace A;
  304. echo 1;',
  305. ];
  306. yield [
  307. [
  308. 'header' => 'Foo',
  309. 'separate' => 'none',
  310. ],
  311. '<?php
  312. declare(strict_types=1);
  313. /*
  314. * Foo
  315. */
  316. namespace SebastianBergmann\Foo;
  317. class Bar
  318. {
  319. }',
  320. '<?php
  321. /*
  322. * Foo
  323. */
  324. declare(strict_types=1);
  325. namespace SebastianBergmann\Foo;
  326. class Bar
  327. {
  328. }',
  329. ];
  330. yield [
  331. ['header' => 'tmp'],
  332. '<?php
  333. /*
  334. * tmp
  335. */
  336. /**
  337. * Foo class doc.
  338. */
  339. class Foo {}',
  340. '<?php
  341. /**
  342. * Foo class doc.
  343. */
  344. class Foo {}',
  345. ];
  346. yield [
  347. ['header' => 'tmp'],
  348. '<?php
  349. /*
  350. * tmp
  351. */
  352. class Foo {}',
  353. '<?php
  354. /*
  355. * Foo class doc.
  356. */
  357. class Foo {}',
  358. ];
  359. yield [
  360. [
  361. 'header' => 'tmp',
  362. 'comment_type' => HeaderCommentFixer::HEADER_PHPDOC,
  363. ],
  364. '<?php
  365. /**
  366. * tmp
  367. */
  368. /**
  369. * Foo class doc.
  370. */
  371. class Foo {}',
  372. '<?php
  373. /**
  374. * Foo class doc.
  375. */
  376. class Foo {}',
  377. ];
  378. yield [
  379. [
  380. 'header' => 'tmp',
  381. 'comment_type' => HeaderCommentFixer::HEADER_PHPDOC,
  382. ],
  383. '<?php
  384. /**
  385. * tmp
  386. */
  387. class Foo {}',
  388. '<?php
  389. /**
  390. * tmp
  391. */
  392. class Foo {}',
  393. ];
  394. yield [
  395. [
  396. 'header' => 'tmp',
  397. 'separate' => 'top',
  398. ],
  399. '<?php
  400. /*
  401. * tmp
  402. */
  403. class Foo {}',
  404. '<?php
  405. /**
  406. * Foo class doc.
  407. */
  408. class Foo {}',
  409. ];
  410. yield [
  411. [
  412. 'header' => 'bar',
  413. 'location' => 'after_open',
  414. ],
  415. '<?php
  416. /*
  417. * bar
  418. */
  419. declare(strict_types=1);
  420. // foo
  421. foo();',
  422. '<?php
  423. /*
  424. * foo
  425. */
  426. declare(strict_types=1);
  427. // foo
  428. foo();',
  429. ];
  430. yield [
  431. [
  432. 'header' => 'bar',
  433. 'location' => 'after_open',
  434. ],
  435. '<?php
  436. /*
  437. * bar
  438. */
  439. declare(strict_types=1);
  440. /* foo */
  441. foo();',
  442. '<?php
  443. /*
  444. * foo
  445. */
  446. declare(strict_types=1);
  447. /* foo */
  448. foo();',
  449. ];
  450. yield [
  451. [
  452. 'header' => 'tmp',
  453. 'location' => 'after_declare_strict',
  454. ],
  455. '<?php
  456. /*
  457. * tmp
  458. */
  459. declare(strict_types=1) ?>',
  460. '<?php
  461. declare(strict_types=1) ?>',
  462. ];
  463. yield [
  464. [
  465. 'header' => 'tmp',
  466. 'location' => 'after_declare_strict',
  467. ],
  468. '#!/usr/bin/env php
  469. <?php
  470. declare(strict_types=1);
  471. /*
  472. * tmp
  473. */
  474. namespace A\B;
  475. echo 1;',
  476. '#!/usr/bin/env php
  477. <?php
  478. declare(strict_types=1);namespace A\B;
  479. echo 1;',
  480. ];
  481. yield [
  482. [
  483. 'header' => 'tmp',
  484. 'location' => 'after_open',
  485. ],
  486. 'Short mixed file A
  487. Hello<?php echo "World!"; ?>',
  488. ];
  489. yield [
  490. [
  491. 'header' => 'tmp',
  492. 'location' => 'after_open',
  493. ],
  494. 'Short mixed file B
  495. <?php echo "Hello"; ?>World!',
  496. ];
  497. yield [
  498. [
  499. 'header' => 'tmp',
  500. 'location' => 'after_open',
  501. ],
  502. 'File with anything at the beginning and with multiple opening tags are not supported
  503. <?php
  504. echo 1;
  505. ?>Hello World!<?php
  506. script_continues_here();',
  507. ];
  508. }
  509. public function testDefaultConfiguration(): void
  510. {
  511. $this->fixer->configure(['header' => 'a']);
  512. $this->doTest(
  513. '<?php
  514. /*
  515. * a
  516. */
  517. echo 1;',
  518. '<?php
  519. echo 1;'
  520. );
  521. }
  522. /**
  523. * @param _AutogeneratedInputConfiguration $configuration
  524. *
  525. * @dataProvider provideMisconfigurationCases
  526. */
  527. public function testMisconfiguration(?array $configuration, string $exceptionMessage): void
  528. {
  529. $this->expectException(InvalidFixerConfigurationException::class);
  530. $this->expectExceptionMessageMatches("#^\\[header_comment\\] {$exceptionMessage}$#");
  531. $this->fixer->configure($configuration);
  532. }
  533. public static function provideMisconfigurationCases(): iterable
  534. {
  535. yield [[], 'Missing required configuration: The required option "header" is missing.'];
  536. yield [
  537. ['header' => 1],
  538. 'Invalid configuration: The option "header" with value 1 is expected to be of type "string", but is of type "(int|integer)"\.',
  539. ];
  540. yield [
  541. [
  542. 'header' => '',
  543. 'comment_type' => 'foo',
  544. ],
  545. 'Invalid configuration: The option "comment_type" with value "foo" is invalid\. Accepted values are: "PHPDoc", "comment"\.',
  546. ];
  547. yield [
  548. [
  549. 'header' => '',
  550. 'comment_type' => new \stdClass(),
  551. ],
  552. 'Invalid configuration: The option "comment_type" with value stdClass is invalid\. Accepted values are: "PHPDoc", "comment"\.',
  553. ];
  554. yield [
  555. [
  556. 'header' => '',
  557. 'location' => new \stdClass(),
  558. ],
  559. 'Invalid configuration: The option "location" with value stdClass is invalid\. Accepted values are: "after_open", "after_declare_strict"\.',
  560. ];
  561. yield [
  562. [
  563. 'header' => '',
  564. 'separate' => new \stdClass(),
  565. ],
  566. 'Invalid configuration: The option "separate" with value stdClass is invalid\. Accepted values are: "both", "top", "bottom", "none"\.',
  567. ];
  568. }
  569. /**
  570. * @dataProvider provideHeaderGenerationCases
  571. *
  572. * @param HeaderCommentFixer::HEADER_* $type
  573. */
  574. public function testHeaderGeneration(string $expected, string $header, string $type): void
  575. {
  576. $this->fixer->configure([
  577. 'header' => $header,
  578. 'comment_type' => $type,
  579. ]);
  580. $this->doTest(
  581. '<?php
  582. '.$expected.'
  583. echo 1;',
  584. '<?php
  585. echo 1;'
  586. );
  587. }
  588. /**
  589. * @return iterable<array{string, string, string}>
  590. */
  591. public static function provideHeaderGenerationCases(): iterable
  592. {
  593. yield [
  594. '/*
  595. * a
  596. */',
  597. 'a',
  598. HeaderCommentFixer::HEADER_COMMENT,
  599. ];
  600. yield [
  601. '/**
  602. * a
  603. */',
  604. 'a',
  605. HeaderCommentFixer::HEADER_PHPDOC,
  606. ];
  607. }
  608. /**
  609. * @dataProvider provideDoNotTouchCases
  610. */
  611. public function testDoNotTouch(string $expected): void
  612. {
  613. $this->fixer->configure([
  614. 'header' => '',
  615. ]);
  616. $this->doTest($expected);
  617. }
  618. /**
  619. * @return iterable<array{string}>
  620. */
  621. public static function provideDoNotTouchCases(): iterable
  622. {
  623. yield ["<?php\nphpinfo();\n?>\n<?"];
  624. yield [" <?php\nphpinfo();\n"];
  625. yield ["<?php\nphpinfo();\n?><hr/>"];
  626. yield [" <?php\n"];
  627. yield ['<?= 1?>'];
  628. yield ["<?= 1?><?php\n"];
  629. yield ["<?= 1?>\n<?php\n"];
  630. yield ["<?php\n// comment 1\n?><?php\n// comment 2\n"];
  631. }
  632. public function testWithoutConfiguration(): void
  633. {
  634. $this->expectException(RequiredFixerConfigurationException::class);
  635. $this->doTest('<?php echo 1;');
  636. }
  637. /**
  638. * @param _AutogeneratedInputConfiguration $configuration
  639. *
  640. * @dataProvider provideMessyWhitespacesCases
  641. */
  642. public function testMessyWhitespaces(array $configuration, string $expected, ?string $input = null): void
  643. {
  644. $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n"));
  645. $this->fixer->configure($configuration);
  646. $this->doTest($expected, $input);
  647. }
  648. public static function provideMessyWhitespacesCases(): iterable
  649. {
  650. yield [
  651. [
  652. 'header' => 'whitemess',
  653. 'location' => 'after_declare_strict',
  654. 'separate' => 'bottom',
  655. 'comment_type' => HeaderCommentFixer::HEADER_PHPDOC,
  656. ],
  657. "<?php\r\ndeclare(strict_types=1);\r\n/**\r\n * whitemess\r\n */\r\n\r\nnamespace A\\B;\r\n\r\necho 1;",
  658. "<?php\r\ndeclare(strict_types=1);\r\n\r\nnamespace A\\B;\r\n\r\necho 1;",
  659. ];
  660. }
  661. public function testConfigurationUpdatedWithWhitespsacesConfig(): void
  662. {
  663. $this->fixer->configure(['header' => 'Foo']);
  664. $this->doTest(
  665. "<?php\n\n/*\n * Foo\n */\n\necho 1;",
  666. "<?php\necho 1;"
  667. );
  668. $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig(' ', "\r\n"));
  669. $this->doTest(
  670. "<?php\r\n\r\n/*\r\n * Foo\r\n */\r\n\r\necho 1;",
  671. "<?php\r\necho 1;"
  672. );
  673. $this->fixer->configure(['header' => 'Bar']);
  674. $this->doTest(
  675. "<?php\r\n\r\n/*\r\n * Bar\r\n */\r\n\r\necho 1;",
  676. "<?php\r\necho 1;"
  677. );
  678. $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig(' ', "\n"));
  679. $this->doTest(
  680. "<?php\n\n/*\n * Bar\n */\n\necho 1;",
  681. "<?php\necho 1;"
  682. );
  683. }
  684. public function testInvalidHeaderConfiguration(): void
  685. {
  686. $this->expectException(InvalidFixerConfigurationException::class);
  687. $this->expectExceptionMessageMatches('#^\[header_comment\] Cannot use \'\*/\' in header\.$#');
  688. $this->fixer->configure([
  689. 'header' => '/** test */',
  690. 'comment_type' => HeaderCommentFixer::HEADER_PHPDOC,
  691. ]);
  692. }
  693. /**
  694. * @param _AutogeneratedInputConfiguration $configuration
  695. *
  696. * @dataProvider provideFix81Cases
  697. *
  698. * @requires PHP 8.1
  699. */
  700. public function testFix81(array $configuration, string $expected, ?string $input = null): void
  701. {
  702. $this->fixer->configure($configuration);
  703. $this->doTest($expected, $input);
  704. }
  705. public static function provideFix81Cases(): iterable
  706. {
  707. yield [
  708. ['header' => 'tmp'],
  709. '<?php
  710. /*
  711. * tmp
  712. */
  713. /**
  714. * Foo class doc.
  715. */
  716. enum Foo {}',
  717. '<?php
  718. /**
  719. * Foo class doc.
  720. */
  721. enum Foo {}',
  722. ];
  723. }
  724. }