UseBlockParserTest::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
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
    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_with_double_slash_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
    public function test_get_commented_use_with_hashtag_then_uses_current_namespace(): void
55
    {
56
        $actual = $this->parser->getUseStatement('CommentedClassInAnotherNs', $this->phpCode());
57
58
        self::assertSame('\Ns\Test\CommentedClassInAnotherNs', $actual);
59
    }
60
61
    private function phpCode(): string
62
    {
63
        return <<<'PHP'
64
<?php 
65
66
// namespace FailingCommentedLine\Test;
67
#namespace FailingCommentedAnotherLine\Test;
68
namespace Ns\Test;
69
70
use Ns\Test\Other\ExistingClassInOtherNs;
71
use Ns\Test\Other\WithAliasClassInOtherNs as AliasClass;
72
//use Ns\Test\Other\CommentedClassInOtherNs;
73
# use Ns\Test\Other\CommentedClassInAnotherNs;
74
use Ns\Test\Duplicated\ExistingClassInOtherNs; // this will be ignored. The first match will win.
75
                                               // this is also illegal in real code. I place it here 
76
                                               // just to verify the actual logic.
77
final class TestClass
78
{
79
    public function foo(): void 
80
    {
81
    }
82
}
83
PHP;
84
    }
85
}
86