|
1
|
|
|
<?php |
|
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
|
|
|
* @copyright 2010-2018 Mike van Riel<[email protected]> |
|
9
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT |
|
10
|
|
|
* @link http://phpdoc.org |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace phpDocumentor\Reflection\Php\Factory; |
|
14
|
|
|
|
|
15
|
|
|
use Mockery as m; |
|
16
|
|
|
use phpDocumentor\Reflection\DocBlock as DocBlockDescriptor; |
|
17
|
|
|
use phpDocumentor\Reflection\File as SourceFile; |
|
18
|
|
|
use phpDocumentor\Reflection\Fqsen; |
|
19
|
|
|
use phpDocumentor\Reflection\Middleware\Middleware; |
|
20
|
|
|
use phpDocumentor\Reflection\Php\Class_ as ClassElement; |
|
21
|
|
|
use phpDocumentor\Reflection\Php\Constant as ConstantElement; |
|
22
|
|
|
use phpDocumentor\Reflection\Php\File as FileElement; |
|
23
|
|
|
use phpDocumentor\Reflection\Php\Function_ as FunctionElement; |
|
24
|
|
|
use phpDocumentor\Reflection\Php\Interface_ as InterfaceElement; |
|
25
|
|
|
use phpDocumentor\Reflection\Php\NodesFactory; |
|
26
|
|
|
use phpDocumentor\Reflection\Php\ProjectFactoryStrategy; |
|
27
|
|
|
use phpDocumentor\Reflection\Php\StrategyContainer; |
|
28
|
|
|
use phpDocumentor\Reflection\Php\Trait_ as TraitElement; |
|
29
|
|
|
use PhpParser\Comment as CommentNode; |
|
30
|
|
|
use PhpParser\Comment\Doc as DocBlockNode; |
|
31
|
|
|
use PhpParser\Node\Const_ as ConstNode; |
|
32
|
|
|
use PhpParser\Node\Name; |
|
33
|
|
|
use PhpParser\Node\Scalar\String_; |
|
34
|
|
|
use PhpParser\Node\Stmt\Class_ as ClassNode; |
|
35
|
|
|
use PhpParser\Node\Stmt\Const_ as ConstantNode; |
|
36
|
|
|
use PhpParser\Node\Stmt\Function_ as FunctionNode; |
|
37
|
|
|
use PhpParser\Node\Stmt\Interface_ as InterfaceNode; |
|
38
|
|
|
use PhpParser\Node\Stmt\Namespace_ as NamespaceNode; |
|
39
|
|
|
use PhpParser\Node\Stmt\Trait_ as TraitNode; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Test case for \phpDocumentor\Reflection\Php\Factory\File |
|
43
|
|
|
* @coversDefaultClass \phpDocumentor\Reflection\Php\Factory\File |
|
44
|
|
|
* @covers ::<!public> |
|
45
|
|
|
* @covers ::__construct |
|
46
|
|
|
*/ |
|
47
|
|
|
final class FileTest extends TestCase |
|
48
|
|
|
{ |
|
49
|
|
|
/** |
|
50
|
|
|
* @var m\MockInterface |
|
51
|
|
|
*/ |
|
52
|
|
|
private $nodesFactoryMock; |
|
53
|
|
|
|
|
54
|
|
|
protected function setUp() |
|
55
|
|
|
{ |
|
56
|
|
|
$this->nodesFactoryMock = m::mock(NodesFactory::class); |
|
57
|
|
|
$this->fixture = new File($this->nodesFactoryMock); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @covers ::matches |
|
62
|
|
|
*/ |
|
63
|
|
|
public function testMatches() |
|
64
|
|
|
{ |
|
65
|
|
|
$this->assertFalse($this->fixture->matches(new \stdClass())); |
|
66
|
|
|
$this->assertTrue($this->fixture->matches(m::mock(SourceFile::class))); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @covers ::create |
|
71
|
|
|
*/ |
|
72
|
|
|
public function testFileWithConstant() |
|
73
|
|
|
{ |
|
74
|
|
|
$constantNode = new ConstantNode([new ConstNode('MY_CONSTANT', new String_('value'))]); |
|
75
|
|
|
$this->nodesFactoryMock->shouldReceive('create') |
|
76
|
|
|
->with(file_get_contents(__FILE__)) |
|
77
|
|
|
->andReturn( |
|
78
|
|
|
[ |
|
79
|
|
|
$constantNode, |
|
80
|
|
|
] |
|
81
|
|
|
); |
|
82
|
|
|
$strategyMock = m::mock(ProjectFactoryStrategy::class); |
|
83
|
|
|
$containerMock = m::mock(StrategyContainer::class); |
|
84
|
|
|
|
|
85
|
|
|
$strategyMock->shouldReceive('create') |
|
86
|
|
|
->with(m::type(GlobalConstantIterator::class), $containerMock, m::any()) |
|
87
|
|
|
->andReturn(new ConstantElement(new Fqsen('\MY_CONSTANT'))); |
|
88
|
|
|
|
|
89
|
|
|
$containerMock->shouldReceive('findMatching') |
|
90
|
|
|
->with(m::type(GlobalConstantIterator::class)) |
|
91
|
|
|
->andReturn($strategyMock); |
|
92
|
|
|
|
|
93
|
|
|
/** @var FileElement $file */ |
|
94
|
|
|
$file = $this->fixture->create(new SourceFile\LocalFile(__FILE__), $containerMock); |
|
95
|
|
|
|
|
96
|
|
|
$this->assertEquals(__FILE__, $file->getPath()); |
|
97
|
|
|
$this->assertArrayHasKey('\MY_CONSTANT', $file->getConstants()); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @covers ::create |
|
102
|
|
|
*/ |
|
103
|
|
|
public function testFileWithFunction() |
|
104
|
|
|
{ |
|
105
|
|
|
$functionNode = new FunctionNode('myFunction'); |
|
106
|
|
|
$this->nodesFactoryMock->shouldReceive('create') |
|
107
|
|
|
->with(file_get_contents(__FILE__)) |
|
108
|
|
|
->andReturn( |
|
109
|
|
|
[ |
|
110
|
|
|
$functionNode, |
|
111
|
|
|
] |
|
112
|
|
|
); |
|
113
|
|
|
$strategyMock = m::mock(ProjectFactoryStrategy::class); |
|
114
|
|
|
$containerMock = m::mock(StrategyContainer::class); |
|
115
|
|
|
|
|
116
|
|
|
$strategyMock->shouldReceive('create') |
|
117
|
|
|
->with($functionNode, $containerMock, m::any()) |
|
118
|
|
|
->andReturn(new FunctionElement(new Fqsen('\myFunction()'))); |
|
119
|
|
|
|
|
120
|
|
|
$containerMock->shouldReceive('findMatching') |
|
121
|
|
|
->with($functionNode) |
|
122
|
|
|
->andReturn($strategyMock); |
|
123
|
|
|
|
|
124
|
|
|
/** @var FileElement $file */ |
|
125
|
|
|
$file = $this->fixture->create(new SourceFile\LocalFile(__FILE__), $containerMock); |
|
126
|
|
|
|
|
127
|
|
|
$this->assertEquals(__FILE__, $file->getPath()); |
|
128
|
|
|
$this->assertArrayHasKey('\myFunction()', $file->getFunctions()); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @covers ::create |
|
133
|
|
|
*/ |
|
134
|
|
|
public function testFileWithClass() |
|
135
|
|
|
{ |
|
136
|
|
|
$classNode = new ClassNode('myClass'); |
|
137
|
|
|
$this->nodesFactoryMock->shouldReceive('create') |
|
138
|
|
|
->with(file_get_contents(__FILE__)) |
|
139
|
|
|
->andReturn( |
|
140
|
|
|
[ |
|
141
|
|
|
$classNode, |
|
142
|
|
|
] |
|
143
|
|
|
); |
|
144
|
|
|
$strategyMock = m::mock(ProjectFactoryStrategy::class); |
|
145
|
|
|
$containerMock = m::mock(StrategyContainer::class); |
|
146
|
|
|
|
|
147
|
|
|
$strategyMock->shouldReceive('create') |
|
148
|
|
|
->with($classNode, $containerMock, m::any()) |
|
149
|
|
|
->andReturn(new ClassElement(new Fqsen('\myClass'))); |
|
150
|
|
|
|
|
151
|
|
|
$containerMock->shouldReceive('findMatching') |
|
152
|
|
|
->with($classNode) |
|
153
|
|
|
->andReturn($strategyMock); |
|
154
|
|
|
|
|
155
|
|
|
/** @var FileElement $file */ |
|
156
|
|
|
$file = $this->fixture->create(new SourceFile\LocalFile(__FILE__), $containerMock); |
|
157
|
|
|
|
|
158
|
|
|
$this->assertEquals(__FILE__, $file->getPath()); |
|
159
|
|
|
$this->assertArrayHasKey('\myClass', $file->getClasses()); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* @covers ::create |
|
164
|
|
|
*/ |
|
165
|
|
|
public function testFileWithNamespace() |
|
166
|
|
|
{ |
|
167
|
|
|
$namespaceNode = new NamespaceNode(new Name('mySpace')); |
|
168
|
|
|
$namespaceNode->fqsen = new Fqsen('\mySpace'); |
|
169
|
|
|
$this->nodesFactoryMock->shouldReceive('create') |
|
170
|
|
|
->with(file_get_contents(__FILE__)) |
|
171
|
|
|
->andReturn( |
|
172
|
|
|
[ |
|
173
|
|
|
$namespaceNode, |
|
174
|
|
|
] |
|
175
|
|
|
); |
|
176
|
|
|
|
|
177
|
|
|
$containerMock = m::mock(StrategyContainer::class); |
|
178
|
|
|
|
|
179
|
|
|
/** @var FileElement $file */ |
|
180
|
|
|
$file = $this->fixture->create(new SourceFile\LocalFile(__FILE__), $containerMock); |
|
181
|
|
|
|
|
182
|
|
|
$this->assertEquals(__FILE__, $file->getPath()); |
|
183
|
|
|
$this->assertArrayHasKey('\mySpace', $file->getNamespaces()); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* @covers ::create |
|
188
|
|
|
*/ |
|
189
|
|
|
public function testFileWithInterface() |
|
190
|
|
|
{ |
|
191
|
|
|
$interfaceNode = new InterfaceNode('myInterface'); |
|
192
|
|
|
$this->nodesFactoryMock->shouldReceive('create') |
|
193
|
|
|
->with(file_get_contents(__FILE__)) |
|
194
|
|
|
->andReturn( |
|
195
|
|
|
[ |
|
196
|
|
|
$interfaceNode, |
|
197
|
|
|
] |
|
198
|
|
|
); |
|
199
|
|
|
$strategyMock = m::mock(ProjectFactoryStrategy::class); |
|
200
|
|
|
$containerMock = m::mock(StrategyContainer::class); |
|
201
|
|
|
|
|
202
|
|
|
$strategyMock->shouldReceive('create') |
|
203
|
|
|
->with($interfaceNode, $containerMock, m::any()) |
|
204
|
|
|
->andReturn(new InterfaceElement(new Fqsen('\myInterface'))); |
|
205
|
|
|
|
|
206
|
|
|
$containerMock->shouldReceive('findMatching') |
|
207
|
|
|
->with($interfaceNode) |
|
208
|
|
|
->andReturn($strategyMock); |
|
209
|
|
|
|
|
210
|
|
|
/** @var FileElement $file */ |
|
211
|
|
|
$file = $this->fixture->create(new SourceFile\LocalFile(__FILE__), $containerMock); |
|
212
|
|
|
|
|
213
|
|
|
$this->assertEquals(__FILE__, $file->getPath()); |
|
214
|
|
|
$this->assertArrayHasKey('\myInterface', $file->getInterfaces()); |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* @covers ::create |
|
219
|
|
|
*/ |
|
220
|
|
|
public function testFileWithTrait() |
|
221
|
|
|
{ |
|
222
|
|
|
$traitNode = new TraitNode('\myTrait'); |
|
223
|
|
|
$this->nodesFactoryMock->shouldReceive('create') |
|
224
|
|
|
->with(file_get_contents(__FILE__)) |
|
225
|
|
|
->andReturn( |
|
226
|
|
|
[ |
|
227
|
|
|
$traitNode, |
|
228
|
|
|
] |
|
229
|
|
|
); |
|
230
|
|
|
$strategyMock = m::mock(ProjectFactoryStrategy::class); |
|
231
|
|
|
$containerMock = m::mock(StrategyContainer::class); |
|
232
|
|
|
|
|
233
|
|
|
$strategyMock->shouldReceive('create') |
|
234
|
|
|
->with($traitNode, $containerMock, m::any()) |
|
235
|
|
|
->andReturn(new TraitElement(new Fqsen('\myTrait'))); |
|
236
|
|
|
|
|
237
|
|
|
$containerMock->shouldReceive('findMatching') |
|
238
|
|
|
->with($traitNode) |
|
239
|
|
|
->andReturn($strategyMock); |
|
240
|
|
|
|
|
241
|
|
|
/** @var FileElement $file */ |
|
242
|
|
|
$file = $this->fixture->create(new SourceFile\LocalFile(__FILE__), $containerMock); |
|
243
|
|
|
|
|
244
|
|
|
$this->assertEquals(__FILE__, $file->getPath()); |
|
245
|
|
|
$this->assertArrayHasKey('\myTrait', $file->getTraits()); |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* @covers ::create |
|
250
|
|
|
*/ |
|
251
|
|
|
public function testMiddlewareIsExecuted() |
|
252
|
|
|
{ |
|
253
|
|
|
$file = new FileElement('aa', __FILE__); |
|
254
|
|
|
$this->nodesFactoryMock->shouldReceive('create') |
|
255
|
|
|
->with(file_get_contents(__FILE__)) |
|
256
|
|
|
->andReturn([]); |
|
257
|
|
|
|
|
258
|
|
|
$middleware = m::mock(Middleware::class); |
|
259
|
|
|
$middleware->shouldReceive('execute') |
|
260
|
|
|
->once() |
|
261
|
|
|
->andReturn($file); |
|
262
|
|
|
$fixture = new File($this->nodesFactoryMock, [$middleware]); |
|
263
|
|
|
|
|
264
|
|
|
$containerMock = m::mock(StrategyContainer::class); |
|
265
|
|
|
$result = $fixture->create(new SourceFile\LocalFile(__FILE__), $containerMock); |
|
266
|
|
|
|
|
267
|
|
|
$this->assertSame($result, $file); |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
/** |
|
271
|
|
|
* @expectedException \InvalidArgumentException |
|
272
|
|
|
*/ |
|
273
|
|
|
public function testMiddlewareIsChecked() |
|
274
|
|
|
{ |
|
275
|
|
|
new File($this->nodesFactoryMock, [new \stdClass()]); |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
/** |
|
279
|
|
|
* @covers ::create |
|
280
|
|
|
*/ |
|
281
|
|
|
public function testFileDocBlockWithNamespace() |
|
282
|
|
|
{ |
|
283
|
|
|
$docBlockNode = new DocBlockNode(''); |
|
284
|
|
|
$docBlockDescriptor = new DocBlockDescriptor(''); |
|
285
|
|
|
|
|
286
|
|
|
$namespaceNode = new NamespaceNode(new Name('mySpace')); |
|
287
|
|
|
$namespaceNode->fqsen = new Fqsen('\mySpace'); |
|
288
|
|
|
$namespaceNode->setAttribute('comments', [$docBlockNode]); |
|
289
|
|
|
|
|
290
|
|
|
$this->nodesFactoryMock->shouldReceive('create') |
|
291
|
|
|
->with(file_get_contents(__FILE__)) |
|
292
|
|
|
->andReturn([$namespaceNode]); |
|
293
|
|
|
|
|
294
|
|
|
$strategyMock = m::mock(ProjectFactoryStrategy::class); |
|
295
|
|
|
$containerMock = m::mock(StrategyContainer::class); |
|
296
|
|
|
|
|
297
|
|
|
$strategyMock->shouldReceive('create') |
|
298
|
|
|
->with($docBlockNode, $containerMock, m::any()) |
|
299
|
|
|
->andReturn($docBlockDescriptor); |
|
300
|
|
|
|
|
301
|
|
|
$containerMock->shouldReceive('findMatching') |
|
302
|
|
|
->with($docBlockNode) |
|
303
|
|
|
->andReturn($strategyMock); |
|
304
|
|
|
|
|
305
|
|
|
/** @var FileElement $file */ |
|
306
|
|
|
$file = $this->fixture->create(new SourceFile\LocalFile(__FILE__), $containerMock); |
|
307
|
|
|
|
|
308
|
|
|
$this->assertSame($docBlockDescriptor, $file->getDocBlock()); |
|
309
|
|
|
} |
|
310
|
|
|
|
|
311
|
|
|
/** |
|
312
|
|
|
* @covers ::create |
|
313
|
|
|
*/ |
|
314
|
|
|
public function testFileDocBlockWithClass() |
|
315
|
|
|
{ |
|
316
|
|
|
$docBlockNode = new DocBlockNode(''); |
|
317
|
|
|
$docBlockDescriptor = new DocBlockDescriptor(''); |
|
318
|
|
|
|
|
319
|
|
|
$classNode = new ClassNode('myClass'); |
|
320
|
|
|
$classNode->setAttribute('comments', [$docBlockNode, new DocBlockNode('')]); |
|
321
|
|
|
|
|
322
|
|
|
$this->nodesFactoryMock->shouldReceive('create') |
|
323
|
|
|
->with(file_get_contents(__FILE__)) |
|
324
|
|
|
->andReturn([$classNode]); |
|
325
|
|
|
|
|
326
|
|
|
$strategyMock = m::mock(ProjectFactoryStrategy::class); |
|
327
|
|
|
$containerMock = m::mock(StrategyContainer::class); |
|
328
|
|
|
|
|
329
|
|
|
$strategyMock->shouldReceive('create') |
|
330
|
|
|
->once() |
|
331
|
|
|
->with($classNode, $containerMock, m::any()) |
|
332
|
|
|
->andReturn(new ClassElement(new Fqsen('\myClass'))); |
|
333
|
|
|
$containerMock->shouldReceive('findMatching') |
|
334
|
|
|
->with($classNode) |
|
335
|
|
|
->andReturn($strategyMock); |
|
336
|
|
|
|
|
337
|
|
|
$strategyMock->shouldReceive('create') |
|
338
|
|
|
->with($docBlockNode, $containerMock, m::any()) |
|
339
|
|
|
->andReturn($docBlockDescriptor); |
|
340
|
|
|
|
|
341
|
|
|
$containerMock->shouldReceive('findMatching') |
|
342
|
|
|
->with($docBlockNode) |
|
343
|
|
|
->andReturn($strategyMock); |
|
344
|
|
|
|
|
345
|
|
|
/** @var FileElement $file */ |
|
346
|
|
|
$file = $this->fixture->create(new SourceFile\LocalFile(__FILE__), $containerMock); |
|
347
|
|
|
|
|
348
|
|
|
$this->assertSame($docBlockDescriptor, $file->getDocBlock()); |
|
349
|
|
|
} |
|
350
|
|
|
|
|
351
|
|
|
/** |
|
352
|
|
|
* @covers ::create |
|
353
|
|
|
*/ |
|
354
|
|
|
public function testFileDocBlockWithComments() |
|
355
|
|
|
{ |
|
356
|
|
|
$docBlockNode = new DocBlockNode(''); |
|
357
|
|
|
$docBlockDescriptor = new DocBlockDescriptor(''); |
|
358
|
|
|
|
|
359
|
|
|
$namespaceNode = new NamespaceNode(new Name('mySpace')); |
|
360
|
|
|
$namespaceNode->fqsen = new Fqsen('\mySpace'); |
|
361
|
|
|
$namespaceNode->setAttribute('comments', [new CommentNode('@codingStandardsIgnoreStart'), $docBlockNode]); |
|
362
|
|
|
|
|
363
|
|
|
$this->nodesFactoryMock->shouldReceive('create') |
|
364
|
|
|
->with(file_get_contents(__FILE__)) |
|
365
|
|
|
->andReturn([$namespaceNode]); |
|
366
|
|
|
|
|
367
|
|
|
$strategyMock = m::mock(ProjectFactoryStrategy::class); |
|
368
|
|
|
$containerMock = m::mock(StrategyContainer::class); |
|
369
|
|
|
|
|
370
|
|
|
$strategyMock->shouldReceive('create') |
|
371
|
|
|
->with($docBlockNode, $containerMock, m::any()) |
|
372
|
|
|
->andReturn($docBlockDescriptor); |
|
373
|
|
|
|
|
374
|
|
|
$containerMock->shouldReceive('findMatching') |
|
375
|
|
|
->with($docBlockNode) |
|
376
|
|
|
->andReturn($strategyMock); |
|
377
|
|
|
|
|
378
|
|
|
/** @var FileElement $file */ |
|
379
|
|
|
$file = $this->fixture->create(new SourceFile\LocalFile(__FILE__), $containerMock); |
|
380
|
|
|
|
|
381
|
|
|
$this->assertSame($docBlockDescriptor, $file->getDocBlock()); |
|
382
|
|
|
} |
|
383
|
|
|
} |
|
384
|
|
|
|