1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of phpDocumentor. |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
* |
9
|
|
|
* @copyright 2010-2018 Mike van Riel<[email protected]> |
10
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT |
11
|
|
|
* @link http://phpdoc.org |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace phpDocumentor\Reflection; |
15
|
|
|
|
16
|
|
|
use Mockery as m; |
17
|
|
|
use phpDocumentor\Reflection\DocBlock\Description; |
18
|
|
|
use phpDocumentor\Reflection\DocBlock\DescriptionFactory; |
19
|
|
|
use phpDocumentor\Reflection\DocBlock\Tag; |
20
|
|
|
use phpDocumentor\Reflection\DocBlock\TagFactory; |
21
|
|
|
use phpDocumentor\Reflection\DocBlock\Tags\Param; |
22
|
|
|
use phpDocumentor\Reflection\Types\Context; |
23
|
|
|
use PHPUnit\Framework\TestCase; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @coversDefaultClass phpDocumentor\Reflection\DocBlockFactory |
27
|
|
|
* @covers ::<private> |
28
|
|
|
* @uses \Webmozart\Assert\Assert |
29
|
|
|
* @uses phpDocumentor\Reflection\DocBlock |
30
|
|
|
*/ |
31
|
|
|
class DocBlockFactoryTest extends TestCase |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* Call Mockery::close after each test. |
35
|
|
|
*/ |
36
|
|
|
public function tearDown(): void |
37
|
|
|
{ |
38
|
|
|
m::close(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @covers ::__construct |
43
|
|
|
* @covers ::createInstance |
44
|
|
|
* @uses \phpDocumentor\Reflection\DocBlock\StandardTagFactory |
45
|
|
|
* @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory |
46
|
|
|
*/ |
47
|
|
|
public function testCreateFactoryUsingFactoryMethod(): void |
48
|
|
|
{ |
49
|
|
|
$fixture = DocBlockFactory::createInstance(); |
50
|
|
|
|
51
|
|
|
$this->assertInstanceOf(DocBlockFactory::class, $fixture); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @covers ::__construct |
56
|
|
|
* @covers ::create |
57
|
|
|
* @uses phpDocumentor\Reflection\DocBlock\Description |
58
|
|
|
*/ |
59
|
|
|
public function testCreateDocBlockFromReflection(): void |
60
|
|
|
{ |
61
|
|
|
$fixture = new DocBlockFactory(m::mock(DescriptionFactory::class), m::mock(TagFactory::class)); |
62
|
|
|
|
63
|
|
|
$docBlock = '/** This is a DocBlock */'; |
64
|
|
|
$classReflector = m::mock(\ReflectionClass::class); |
65
|
|
|
$classReflector->shouldReceive('getDocComment')->andReturn($docBlock); |
66
|
|
|
$docblock = $fixture->create($classReflector); |
67
|
|
|
|
68
|
|
|
$this->assertInstanceOf(DocBlock::class, $docblock); |
69
|
|
|
$this->assertSame('This is a DocBlock', $docblock->getSummary()); |
70
|
|
|
$this->assertEquals(new Description(''), $docblock->getDescription()); |
71
|
|
|
$this->assertSame([], $docblock->getTags()); |
72
|
|
|
$this->assertEquals(new Context(''), $docblock->getContext()); |
73
|
|
|
$this->assertNull($docblock->getLocation()); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @covers ::__construct |
78
|
|
|
* @covers ::create |
79
|
|
|
* @uses phpDocumentor\Reflection\DocBlock\Description |
80
|
|
|
*/ |
81
|
|
|
public function testCreateDocBlockFromStringWithDocComment(): void |
82
|
|
|
{ |
83
|
|
|
$fixture = new DocBlockFactory(m::mock(DescriptionFactory::class), m::mock(TagFactory::class)); |
84
|
|
|
|
85
|
|
|
$docblock = $fixture->create('/** This is a DocBlock */'); |
86
|
|
|
|
87
|
|
|
$this->assertInstanceOf(DocBlock::class, $docblock); |
88
|
|
|
$this->assertSame('This is a DocBlock', $docblock->getSummary()); |
89
|
|
|
$this->assertEquals(new Description(''), $docblock->getDescription()); |
90
|
|
|
$this->assertSame([], $docblock->getTags()); |
91
|
|
|
$this->assertEquals(new Context(''), $docblock->getContext()); |
92
|
|
|
$this->assertNull($docblock->getLocation()); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @covers ::create |
97
|
|
|
* @covers ::__construct |
98
|
|
|
* @uses phpDocumentor\Reflection\DocBlock\Description |
99
|
|
|
*/ |
100
|
|
|
public function testCreateDocBlockFromStringWithoutDocComment(): void |
101
|
|
|
{ |
102
|
|
|
$fixture = new DocBlockFactory(m::mock(DescriptionFactory::class), m::mock(TagFactory::class)); |
103
|
|
|
|
104
|
|
|
$docblock = $fixture->create('This is a DocBlock'); |
105
|
|
|
|
106
|
|
|
$this->assertInstanceOf(DocBlock::class, $docblock); |
107
|
|
|
$this->assertSame('This is a DocBlock', $docblock->getSummary()); |
108
|
|
|
$this->assertEquals(new Description(''), $docblock->getDescription()); |
109
|
|
|
$this->assertSame([], $docblock->getTags()); |
110
|
|
|
$this->assertEquals(new Context(''), $docblock->getContext()); |
111
|
|
|
$this->assertNull($docblock->getLocation()); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @covers ::__construct |
116
|
|
|
* @covers ::create |
117
|
|
|
* @uses phpDocumentor\Reflection\DocBlock\DescriptionFactory |
118
|
|
|
* @uses phpDocumentor\Reflection\DocBlock\Description |
119
|
|
|
* @dataProvider provideSummaryAndDescriptions |
120
|
|
|
*/ |
121
|
|
|
public function testSummaryAndDescriptionAreSeparated($given, $summary, $description): void |
122
|
|
|
{ |
123
|
|
|
$tagFactory = m::mock(TagFactory::class); |
124
|
|
|
$fixture = new DocBlockFactory(new DescriptionFactory($tagFactory), $tagFactory); |
125
|
|
|
|
126
|
|
|
$docblock = $fixture->create($given); |
127
|
|
|
|
128
|
|
|
$this->assertSame($summary, $docblock->getSummary()); |
129
|
|
|
$this->assertEquals(new Description($description), $docblock->getDescription()); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @covers ::__construct |
134
|
|
|
* @covers ::create |
135
|
|
|
* @uses phpDocumentor\Reflection\DocBlock\DescriptionFactory |
136
|
|
|
* @uses phpDocumentor\Reflection\DocBlock\Description |
137
|
|
|
*/ |
138
|
|
|
public function testDescriptionsRetainFormatting(): void |
139
|
|
|
{ |
140
|
|
|
$tagFactory = m::mock(TagFactory::class); |
141
|
|
|
$fixture = new DocBlockFactory(new DescriptionFactory($tagFactory), $tagFactory); |
142
|
|
|
|
143
|
|
|
$given = <<<DOCBLOCK |
144
|
|
|
/** |
145
|
|
|
* This is a summary. |
146
|
|
|
* This is a multiline Description |
147
|
|
|
* that contains a code block. |
148
|
|
|
* |
149
|
|
|
* See here: a CodeBlock |
150
|
|
|
*/ |
151
|
|
|
DOCBLOCK; |
152
|
|
|
|
153
|
|
|
$description = <<<DESCRIPTION |
154
|
|
|
This is a multiline Description |
155
|
|
|
that contains a code block. |
156
|
|
|
|
157
|
|
|
See here: a CodeBlock |
158
|
|
|
DESCRIPTION; |
159
|
|
|
|
160
|
|
|
$docblock = $fixture->create($given); |
161
|
|
|
|
162
|
|
|
$this->assertEquals(new Description($description), $docblock->getDescription()); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @covers ::__construct |
167
|
|
|
* @covers ::create |
168
|
|
|
* @uses phpDocumentor\Reflection\DocBlock\DescriptionFactory |
169
|
|
|
* @uses phpDocumentor\Reflection\DocBlock\Description |
170
|
|
|
*/ |
171
|
|
|
public function testTagsAreInterpretedUsingFactory(): void |
172
|
|
|
{ |
173
|
|
|
$tagString = <<<TAG |
174
|
|
|
@author Mike van Riel <[email protected]> This is with |
175
|
|
|
multiline description. |
176
|
|
|
TAG; |
177
|
|
|
|
178
|
|
|
$tag = m::mock(Tag::class); |
179
|
|
|
$tagFactory = m::mock(TagFactory::class); |
180
|
|
|
$tagFactory->shouldReceive('create')->with($tagString, m::type(Context::class))->andReturn($tag); |
181
|
|
|
|
182
|
|
|
$fixture = new DocBlockFactory(new DescriptionFactory($tagFactory), $tagFactory); |
183
|
|
|
|
184
|
|
|
$given = <<<DOCBLOCK |
185
|
|
|
/** |
186
|
|
|
* This is a summary. |
187
|
|
|
* |
188
|
|
|
* @author Mike van Riel <[email protected]> This is with |
189
|
|
|
* multiline description. |
190
|
|
|
*/ |
191
|
|
|
DOCBLOCK; |
192
|
|
|
|
193
|
|
|
$docblock = $fixture->create($given, new Context('')); |
194
|
|
|
|
195
|
|
|
$this->assertEquals([$tag], $docblock->getTags()); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
public function provideSummaryAndDescriptions() |
199
|
|
|
{ |
200
|
|
|
return [ |
201
|
|
|
['This is a DocBlock', 'This is a DocBlock', ''], |
202
|
|
|
[ |
203
|
|
|
'This is a DocBlock. This should still be summary.', |
204
|
|
|
'This is a DocBlock. This should still be summary.', |
205
|
|
|
'', |
206
|
|
|
], |
207
|
|
|
[ |
208
|
|
|
<<<DOCBLOCK |
209
|
|
|
This is a DocBlock. |
210
|
|
|
This should be a Description. |
211
|
|
|
DOCBLOCK |
212
|
|
|
, |
213
|
|
|
'This is a DocBlock.', |
214
|
|
|
'This should be a Description.', |
215
|
|
|
], |
216
|
|
|
[ |
217
|
|
|
<<<DOCBLOCK |
218
|
|
|
This is a |
219
|
|
|
multiline Summary. |
220
|
|
|
This should be a Description. |
221
|
|
|
DOCBLOCK |
222
|
|
|
, |
223
|
|
|
"This is a\nmultiline Summary.", |
224
|
|
|
'This should be a Description.', |
225
|
|
|
], |
226
|
|
|
[ |
227
|
|
|
<<<DOCBLOCK |
228
|
|
|
This is a Summary without dot but with a whiteline |
229
|
|
|
|
230
|
|
|
This should be a Description. |
231
|
|
|
DOCBLOCK |
232
|
|
|
, |
233
|
|
|
'This is a Summary without dot but with a whiteline', |
234
|
|
|
'This should be a Description.', |
235
|
|
|
], |
236
|
|
|
[ |
237
|
|
|
<<<DOCBLOCK |
238
|
|
|
This is a Summary with dot and with a whiteline. |
239
|
|
|
|
240
|
|
|
This should be a Description. |
241
|
|
|
DOCBLOCK |
242
|
|
|
, |
243
|
|
|
'This is a Summary with dot and with a whiteline.', |
244
|
|
|
'This should be a Description.', |
245
|
|
|
], |
246
|
|
|
]; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* @covers ::__construct |
251
|
|
|
* @covers ::create |
252
|
|
|
* |
253
|
|
|
* @uses phpDocumentor\Reflection\DocBlock\DescriptionFactory |
254
|
|
|
* @uses phpDocumentor\Reflection\DocBlock\Description |
255
|
|
|
* @uses phpDocumentor\Reflection\Types\Context |
256
|
|
|
* @uses phpDocumentor\Reflection\DocBlock\Tags\Param |
257
|
|
|
*/ |
258
|
|
|
public function testTagsWithContextNamespace(): void |
259
|
|
|
{ |
260
|
|
|
$tagFactoryMock = m::mock(TagFactory::class); |
261
|
|
|
$fixture = new DocBlockFactory(m::mock(DescriptionFactory::class), $tagFactoryMock); |
262
|
|
|
$context = new Context('MyNamespace'); |
263
|
|
|
|
264
|
|
|
$tagFactoryMock->shouldReceive('create')->with(m::any(), $context)->andReturn(new Param('param')); |
265
|
|
|
$docblock = $fixture->create('/** @param MyType $param */', $context); |
266
|
|
|
|
267
|
|
|
$this->assertInstanceOf(DocBlock::class, $docblock); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* @covers ::__construct |
272
|
|
|
* @covers ::create |
273
|
|
|
* |
274
|
|
|
* @uses phpDocumentor\Reflection\DocBlock\DescriptionFactory |
275
|
|
|
* @uses phpDocumentor\Reflection\DocBlock\Description |
276
|
|
|
*/ |
277
|
|
|
public function testTagsAreFilteredForNullValues(): void |
278
|
|
|
{ |
279
|
|
|
$tagString = <<<TAG |
280
|
|
|
@author Mike van Riel <[email protected]> This is with |
281
|
|
|
multiline description. |
282
|
|
|
TAG; |
283
|
|
|
|
284
|
|
|
$tagFactory = m::mock(TagFactory::class); |
285
|
|
|
$tagFactory->shouldReceive('create')->with($tagString, m::any())->andReturn(null); |
286
|
|
|
|
287
|
|
|
$fixture = new DocBlockFactory(new DescriptionFactory($tagFactory), $tagFactory); |
288
|
|
|
|
289
|
|
|
$given = <<<DOCBLOCK |
290
|
|
|
/** |
291
|
|
|
* This is a summary. |
292
|
|
|
* |
293
|
|
|
* @author Mike van Riel <[email protected]> This is with |
294
|
|
|
* multiline description. |
295
|
|
|
*/ |
296
|
|
|
DOCBLOCK; |
297
|
|
|
|
298
|
|
|
$docblock = $fixture->create($given, new Context('')); |
299
|
|
|
|
300
|
|
|
$this->assertEquals([], $docblock->getTags()); |
301
|
|
|
} |
302
|
|
|
} |
303
|
|
|
|