Passed
Push — feature/custom-services-v2 ( 35de26...30061f )
by Chema
03:34
created

UseBlockParserTest::test_get_class_from_use()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
rs 10
c 2
b 0
f 0
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
    private function phpCode(): string
41
    {
42
        return <<<'PHP'
43
<?php 
44
45
namespace Ns\Test;
46
47
use Ns\Test\Other\ExistingClassInOtherNs;
48
use Ns\Test\Duplicated\ExistingClassInOtherNs; // this will be ignored. The first match will win.
49
                                               // this is also illegal in real code. I place it here 
50
                                               // just to verify the actual logic.
51
final class TestClass
52
{
53
    public function foo(): void 
54
    {
55
        echo ExistingClassInOtherNamespace::class;
56
        
57
        // This class is in the same namespace `Ns\Test`, that's why there is no use statement
58
        echo ExistingClassInSameNs::class;
59
    }
60
}
61
PHP;
62
    }
63
}
64