* Dariusz RumiƄski * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\AttributeNotation; use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException; use PhpCsFixer\Fixer\AttributeNotation\OrderedAttributesFixer; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @author HypeMC * * @internal * * @covers \PhpCsFixer\Fixer\AttributeNotation\OrderedAttributesFixer * * @requires PHP 8.0 * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\AttributeNotation\OrderedAttributesFixer> * * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\AttributeNotation\OrderedAttributesFixer */ final class OrderedAttributesFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideInvalidConfigurationCases * * @param _AutogeneratedInputConfiguration $configuration */ public function testInvalidConfiguration(array $configuration, string $expectedExceptionMessage): void { self::expectException(InvalidFixerConfigurationException::class); self::expectExceptionMessage($expectedExceptionMessage); $this->fixer->configure($configuration); } /** * @return iterable}, 1: string}> */ public static function provideInvalidConfigurationCases(): iterable { yield 'Custom order strategy without `order` option' => [ ['sort_algorithm' => OrderedAttributesFixer::ORDER_CUSTOM], 'The custom order strategy requires providing `order` option with a list of attributes\'s FQNs.', ]; yield 'Custom order strategy with empty `order` option' => [ [ 'sort_algorithm' => OrderedAttributesFixer::ORDER_CUSTOM, 'order' => [], ], 'The custom order strategy requires providing `order` option with a list of attributes\'s FQNs.', ]; yield 'Non unique attributes throw an exception' => [ [ 'sort_algorithm' => OrderedAttributesFixer::ORDER_CUSTOM, 'order' => ['A\B\Bar', 'Test\Corge', 'A\B\Bar'], ], 'The list includes attributes that are not unique.', ]; } /** * @param _AutogeneratedInputConfiguration $configuration * * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null, array $configuration = []): void { $this->fixer->configure($configuration); $this->doTest($expected, $input); } /** * @return iterable}> */ public static function provideFixCases(): iterable { yield 'Alpha on various declarations' => [ ' null; ', ' null; ', ]; yield 'Explicit in namespace' => [ ' OrderedAttributesFixer::ORDER_CUSTOM, 'order' => ['Test\A\B\Quux', 'A\B\Bar', 'Test\Corge', 'A\B\Baz', 'A\B\Foo', 'A\B\Qux'], ], ]; yield 'Explicit in global namespace' => [ ' OrderedAttributesFixer::ORDER_CUSTOM, 'order' => ['A\B\Quux', 'A\B\Bar', 'Corge', 'A\B\Baz', 'A\B\Foo', 'A\B\Qux'], ], ]; yield 'Explicit with unconfigured attributes' => [ ' OrderedAttributesFixer::ORDER_CUSTOM, 'order' => ['Test\A\B\Quux', 'A\B\Baz', 'A\B\Qux'], ], ]; yield 'Multiple namespaces' => [ ' OrderedAttributesFixer::ORDER_CUSTOM, 'order' => ['A\B\Bar', 'Test\AB\Baz', 'A\B\Quux', 'A\B\Baz', 'A\B\Foo', 'AB\Baz'], ], ]; yield 'With whitespaces' => [ ' [ ' [ ' OrderedAttributesFixer::ORDER_CUSTOM, 'order' => ['Test\A\B\Quux', 'A\B\Bar', 'Test\Corge', 'A\B\Baz', 'A\B\Foo', 'A\B\Qux'], ], ]; yield 'Alpha with multiple attributes' => [ ' [ ' OrderedAttributesFixer::ORDER_CUSTOM, 'order' => ['Test\A\B\Quux', 'A\B\Bar', 'Test\Corge', 'A\B\Baz', 'A\B\Qux'], ], ]; yield 'Multiline with no trailing comma' => [ ' [ ' [ ' [ ' OrderedAttributesFixer::ORDER_CUSTOM, 'order' => ['Test\A\B\Quux', 'A\B\Bar', 'Test\Corge', 'A\B\Baz', 'A\B\Qux'], ], ]; } /** * @param _AutogeneratedInputConfiguration $configuration * * @dataProvider provideFix81Cases * * @requires PHP 8.1 */ public function testFix81(string $expected, ?string $input = null, array $configuration = []): void { $this->fixer->configure($configuration); $this->doTest($expected, $input); } /** * @return iterable}> */ public static function provideFix81Cases(): iterable { yield 'Alpha with nested attribute' => [ ' [ ' OrderedAttributesFixer::ORDER_CUSTOM, 'order' => ['Test\A\B\Quux', 'A\B\Bar', 'Test\Corge', 'A\B\Baz', 'A\B\Foo', 'A\B\Qux'], ], ]; } }