Completed
Pull Request — master (#179)
by Jaap
09:18
created

DocBlockFactoryTest::testCreateDocBlockFromReflection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

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