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()); |
|
|
|
|
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
|
|
|
/** @return array<int, array<int, string>> */ |
96
|
|
|
public static function thisClassPossiblePaths() : array |
97
|
|
|
{ |
98
|
|
|
return [ |
99
|
|
|
[__FILE__], |
100
|
|
|
[__DIR__ . '/../SourceLocator/StaticClassMapSourceLocatorTest.php'], |
101
|
|
|
]; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function testWillNotLocateUnknownClass() : void |
105
|
|
|
{ |
106
|
|
|
$locator = new StaticClassMapSourceLocator([self::class => __FILE__], $this->astLocator); |
107
|
|
|
|
108
|
|
|
$this |
109
|
|
|
->astLocator |
110
|
|
|
->expects(self::never()) |
|
|
|
|
111
|
|
|
->method('findReflection'); |
112
|
|
|
|
113
|
|
|
self::assertNull($locator->locateIdentifier( |
114
|
|
|
$this->reflector, |
115
|
|
|
new Identifier('Unknown\\ClassName', new IdentifierType(IdentifierType::IDENTIFIER_CLASS)) |
116
|
|
|
)); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function testWillNotLocateFunctions() : void |
120
|
|
|
{ |
121
|
|
|
$locator = new StaticClassMapSourceLocator([self::class => __FILE__], $this->astLocator); |
122
|
|
|
|
123
|
|
|
$this |
124
|
|
|
->astLocator |
125
|
|
|
->expects(self::never()) |
126
|
|
|
->method('findReflection'); |
127
|
|
|
|
128
|
|
|
self::assertNull($locator->locateIdentifier( |
129
|
|
|
$this->reflector, |
130
|
|
|
new Identifier(self::class, new IdentifierType(IdentifierType::IDENTIFIER_FUNCTION)) |
131
|
|
|
)); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|