123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- <?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\ClassNotation;
- use PhpCsFixer\Fixer\ClassNotation\OrderedInterfacesFixer;
- use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
- /**
- * @author Dave van der Brugge <dmvdbrugge@gmail.com>
- *
- * @internal
- *
- * @covers \PhpCsFixer\Fixer\ClassNotation\OrderedInterfacesFixer
- *
- * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ClassNotation\OrderedInterfacesFixer>
- *
- * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\ClassNotation\OrderedInterfacesFixer
- */
- final class OrderedInterfacesFixerTest extends AbstractFixerTestCase
- {
- /**
- * @dataProvider provideFixCases
- *
- * @param _AutogeneratedInputConfiguration $configuration
- */
- 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 'single' => [
- '<?php class T implements A {}',
- ];
- yield 'multiple' => [
- '<?php class T implements A, B, C {}',
- '<?php class T implements C, A, B {}',
- ];
- yield 'newlines' => [
- "<?php class T implements\nB,\nC\n{}",
- "<?php class T implements\nC,\nB\n{}",
- ];
- yield 'newlines and comments' => [
- "<?php class T implements\n// Here's A\nA,\n// Here's B\nB\n{}",
- "<?php class T implements\n// Here's B\nB,\n// Here's A\nA\n{}",
- ];
- yield 'no whitespace' => [
- '<?php class T implements/*An*/AnInterface,/*Second*/SecondInterface/*end*/{}',
- '<?php class T implements/*Second*/SecondInterface,/*An*/AnInterface/*end*/{}',
- ];
- yield 'FQCN' => [
- '<?php class T implements \F\Q\C\N, \F\Q\I\N {}',
- '<?php class T implements \F\Q\I\N, \F\Q\C\N {}',
- ];
- yield 'mixed' => [
- '<?php class T implements \F\Q\C\N, Partially\Q\C\N, /* Who mixes these? */ UnNamespaced {}',
- '<?php class T implements /* Who mixes these? */ UnNamespaced, \F\Q\C\N, Partially\Q\C\N {}',
- ];
- yield 'multiple in file' => [
- '<?php
- class A1 implements A\B\C, Z\X\Y {}
- class B2 implements A\B, Z\X {}
- class C3 implements A, Z\X {}
- class D4 implements A\B, B\V, Z\X\V {}
- class E5 implements U\B, X\B, Y\V, Z\X\V {}
- ',
- '<?php
- class A1 implements Z\X\Y, A\B\C {}
- class B2 implements Z\X, A\B {}
- class C3 implements Z\X, A {}
- class D4 implements Z\X\V, B\V, A\B {}
- class E5 implements Z\X\V, Y\V, X\B, U\B {}
- ',
- ];
- yield 'interface extends' => [
- '<?php interface T extends A, B, C {}',
- '<?php interface T extends C, A, B {}',
- ];
- yield 'nested anonymous classes' => [
- '<?php
- class T implements A, B, C
- {
- public function getAnonymousClassObject()
- {
- return new class() implements C, D, E
- {
- public function getNestedAnonymousClassObject()
- {
- return new class() implements E, F, G {};
- }
- };
- }
- }
- ',
- '<?php
- class T implements C, A, B
- {
- public function getAnonymousClassObject()
- {
- return new class() implements E, C, D
- {
- public function getNestedAnonymousClassObject()
- {
- return new class() implements F, G, E {};
- }
- };
- }
- }
- ',
- ];
- yield 'single line after interfaces' => [
- '<?php
- class Foo implements B, C //, A
- {}
- ',
- '<?php
- class Foo implements C, B //, A
- {}
- ',
- ];
- yield 'descend single' => [
- '<?php class T implements A {}',
- null,
- [OrderedInterfacesFixer::OPTION_DIRECTION => OrderedInterfacesFixer::DIRECTION_DESCEND],
- ];
- yield 'descend multiple' => [
- '<?php class T implements C, B, A {}',
- '<?php class T implements C, A, B {}',
- [OrderedInterfacesFixer::OPTION_DIRECTION => OrderedInterfacesFixer::DIRECTION_DESCEND],
- ];
- yield 'descend mixed' => [
- '<?php class T implements /* Who mixes these? */ UnNamespaced, Partially\Q\C\N, \F\Q\C\N {}',
- '<?php class T implements /* Who mixes these? */ UnNamespaced, \F\Q\C\N, Partially\Q\C\N {}',
- [OrderedInterfacesFixer::OPTION_DIRECTION => OrderedInterfacesFixer::DIRECTION_DESCEND],
- ];
- yield 'length single' => [
- '<?php class A implements A {}',
- null,
- [OrderedInterfacesFixer::OPTION_ORDER => OrderedInterfacesFixer::ORDER_LENGTH],
- ];
- yield 'length multiple' => [
- '<?php class A implements Short, Longer, MuchLonger {}',
- '<?php class A implements MuchLonger, Short, Longer {}',
- [OrderedInterfacesFixer::OPTION_ORDER => OrderedInterfacesFixer::ORDER_LENGTH],
- ];
- yield 'length mixed' => [
- '<?php class T implements \F\Q\C\N, /* Who mixes these? */ UnNamespaced, Partially\Q\C\N {}',
- '<?php class T implements /* Who mixes these? */ UnNamespaced, \F\Q\C\N, Partially\Q\C\N {}',
- [OrderedInterfacesFixer::OPTION_ORDER => OrderedInterfacesFixer::ORDER_LENGTH],
- ];
- yield 'length normalized' => [
- '<?php
- class A implements
- ABCDE,
- A\B\C\D
- { /* */ }
- ',
- '<?php
- class A implements
- A\B\C\D,
- ABCDE
- { /* */ }
- ',
- [OrderedInterfacesFixer::OPTION_ORDER => OrderedInterfacesFixer::ORDER_LENGTH],
- ];
- yield 'length, descend single' => [
- '<?php class A implements A {}',
- null,
- [
- OrderedInterfacesFixer::OPTION_ORDER => OrderedInterfacesFixer::ORDER_LENGTH,
- OrderedInterfacesFixer::OPTION_DIRECTION => OrderedInterfacesFixer::DIRECTION_DESCEND,
- ],
- ];
- yield 'length, descend multiple' => [
- '<?php class A implements MuchLonger, Longer, Short {}',
- '<?php class A implements MuchLonger, Short, Longer {}',
- [
- OrderedInterfacesFixer::OPTION_ORDER => OrderedInterfacesFixer::ORDER_LENGTH,
- OrderedInterfacesFixer::OPTION_DIRECTION => OrderedInterfacesFixer::DIRECTION_DESCEND,
- ],
- ];
- yield 'length, descend mixed' => [
- '<?php class T implements Partially\Q\C\N, /* Who mixes these? */ UnNamespaced, \F\Q\C\N {}',
- '<?php class T implements /* Who mixes these? */ UnNamespaced, \F\Q\C\N, Partially\Q\C\N {}',
- [
- OrderedInterfacesFixer::OPTION_ORDER => OrderedInterfacesFixer::ORDER_LENGTH,
- OrderedInterfacesFixer::OPTION_DIRECTION => OrderedInterfacesFixer::DIRECTION_DESCEND,
- ],
- ];
- yield 'length, descend normalized' => [
- '<?php
- class A implements
- A\B\C\D,
- ABCDE
- { /* */ }
- ',
- '<?php
- class A implements
- ABCDE,
- A\B\C\D
- { /* */ }
- ',
- [
- OrderedInterfacesFixer::OPTION_ORDER => OrderedInterfacesFixer::ORDER_LENGTH,
- OrderedInterfacesFixer::OPTION_DIRECTION => OrderedInterfacesFixer::DIRECTION_DESCEND,
- ],
- ];
- yield 'case sensitive single' => [
- '<?php class A implements A {}',
- null,
- [
- OrderedInterfacesFixer::OPTION_ORDER => OrderedInterfacesFixer::ORDER_ALPHA,
- 'case_sensitive' => true,
- ],
- ];
- yield 'alpha multiple' => [
- '<?php class A implements AA, Aaa, FF, Fff {}',
- '<?php class A implements Fff, Aaa, FF, AA {}',
- [
- OrderedInterfacesFixer::OPTION_ORDER => OrderedInterfacesFixer::ORDER_ALPHA,
- 'case_sensitive' => true,
- ],
- ];
- yield 'alpha mixed' => [
- '<?php class T implements \F\Q\C\N, Partially\Q\C\N, /* Who mixes these? */ UnNamespaced {}',
- '<?php class T implements /* Who mixes these? */ UnNamespaced, \F\Q\C\N, Partially\Q\C\N {}',
- [
- OrderedInterfacesFixer::OPTION_ORDER => OrderedInterfacesFixer::ORDER_ALPHA,
- 'case_sensitive' => true,
- ],
- ];
- yield 'alpha normalized' => [
- '<?php
- class A implements
- A\B\C\D,
- AAa\B\C\D,
- ABCDE,
- Aaa\B\C\D
- { /* */ }
- ',
- '<?php
- class A implements
- Aaa\B\C\D,
- AAa\B\C\D,
- ABCDE,
- A\B\C\D
- { /* */ }
- ',
- [
- OrderedInterfacesFixer::OPTION_ORDER => OrderedInterfacesFixer::ORDER_ALPHA,
- 'case_sensitive' => true,
- ],
- ];
- }
- }
|