123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- <?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\Analyzer;
- use PhpCsFixer\Tests\TestCase;
- use PhpCsFixer\Tokenizer\Analyzer\Analysis\NamespaceAnalysis;
- use PhpCsFixer\Tokenizer\Analyzer\NamespacesAnalyzer;
- use PhpCsFixer\Tokenizer\Tokens;
- /**
- * @author VeeWee <toonverwerft@gmail.com>
- *
- * @internal
- *
- * @covers \PhpCsFixer\Tokenizer\Analyzer\NamespacesAnalyzer
- */
- final class NamespacesAnalyzerTest extends TestCase
- {
- /**
- * @param list<NamespaceAnalysis> $expected
- *
- * @dataProvider provideNamespacesCases
- */
- public function testNamespaces(string $code, array $expected): void
- {
- $tokens = Tokens::fromCode($code);
- $analyzer = new NamespacesAnalyzer();
- self::assertSame(
- serialize($expected),
- serialize($analyzer->getDeclarations($tokens))
- );
- }
- public static function provideNamespacesCases(): iterable
- {
- yield ['<?php // no namespaces', [
- new NamespaceAnalysis(
- '',
- '',
- 0,
- 0,
- 0,
- 1
- ),
- ]];
- yield ['<?php namespace Foo\Bar;', [
- new NamespaceAnalysis(
- 'Foo\Bar',
- 'Bar',
- 1,
- 6,
- 1,
- 6
- ),
- ]];
- yield ['<?php namespace Foo\Bar{}; namespace Foo\Baz {};', [
- new NamespaceAnalysis(
- 'Foo\Bar',
- 'Bar',
- 1,
- 6,
- 1,
- 7
- ),
- new NamespaceAnalysis(
- 'Foo\Baz',
- 'Baz',
- 10,
- 16,
- 10,
- 17
- ),
- ]];
- yield [
- <<<'PHP'
- #!/usr/bin/php
- <?php
- return true;
- PHP,
- [
- new NamespaceAnalysis(
- '',
- '',
- 1,
- 1,
- 1,
- 5
- ),
- ],
- ];
- yield [
- 'there is no namespace if there is no PHP code',
- [],
- ];
- }
- /**
- * @dataProvider provideGetNamespaceAtCases
- */
- public function testGetNamespaceAt(string $code, int $index, NamespaceAnalysis $expected): void
- {
- $tokens = Tokens::fromCode($code);
- $analyzer = new NamespacesAnalyzer();
- self::assertSame(
- serialize($expected),
- serialize($analyzer->getNamespaceAt($tokens, $index))
- );
- }
- /**
- * @return iterable<array{string, int, NamespaceAnalysis}>
- */
- public static function provideGetNamespaceAtCases(): iterable
- {
- yield [
- '<?php // no namespaces',
- 1,
- new NamespaceAnalysis(
- '',
- '',
- 0,
- 0,
- 0,
- 1
- ),
- ];
- yield [
- '<?php namespace Foo\Bar;',
- 5,
- new NamespaceAnalysis(
- 'Foo\Bar',
- 'Bar',
- 1,
- 6,
- 1,
- 6
- ),
- ];
- yield [
- '<?php namespace Foo\Bar{}; namespace Foo\Baz {};',
- 5,
- new NamespaceAnalysis(
- 'Foo\Bar',
- 'Bar',
- 1,
- 6,
- 1,
- 7
- ),
- ];
- yield [
- '<?php namespace Foo\Bar{}; namespace Foo\Baz {};',
- 13,
- new NamespaceAnalysis(
- 'Foo\Baz',
- 'Baz',
- 10,
- 16,
- 10,
- 17
- ),
- ];
- }
- public function testInvalidIndex(): void
- {
- $this->expectException(\InvalidArgumentException::class);
- $this->expectExceptionMessage('Token index 666 does not exist.');
- $analyzer = new NamespacesAnalyzer();
- $analyzer->getNamespaceAt(new Tokens(), 666);
- }
- }
|