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