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

UseBlockParserTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 29
dl 0
loc 33
rs 10
c 3
b 0
f 0
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 3 1
A phpCode() 0 3 1
A test_get_class_in_same_namespace() 0 5 1
A test_get_class_from_empty_php_code() 0 5 1
A test_get_class_from_use() 0 5 1
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