Completed
Pull Request — master (#134)
by Tomáš
01:18
created

DescriptionFactoryTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 158
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 7
dl 0
loc 158
rs 10
c 0
b 0
f 0
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\DocBlock;
14
15
use Mockery as m;
16
use phpDocumentor\Reflection\DocBlock\Tags\Link;
17
use phpDocumentor\Reflection\Types\Context;
18
use PHPUnit\Framework\TestCase;
19
20
/**
21
 * @coversDefaultClass \phpDocumentor\Reflection\DocBlock\DescriptionFactory
22
 * @covers ::<private>
23
 */
24
class DescriptionFactoryTest extends TestCase
25
{
26
    /**
27
     * Call Mockery::close after each test.
28
     */
29
    public function tearDown()
30
    {
31
        m::close();
32
    }
33
34
    /**
35
     * @covers ::__construct
36
     * @covers ::create
37
     * @uses         phpDocumentor\Reflection\DocBlock\Description
38
     * @dataProvider provideSimpleExampleDescriptions
39
     */
40
    public function testDescriptionCanParseASimpleString($contents)
41
    {
42
        $tagFactory = m::mock(TagFactory::class);
43
        $tagFactory->shouldReceive('create')->never();
44
45
        $factory     = new DescriptionFactory($tagFactory);
46
        $description = $factory->create($contents, new Context(''));
47
48
        $this->assertSame($contents, $description->render());
49
    }
50
51
    /**
52
     * @covers ::__construct
53
     * @covers ::create
54
     * @uses         phpDocumentor\Reflection\DocBlock\Description
55
     * @dataProvider provideEscapeSequences
56
     */
57
    public function testEscapeSequences($contents, $expected)
58
    {
59
        $tagFactory = m::mock(TagFactory::class);
60
        $tagFactory->shouldReceive('create')->never();
61
62
        $factory     = new DescriptionFactory($tagFactory);
63
        $description = $factory->create($contents, new Context(''));
64
65
        $this->assertSame($expected, $description->render());
66
    }
67
68
    /**
69
     * @covers ::__construct
70
     * @covers ::create
71
     * @uses   phpDocumentor\Reflection\DocBlock\Description
72
     * @uses   phpDocumentor\Reflection\DocBlock\Tags\Link
73
     * @uses   phpDocumentor\Reflection\DocBlock\Tags\BaseTag
74
     * @uses   phpDocumentor\Reflection\DocBlock\Tags\Formatter\PassthroughFormatter
75
     * @uses   phpDocumentor\Reflection\Types\Context
76
     */
77
    public function testDescriptionCanParseAStringWithInlineTag()
78
    {
79
        $contents   = 'This is text for a {@link http://phpdoc.org/ description} that uses an inline tag.';
80
        $context    = new Context('');
81
        $tagFactory = m::mock(TagFactory::class);
82
        $tagFactory->shouldReceive('create')
83
            ->once()
84
            ->with('@link http://phpdoc.org/ description', $context)
85
            ->andReturn(new Link('http://phpdoc.org/', new Description('description')));
86
87
        $factory     = new DescriptionFactory($tagFactory);
88
        $description = $factory->create($contents, $context);
89
90
        $this->assertSame($contents, $description->render());
91
    }
92
93
    /**
94
     * @covers ::__construct
95
     * @covers ::create
96
     * @uses   phpDocumentor\Reflection\DocBlock\Description
97
     * @uses   phpDocumentor\Reflection\DocBlock\Tags\Link
98
     * @uses   phpDocumentor\Reflection\DocBlock\Tags\BaseTag
99
     * @uses   phpDocumentor\Reflection\DocBlock\Tags\Formatter\PassthroughFormatter
100
     * @uses   phpDocumentor\Reflection\Types\Context
101
     */
102
    public function testDescriptionCanParseAStringStartingWithInlineTag()
103
    {
104
        $contents   = '{@link http://phpdoc.org/ This} is text for a description that starts with an inline tag.';
105
        $context    = new Context('');
106
        $tagFactory = m::mock(TagFactory::class);
107
        $tagFactory->shouldReceive('create')
108
            ->once()
109
            ->with('@link http://phpdoc.org/ This', $context)
110
            ->andReturn(new Link('http://phpdoc.org/', new Description('This')));
111
112
        $factory     = new DescriptionFactory($tagFactory);
113
        $description = $factory->create($contents, $context);
114
115
        $this->assertSame($contents, $description->render());
116
    }
117
118
    /**
119
     * @covers ::__construct
120
     * @covers ::create
121
     * @uses   phpDocumentor\Reflection\DocBlock\Description
122
     */
123
    public function testIfSuperfluousStartingSpacesAreRemoved()
124
    {
125
        $factory         = new DescriptionFactory(m::mock(TagFactory::class));
126
        $descriptionText = <<<DESCRIPTION
127
This is a multiline
128
  description that you commonly
129
  see with tags.
130
131
      It does have a multiline code sample
132
      that should align, no matter what
133
134
  All spaces superfluous spaces on the
135
  second and later lines should be
136
  removed but the code sample should
137
  still be indented.
138
DESCRIPTION;
139
140
        $expectedDescription = <<<DESCRIPTION
141
This is a multiline
142
description that you commonly
143
see with tags.
144
145
    It does have a multiline code sample
146
    that should align, no matter what
147
148
All spaces superfluous spaces on the
149
second and later lines should be
150
removed but the code sample should
151
still be indented.
152
DESCRIPTION;
153
154
        $description = $factory->create($descriptionText, new Context(''));
155
156
        $this->assertSame($expectedDescription, $description->render());
157
    }
158
159
    /**
160
     * Provides a series of example strings that the parser should correctly interpret and return.
161
     *
162
     * @return string[][]
163
     */
164
    public function provideSimpleExampleDescriptions()
165
    {
166
        return [
167
            ['This is text for a description.'],
168
            ['This is text for a description containing { that is literal.'],
169
            ['This is text for a description containing } that is literal.'],
170
            ['This is text for a description with {just a text} that is not a tag.'],
171
        ];
172
    }
173
174
    public function provideEscapeSequences()
175
    {
176
        return [
177
            ['This is text for a description with a {@}.', 'This is text for a description with a @.'],
178
            ['This is text for a description with a {}.', 'This is text for a description with a }.'],
179
        ];
180
    }
181
}
182