123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575 |
- <?php
- /*
- * 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\Tests\Test\AbstractFixerTestCase;
- use PhpCsFixer\Tokenizer\Tokens;
- use PhpCsFixer\WhitespacesFixerConfig;
- /**
- * @internal
- *
- * @covers \PhpCsFixer\Fixer\Comment\HeaderCommentFixer
- */
- final class HeaderCommentFixerTest extends AbstractFixerTestCase
- {
- /**
- * @param string $expected
- * @param string $input
- *
- * @dataProvider provideFixCases
- */
- public function testFix(array $configuration, $expected, $input)
- {
- $this->fixer->configure($configuration);
- $this->doTest($expected, $input);
- }
- public function provideFixCases()
- {
- return [
- [
- ['header' => ''],
- '<?php
- $a;',
- '<?php
- /**
- * new
- */
- $a;',
- ],
- [
- [
- '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;',
- ],
- [
- [
- 'header' => 'tmp',
- 'location' => 'after_declare_strict',
- 'separate' => 'bottom',
- 'commentType' => 'PHPDoc',
- ],
- '<?php
- declare(strict_types=1);
- /**
- * tmp
- */
- namespace A\B;
- echo 1;',
- '<?php
- declare(strict_types=1);
- namespace A\B;
- echo 1;',
- ],
- [
- [
- '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;',
- ],
- [
- [
- 'header' => 'new',
- 'commentType' => 'comment',
- ],
- '<?php
- /*
- * new
- */
- '.'
- ',
- '<?php
- /** test */
- ',
- ],
- [
- [
- 'header' => 'new',
- 'commentType' => 'PHPDoc',
- ],
- '<?php
- /**
- * new
- */
- '.'
- ',
- '<?php
- /* test */
- ',
- ],
- [
- [
- 'header' => 'def',
- 'commentType' => 'PHPDoc',
- ],
- '<?php
- /**
- * def
- */
- ',
- '<?php
- ',
- ],
- [
- ['header' => 'xyz'],
- '<?php
- /*
- * xyz
- */
- $b;',
- '<?php
- $b;',
- ],
- [
- [
- 'header' => 'xyz123',
- 'separate' => 'none',
- ],
- '<?php
- /*
- * xyz123
- */
- $a;',
- '<?php
- $a;',
- ],
- [
- [
- 'header' => 'abc',
- 'commentType' => 'PHPDoc',
- ],
- '<?php
- /**
- * abc
- */
- $c;',
- '<?php
- $c;',
- ],
- [
- [
- 'header' => 'ghi',
- 'separate' => 'both',
- ],
- '<?php
- /*
- * ghi
- */
- $d;',
- '<?php
- $d;',
- ],
- [
- [
- 'header' => 'ghi',
- 'separate' => 'top',
- ],
- '<?php
- /*
- * ghi
- */
- $d;',
- '<?php
- $d;',
- ],
- [
- [
- 'header' => 'tmp',
- 'location' => 'after_declare_strict',
- ],
- '<?php
- /*
- * tmp
- */
- declare(ticks=1);
- echo 1;',
- '<?php
- declare(ticks=1);
- echo 1;',
- ],
- [
- ['header' => 'Foo'],
- '<?php
- /*
- * Foo
- */
- echo \'bar\';',
- '<?php echo \'bar\';',
- ],
- [
- ['header' => 'x'],
- '<?php
- /*
- * x
- */
- echo \'a\';',
- '<?php
- /*
- * y
- * z
- */
- echo \'a\';',
- ],
- [
- ['header' => "a\na"],
- '<?php
- /*
- * a
- * a
- */
- echo \'x\';',
- '<?php
- /*
- * b
- * c
- */
- echo \'x\';',
- ],
- ];
- }
- public function testDefaultConfiguration()
- {
- $this->fixer->configure(['header' => 'a']);
- $this->doTest(
- '<?php
- /*
- * a
- */
- echo 1;',
- '<?php
- echo 1;'
- );
- }
- /**
- * @param null|array $configuration
- * @param string $exceptionMessage
- *
- * @dataProvider provideMisconfigurationCases
- */
- public function testMisconfiguration($configuration, $exceptionMessage)
- {
- $this->expectException(\PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException::class);
- $this->expectExceptionMessage('[header_comment] '.$exceptionMessage);
- $this->fixer->configure($configuration);
- }
- public function provideMisconfigurationCases()
- {
- return [
- [[], 'Missing required configuration: The required option "header" is missing.'],
- [
- ['header' => 1],
- 'Invalid configuration: The option "header" with value 1 is expected to be of type "string", but is of type "integer".',
- ],
- [
- [
- 'header' => '',
- 'commentType' => 'foo',
- ],
- 'Invalid configuration: The option "commentType" with value "foo" is invalid. Accepted values are: "PHPDoc", "comment".',
- ],
- [
- [
- 'header' => '',
- 'commentType' => new \stdClass(),
- ],
- 'Invalid configuration: The option "commentType" with value stdClass is invalid. Accepted values are: "PHPDoc", "comment".',
- ],
- [
- [
- 'header' => '',
- 'location' => new \stdClass(),
- ],
- 'Invalid configuration: The option "location" with value stdClass is invalid. Accepted values are: "after_open", "after_declare_strict".',
- ],
- [
- [
- 'header' => '',
- 'separate' => new \stdClass(),
- ],
- 'Invalid configuration: The option "separate" with value stdClass is invalid. Accepted values are: "both", "top", "bottom", "none".',
- ],
- ];
- }
- /**
- * @param string $expected
- * @param string $header
- * @param string $type
- *
- * @dataProvider provideHeaderGenerationCases
- */
- public function testHeaderGeneration($expected, $header, $type)
- {
- $this->fixer->configure([
- 'header' => $header,
- 'commentType' => $type,
- ]);
- $this->doTest(
- '<?php
- '.$expected.'
- echo 1;',
- '<?php
- echo 1;'
- );
- }
- public function provideHeaderGenerationCases()
- {
- return [
- [
- '/*
- * a
- */',
- 'a',
- 'comment',
- ],
- [
- '/**
- * a
- */',
- 'a',
- 'PHPDoc',
- ],
- ];
- }
- /**
- * @param int $expected
- * @param string $code
- *
- * @dataProvider provideFindHeaderCommentInsertionIndexCases
- */
- public function testFindHeaderCommentInsertionIndex($expected, $code, array $config)
- {
- Tokens::clearCache();
- $tokens = Tokens::fromCode($code);
- $this->fixer->configure($config);
- $method = new \ReflectionMethod($this->fixer, 'findHeaderCommentInsertionIndex');
- $method->setAccessible(true);
- $this->assertSame($expected, $method->invoke($this->fixer, $tokens));
- }
- public function provideFindHeaderCommentInsertionIndexCases()
- {
- $config = ['header' => ''];
- $cases = [
- [1, '<?php #', $config],
- [1, '<?php /**/ $bc;', $config],
- [1, '<?php $bc;', $config],
- [1, "<?php\n\n", $config],
- [1, '<?php ', $config],
- ];
- $config['location'] = 'after_declare_strict';
- $cases[] = [
- 8,
- '<?php
- declare(strict_types=1);
- namespace A\B;
- echo 1;',
- $config,
- ];
- $cases[] = [
- 8,
- '<?php
- declare(strict_types=0);
- echo 1;',
- $config,
- ];
- $cases[] = [
- 1,
- '<?php
- declare(strict_types=1)?>',
- $config,
- ];
- return $cases;
- }
- /**
- * @param string $expected
- *
- * @dataProvider provideDoNotTouchCases
- */
- public function testDoNotTouch($expected)
- {
- $this->fixer->configure([
- 'header' => '',
- ]);
- $this->doTest($expected);
- }
- public function provideDoNotTouchCases()
- {
- return [
- ["<?php\nphpinfo();\n?>\n<?"],
- [" <?php\nphpinfo();\n"],
- ["<?php\nphpinfo();\n?><hr/>"],
- [" <?php\n"],
- ['<?= 1?>'],
- ['<?= 1?><?php'],
- ["<?= 1?>\n<?php"],
- ];
- }
- public function testWithoutConfiguration()
- {
- $this->expectException(\PhpCsFixer\ConfigurationException\RequiredFixerConfigurationException::class);
- $this->doTest('<?php echo 1;');
- }
- /**
- * @param string $expected
- * @param null|string $input
- *
- * @dataProvider provideMessyWhitespacesCases
- */
- public function testMessyWhitespaces(array $configuration, $expected, $input = null)
- {
- $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n"));
- $this->fixer->configure($configuration);
- $this->doTest($expected, $input);
- }
- public function provideMessyWhitespacesCases()
- {
- return [
- [
- [
- 'header' => 'whitemess',
- 'location' => 'after_declare_strict',
- 'separate' => 'bottom',
- 'commentType' => '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()
- {
- $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;"
- );
- }
- }
|