123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348 |
- <?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\Phpdoc;
- use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
- use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
- /**
- * @internal
- *
- * @covers \PhpCsFixer\Fixer\Phpdoc\GeneralPhpdocTagRenameFixer
- *
- * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Phpdoc\GeneralPhpdocTagRenameFixer>
- *
- * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Phpdoc\GeneralPhpdocTagRenameFixer
- */
- final class GeneralPhpdocTagRenameFixerTest extends AbstractFixerTestCase
- {
- /**
- * @param _AutogeneratedInputConfiguration $configuration
- *
- * @dataProvider provideFixCases
- */
- public function testFix(string $expected, ?string $input = null, array $configuration = []): void
- {
- $this->fixer->configure($configuration);
- $this->doTest($expected, $input);
- }
- public static function provideFixCases(): iterable
- {
- yield [
- '<?php
- /**
- * @inheritdocs
- * @inheritDocs
- * {@inheritdocs}
- * {@inheritDocs}
- * @see Foo::bar()
- * {@see Foo::bar()}
- */',
- ];
- yield [
- '<?php
- /**
- * @inheritDoc
- * @inheritDoc
- * {@inheritDoc}
- * {@inheritDoc}
- * @see Foo::bar()
- * {@see Foo::bar()}
- */',
- '<?php
- /**
- * @inheritdocs
- * @inheritDocs
- * {@inheritdocs}
- * {@inheritDocs}
- * @see Foo::bar()
- * {@see Foo::bar()}
- */',
- [
- 'replacements' => ['inheritDocs' => 'inheritDoc'],
- ],
- ];
- yield [
- '<?php
- /**
- * @inheritdoc
- * @inheritdoc
- * {@inheritdoc}
- * {@inheritdoc}
- * @see Foo::bar()
- * {@see Foo::bar()}
- */',
- '<?php
- /**
- * @inheritdocs
- * @inheritDocs
- * {@inheritdocs}
- * {@inheritDocs}
- * @see Foo::bar()
- * {@see Foo::bar()}
- */',
- [
- 'fix_annotation' => true,
- 'fix_inline' => true,
- 'replacements' => ['inheritdocs' => 'inheritdoc'],
- 'case_sensitive' => false,
- ],
- ];
- yield [
- '<?php
- /**
- * @inheritDoc
- * @inheritDoc
- * {@inheritdocs}
- * {@inheritDocs}
- * @see Foo::bar()
- * {@see Foo::bar()}
- */',
- '<?php
- /**
- * @inheritdocs
- * @inheritDocs
- * {@inheritdocs}
- * {@inheritDocs}
- * @see Foo::bar()
- * {@see Foo::bar()}
- */',
- [
- 'fix_inline' => false,
- 'replacements' => ['inheritDocs' => 'inheritDoc'],
- ],
- ];
- yield [
- '<?php
- /**
- * @inheritdocs
- * @inheritDocs
- * {@inheritDoc}
- * {@inheritDoc}
- * @see Foo::bar()
- * {@see Foo::bar()}
- */',
- '<?php
- /**
- * @inheritdocs
- * @inheritDocs
- * {@inheritdocs}
- * {@inheritDocs}
- * @see Foo::bar()
- * {@see Foo::bar()}
- */',
- [
- 'fix_annotation' => false,
- 'replacements' => ['inheritDocs' => 'inheritDoc'],
- ],
- ];
- yield [
- '<?php
- /**
- * @inheritdocs
- * @inheritDoc
- * {@inheritdocs}
- * {@inheritDoc}
- * @see Foo::bar()
- * {@see Foo::bar()}
- */',
- '<?php
- /**
- * @inheritdocs
- * @inheritDocs
- * {@inheritdocs}
- * {@inheritDocs}
- * @see Foo::bar()
- * {@see Foo::bar()}
- */',
- [
- 'case_sensitive' => true,
- 'replacements' => ['inheritDocs' => 'inheritDoc'],
- ],
- ];
- yield [
- '<?php
- /**
- * @inheritdoc
- * @inheritdoc
- * {@inheritdoc}
- * {@inheritdoc}
- * @see Foo::bar()
- * {@see Foo::bar()}
- */',
- '<?php
- /**
- * @inheritdocs
- * @inheritDocs
- * {@inheritdocs}
- * {@inheritDocs}
- * @link Foo::bar()
- * {@link Foo::bar()}
- */',
- [
- 'replacements' => [
- 'inheritdocs' => 'inheritdoc',
- 'link' => 'see',
- ],
- ],
- ];
- yield [
- '<?php
- /**
- * @var int $foo
- * @Annotation("@type")
- */',
- '<?php
- /**
- * @type int $foo
- * @Annotation("@type")
- */',
- [
- 'fix_annotation' => true,
- 'fix_inline' => true,
- 'replacements' => [
- 'type' => 'var',
- ],
- ],
- ];
- yield [
- '<?php
- /**
- * @var int $foo
- * @Annotation("@type")
- */',
- '<?php
- /**
- * @type int $foo
- * @Annotation("@type")
- */',
- [
- 'fix_annotation' => true,
- 'fix_inline' => false,
- 'replacements' => [
- 'type' => 'var',
- ],
- ],
- ];
- }
- public function testConfigureWithInvalidOption(): void
- {
- $this->expectException(InvalidFixerConfigurationException::class);
- $this->expectExceptionMessageMatches('/^\[general_phpdoc_tag_rename\] Invalid configuration: The option "replacements" with value true is expected to be of type "string\[\]", but is of type "bool"\.$/');
- $this->fixer->configure([ // @phpstan-ignore-line
- 'replacements' => true,
- ]);
- }
- public function testConfigureWithUnknownOption(): void
- {
- $this->expectException(InvalidFixerConfigurationException::class);
- $this->expectExceptionMessageMatches('/^\[general_phpdoc_tag_rename\] Invalid configuration: The option "foo" does not exist\. (Known|Defined) options are: "case_sensitive", "fix_annotation", "fix_inline", "replacements"\.$/');
- $this->fixer->configure([
- 'foo' => true,
- ]);
- }
- /**
- * @param array<array-key, mixed> $replacements
- *
- * @dataProvider provideConfigureWithInvalidReplacementsCases
- */
- public function testConfigureWithInvalidReplacements(array $replacements, bool $caseSensitive, string $expectedMessage): void
- {
- $this->expectException(InvalidFixerConfigurationException::class);
- $this->expectExceptionMessageMatches(\sprintf(
- '/^\[general_phpdoc_tag_rename\] Invalid configuration: %s$/',
- preg_quote($expectedMessage, '/')
- ));
- $this->fixer->configure([
- 'replacements' => $replacements,
- 'case_sensitive' => $caseSensitive,
- ]);
- }
- public static function provideConfigureWithInvalidReplacementsCases(): iterable
- {
- yield [
- [1 => 'abc'],
- true,
- 'Tag to replace must be a string.',
- ];
- yield [
- ['a' => null],
- true,
- 'The option "replacements" with value array is expected to be of type "string[]", but one of the elements is of type "null".',
- ];
- yield [
- ['see' => 'link*/'],
- true,
- 'Tag "see" cannot be replaced by invalid tag "link*/".',
- ];
- yield [
- [
- 'link' => 'see',
- 'a' => 'b',
- 'see' => 'link',
- ],
- true,
- 'Cannot change tag "link" to tag "see", as the tag "see" is configured to be replaced to "link".',
- ];
- yield [
- [
- 'b' => 'see',
- 'see' => 'link',
- 'link' => 'b',
- ],
- true,
- 'Cannot change tag "b" to tag "see", as the tag "see" is configured to be replaced to "link".',
- ];
- yield [
- [
- 'see' => 'link',
- 'link' => 'b',
- ],
- true,
- 'Cannot change tag "see" to tag "link", as the tag "link" is configured to be replaced to "b".',
- ];
- yield [
- [
- 'Foo' => 'bar',
- 'foo' => 'baz',
- ],
- false,
- 'Tag "foo" cannot be configured to be replaced with several different tags when case sensitivity is off.',
- ];
- }
- }
|