123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- <?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\Tokenizer\Transformer;
- use PhpCsFixer\Tests\Test\AbstractTransformerTestCase;
- use PhpCsFixer\Tests\Test\Assert\AssertTokensTrait;
- use PhpCsFixer\Tokenizer\CT;
- use PhpCsFixer\Tokenizer\Token;
- use PhpCsFixer\Tokenizer\Tokens;
- /**
- * @internal
- *
- * @requires PHP 8.0
- *
- * @covers \PhpCsFixer\Tokenizer\Transformer\NameQualifiedTransformer
- */
- final class NameQualifiedTransformerTest extends AbstractTransformerTestCase
- {
- use AssertTokensTrait;
- /**
- * @param list<Token> $expected
- * @param null|list<Token> $input
- *
- * @dataProvider provideProcessCases
- *
- * @requires PHP 8.0
- */
- public function testProcess(array $expected, ?array $input = null): void
- {
- $expectedTokens = Tokens::fromArray($expected);
- $tokens = null === $input
- ? Tokens::fromArray($expected)
- : Tokens::fromArray($input);
- $tokenCount = \count($tokens);
- for ($i = 0; $i < $tokenCount; ++$i) {
- $this->transformer->process($tokens, $tokens[$i], $i);
- }
- self::assertTokens($expectedTokens, $tokens);
- if (null === $input) {
- self::assertFalse($tokens->isChanged());
- } else {
- self::testProcess($expected);
- self::assertTrue($tokens->isChanged());
- }
- }
- public static function provideProcessCases(): iterable
- {
- yield 'string' => [
- [
- new Token([T_OPEN_TAG, "<?php\n"]),
- new Token([T_STRING, 'Foo']),
- new Token(';'),
- ],
- ];
- if (\defined('T_NAME_RELATIVE')) { // @TODO: drop condition when PHP 8.0+ is required
- yield 'relative 1' => [
- [
- new Token([T_OPEN_TAG, "<?php\n"]),
- new Token([T_NAMESPACE, 'namespace']),
- new Token([T_NS_SEPARATOR, '\\']),
- new Token([T_STRING, 'Transformer']),
- new Token(';'),
- ],
- [
- new Token([T_OPEN_TAG, "<?php\n"]),
- new Token([T_NAME_RELATIVE, 'namespace\Transformer']),
- new Token(';'),
- ],
- ];
- yield 'relative 2' => [
- [
- new Token([T_OPEN_TAG, "<?php\n"]),
- new Token([T_NAMESPACE, 'namespace']),
- new Token([T_NS_SEPARATOR, '\\']),
- new Token([T_STRING, 'Transformer']),
- new Token([T_NS_SEPARATOR, '\\']),
- new Token([T_STRING, 'Foo']),
- new Token([T_NS_SEPARATOR, '\\']),
- new Token([T_STRING, 'Bar']),
- new Token(';'),
- ],
- [
- new Token([T_OPEN_TAG, "<?php\n"]),
- new Token([T_NAME_RELATIVE, 'namespace\Transformer\Foo\Bar']),
- new Token(';'),
- ],
- ];
- }
- if (\defined('T_NAME_FULLY_QUALIFIED')) { // @TODO: drop condition when PHP 8.0+ is required
- yield 'name fully qualified 1' => [
- [
- new Token([T_OPEN_TAG, "<?php\n"]),
- new Token([T_NS_SEPARATOR, '\\']),
- new Token([T_STRING, 'Foo']),
- new Token(';'),
- ],
- [
- new Token([T_OPEN_TAG, "<?php\n"]),
- new Token([T_NAME_FULLY_QUALIFIED, '\Foo']),
- new Token(';'),
- ],
- ];
- yield 'name qualified 1' => [
- [
- new Token([T_OPEN_TAG, "<?php\n"]),
- new Token([T_STRING, 'Foo']),
- new Token([T_NS_SEPARATOR, '\\']),
- new Token([T_STRING, 'Bar']),
- new Token(';'),
- ],
- [
- new Token([T_OPEN_TAG, "<?php\n"]),
- new Token([T_NAME_QUALIFIED, 'Foo\Bar']),
- new Token(';'),
- ],
- ];
- yield 'name qualified 2' => [
- [
- new Token([T_OPEN_TAG, "<?php\n"]),
- new Token([T_NS_SEPARATOR, '\\']),
- new Token([T_STRING, 'Foo']),
- new Token([T_NS_SEPARATOR, '\\']),
- new Token([T_STRING, 'Bar']),
- new Token(';'),
- ],
- [
- new Token([T_OPEN_TAG, "<?php\n"]),
- new Token([T_NAME_QUALIFIED, '\Foo\Bar']),
- new Token(';'),
- ],
- ];
- }
- }
- /**
- * @param list<Token> $expected
- *
- * @dataProvider providePriorityCases
- */
- public function testPriority(array $expected, string $source): void
- {
- self::assertTokens(Tokens::fromArray($expected), Tokens::fromCode($source));
- }
- public static function providePriorityCases(): iterable
- {
- yield [
- [
- new Token([T_OPEN_TAG, '<?php ']),
- new Token([T_STRING, 'Foo']),
- new Token(';'),
- new Token([T_STRING, 'Bar']),
- new Token(';'),
- ],
- '<?php Foo;Bar;',
- ];
- yield [
- [
- new Token([T_OPEN_TAG, '<?php ']),
- new Token([T_STRING, 'Foo']),
- new Token([T_NS_SEPARATOR, '\\']),
- new Token([T_STRING, 'Bar1']),
- new Token(';'),
- new Token([T_NS_SEPARATOR, '\\']),
- new Token([T_STRING, 'Foo']),
- new Token([T_NS_SEPARATOR, '\\']),
- new Token([T_STRING, 'Bar2']),
- new Token(';'),
- new Token([T_STRING, 'Foo']),
- new Token([T_NS_SEPARATOR, '\\']),
- new Token([T_STRING, 'Bar3']),
- new Token(';'),
- ],
- '<?php Foo\Bar1;\Foo\Bar2;Foo\Bar3;',
- ];
- yield [
- [
- new Token([T_OPEN_TAG, '<?php ']),
- new Token([T_STRING, 'Foo']),
- new Token([T_NS_SEPARATOR, '\\']),
- new Token([T_STRING, 'Bar1']),
- new Token(';'),
- new Token([T_STRING, 'Foo']),
- new Token([T_NS_SEPARATOR, '\\']),
- new Token([T_STRING, 'Bar2']),
- new Token(';'),
- new Token([T_STRING, 'Foo']),
- new Token([T_NS_SEPARATOR, '\\']),
- new Token([T_STRING, 'Bar3']),
- new Token(';'),
- ],
- '<?php Foo\Bar1;Foo\Bar2;Foo\Bar3;',
- ];
- yield [
- [
- new Token([T_OPEN_TAG, '<?php ']),
- new Token([T_NS_SEPARATOR, '\\']),
- new Token([T_STRING, 'Foo']),
- new Token([T_NS_SEPARATOR, '\\']),
- new Token([T_STRING, 'Bar1']),
- new Token(';'),
- new Token([T_NS_SEPARATOR, '\\']),
- new Token([T_STRING, 'Foo']),
- new Token([T_NS_SEPARATOR, '\\']),
- new Token([T_STRING, 'Bar2']),
- new Token(';'),
- new Token([T_NS_SEPARATOR, '\\']),
- new Token([T_STRING, 'Foo']),
- new Token([T_NS_SEPARATOR, '\\']),
- new Token([T_STRING, 'Bar3']),
- new Token(';'),
- ],
- '<?php \Foo\Bar1;\Foo\Bar2;\Foo\Bar3;',
- ];
- yield [
- [
- new Token([T_OPEN_TAG, '<?php ']),
- new Token([CT::T_NAMESPACE_OPERATOR, 'namespace']),
- new Token([T_NS_SEPARATOR, '\\']),
- new Token([T_STRING, 'Transformer']),
- new Token(';'),
- ],
- '<?php namespace\Transformer;',
- ];
- yield [
- [
- new Token([T_OPEN_TAG, '<?php ']),
- new Token([T_NAMESPACE, 'namespace']),
- new Token([T_NS_SEPARATOR, '\\']),
- new Token([T_STRING, 'Foo']),
- new Token(';'),
- new Token([T_NAMESPACE, 'namespace']),
- new Token([T_NS_SEPARATOR, '\\']),
- new Token([T_STRING, 'Bar']),
- new Token(';'),
- ],
- '<?php namespace\Foo;namespace\Bar;',
- ];
- }
- }
|