1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/** |
3
|
|
|
* This file is part of phpDocumentor. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* @link http://phpdoc.org |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace phpDocumentor\Reflection\Types { |
12
|
|
|
|
13
|
|
|
// Added imports on purpose as mock for the unit tests, please do not remove. |
14
|
|
|
use Mockery as m, phpDocumentor; |
15
|
|
|
use phpDocumentor\Reflection\DocBlock; |
16
|
|
|
use phpDocumentor\Reflection\DocBlock\Tag; |
17
|
|
|
use PHPUnit\Framework\TestCase; // yes, the slash is part of the test |
18
|
|
|
use PHPUnit\Framework\{ |
19
|
|
|
Assert, |
20
|
|
|
Exception as e |
21
|
|
|
}; |
22
|
|
|
use \ReflectionClass; |
23
|
|
|
use stdClass; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @coversDefaultClass \phpDocumentor\Reflection\Types\ContextFactory |
27
|
|
|
* @covers ::<private> |
28
|
|
|
*/ |
29
|
|
|
class ContextFactoryTest extends TestCase |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @covers ::createFromReflector |
33
|
|
|
* @covers ::createForNamespace |
34
|
|
|
* @uses phpDocumentor\Reflection\Types\Context |
35
|
|
|
*/ |
36
|
|
|
public function testReadsNamespaceFromClassReflection() : void |
37
|
|
|
{ |
38
|
|
|
$fixture = new ContextFactory(); |
39
|
|
|
$context = $fixture->createFromReflector(new ReflectionClass($this)); |
40
|
|
|
|
41
|
|
|
$this->assertSame(__NAMESPACE__, $context->getNamespace()); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @covers ::createFromReflector |
46
|
|
|
* @covers ::createForNamespace |
47
|
|
|
* @uses phpDocumentor\Reflection\Types\Context |
48
|
|
|
*/ |
49
|
|
|
public function testReadsAliasesFromClassReflection() : void |
50
|
|
|
{ |
51
|
|
|
$fixture = new ContextFactory(); |
52
|
|
|
$context = $fixture->createFromReflector(new ReflectionClass($this)); |
53
|
|
|
|
54
|
|
|
$this->assertNamespaceAliasesFrom($context); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @covers ::createForNamespace |
59
|
|
|
* @uses phpDocumentor\Reflection\Types\Context |
60
|
|
|
*/ |
61
|
|
|
public function testReadsNamespaceFromProvidedNamespaceAndContent() : void |
62
|
|
|
{ |
63
|
|
|
$fixture = new ContextFactory(); |
64
|
|
|
$context = $fixture->createForNamespace(__NAMESPACE__, file_get_contents(__FILE__)); |
65
|
|
|
|
66
|
|
|
$this->assertSame(__NAMESPACE__, $context->getNamespace()); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @covers ::createForNamespace |
71
|
|
|
* @uses phpDocumentor\Reflection\Types\Context |
72
|
|
|
*/ |
73
|
|
|
public function testReadsAliasesFromProvidedNamespaceAndContent() : void |
74
|
|
|
{ |
75
|
|
|
$fixture = new ContextFactory(); |
76
|
|
|
$context = $fixture->createForNamespace(__NAMESPACE__, file_get_contents(__FILE__)); |
77
|
|
|
|
78
|
|
|
$this->assertNamespaceAliasesFrom($context); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @covers ::createForNamespace |
83
|
|
|
* @uses phpDocumentor\Reflection\Types\Context |
84
|
|
|
*/ |
85
|
|
|
public function testTraitUseIsNotDetectedAsNamespaceUse() : void |
86
|
|
|
{ |
87
|
|
|
$php = '<?php declare(strict_types=1); |
88
|
|
|
namespace Foo; |
89
|
|
|
|
90
|
|
|
trait FooTrait {} |
91
|
|
|
|
92
|
|
|
class FooClass { |
93
|
|
|
use FooTrait; |
94
|
|
|
} |
95
|
|
|
'; |
96
|
|
|
|
97
|
|
|
$fixture = new ContextFactory(); |
98
|
|
|
$context = $fixture->createForNamespace('Foo', $php); |
99
|
|
|
|
100
|
|
|
$this->assertSame([], $context->getNamespaceAliases()); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @covers ::createForNamespace |
105
|
|
|
* @uses phpDocumentor\Reflection\Types\Context |
106
|
|
|
*/ |
107
|
|
|
public function testAllOpeningBracesAreCheckedWhenSearchingForEndOfClass() : void |
108
|
|
|
{ |
109
|
|
|
$php = '<?php declare(strict_types=1); |
110
|
|
|
namespace Foo; |
111
|
|
|
|
112
|
|
|
trait FooTrait {} |
113
|
|
|
trait BarTrait {} |
114
|
|
|
|
115
|
|
|
class FooClass { |
116
|
|
|
use FooTrait; |
117
|
|
|
|
118
|
|
|
public function bar() |
119
|
|
|
{ |
120
|
|
|
echo "{$baz}"; |
121
|
|
|
echo "${baz}"; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
class BarClass { |
126
|
|
|
use BarTrait; |
127
|
|
|
|
128
|
|
|
public function bar() |
129
|
|
|
{ |
130
|
|
|
echo "{$baz}"; |
131
|
|
|
echo "${baz}"; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
'; |
135
|
|
|
|
136
|
|
|
$fixture = new ContextFactory(); |
137
|
|
|
$context = $fixture->createForNamespace('Foo', $php); |
138
|
|
|
|
139
|
|
|
$this->assertSame([], $context->getNamespaceAliases()); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @covers ::createFromReflector |
144
|
|
|
*/ |
145
|
|
|
public function testEmptyFileName() : void |
146
|
|
|
{ |
147
|
|
|
$fixture = new ContextFactory(); |
148
|
|
|
$context = $fixture->createFromReflector(new ReflectionClass(stdClass::class)); |
149
|
|
|
|
150
|
|
|
$this->assertSame([], $context->getNamespaceAliases()); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @covers ::createFromReflector |
155
|
|
|
*/ |
156
|
|
|
public function testEvalDClass() : void |
157
|
|
|
{ |
158
|
|
|
eval(<<<PHP |
159
|
|
|
namespace Foo; |
160
|
|
|
|
161
|
|
|
class Bar |
162
|
|
|
{ |
163
|
|
|
} |
164
|
|
|
PHP |
165
|
|
|
); |
166
|
|
|
$fixture = new ContextFactory(); |
167
|
|
|
$context = $fixture->createFromReflector(new ReflectionClass('Foo\Bar')); |
168
|
|
|
|
169
|
|
|
$this->assertSame([], $context->getNamespaceAliases()); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function assertNamespaceAliasesFrom(Context $context) |
173
|
|
|
{ |
174
|
|
|
$expected = [ |
175
|
|
|
'm' => m::class, |
176
|
|
|
'DocBlock' => DocBlock::class, |
177
|
|
|
'Tag' => Tag::class, |
178
|
|
|
'phpDocumentor' => 'phpDocumentor', |
179
|
|
|
'TestCase' => TestCase::class, |
180
|
|
|
'Assert' => Assert::class, |
181
|
|
|
'e' => e::class, |
182
|
|
|
ReflectionClass::class => ReflectionClass::class, |
183
|
|
|
'stdClass' => 'stdClass', |
184
|
|
|
]; |
185
|
|
|
|
186
|
|
|
$actual = $context->getNamespaceAliases(); |
187
|
|
|
|
188
|
|
|
// sort so that order differences don't break it |
189
|
|
|
asort($expected); |
190
|
|
|
asort($actual); |
191
|
|
|
|
192
|
|
|
$this->assertSame($expected, $actual); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
public function tearDown() |
196
|
|
|
{ |
197
|
|
|
m::close(); |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
namespace phpDocumentor\Reflection\Types\Mock { |
203
|
|
|
|
204
|
|
|
// the following import should not show in the tests above |
205
|
|
|
use phpDocumentor\Reflection\DocBlock\Description; |
206
|
|
|
|
207
|
|
|
class Foo extends Description |
208
|
|
|
{ |
209
|
|
|
// dummy class |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|