StaticClassMapSourceLocatorTest   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 51
c 4
b 0
f 0
dl 0
loc 113
rs 10
wmc 8

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 6 1
A testAcceptsEmptySet() 0 7 1
A testRejectsEmptyKeys() 0 7 1
A testRejectsEmptyStringFiles() 0 7 1
A testWillLocateThisClass() 0 21 1
A testWillNotLocateFunctions() 0 12 1
A thisClassPossiblePaths() 0 5 1
A testWillNotLocateUnknownClass() 0 12 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace RoaveTest\BackwardCompatibility\SourceLocator;
6
7
use InvalidArgumentException;
8
use PHPUnit\Framework\MockObject\MockObject;
9
use PHPUnit\Framework\TestCase;
10
use Roave\BackwardCompatibility\SourceLocator\StaticClassMapSourceLocator;
11
use Roave\BetterReflection\Identifier\Identifier;
12
use Roave\BetterReflection\Identifier\IdentifierType;
13
use Roave\BetterReflection\Reflection\Reflection;
14
use Roave\BetterReflection\Reflector\Reflector;
15
use Roave\BetterReflection\SourceLocator\Ast\Locator;
16
use Roave\BetterReflection\SourceLocator\Located\LocatedSource;
17
use function Safe\file_get_contents;
18
19
/**
20
 * @covers \Roave\BackwardCompatibility\SourceLocator\StaticClassMapSourceLocator
21
 */
22
final class StaticClassMapSourceLocatorTest extends TestCase
23
{
24
    /** @var Locator&MockObject */
25
    private $astLocator;
26
27
    /** @var Reflector&MockObject */
28
    private $reflector;
29
30
    protected function setUp() : void
31
    {
32
        parent::setUp();
33
34
        $this->astLocator = $this->createMock(Locator::class);
35
        $this->reflector  = $this->createMock(Reflector::class);
36
    }
37
38
    public function testRejectsEmptyKeys() : void
39
    {
40
        $this->expectException(InvalidArgumentException::class);
41
42
        new StaticClassMapSourceLocator(
43
            ['' => __FILE__],
44
            $this->astLocator
45
        );
46
    }
47
48
    public function testRejectsEmptyStringFiles() : void
49
    {
50
        $this->expectException(InvalidArgumentException::class);
51
52
        new StaticClassMapSourceLocator(
53
            ['foo' => ''],
54
            $this->astLocator
55
        );
56
    }
57
58
    public function testAcceptsEmptySet() : void
59
    {
60
        $locator = new StaticClassMapSourceLocator([], $this->astLocator);
61
62
        self::assertNull($locator->locateIdentifier(
63
            $this->reflector,
64
            new Identifier(self::class, new IdentifierType(IdentifierType::IDENTIFIER_CLASS))
65
        ));
66
    }
67
68
    /**
69
     * @dataProvider thisClassPossiblePaths
70
     */
71
    public function testWillLocateThisClass(string $thisClassFilePath) : void
72
    {
73
        $locator    = new StaticClassMapSourceLocator([self::class => $thisClassFilePath], $this->astLocator);
74
        $reflection = $this->createMock(Reflection::class);
75
76
        $this
77
            ->astLocator
78
            ->expects(self::once())
79
            ->method('findReflection')
80
            ->with($this->reflector, self::callback(static function (LocatedSource $source) : bool {
81
                self::assertSame(file_get_contents(__FILE__), $source->getSource());
82
                self::assertSame(__FILE__, $source->getFileName());
83
                self::assertNull($source->getExtensionName());
0 ignored issues
show
Bug introduced by
Are you sure the usage of $source->getExtensionName() targeting Roave\BetterReflection\S...rce::getExtensionName() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
84
85
                return true;
86
            }))
87
            ->willReturn($reflection);
88
89
        self::assertSame($reflection, $locator->locateIdentifier(
90
            $this->reflector,
91
            new Identifier(self::class, new IdentifierType(IdentifierType::IDENTIFIER_CLASS))
92
        ));
93
    }
94
95
    /**
96
     * @return array<int, array<int, string>>
97
     *
98
     * @psalm-return list<list<string>>
99
     */
100
    public static function thisClassPossiblePaths() : array
101
    {
102
        return [
103
            [__FILE__],
104
            [__DIR__ . '/../SourceLocator/StaticClassMapSourceLocatorTest.php'],
105
        ];
106
    }
107
108
    public function testWillNotLocateUnknownClass() : void
109
    {
110
        $locator = new StaticClassMapSourceLocator([self::class => __FILE__], $this->astLocator);
111
112
        $this
113
            ->astLocator
114
            ->expects(self::never())
115
            ->method('findReflection');
116
117
        self::assertNull($locator->locateIdentifier(
118
            $this->reflector,
119
            new Identifier('Unknown\\ClassName', new IdentifierType(IdentifierType::IDENTIFIER_CLASS))
120
        ));
121
    }
122
123
    public function testWillNotLocateFunctions() : void
124
    {
125
        $locator = new StaticClassMapSourceLocator([self::class => __FILE__], $this->astLocator);
126
127
        $this
128
            ->astLocator
129
            ->expects(self::never())
130
            ->method('findReflection');
131
132
        self::assertNull($locator->locateIdentifier(
133
            $this->reflector,
134
            new Identifier(self::class, new IdentifierType(IdentifierType::IDENTIFIER_FUNCTION))
135
        ));
136
    }
137
}
138