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