Passed
Pull Request — master (#48)
by Marco
02:59
created

StaticClassMapSourceLocatorTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
dl 0
loc 97
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A acceptsEmptySet() 0 7 1
A testWillLocateThisClass() 0 21 1
A testWillNotLocateFunctions() 0 12 1
A rejectsEmptyKeys() 0 7 1
A testWillNotLocateUnknownClass() 0 12 1
A setUp() 0 6 1
A rejectsNonStringKeys() 0 7 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace RoaveTest\ApiCompare\SourceLocator;
6
7
use InvalidArgumentException;
8
use PHPUnit\Framework\MockObject\MockObject;
9
use PHPUnit\Framework\TestCase;
10
use Roave\ApiCompare\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 file_get_contents;
18
19
/**
20
 * @covers \Roave\ApiCompare\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 rejectsEmptyKeys() : void
39
    {
40
        $this->expectException(InvalidArgumentException::class);
41
42
        new StaticClassMapSourceLocator(
43
            ['' => __FILE__],
44
            $this->astLocator
45
        );
46
    }
47
48
    public function rejectsNonStringKeys() : void
49
    {
50
        $this->expectException(InvalidArgumentException::class);
51
52
        new StaticClassMapSourceLocator(
53
            [__FILE__],
54
            $this->astLocator
55
        );
56
    }
57
58
    public function acceptsEmptySet() : 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
    public function testWillLocateThisClass() : void
69
    {
70
        $locator    = new StaticClassMapSourceLocator([self::class => __FILE__], $this->astLocator);
71
        $reflection = $this->createMock(Reflection::class);
72
73
        $this
74
            ->astLocator
75
            ->expects(self::once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Roave\BetterReflection\SourceLocator\Ast\Locator. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

75
            ->/** @scrutinizer ignore-call */ 
76
              expects(self::once())

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
76
            ->method('findReflection')
77
            ->with($this->reflector, self::callback(function (LocatedSource $source) : bool {
78
                self::assertSame(file_get_contents(__FILE__), $source->getSource());
79
                self::assertSame(__FILE__, $source->getFileName());
80
                self::assertNull($source->getExtensionName());
81
82
                return true;
83
            }))
84
            ->willReturn($reflection);
85
86
        self::assertSame($reflection, $locator->locateIdentifier(
87
            $this->reflector,
88
            new Identifier(self::class, new IdentifierType(IdentifierType::IDENTIFIER_CLASS))
89
        ));
90
    }
91
92
    public function testWillNotLocateUnknownClass() : void
93
    {
94
        $locator = new StaticClassMapSourceLocator([self::class => __FILE__], $this->astLocator);
95
96
        $this
97
            ->astLocator
98
            ->expects(self::never())
99
            ->method('findReflection');
100
101
        self::assertNull($locator->locateIdentifier(
102
            $this->reflector,
103
            new Identifier('Unknown\\ClassName', new IdentifierType(IdentifierType::IDENTIFIER_CLASS))
104
        ));
105
    }
106
107
    public function testWillNotLocateFunctions() : void
108
    {
109
        $locator = new StaticClassMapSourceLocator([self::class => __FILE__], $this->astLocator);
110
111
        $this
112
            ->astLocator
113
            ->expects(self::never())
114
            ->method('findReflection');
115
116
        self::assertNull($locator->locateIdentifier(
117
            $this->reflector,
118
            new Identifier(self::class, new IdentifierType(IdentifierType::IDENTIFIER_FUNCTION))
119
        ));
120
    }
121
}
122