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
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @covers \Roave\ApiCompare\SourceLocator\StaticClassMapSourceLocator |
20
|
|
|
*/ |
21
|
|
|
final class StaticClassMapSourceLocatorTest extends TestCase |
22
|
|
|
{ |
23
|
|
|
/** @var Locator|MockObject */ |
24
|
|
|
private $astLocator; |
25
|
|
|
|
26
|
|
|
/** @var Reflector|MockObject */ |
27
|
|
|
private $reflector; |
28
|
|
|
|
29
|
|
|
protected function setUp() : void |
30
|
|
|
{ |
31
|
|
|
parent::setUp(); |
32
|
|
|
|
33
|
|
|
$this->astLocator = $this->createMock(Locator::class); |
34
|
|
|
$this->reflector = $this->createMock(Reflector::class); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function rejectsEmptyKeys() : void |
38
|
|
|
{ |
39
|
|
|
$this->expectException(InvalidArgumentException::class); |
40
|
|
|
|
41
|
|
|
new StaticClassMapSourceLocator( |
42
|
|
|
['' => __FILE__], |
43
|
|
|
$this->astLocator |
44
|
|
|
); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function rejectsNonStringKeys() : void |
48
|
|
|
{ |
49
|
|
|
$this->expectException(InvalidArgumentException::class); |
50
|
|
|
|
51
|
|
|
new StaticClassMapSourceLocator( |
52
|
|
|
[__FILE__], |
53
|
|
|
$this->astLocator |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function acceptsEmptySet() : void |
58
|
|
|
{ |
59
|
|
|
$locator = new StaticClassMapSourceLocator([], $this->astLocator); |
60
|
|
|
|
61
|
|
|
self::assertNull($locator->locateIdentifier( |
62
|
|
|
$this->reflector, |
63
|
|
|
new Identifier(self::class, new IdentifierType(IdentifierType::IDENTIFIER_CLASS)) |
64
|
|
|
)); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function testWillLocateThisClass() : void |
68
|
|
|
{ |
69
|
|
|
$locator = new StaticClassMapSourceLocator([self::class => __FILE__], $this->astLocator); |
70
|
|
|
$reflection = $this->createMock(Reflection::class); |
71
|
|
|
|
72
|
|
|
$this |
73
|
|
|
->astLocator |
74
|
|
|
->expects(self::once()) |
|
|
|
|
75
|
|
|
->method('findReflection') |
76
|
|
|
->with($this->reflector, self::callback(function (LocatedSource $source) : bool { |
77
|
|
|
self::assertSame(file_get_contents(__FILE__), $source->getSource()); |
78
|
|
|
self::assertSame(__FILE__, $source->getFileName()); |
79
|
|
|
self::assertNull($source->getExtensionName()); |
80
|
|
|
|
81
|
|
|
return true; |
82
|
|
|
})) |
83
|
|
|
->willReturn($reflection); |
84
|
|
|
|
85
|
|
|
self::assertSame($reflection, $locator->locateIdentifier( |
86
|
|
|
$this->reflector, |
87
|
|
|
new Identifier(self::class, new IdentifierType(IdentifierType::IDENTIFIER_CLASS)) |
88
|
|
|
)); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testWillNotLocateUnknownClass() : void |
92
|
|
|
{ |
93
|
|
|
$locator = new StaticClassMapSourceLocator([self::class => __FILE__], $this->astLocator); |
94
|
|
|
|
95
|
|
|
$this |
96
|
|
|
->astLocator |
97
|
|
|
->expects(self::never()) |
98
|
|
|
->method('findReflection'); |
99
|
|
|
|
100
|
|
|
self::assertNull($locator->locateIdentifier( |
101
|
|
|
$this->reflector, |
102
|
|
|
new Identifier('Unknown\\ClassName', new IdentifierType(IdentifierType::IDENTIFIER_CLASS)) |
103
|
|
|
)); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function testWillNotLocateFunctions() : void |
107
|
|
|
{ |
108
|
|
|
$locator = new StaticClassMapSourceLocator([self::class => __FILE__], $this->astLocator); |
109
|
|
|
|
110
|
|
|
$this |
111
|
|
|
->astLocator |
112
|
|
|
->expects(self::never()) |
113
|
|
|
->method('findReflection'); |
114
|
|
|
|
115
|
|
|
self::assertNull($locator->locateIdentifier( |
116
|
|
|
$this->reflector, |
117
|
|
|
new Identifier(self::class, new IdentifierType(IdentifierType::IDENTIFIER_FUNCTION)) |
118
|
|
|
)); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
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.