* 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\Import; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @author Gregor Harlan * * @internal * * @covers \PhpCsFixer\Fixer\Import\GlobalNamespaceImportFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Import\GlobalNamespaceImportFixer> * * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Import\GlobalNamespaceImportFixer */ final class GlobalNamespaceImportFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixImportConstantsCases */ public function testFixImportConstants(string $expected, ?string $input = null): void { $this->fixer->configure(['import_constants' => true]); $this->doTest($expected, $input); } /** * @return iterable */ public static function provideFixImportConstantsCases(): iterable { yield 'non-global names' => [ <<<'EXPECTED' [ <<<'EXPECTED' [ <<<'EXPECTED' [ <<<'EXPECTED' [ <<<'INPUT' [ <<<'EXPECTED' [ <<<'EXPECTED' [ <<<'EXPECTED' [ <<<'EXPECTED' [ <<<'EXPECTED' [ <<<'EXPECTED' [ <<<'EXPECTED' [ <<<'EXPECTED' [ <<<'INPUT' fixer->configure(['import_functions' => true]); $this->doTest($expected, $input); } /** * @return iterable */ public static function provideFixImportFunctionsCases(): iterable { yield 'non-global names' => [ <<<'EXPECTED' [ <<<'EXPECTED' [ <<<'EXPECTED' [ <<<'EXPECTED' [ <<<'INPUT' [ <<<'EXPECTED' [ <<<'EXPECTED' [ <<<'EXPECTED' [ <<<'EXPECTED' [ <<<'EXPECTED' [ <<<'EXPECTED' [ <<<'EXPECTED' [ <<<'EXPECTED' fixer->configure(['import_classes' => true]); $this->doTest($expected, $input); } /** * @return iterable */ public static function provideFixImportClassesCases(): iterable { yield 'non-global names' => [ <<<'EXPECTED' [ <<<'EXPECTED' [ <<<'EXPECTED' [ <<<'EXPECTED' [ <<<'EXPECTED' */ function x() {} /** @var \Foo $foo */ $foo = new \Foo(); EXPECTED, ]; yield 'without namespace / do not import' => [ <<<'INPUT' [ <<<'EXPECTED' */ function x() {} EXPECTED, <<<'INPUT' */ function x() {} INPUT, ]; yield 'with namespace with {} syntax' => [ <<<'EXPECTED' [ <<<'EXPECTED' [ <<<'EXPECTED' [ <<<'EXPECTED' [ <<<'EXPECTED' [ <<<'EXPECTED' [ <<<'EXPECTED' [ <<<'EXPECTED' [ <<<'EXPECTED' [ <<<'EXPECTED' [ <<<'EXPECTED' fixer->configure(['import_classes' => true]); $this->doTest($expected, $input); } /** * @return iterable */ public static function provideFixImportClasses80Cases(): iterable { yield 'try catch without variable' => [ <<<'EXPECTED' [ <<<'EXPECTED' fixer->configure(['import_constants' => false]); $this->doTest($expected, $input); } /** * @return iterable */ public static function provideFixFullyQualifyConstantsCases(): iterable { yield 'already fqn or sub namespace' => [ <<<'EXPECTED' [ <<<'EXPECTED' [ <<<'EXPECTED' fixer->configure(['import_functions' => false]); $this->doTest($expected, $input); } /** * @return iterable */ public static function provideFixFullyQualifyFunctionsCases(): iterable { yield 'already fqn or sub namespace' => [ <<<'EXPECTED' [ <<<'EXPECTED' [ <<<'EXPECTED' fixer->configure(['import_classes' => false]); $this->doTest($expected, $input); } /** * @return iterable */ public static function provideFixFullyQualifyClassesCases(): iterable { yield 'already fqn or sub namespace' => [ <<<'EXPECTED' [ <<<'EXPECTED' >|null */ function abc($foo, \Bar $bar = null) {} EXPECTED, <<<'INPUT' >|null */ function abc($foo, Bar $bar = null) {} INPUT, ]; yield 'ignore other imports and non-imported names' => [ <<<'EXPECTED' [ <<<'EXPECTED' [ <<<'EXPECTED' [ '} */ public function f3(): array {} }', '} */ public function f3(): array {} }', ]; } /** * @dataProvider provideFixFullyQualifyClasses80Cases * * @requires PHP 8.0 */ public function testFixFullyQualifyClasses80(string $expected, string $input): void { $this->fixer->configure(['import_classes' => false]); $this->doTest($expected, $input); } /** * @return iterable */ public static function provideFixFullyQualifyClasses80Cases(): iterable { yield 'try catch without variable' => [ <<<'EXPECTED' [ <<<'EXPECTED' fixer->configure(['import_constants' => true]); $this->doTest($expected); } /** * @return iterable */ public static function provideMultipleNamespacesCases(): iterable { yield [ <<<'INPUT' fixer->configure([ 'import_classes' => true, 'import_constants' => true, 'import_functions' => true, ]); $this->doTest( 'fixer->configure([ 'import_constants' => true, 'import_functions' => true, ]); $this->doTest($expected, $input); } /** * @return iterable */ public static function provideFix81Cases(): iterable { yield 'ignore enum methods' => [ <<<'EXPECTED' [ <<<'EXPECTED'