12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040 |
- <?php
- declare(strict_types=1);
- /*
- * This file is part of PHP CS Fixer.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- * Dariusz Rumiński <dariusz.ruminski@gmail.com>
- *
- * This source file is subject to the MIT license that is bundled
- * with this source code in the file LICENSE.
- */
- namespace PhpCsFixer\Tests\Fixer\Comment;
- use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
- use PhpCsFixer\ConfigurationException\RequiredFixerConfigurationException;
- use PhpCsFixer\Fixer\Comment\HeaderCommentFixer;
- use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
- use PhpCsFixer\WhitespacesFixerConfig;
- /**
- * @internal
- *
- * @covers \PhpCsFixer\Fixer\Comment\HeaderCommentFixer
- *
- * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Comment\HeaderCommentFixer>
- *
- * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Comment\HeaderCommentFixer
- */
- final class HeaderCommentFixerTest extends AbstractFixerTestCase
- {
- /**
- * @param _AutogeneratedInputConfiguration $configuration
- *
- * @dataProvider provideFixCases
- */
- public function testFix(array $configuration, string $expected, ?string $input = null): void
- {
- $this->fixer->configure($configuration);
- $this->doTest($expected, $input);
- }
- public static function provideFixCases(): iterable
- {
- yield [
- ['header' => ''],
- '<?php
- $a;',
- '<?php
- /**
- * new
- */
- $a;',
- ];
- yield [
- [
- 'header' => 'tmp',
- 'location' => 'after_declare_strict',
- ],
- '<?php
- declare(strict_types=1);
- /*
- * tmp
- */
- namespace A\B;
- echo 1;',
- '<?php
- declare(strict_types=1);namespace A\B;
- echo 1;',
- ];
- yield [
- [
- 'header' => 'tmp',
- 'location' => 'after_declare_strict',
- 'separate' => 'bottom',
- 'comment_type' => HeaderCommentFixer::HEADER_PHPDOC,
- ],
- '<?php
- declare(strict_types=1);
- /**
- * tmp
- */
- namespace A\B;
- echo 1;',
- '<?php
- declare(strict_types=1);
- namespace A\B;
- echo 1;',
- ];
- yield [
- [
- 'header' => 'tmp',
- 'location' => 'after_open',
- ],
- '<?php
- /*
- * tmp
- */
- declare(strict_types=1);
- namespace A\B;
- echo 1;',
- '<?php
- declare(strict_types=1);
- namespace A\B;
- echo 1;',
- ];
- yield [
- [
- 'header' => 'new',
- 'comment_type' => HeaderCommentFixer::HEADER_COMMENT,
- ],
- '<?php
- /*
- * new
- */
- ',
- '<?php
- /** test */
- ',
- ];
- yield [
- [
- 'header' => 'new',
- 'comment_type' => HeaderCommentFixer::HEADER_PHPDOC,
- ],
- '<?php
- /**
- * new
- */
- ',
- '<?php
- /* test */
- ',
- ];
- yield [
- [
- 'header' => 'def',
- 'comment_type' => HeaderCommentFixer::HEADER_PHPDOC,
- ],
- '<?php
- /**
- * def
- */
- ',
- '<?php
- ',
- ];
- yield [
- ['header' => 'xyz'],
- '<?php
- /*
- * xyz
- */
- $b;',
- '<?php
- $b;',
- ];
- yield [
- [
- 'header' => 'xyz123',
- 'separate' => 'none',
- ],
- '<?php
- /*
- * xyz123
- */
- $a;',
- '<?php
- $a;',
- ];
- yield [
- [
- 'header' => 'abc',
- 'comment_type' => HeaderCommentFixer::HEADER_PHPDOC,
- ],
- '<?php
- /**
- * abc
- */
- $c;',
- '<?php
- $c;',
- ];
- yield [
- [
- 'header' => 'ghi',
- 'separate' => 'both',
- ],
- '<?php
- /*
- * ghi
- */
- $d;',
- '<?php
- $d;',
- ];
- yield [
- [
- 'header' => 'ghi',
- 'separate' => 'top',
- ],
- '<?php
- /*
- * ghi
- */
- $d;',
- '<?php
- $d;',
- ];
- yield [
- [
- 'header' => 'tmp',
- 'location' => 'after_declare_strict',
- ],
- '<?php
- /*
- * tmp
- */
- declare(ticks=1);
- echo 1;',
- '<?php
- declare(ticks=1);
- echo 1;',
- ];
- yield [
- ['header' => 'Foo'],
- '<?php
- /*
- * Foo
- */
- echo \'bar\';',
- '<?php echo \'bar\';',
- ];
- yield [
- ['header' => 'x'],
- '<?php
- /*
- * x
- */
- echo \'a\';',
- '<?php
- /*
- * y
- * z
- */
- echo \'a\';',
- ];
- yield [
- ['header' => "a\na"],
- '<?php
- /*
- * a
- * a
- */
- echo \'x\';',
- '<?php
- /*
- * b
- * c
- */
- echo \'x\';',
- ];
- yield [
- [
- 'header' => 'foo',
- 'location' => 'after_open',
- 'separate' => 'bottom',
- 'comment_type' => HeaderCommentFixer::HEADER_PHPDOC,
- ],
- '<?php
- /**
- * foo
- */
- declare(strict_types=1);
- namespace A;
- echo 1;',
- '<?php
- declare(strict_types=1);
- /**
- * foo
- */
- namespace A;
- echo 1;',
- ];
- yield [
- [
- 'header' => 'foo',
- 'location' => 'after_open',
- 'separate' => 'bottom',
- 'comment_type' => HeaderCommentFixer::HEADER_PHPDOC,
- ],
- '<?php
- /**
- * foo
- */
- declare(strict_types=1);
- /**
- * bar
- */
- namespace A;
- echo 1;',
- '<?php
- declare(strict_types=1);
- /**
- * bar
- */
- namespace A;
- echo 1;',
- ];
- yield [
- [
- 'header' => 'Foo',
- 'separate' => 'none',
- ],
- '<?php
- declare(strict_types=1);
- /*
- * Foo
- */
- namespace SebastianBergmann\Foo;
- class Bar
- {
- }',
- '<?php
- /*
- * Foo
- */
- declare(strict_types=1);
- namespace SebastianBergmann\Foo;
- class Bar
- {
- }',
- ];
- yield [
- ['header' => 'tmp'],
- '<?php
- /*
- * tmp
- */
- /**
- * Foo class doc.
- */
- class Foo {}',
- '<?php
- /**
- * Foo class doc.
- */
- class Foo {}',
- ];
- yield [
- ['header' => 'tmp'],
- '<?php
- /*
- * tmp
- */
- class Foo {}',
- '<?php
- /*
- * Foo class doc.
- */
- class Foo {}',
- ];
- yield [
- [
- 'header' => 'tmp1',
- 'comment_type' => HeaderCommentFixer::HEADER_PHPDOC,
- ],
- '<?php
- /**
- * tmp1
- */
- /**
- * Foo class doc.
- */
- class Foo {}',
- '<?php
- /**
- * Foo class doc.
- */
- class Foo {}',
- ];
- yield [
- [
- 'header' => 'tmp2',
- 'comment_type' => HeaderCommentFixer::HEADER_PHPDOC,
- ],
- '<?php
- /**
- * tmp2
- */
- class Foo {}',
- '<?php
- /**
- * tmp2
- */
- class Foo {}',
- ];
- yield [
- [
- 'header' => 'tmp3',
- 'separate' => 'top',
- ],
- '<?php
- /*
- * tmp3
- */
- class Foo {}',
- '<?php
- /**
- * Foo class doc.
- */
- class Foo {}',
- ];
- yield [
- [
- 'header' => 'bar',
- 'location' => 'after_open',
- ],
- '<?php
- /*
- * bar
- */
- declare(strict_types=1);
- // foo
- foo();',
- '<?php
- /*
- * foo
- */
- declare(strict_types=1);
- // foo
- foo();',
- ];
- yield [
- [
- 'header' => 'bar',
- 'location' => 'after_open',
- ],
- '<?php
- /*
- * bar
- */
- declare(strict_types=1);
- /* foo */
- foo();',
- '<?php
- /*
- * foo
- */
- declare(strict_types=1);
- /* foo */
- foo();',
- ];
- yield [
- [
- 'header' => 'tmp4',
- 'location' => 'after_declare_strict',
- ],
- '<?php
- /*
- * tmp4
- */
- declare(strict_types=1) ?>',
- '<?php
- declare(strict_types=1) ?>',
- ];
- yield [
- [
- 'header' => 'tmp5',
- 'location' => 'after_declare_strict',
- ],
- '#!/usr/bin/env php
- <?php
- declare(strict_types=1);
- /*
- * tmp5
- */
- namespace A\B;
- echo 1;',
- '#!/usr/bin/env php
- <?php
- declare(strict_types=1);namespace A\B;
- echo 1;',
- ];
- yield [
- [
- 'header' => 'tmp',
- 'location' => 'after_open',
- ],
- 'Short mixed file A
- Hello<?php echo "World!"; ?>',
- ];
- yield [
- [
- 'header' => 'tmp',
- 'location' => 'after_open',
- ],
- 'Short mixed file B
- <?php echo "Hello"; ?>World!',
- ];
- yield [
- [
- 'header' => 'tmp',
- 'location' => 'after_open',
- ],
- 'File with anything at the beginning and with multiple opening tags are not supported
- <?php
- echo 1;
- ?>Hello World!<?php
- script_continues_here();',
- ];
- $fileHeaderParts = [
- <<<'EOF'
- This file is part of the xxx.
- (c) Foo Bar <foo@bar.com>
- EOF,
- <<<'EOF'
- For the full copyright and license information, please view the LICENSE
- file that was distributed with this source code.
- EOF,
- ];
- $fileHeaderComment = implode('', $fileHeaderParts);
- $fileHeaderCommentValidator = implode('', [
- '/',
- preg_quote($fileHeaderParts[0], '/'),
- '(?P<EXTRA>.*)??',
- preg_quote($fileHeaderParts[1], '/'),
- '/s',
- ]);
- yield 'using validator, but existing comment in wrong place - adding new one' => [
- [
- 'header' => $fileHeaderComment,
- 'validator' => $fileHeaderCommentValidator,
- 'location' => 'after_open',
- ],
- '<?php
- /*
- * This file is part of the xxx.
- *
- * (c) Foo Bar <foo@bar.com>
- *
- * This
- * is
- * sub
- * note.
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- declare(strict_types=1);
- namespace A;
- echo 1;',
- '<?php
- declare(strict_types=1);
- /*
- * This file is part of the xxx.
- *
- * (c) Foo Bar <foo@bar.com>
- *
- * This
- * is
- * sub
- * note.
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace A;
- echo 1;',
- ];
- yield 'using validator, existing comment matches' => [
- [
- 'header' => $fileHeaderComment,
- 'validator' => $fileHeaderCommentValidator,
- 'location' => 'after_open',
- ],
- '<?php
- /*
- * This file is part of the xxx.
- *
- * (c) Foo Bar <foo@bar.com>
- *
- * This
- * is
- * sub
- * note.
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- declare(strict_types=1);
- namespace A;
- echo 1;',
- ];
- yield 'using validator, existing comment matches but misplaced' => [
- [
- 'header' => $fileHeaderComment,
- 'validator' => $fileHeaderCommentValidator,
- 'comment_type' => HeaderCommentFixer::HEADER_PHPDOC,
- ],
- '<?php
- /**
- * This file is part of the xxx.
- *
- * (c) Foo Bar <foo@bar.com>
- *
- * This
- * is
- * sub
- * note.
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- class Foo {}',
- '<?php
- /**
- * This file is part of the xxx.
- *
- * (c) Foo Bar <foo@bar.com>
- *
- * This
- * is
- * sub
- * note.
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- class Foo {}',
- ];
- }
- public function testDefaultConfiguration(): void
- {
- $this->fixer->configure(['header' => 'a']);
- $this->doTest(
- '<?php
- /*
- * a
- */
- echo 1;',
- '<?php
- echo 1;'
- );
- }
- /**
- * @param _AutogeneratedInputConfiguration $configuration
- *
- * @dataProvider provideMisconfigurationCases
- */
- public function testMisconfiguration(?array $configuration, string $exceptionMessage): void
- {
- $this->expectException(InvalidFixerConfigurationException::class);
- $this->expectExceptionMessageMatches("#^\\[header_comment\\] {$exceptionMessage}$#");
- $this->fixer->configure($configuration);
- }
- public static function provideMisconfigurationCases(): iterable
- {
- yield [[], 'Missing required configuration: The required option "header" is missing.'];
- yield [
- ['header' => 1],
- 'Invalid configuration: The option "header" with value 1 is expected to be of type "string", but is of type "(int|integer)"\.',
- ];
- yield [
- [
- 'header' => '',
- 'comment_type' => 'foo',
- ],
- 'Invalid configuration: The option "comment_type" with value "foo" is invalid\. Accepted values are: "PHPDoc", "comment"\.',
- ];
- yield [
- [
- 'header' => '',
- 'comment_type' => new \stdClass(),
- ],
- 'Invalid configuration: The option "comment_type" with value stdClass is invalid\. Accepted values are: "PHPDoc", "comment"\.',
- ];
- yield [
- [
- 'header' => '',
- 'location' => new \stdClass(),
- ],
- 'Invalid configuration: The option "location" with value stdClass is invalid\. Accepted values are: "after_open", "after_declare_strict"\.',
- ];
- yield [
- [
- 'header' => '',
- 'separate' => new \stdClass(),
- ],
- 'Invalid configuration: The option "separate" with value stdClass is invalid\. Accepted values are: "both", "top", "bottom", "none"\.',
- ];
- yield [
- [
- 'header' => 'Foo',
- 'validator' => '/\w+++/',
- ],
- 'Provided RegEx is not valid.',
- ];
- }
- /**
- * @dataProvider provideHeaderGenerationCases
- *
- * @param HeaderCommentFixer::HEADER_* $type
- */
- public function testHeaderGeneration(string $expected, string $header, string $type): void
- {
- $this->fixer->configure([
- 'header' => $header,
- 'comment_type' => $type,
- ]);
- $this->doTest(
- '<?php
- '.$expected.'
- echo 1;',
- '<?php
- echo 1;'
- );
- }
- /**
- * @return iterable<array{string, string, string}>
- */
- public static function provideHeaderGenerationCases(): iterable
- {
- yield [
- '/*
- * a
- */',
- 'a',
- HeaderCommentFixer::HEADER_COMMENT,
- ];
- yield [
- '/**
- * a
- */',
- 'a',
- HeaderCommentFixer::HEADER_PHPDOC,
- ];
- }
- /**
- * @dataProvider provideDoNotTouchCases
- */
- public function testDoNotTouch(string $expected): void
- {
- $this->fixer->configure([
- 'header' => '',
- ]);
- $this->doTest($expected);
- }
- /**
- * @return iterable<array{string}>
- */
- public static function provideDoNotTouchCases(): iterable
- {
- yield ["<?php\nphpinfo();\n?>\n<?"];
- yield [" <?php\nphpinfo();\n"];
- yield ["<?php\nphpinfo();\n?><hr/>"];
- yield [" <?php\n"];
- yield ['<?= 1?>'];
- yield ["<?= 1?><?php\n"];
- yield ["<?= 1?>\n<?php\n"];
- yield ["<?php\n// comment 1\n?><?php\n// comment 2\n"];
- }
- public function testWithoutConfiguration(): void
- {
- $this->expectException(RequiredFixerConfigurationException::class);
- $this->doTest('<?php echo 1;');
- }
- /**
- * @param _AutogeneratedInputConfiguration $configuration
- *
- * @dataProvider provideMessyWhitespacesCases
- */
- public function testMessyWhitespaces(array $configuration, string $expected, ?string $input = null): void
- {
- $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n"));
- $this->fixer->configure($configuration);
- $this->doTest($expected, $input);
- }
- public static function provideMessyWhitespacesCases(): iterable
- {
- yield [
- [
- 'header' => 'whitemess',
- 'location' => 'after_declare_strict',
- 'separate' => 'bottom',
- 'comment_type' => HeaderCommentFixer::HEADER_PHPDOC,
- ],
- "<?php\r\ndeclare(strict_types=1);\r\n/**\r\n * whitemess\r\n */\r\n\r\nnamespace A\\B;\r\n\r\necho 1;",
- "<?php\r\ndeclare(strict_types=1);\r\n\r\nnamespace A\\B;\r\n\r\necho 1;",
- ];
- }
- public function testConfigurationUpdatedWithWhitespsacesConfig(): void
- {
- $this->fixer->configure(['header' => 'Foo']);
- $this->doTest(
- "<?php\n\n/*\n * Foo\n */\n\necho 1;",
- "<?php\necho 1;"
- );
- $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig(' ', "\r\n"));
- $this->doTest(
- "<?php\r\n\r\n/*\r\n * Foo\r\n */\r\n\r\necho 1;",
- "<?php\r\necho 1;"
- );
- $this->fixer->configure(['header' => 'Bar']);
- $this->doTest(
- "<?php\r\n\r\n/*\r\n * Bar\r\n */\r\n\r\necho 1;",
- "<?php\r\necho 1;"
- );
- $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig(' ', "\n"));
- $this->doTest(
- "<?php\n\n/*\n * Bar\n */\n\necho 1;",
- "<?php\necho 1;"
- );
- }
- public function testInvalidHeaderConfiguration(): void
- {
- $this->expectException(InvalidFixerConfigurationException::class);
- $this->expectExceptionMessageMatches('#^\[header_comment\] Cannot use \'\*/\' in header\.$#');
- $this->fixer->configure([
- 'header' => '/** test */',
- 'comment_type' => HeaderCommentFixer::HEADER_PHPDOC,
- ]);
- }
- /**
- * @param _AutogeneratedInputConfiguration $configuration
- *
- * @dataProvider provideFix81Cases
- *
- * @requires PHP 8.1
- */
- public function testFix81(array $configuration, string $expected, ?string $input = null): void
- {
- $this->fixer->configure($configuration);
- $this->doTest($expected, $input);
- }
- public static function provideFix81Cases(): iterable
- {
- yield [
- ['header' => 'tmp'],
- '<?php
- /*
- * tmp
- */
- /**
- * Foo class doc.
- */
- enum Foo {}',
- '<?php
- /**
- * Foo class doc.
- */
- enum Foo {}',
- ];
- }
- }
|