123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470 |
- <?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\Basic;
- use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
- /**
- * @author Graham Campbell <hello@gjcampbell.co.uk>
- * @author Kuba Werłos <werlos@gmail.com>
- *
- * @internal
- *
- * @covers \PhpCsFixer\Fixer\Basic\PsrAutoloadingFixer
- *
- * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Basic\PsrAutoloadingFixer>
- *
- * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Basic\PsrAutoloadingFixer
- */
- final class PsrAutoloadingFixerTest extends AbstractFixerTestCase
- {
- /**
- * @dataProvider provideFixCases
- */
- public function testFix(string $expected, ?string $input = null, ?string $filepath = null, ?string $dir = null): void
- {
- if (null === $filepath) {
- $filepath = __FILE__;
- }
- $file = new \SplFileInfo($filepath);
- if (null !== $dir) {
- $this->fixer->configure(['dir' => $dir]);
- }
- $this->doTest($expected, $input, $file);
- }
- /**
- * @return iterable<int|string, array{0: string, 1?: null|string, 2?: null|string, 3?: string}>
- */
- public static function provideFixCases(): iterable
- {
- foreach (['class', 'interface', 'trait'] as $element) {
- yield \sprintf('%s with originally short name', $element) => [
- \sprintf('<?php %s PsrAutoloadingFixerTest {}', $element),
- \sprintf('<?php %s Foo {}', $element),
- ];
- }
- yield 'abstract class' => [
- '<?php abstract class PsrAutoloadingFixerTest {}',
- '<?php abstract class WrongName {}',
- ];
- yield 'final class' => [
- '<?php final class PsrAutoloadingFixerTest {}',
- '<?php final class WrongName {}',
- ];
- yield 'class with originally long name' => [
- '<?php class PsrAutoloadingFixerTest {}',
- '<?php class FooFooFooFooFooFooFooFooFooFooFooFooFoo {}',
- ];
- yield 'class with wrong casing' => [
- '<?php class PsrAutoloadingFixerTest {}',
- '<?php class psrautoloadingfixertest {}',
- ];
- yield 'namespaced class with wrong casing' => [
- '<?php namespace Foo; class PsrAutoloadingFixerTest {}',
- '<?php namespace Foo; class psrautoloadingfixertest {}',
- ];
- yield 'class with wrong casing (1 level namespace)' => [
- '<?php class Basic_PsrAutoloadingFixerTest {}',
- '<?php class BASIC_PSRAUTOLOADINGFIXERTEST {}',
- ];
- yield 'class with wrong casing (2 levels namespace)' => [
- '<?php class Fixer_Basic_PsrAutoloadingFixerTest {}',
- '<?php class FIXER_BASIC_PSRAUTOLOADINGFIXERTEST {}',
- ];
- yield 'class with name not matching directory structure' => [
- '<?php class PsrAutoloadingFixerTest {}',
- '<?php class Aaaaa_Bbbbb_PsrAutoloadingFixerTest {}',
- ];
- yield 'configured directory (1 subdirectory)' => [
- '<?php class Basic_PsrAutoloadingFixerTest {}',
- '<?php class PsrAutoloadingFixerTest {}',
- null,
- __DIR__.'/..',
- ];
- yield 'configured directory (2 subdirectories)' => [
- '<?php class Fixer_Basic_PsrAutoloadingFixerTest {}',
- '<?php class PsrAutoloadingFixerTest {}',
- null,
- __DIR__.'/../..',
- ];
- yield 'configured directory (other directory)' => [
- '<?php namespace Basic; class Foobar {}',
- null,
- null,
- __DIR__.'/../../Test',
- ];
- yield 'multiple classy elements in file' => [
- '<?php interface Foo {} class Bar {}',
- ];
- yield 'namespace with wrong casing' => [
- '<?php namespace Fixer\Basic; class PsrAutoloadingFixerTest {}',
- '<?php namespace Fixer\BASIC; class PsrAutoloadingFixerTest {}',
- null,
- __DIR__.'/../..',
- ];
- yield 'multiple namespaces in file' => [
- '<?php namespace Foo\Helpers; function helper() {}; namespace Foo\Domain; class Feature {}',
- ];
- yield 'namespace and class with comments' => [
- '<?php namespace /* namespace here */ PhpCsFixer\Tests\Fixer\Basic; class /* hi there */ PsrAutoloadingFixerTest /* hello */ {} /* class end */',
- '<?php namespace /* namespace here */ PhpCsFixer\Tests\Fixer\Basic; class /* hi there */ Foo /* hello */ {} /* class end */',
- ];
- yield 'namespace partially matching directory structure' => [
- '<?php namespace Foo\Bar\Baz\FIXER\Basic; class PsrAutoloadingFixerTest {}',
- ];
- yield 'namespace partially matching directory structure with comment' => [
- '<?php namespace /* hi there */ Foo\Bar\Baz\FIXER\Basic; class /* hi there */ PsrAutoloadingFixerTest {}',
- ];
- yield 'namespace partially matching directory structure with configured directory' => [
- '<?php namespace Foo\Bar\Baz\Fixer\Basic; class PsrAutoloadingFixerTest {}',
- '<?php namespace Foo\Bar\Baz\FIXER\Basic; class PsrAutoloadingFixerTest {}',
- null,
- __DIR__.'/../..',
- ];
- yield 'namespace partially matching directory structure with comment and configured directory' => [
- '<?php namespace /* hi there */ Foo\Bar\Baz\Fixer\Basic; class /* hi there */ PsrAutoloadingFixerTest {}',
- '<?php namespace /* hi there */ Foo\Bar\Baz\FIXER\Basic; class /* hi there */ PsrAutoloadingFixerTest {}',
- null,
- __DIR__.'/../..',
- ];
- yield 'namespace not matching directory structure' => [
- '<?php namespace Foo\Bar\Baz; class PsrAutoloadingFixerTest {}',
- ];
- yield 'namespace not matching directory structure with configured directory' => [
- '<?php namespace Foo\Bar\Baz; class PsrAutoloadingFixerTest {}',
- null,
- null,
- __DIR__,
- ];
- yield [ // namespace with wrong casing
- '<?php
- namespace PhpCsFixer\Fixer;
- class FixerInterface {}
- ',
- '<?php
- namespace PhpCsFixer\fixer;
- class FixerInterface {}
- ',
- __DIR__.'/../../../src/Fixer/FixerInterface.php',
- __DIR__.'/../../../src',
- ];
- yield [ // class with wrong casing (2 levels namespace)
- '<?php
- class Fixer_Basic_PsrAutoloadingFixer {}
- ',
- '<?php
- class Fixer_bASIc_PsrAutoloadingFixer {}
- ',
- __DIR__.'/../../../src/Fixer/Basic/PsrAutoloadingFixer.php',
- __DIR__.'/../../../src',
- ];
- yield [ // namespaced class with wrong casing
- '<?php
- namespace PhpCsFixer\Fixer;
- class FixerInterface {}
- ',
- '<?php
- namespace PhpCsFixer\Fixer;
- class fixerinterface {}
- ',
- __DIR__.'/../../../src/Fixer/FixerInterface.php',
- __DIR__.'/../../../src',
- ];
- yield [ // multiple classy elements in file
- '<?php
- namespace PhpCsFixer\Tests\Fixer\Basic;
- interface SomeInterfaceToBeUsedInTests {}
- class blah {}
- /* class foo */',
- ];
- yield [ // multiple namespaces in file
- '<?php
- namespace PhpCsFixer\Tests\Fixer\Basic;
- interface SomeInterfaceToBeUsedInTests {}
- namespace AnotherNamespace;
- class blah {}
- /* class foo */',
- ];
- yield [ // fix class
- '<?php
- namespace PhpCsFixer\Tests\Fixer\Basic;
- class PsrAutoloadingFixerTest {}
- /* class foo */
- ',
- '<?php
- namespace PhpCsFixer\Tests\Fixer\Basic;
- class blah {}
- /* class foo */
- ',
- ];
- yield [ // abstract class
- '<?php
- namespace PhpCsFixer\Tests\Fixer\Basic;
- abstract class PsrAutoloadingFixerTest {}
- /* class foo */
- ',
- '<?php
- namespace PhpCsFixer\Tests\Fixer\Basic;
- abstract class blah {}
- /* class foo */
- ',
- ];
- yield [ // final class
- '<?php
- namespace PhpCsFixer\Tests\Fixer\Basic;
- final class PsrAutoloadingFixerTest {}
- /* class foo */
- ',
- '<?php
- namespace PhpCsFixer\Tests\Fixer\Basic;
- final class blah {}
- /* class foo */
- ',
- ];
- yield [ // namespace and class with comments
- '<?php
- namespace /* namespace here */ PhpCsFixer\Fixer\Psr;
- class /* hi there */ PsrAutoloadingFixerTest /* why hello */ {}
- /* class foo */
- ',
- '<?php
- namespace /* namespace here */ PhpCsFixer\Fixer\Psr;
- class /* hi there */ blah /* why hello */ {}
- /* class foo */
- ',
- ];
- yield [ // namespace partially matching directory structure
- '<?php
- namespace Foo\Bar\Baz\FIXER\Basic;
- class PsrAutoloadingFixer {}
- ',
- null,
- __DIR__.'/../../../src/Fixer/Basic/PsrAutoloadingFixer.php',
- ];
- yield [ // namespace partially matching directory structure with comment
- '<?php
- namespace /* hi there */ Foo\Bar\Baz\FIXER\Basic;
- class /* hi there */ PsrAutoloadingFixer {}
- ',
- null,
- __DIR__.'/../../../src/Fixer/Basic/PsrAutoloadingFixer.php',
- ];
- yield [ // namespace not matching directory structure
- '<?php
- namespace Foo\Bar\Baz;
- class PsrAutoloadingFixer {}
- ',
- null,
- __DIR__.'/../../../src/Fixer/Basic/PsrAutoloadingFixer.php',
- ];
- yield [ // namespace partially matching directory structure with configured directory
- '<?php
- namespace Foo\Bar\Baz\Fixer\Basic;
- class PsrAutoloadingFixer {}
- ',
- '<?php
- namespace Foo\Bar\Baz\FIXER\Basic;
- class PsrAutoloadingFixer {}
- ',
- __DIR__.'/../../../src/Fixer/Basic/PsrAutoloadingFixer.php',
- __DIR__.'/../../../src/',
- ];
- yield [ // namespace partially matching directory structure with comment and configured directory
- '<?php
- namespace /* hi there */ Foo\Bar\Baz\Fixer\Basic;
- class /* hi there */ PsrAutoloadingFixer {}
- ',
- '<?php
- namespace /* hi there */ Foo\Bar\Baz\FIXER\Basic;
- class /* hi there */ PsrAutoloadingFixer {}
- ',
- __DIR__.'/../../../src/Fixer/Basic/PsrAutoloadingFixer.php',
- __DIR__.'/../../../src/',
- ];
- yield [ // namespace not matching directory structure with configured directory
- '<?php
- namespace Foo\Bar\Baz;
- class PsrAutoloadingFixer {}
- ',
- null,
- __DIR__.'/../../../src/Fixer/Basic/PsrAutoloadingFixer.php',
- __DIR__.'/../../../src/Fixer/Basic',
- ];
- yield [ // class with originally long name
- '<?php class PsrAutoloadingFixerTest {}',
- '<?php class PsrAutoloadingFixerTestFoo {}',
- ];
- $cases = ['.php', 'Foo.class.php', '4Foo.php', '$#.php'];
- foreach (['__halt_compiler', 'abstract', 'and', 'array', 'as', 'break', 'case', 'catch', 'class', 'clone', 'const', 'continue', 'declare', 'default', 'die', 'do', 'echo', 'else', 'elseif', 'empty', 'enddeclare', 'endfor', 'endforeach', 'endif', 'endswitch', 'endwhile', 'eval', 'exit', 'extends', 'final', 'for', 'foreach', 'function', 'global', 'goto', 'if', 'implements', 'include', 'include_once', 'instanceof', 'interface', 'isset', 'list', 'namespace', 'new', 'or', 'print', 'private', 'protected', 'public', 'require', 'require_once', 'return', 'static', 'switch', 'throw', 'try', 'unset', 'use', 'var', 'while', 'xor'] as $keyword) {
- $cases[] = $keyword.'.php';
- }
- foreach (['__CLASS__', '__DIR__', '__FILE__', '__FUNCTION__', '__LINE__', '__METHOD__', '__NAMESPACE__'] as $magicConstant) {
- $cases[] = $magicConstant.'.php';
- $cases[] = strtolower($magicConstant).'.php';
- }
- foreach ([
- 'T_CALLABLE' => 'callable',
- 'T_FINALLY' => 'finally',
- 'T_INSTEADOF' => 'insteadof',
- 'T_TRAIT' => 'trait',
- 'T_TRAIT_C' => '__trait__',
- ] as $tokenType => $tokenValue) {
- if (\defined($tokenType)) {
- $cases[] = $tokenValue.'.php';
- $cases[] = strtoupper($tokenValue).'.php';
- }
- }
- yield from array_map(static fn ($case): array => [
- '<?php
- namespace Aaa;
- class Bar {}',
- null,
- $case,
- ], $cases);
- yield 'class with anonymous class' => [
- '<?php
- namespace PhpCsFixer\Tests\Fixer\Basic;
- class PsrAutoloadingFixerTest {
- public function foo() {
- return new class() implements FooInterface {};
- }
- }
- ',
- '<?php
- namespace PhpCsFixer\Tests\Fixer\Basic;
- class stdClass {
- public function foo() {
- return new class() implements FooInterface {};
- }
- }
- ',
- ];
- yield 'ignore anonymous class implementing interface' => [
- '<?php
- namespace PhpCsFixer\Tests\Fixer\Basic;
- new class implements Countable {};
- ',
- ];
- yield 'ignore anonymous class extending other class' => [
- '<?php
- namespace PhpCsFixer\Tests\Fixer\Basic;
- new class extends stdClass {};
- ',
- ];
- yield 'ignore multiple classy in file with anonymous class between them' => [
- '<?php
- namespace PhpCsFixer\Tests\Fixer\Basic;
- class ClassOne {};
- new class extends stdClass {};
- class ClassTwo {};
- ',
- ];
- }
- /**
- * @requires PHP 8.0
- *
- * @dataProvider provideFix80Cases
- */
- public function testFix80(string $expected, ?string $input = null): void
- {
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<string, array{string}>
- */
- public static function provideFix80Cases(): iterable
- {
- yield 'anonymous + annotation' => [
- '<?php
- namespace PhpCsFixer\Tests\Fixer\Basic;
- new
- #[Foo]
- class extends stdClass {};
- ',
- ];
- }
- /**
- * @requires PHP 8.1
- *
- * @dataProvider provideFix81Cases
- */
- public function testFix81(string $expected, ?string $input = null): void
- {
- $this->doTest($expected, $input, new \SplFileInfo(__FILE__));
- }
- /**
- * @return iterable<string, array{string, string}>
- */
- public static function provideFix81Cases(): iterable
- {
- yield 'enum with wrong casing' => [
- '<?php enum PsrAutoloadingFixerTest {}',
- '<?php enum psrautoloadingfixertest {}',
- ];
- }
- }
|