Passed
Push — feature/custom-services-v2 ( 30061f...3b6089 )
by Chema
03:57
created

UseBlockParserTest::test_get_class_with_alias()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GacelaTest\Unit\Framework\ClassResolver\DocBlockService;
6
7
use Gacela\Framework\ClassResolver\DocBlockService\UseBlockParser;
8
use PHPUnit\Framework\TestCase;
9
10
final class UseBlockParserTest extends TestCase
11
{
12
    private UseBlockParser $parser;
13
14
    protected function setUp(): void
15
    {
16
        $this->parser = new UseBlockParser();
17
    }
18
19
    public function test_get_class_from_empty_php_code(): void
20
    {
21
        $actual = $this->parser->getUseStatement('TestClass', '');
22
23
        self::assertSame('', $actual);
24
    }
25
26
    public function test_get_class_from_use(): void
27
    {
28
        $actual = $this->parser->getUseStatement('ExistingClassInOtherNs', $this->phpCode());
29
30
        self::assertSame('Ns\Test\Other\ExistingClassInOtherNs', $actual);
31
    }
32
33
    public function test_get_class_in_same_namespace(): void
34
    {
35
        $actual = $this->parser->getUseStatement('ExistingClassInSameNs', $this->phpCode());
36
37
        self::assertSame('Ns\Test\ExistingClassInSameNs', $actual);
38
    }
39
40
    public function test_get_class_with_alias(): void
41
    {
42
        $actual = $this->parser->getUseStatement('AliasClass', $this->phpCode());
43
44
        self::assertSame('Ns\Test\Other\WithAliasClassInOtherNs', $actual);
45
    }
46
47
    public function test_get_commented_use_then_uses_current_namespace(): void
48
    {
49
        $actual = $this->parser->getUseStatement('CommentedClassInOtherNs', $this->phpCode());
50
51
        self::assertSame('Ns\Test\CommentedClassInOtherNs', $actual);
52
    }
53
54
    private function phpCode(): string
55
    {
56
        return <<<'PHP'
57
<?php 
58
59
namespace Ns\Test;
60
61
use Ns\Test\Other\ExistingClassInOtherNs;
62
use Ns\Test\Other\WithAliasClassInOtherNs as AliasClass;
63
// use Ns\Test\Other\CommentedClassInOtherNs;
64
use Ns\Test\Duplicated\ExistingClassInOtherNs; // this will be ignored. The first match will win.
65
                                               // this is also illegal in real code. I place it here 
66
                                               // just to verify the actual logic.
67
final class TestClass
68
{
69
    public function foo(): void 
70
    {
71
        echo ExistingClassInOtherNamespace::class;
72
73
        // This class is in the same namespace `Ns\Test`, that's why there is no use statement
74
        echo ExistingClassInSameNs::class;
75
76
        // This will use the current ns, because its "use" is commented out
77
        echo CommentedClassInOtherNs::class;
78
    }
79
}
80
PHP;
81
    }
82
}
83