123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558 |
- <?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\Import;
- use PhpCsFixer\Fixer\Import\GroupImportFixer;
- use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
- /**
- * @author Volodymyr Kupriienko <vldmr.kuprienko@gmail.com>
- * @author Greg Korba <greg@codito.dev>
- *
- * @internal
- *
- * @covers \PhpCsFixer\Fixer\Import\GroupImportFixer
- *
- * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Import\GroupImportFixer>
- *
- * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Import\GroupImportFixer
- */
- final class GroupImportFixerTest extends AbstractFixerTestCase
- {
- /**
- * @dataProvider provideFixCases
- *
- * @phpstan-param _AutogeneratedInputConfiguration $config
- */
- public function testFix(string $expected, ?string $input = null, ?array $config = []): void
- {
- $this->fixer->configure($config);
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<array{0: string, 1?: string, 2?: _AutogeneratedInputConfiguration}>
- */
- public static function provideFixCases(): iterable
- {
- yield [
- '<?php
- namespace X1\Y1;
- use A\{B as X, C as Y};
- use B\C;
- ',
- '<?php
- namespace X1\Y1;
- use A\B as X;
- use A\C as Y;
- use B\C;
- ',
- ];
- yield [
- '<?php
- namespace Foo\Bar;
- use A\{B as X, C as Y};
- ',
- '<?php
- namespace Foo\Bar;
- use A\B as X;
- use A\C as Y;
- ',
- ];
- yield [
- '<?php
- namespace Test;
- use Foo\{Bar, Baz, Test};
- ',
- '<?php
- namespace Test;
- use Foo\Bar;
- use Foo\Baz;
- use Foo\Test;
- ',
- ];
- yield [
- '<?php
- use App\First;
- use Foo\{Bar, Baz};
- use Test\Second;
- ',
- '<?php
- use App\First;
- use Foo\Bar;
- use Test\Second;
- use Foo\Baz;
- ',
- ];
- yield [
- '<?php
- use App\{First, Second};
- use Foo\{Bar, Baz};
- ',
- '<?php
- use Foo\Bar;
- use Foo\Baz;
- use App\First;
- use App\Second;
- ',
- ];
- yield [
- '<?php
- use App\{First, Second};
- use Foo\{Bar, Baz};
- ',
- '<?php
- use Foo\Bar;
- use App\First;
- use Foo\Baz;
- use App\Second;
- ',
- ];
- yield [
- '<?php
- use Foo\{Bar as Test, Baz};
- use App;
- ',
- '<?php
- use Foo\Bar as Test;
- use Foo\Baz;
- use App;
- ',
- ];
- yield [
- '<?php
- use App\Repository\{Customer as Client, Profile, User};
- ',
- '<?php
- use App\Repository\User;
- use App\Repository\Profile;
- use App\Repository\Customer as Client;
- ',
- ];
- yield [
- '<?php
- use function Foo\{Bar, Baz, Test as Alias};
- ',
- '<?php
- use function Foo\Bar;
- use function Foo\Baz;
- use function Foo\Test as Alias;
- ',
- ];
- yield [
- '<?php
- use const Some\Place\{A, B, C as D};
- ',
- '<?php
- use const Some\Place\A;
- use const Some\Place\B;
- use const Some\Place\C as D;
- ',
- ];
- yield [
- '<?php
- use Foo\Bar;
- use Foo\Baz\Lorem\Ipsum\Lets\Write\Some\More\Strings\{One, Two};
- ',
- '<?php
- use Foo\Bar;
- use Foo\Baz\Lorem\Ipsum\Lets\Write\Some\More\Strings\One;
- use Foo\Baz\Lorem\Ipsum\Lets\Write\Some\More\Strings\Two;
- ',
- ];
- yield [
- '<?php
- use Foo\{Bar, Baz};
- use Foo\Baz\John\{Doe, Smith};
- use Foo\Baz\Johnny\{DoeSecond, SmithSecond};
- use Foo\Baz\John\Smith\Junior;
- ',
- '<?php
- use Foo\Bar;
- use Foo\Baz;
- use Foo\Baz\John\Doe;
- use Foo\Baz\John\Smith;
- use Foo\Baz\John\Smith\Junior;
- use Foo\Baz\Johnny\DoeSecond;
- use Foo\Baz\Johnny\SmithSecond;
- ',
- ];
- yield [
- '<?php
- use PhpCsFixer\Tokenizer\{AbstractTransformer, CT, Token, Tokens};
- ',
- '<?php
- use PhpCsFixer\Tokenizer\AbstractTransformer;
- use PhpCsFixer\Tokenizer\CT;
- use PhpCsFixer\Tokenizer\Token;
- use PhpCsFixer\Tokenizer\Tokens;
- ',
- ];
- yield [
- '<?php
- use Foo\{Bar, Baz};
- ',
- '<?php
- use Foo\Bar;use Foo\Baz;
- ',
- ];
- yield [
- '<?php
- use Foo\{Bar, Baz};
- \DontTouch::me();
- ',
- '<?php
- use Foo\Bar;use Foo\Baz;\DontTouch::me();
- ',
- ];
- yield [
- '<?php
- use Foo\{Bar, Baz};
- use ReflectionClass;
- use ReflectionMethod;
- ',
- '<?php
- use Foo\Bar;
- use Foo\Baz;
- use ReflectionClass;
- use ReflectionMethod;
- ',
- ];
- yield [
- '<?php
- use Foo\{Bar, Baz};
- use \ReflectionClass;
- use \ReflectionMethod;
- ',
- '<?php
- use Foo\Bar;
- use Foo\Baz;
- use \ReflectionClass;
- use \ReflectionMethod;
- ',
- ];
- yield [
- '<?php
- use Framework\Database\Query\JoinClause;
- use Framework\Support\{Arr, Collection};
- use Framework\Database\ORM\{Model, SoftDeletes};
- use Framework\Notifications\{Notifiable, Notification};
- use Framework\Support\Facades\{DB, Log};
- use Framework\Database\ORM\Relations\{BelongsTo, HasOne};
- ',
- '<?php
- use Framework\Database\ORM\Model;
- use Framework\Database\ORM\Relations\BelongsTo;
- use Framework\Database\ORM\Relations\HasOne;
- use Framework\Database\ORM\SoftDeletes;
- use Framework\Database\Query\JoinClause;
- use Framework\Notifications\Notifiable;
- use Framework\Notifications\Notification;
- use Framework\Support\Arr;
- use Framework\Support\Collection;
- use Framework\Support\Facades\DB;
- use Framework\Support\Facades\Log;
- ',
- ];
- yield [
- '<?php
- use Framework\Bar\{Class3, Class4, Class5};
- use Framework\Foo\{Class1, Class2, Class7};
- use Framework\Baz\Class6;
- ',
- '<?php
- use Framework\Foo\Class1;
- use Framework\Foo\Class2;
- use Framework\Bar\Class3;
- use Framework\Bar\Class4;
- use Framework\Bar\Class5;
- use Framework\Baz\Class6;
- use Framework\Foo\Class7;
- ',
- ];
- yield [
- '<?php
- use Foo\{Bar, Baz};
- use function Foo\baz;
- ',
- '<?php
- use Foo\Bar;
- use function Foo\baz;
- use Foo\Baz;
- ',
- ];
- yield [
- '<?php
- use Foo\Bar\C;
- use Foo\{D, E};
- use Foo\Bar\Baz\{A, B};
- use function Foo\{a, b};
- ',
- '<?php
- use Foo\Bar\Baz\A;
- use Foo\Bar\Baz\B;
- use Foo\Bar\C;
- use Foo\D;
- use Foo\E;
- use function Foo\a;
- use function Foo\b;
- ',
- ];
- yield 'only class-like' => [
- '<?php
- use Foo\Bar\C;
- use Foo\{D, E};
- use Foo\Bar\Baz\{A, B};
- use function Foo\a;
- use function Foo\b;
- use const Foo\X;
- use const Foo\Y;
- ',
- '<?php
- use Foo\Bar\Baz\A;
- use Foo\Bar\Baz\B;
- use Foo\Bar\C;
- use Foo\D;
- use Foo\E;
- use function Foo\a;
- use function Foo\b;
- use const Foo\X;
- use const Foo\Y;
- ',
- ['group_types' => [GroupImportFixer::GROUP_CLASSY]],
- ];
- yield 'only functions' => [
- '<?php
- use Foo\Bar\Baz\A;
- use Foo\Bar\Baz\B;
- use function Foo\{a, b};
- use Foo\Bar\C;
- use Foo\D;
- use Foo\E;
- use const Foo\X;
- use const Foo\Y;
- ',
- '<?php
- use Foo\Bar\Baz\A;
- use Foo\Bar\Baz\B;
- use function Foo\a;
- use function Foo\b;
- use Foo\Bar\C;
- use Foo\D;
- use Foo\E;
- use const Foo\X;
- use const Foo\Y;
- ',
- ['group_types' => [GroupImportFixer::GROUP_FUNCTIONS]],
- ];
- yield 'only constants' => [
- '<?php
- use Foo\Bar\Baz\A;
- use Foo\Bar\Baz\B;
- use function Foo\a;
- use function Foo\b;
- use Foo\Bar\C;
- use Foo\D;
- use Foo\E;
- use const Foo\{X, Y};
- ',
- '<?php
- use Foo\Bar\Baz\A;
- use Foo\Bar\Baz\B;
- use function Foo\a;
- use function Foo\b;
- use Foo\Bar\C;
- use Foo\D;
- use Foo\E;
- use const Foo\X;
- use const Foo\Y;
- ',
- ['group_types' => [GroupImportFixer::GROUP_CONSTANTS]],
- ];
- yield 'class-likes and functions' => [
- '<?php
- use function Foo\{a, b};
- use Foo\Bar\C;
- use Foo\{D, E};
- use Foo\Bar\Baz\{A, B};
- use const Foo\X;
- use const Foo\Y;
- ',
- '<?php
- use Foo\Bar\Baz\A;
- use Foo\Bar\Baz\B;
- use function Foo\a;
- use function Foo\b;
- use Foo\Bar\C;
- use Foo\D;
- use Foo\E;
- use const Foo\X;
- use const Foo\Y;
- ',
- ['group_types' => [GroupImportFixer::GROUP_CLASSY, GroupImportFixer::GROUP_FUNCTIONS]],
- ];
- yield 'class-likes and constants' => [
- '<?php
- use function Foo\a;
- use function Foo\b;
- use Foo\Bar\C;
- use Foo\{D, E};
- use Foo\Bar\Baz\{A, B};
- use const Foo\{X, Y};
- ',
- '<?php
- use Foo\Bar\Baz\A;
- use Foo\Bar\Baz\B;
- use function Foo\a;
- use function Foo\b;
- use Foo\Bar\C;
- use Foo\D;
- use Foo\E;
- use const Foo\X;
- use const Foo\Y;
- ',
- ['group_types' => [GroupImportFixer::GROUP_CLASSY, GroupImportFixer::GROUP_CONSTANTS]],
- ];
- yield 'functions and constants' => [
- '<?php
- use Foo\Bar\Baz\A;
- use Foo\Bar\Baz\B;
- use function Foo\{a, b};
- use Foo\Bar\C;
- use Foo\D;
- use Foo\E;
- use const Foo\{X, Y};
- ',
- '<?php
- use Foo\Bar\Baz\A;
- use Foo\Bar\Baz\B;
- use function Foo\a;
- use function Foo\b;
- use Foo\Bar\C;
- use Foo\D;
- use Foo\E;
- use const Foo\X;
- use const Foo\Y;
- ',
- ['group_types' => [GroupImportFixer::GROUP_FUNCTIONS, GroupImportFixer::GROUP_CONSTANTS]],
- ];
- }
- /**
- * @dataProvider provideFixPre80Cases
- *
- * @requires PHP <8.0
- */
- public function testFixPre80(string $expected, ?string $input = null): void
- {
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<array{string, string}>
- */
- public static function provideFixPre80Cases(): iterable
- {
- yield [
- '<?php
- /*1*/use A\{B, C, D};
- /*2*//*3*//*4*//*5*//*6*/
- /*7*//*8*//*9*//*10*//*11*//*12*/
- /*13*//*14*//*15*//*16*//*17*//*18*/
- ',
- '<?php
- /*1*/use/*2*/A/*3*/\/*4*/B/*5*/;/*6*/
- /*7*/use/*8*/A/*9*/\/*10*/C/*11*/;/*12*/
- /*13*/use/*14*/A/*15*/\/*16*/D/*17*/;/*18*/
- ',
- ];
- }
- }
|