123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398 |
- <?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\Tests\Test\AbstractFixerTestCase;
- use PhpCsFixer\WhitespacesFixerConfig;
- /**
- * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
- *
- * @internal
- *
- * @covers \PhpCsFixer\Fixer\Import\SingleImportPerStatementFixer
- *
- * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Import\SingleImportPerStatementFixer>
- *
- * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Import\SingleImportPerStatementFixer
- */
- final class SingleImportPerStatementFixerTest extends AbstractFixerTestCase
- {
- /**
- * @dataProvider provideFixCases
- */
- public function testFix(string $expected, ?string $input = null): void
- {
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<array{string, string}>
- */
- public static function provideFixCases(): iterable
- {
- yield [
- '<?php
- /**/use Foo;
- use FooB;
- ',
- '<?php
- /**/use Foo,FooB;
- ',
- ];
- yield [
- <<<'EOF'
- use Some, Not, PHP, Like, Use, Statement;
- <?php
- use Foo;
- use FooA;
- use FooB;
- use FooC;
- use FooD as D;
- use FooE;
- use FooF;
- use FooG as G;
- use FooH;
- use FooI;
- use FooJ;
- use FooZ;
- EOF,
- <<<'EOF'
- use Some, Not, PHP, Like, Use, Statement;
- <?php
- use Foo;
- use FooA, FooB;
- use FooC, FooD as D, FooE;
- use FooF,
- FooG as G,
- FooH, FooI,
- FooJ;
- use FooZ;
- EOF,
- ];
- yield [
- <<<'EOF'
- <?php
- namespace {
- use Foo;
- use FooA;
- use FooB;
- use FooC;
- use FooD as D;
- use FooE;
- use FooF;
- use FooG as G;
- use FooH;
- use FooI;
- use FooJ;
- use FooZ;
- }
- namespace Boo {
- use Bar;
- use BarA;
- use BarB;
- use BarC;
- use BarD as D;
- use BarE;
- use BarF;
- use BarG as G;
- use BarH;
- use BarI;
- use BarJ;
- use BarZ;
- }
- EOF,
- <<<'EOF'
- <?php
- namespace {
- use Foo;
- use FooA, FooB;
- use FooC, FooD as D, FooE;
- use FooF,
- FooG as G,
- FooH, FooI,
- FooJ;
- use FooZ;
- }
- namespace Boo {
- use Bar;
- use BarA, BarB;
- use BarC, BarD as D, BarE;
- use BarF,
- BarG as G,
- BarH, BarI,
- BarJ;
- use BarZ;
- }
- EOF,
- ];
- yield [
- '<?php
- use FooA;
- use FooB;
- ',
- '<?php
- use FooA, FooB;
- ',
- ];
- yield [
- '<?php use FooA;
- use FooB?>',
- '<?php use FooA, FooB?>',
- ];
- yield [
- '<?php
- use B;
- use C;
- use E;
- use F;
- use G;
- use H;
- ',
- '<?php
- use B,C;
- use E,F;
- use G,H;
- ',
- ];
- yield [
- '<?php
- use B;
- /*
- */use C;
- ',
- '<?php
- use B,
- /*
- */C;
- ',
- ];
- yield [
- '<?php
- use A;
- use B;
- //,{} use ; :
- #,{} use ; :
- /*,{} use ; :*/
- use C ; ',
- '<?php
- use A,B,
- //,{} use ; :
- #,{} use ; :
- /*,{} use ; :*/
- C ; ',
- ];
- yield [
- '<?php use Z ;
- use X ?><?php new X(); // run before white space around semicolon',
- '<?php use Z , X ?><?php new X(); // run before white space around semicolon',
- ];
- yield [
- '<?php use FooA#
- ;#
- #
- use FooB;',
- '<?php use FooA#
- ,#
- #
- FooB;',
- ];
- yield [
- '<?php use some\b\ClassB;
- use function some\b\CC as C;
- use function some\b\D;
- use const some\b\E;
- use function some\b\A\B;',
- '<?php use some\b\{ClassB, function CC as C, function D, const E, function A\B};',
- ];
- yield [
- '<?php
- use Foo\Bar;
- use Foo\Baz;',
- '<?php
- use Foo\ {
- Bar, Baz
- };',
- ];
- yield [
- '<?php
- use Foo\Bar;
- use Foo\Baz;',
- '<?php
- use Foo\
- {
- Bar, Baz
- };',
- ];
- yield [
- '<?php
- use function md5;
- use function str_repeat;
- use const true;
- use const false;
- use A;
- use B;
- ',
- '<?php
- use function md5, str_repeat;
- use const true, false;
- use A,B;
- ',
- ];
- yield [
- '<?php
- use D\E;
- use D\F;
- use G\H;
- use G\I/*1*//*2*/;
- ',
- '<?php
- use D\{E,F,};
- use G\{H,I/*1*/,/*2*/};
- ',
- ];
- yield [
- '<?php
- use A\B;
- ',
- '<?php
- use A\{B};
- ',
- ];
- }
- public function testWithConfig(): void
- {
- $expected = '<?php
- use Space\Models\TestModelA;
- use Space\Models\TestModelB;
- use Space\Models\TestModel;';
- $input = '<?php
- use Space\Models\ {
- TestModelA,
- TestModelB,
- TestModel,
- };';
- $this->doTest($expected, $input);
- $this->fixer->configure(['group_to_single_imports' => false]);
- $this->doTest($input);
- }
- /**
- * @dataProvider provideMessyWhitespacesCases
- */
- public function testMessyWhitespaces(string $expected, ?string $input = null): void
- {
- $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n"));
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<array{string, string}>
- */
- public static function provideMessyWhitespacesCases(): iterable
- {
- yield [
- "<?php\r\n use FooA;\r\n use FooB;",
- "<?php\r\n use FooA, FooB;",
- ];
- }
- /**
- * @dataProvider provideFixPrePHP80Cases
- *
- * @requires PHP <8.0
- */
- public function testFixPrePHP80(string $expected, string $input): void
- {
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<int|string, array{string, string}>
- */
- public static function provideFixPrePHP80Cases(): iterable
- {
- yield [
- '<?php
- use some\a\ClassA;
- use some\a\ClassB;
- use some\a\ClassC as C;
- use function some\b\fn_a;
- use function some\b\fn_b;
- use function some\b\fn_c;
- use const some\c\ConstA/**/as/**/E; /* group comment */
- use const some\c\ConstB as D;
- use const some\c\// use.,{}
- ConstC;
- use A\B;
- use D\E;
- use D\F;
- ',
- '<?php
- use some\a\{ClassA, ClassB, ClassC as C};
- use function some\b\{fn_a, fn_b, fn_c};
- use const/* group comment */some\c\{ConstA/**/as/**/ E , ConstB AS D, '.'
- // use.,{}
- ConstC};
- use A\{B};
- use D\{E,F};
- ',
- ];
- yield 'messy comments' => [
- '<?php
- use D\/*1*//*2*//*3*/E;
- use D\/*4*//*5*//*6*//*7*//*8*//*9*/F/*10*//*11*//*12*/;
- ',
- '<?php
- use D\{
- /*1*//*2*//*3*/E,/*4*//*5*//*6*/
- /*7*//*8*//*9*/F/*10*//*11*//*12*/
- };
- ',
- ];
- }
- }
|