Completed
Push — master ( e3324e...f8d350 )
by Jaap
10:07 queued 10s
created

testFactoryMethodWithoutSpaceBeforeClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
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\DocBlock\Tags;
15
16
use Mockery as m;
17
use phpDocumentor\Reflection\DocBlock\Description;
18
use phpDocumentor\Reflection\DocBlock\DescriptionFactory;
19
use phpDocumentor\Reflection\DocBlock\StandardTagFactory;
20
use phpDocumentor\Reflection\Fqsen;
21
use phpDocumentor\Reflection\FqsenResolver;
22
use phpDocumentor\Reflection\Types\Context;
23
use PHPUnit\Framework\TestCase;
24
25
/**
26
 * @coversDefaultClass \phpDocumentor\Reflection\DocBlock\Tags\Uses
27
 * @covers ::<private>
28
 */
29
class UsesTest extends TestCase
30
{
31
    /**
32
     * Call Mockery::close after each test.
33
     */
34
    public function tearDown() : void
35
    {
36
        m::close();
37
    }
38
39
    /**
40
     * @uses   \phpDocumentor\Reflection\DocBlock\Tags\Uses::__construct
41
     * @uses   \phpDocumentor\Reflection\DocBlock\Description
42
     *
43
     * @covers \phpDocumentor\Reflection\DocBlock\Tags\BaseTag::getName
44
     */
45
    public function testIfCorrectTagNameIsReturned() : void
46
    {
47
        $fixture = new Uses(new Fqsen('\DateTime'), new Description('Description'));
48
49
        $this->assertSame('uses', $fixture->getName());
50
    }
51
52
    /**
53
     * @uses   \phpDocumentor\Reflection\DocBlock\Tags\Uses::__construct
54
     * @uses   \phpDocumentor\Reflection\DocBlock\Tags\Uses::__toString
55
     * @uses   \phpDocumentor\Reflection\DocBlock\Tags\Formatter\PassthroughFormatter
56
     * @uses   \phpDocumentor\Reflection\DocBlock\Description
57
     *
58
     * @covers \phpDocumentor\Reflection\DocBlock\Tags\BaseTag::render
59
     * @covers \phpDocumentor\Reflection\DocBlock\Tags\BaseTag::getName
60
     */
61
    public function testIfTagCanBeRenderedUsingDefaultFormatter() : void
62
    {
63
        $fixture = new Uses(new Fqsen('\DateTime'), new Description('Description'));
64
65
        $this->assertSame('@uses \DateTime Description', $fixture->render());
66
    }
67
68
    /**
69
     * @uses   \phpDocumentor\Reflection\DocBlock\Tags\Uses::__construct
70
     * @uses   \phpDocumentor\Reflection\DocBlock\Description
71
     *
72
     * @covers \phpDocumentor\Reflection\DocBlock\Tags\BaseTag::render
73
     */
74
    public function testIfTagCanBeRenderedUsingSpecificFormatter() : void
75
    {
76
        $fixture = new Uses(new Fqsen('\DateTime'), new Description('Description'));
77
78
        $formatter = m::mock(Formatter::class);
79
        $formatter->shouldReceive('format')->with($fixture)->andReturn('Rendered output');
80
81
        $this->assertSame('Rendered output', $fixture->render($formatter));
82
    }
83
84
    /**
85
     * @covers ::__construct
86
     * @covers ::getReference
87
     */
88
    public function testHasReferenceToFqsen() : void
89
    {
90
        $expected = new Fqsen('\DateTime');
91
92
        $fixture = new Uses($expected);
93
94
        $this->assertSame($expected, $fixture->getReference());
95
    }
96
97
    /**
98
     * @uses   \phpDocumentor\Reflection\DocBlock\Description
99
     *
100
     * @covers ::__construct
101
     * @covers \phpDocumentor\Reflection\DocBlock\Tags\BaseTag::getDescription
102
     */
103
    public function testHasDescription() : void
104
    {
105
        $expected = new Description('Description');
106
107
        $fixture = new Uses(new Fqsen('\DateTime'), $expected);
108
109
        $this->assertSame($expected, $fixture->getDescription());
110
    }
111
112
    /**
113
     * @uses   \phpDocumentor\Reflection\DocBlock\Description
114
     *
115
     * @covers ::__construct
116
     * @covers ::__toString
117
     */
118
    public function testStringRepresentationIsReturned() : void
119
    {
120
        $fixture = new Uses(new Fqsen('\DateTime'), new Description('Description'));
121
122
        $this->assertSame('\DateTime Description', (string) $fixture);
123
    }
124
125
    /**
126
     * @uses   \phpDocumentor\Reflection\DocBlock\Description
127
     *
128
     * @covers ::__construct
129
     * @covers ::__toString
130
     */
131
    public function testStringRepresentationIsReturnedWithoutDescription() : void
132
    {
133
        $fixture = new Uses(new Fqsen('\\'));
134
135
        $this->assertSame('\\', (string) $fixture);
136
137
        // ---
138
139
        $fixture = new Uses(new Fqsen('\DateTime'));
140
141
        $this->assertSame('\DateTime', (string) $fixture);
142
143
        // ---
144
145
        $fixture = new Uses(new Fqsen('\DateTime'), new Description(''));
146
147
        $this->assertSame('\DateTime', (string) $fixture);
148
    }
149
150
    /**
151
     * @uses \phpDocumentor\Reflection\DocBlock\Tags\Uses::<public>
152
     * @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory
153
     * @uses \phpDocumentor\Reflection\FqsenResolver
154
     * @uses \phpDocumentor\Reflection\DocBlock\Description
155
     * @uses \phpDocumentor\Reflection\Fqsen
156
     * @uses \phpDocumentor\Reflection\Types\Context
157
     *
158
     * @covers ::create
159
     */
160
    public function testFactoryMethod() : void
161
    {
162
        $descriptionFactory = m::mock(DescriptionFactory::class);
163
        $resolver           = m::mock(FqsenResolver::class);
164
        $context            = new Context('');
165
166
        $fqsen       = new Fqsen('\DateTime');
167
        $description = new Description('My Description');
168
169
        $descriptionFactory
170
            ->shouldReceive('create')->with('My Description', $context)->andReturn($description);
171
        $resolver->shouldReceive('resolve')->with('DateTime', $context)->andReturn($fqsen);
172
173
        $fixture = Uses::create('DateTime My Description', $resolver, $descriptionFactory, $context);
174
175
        $this->assertSame('\DateTime My Description', (string) $fixture);
176
        $this->assertSame($fqsen, $fixture->getReference());
177
        $this->assertSame($description, $fixture->getDescription());
178
    }
179
180
    /**
181
     * @uses \phpDocumentor\Reflection\DocBlock\Tags\See::<public>
182
     * @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory
183
     * @uses \phpDocumentor\Reflection\FqsenResolver
184
     * @uses \phpDocumentor\Reflection\DocBlock\Description
185
     * @uses \phpDocumentor\Reflection\DocBlock\Tags\Reference\Url
186
     * @uses \phpDocumentor\Reflection\Types\Context
187
     *
188
     * @covers ::create
189
     */
190
    public function testFactoryMethodWithoutSpaceBeforeClass() : void
191
    {
192
        $fqsenResolver      = new FqsenResolver();
193
        $tagFactory         = new StandardTagFactory($fqsenResolver);
194
        $descriptionFactory = new DescriptionFactory($tagFactory);
195
        $context            = new Context('');
196
197
        $fixture = Uses::create(
198
            'Foo My Description ',
199
            $fqsenResolver,
200
            $descriptionFactory,
201
            $context
202
        );
203
204
        $this->assertSame('\Foo My Description ', (string) $fixture);
205
        $this->assertInstanceOf(Fqsen::class, $fixture->getReference());
206
        $this->assertSame('\\Foo', (string) $fixture->getReference());
207
        $this->assertSame('My Description ', $fixture->getDescription() . '');
208
    }
209
210
    /**
211
     * @uses \phpDocumentor\Reflection\DocBlock\Tags\See::<public>
212
     * @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory
213
     * @uses \phpDocumentor\Reflection\FqsenResolver
214
     * @uses \phpDocumentor\Reflection\DocBlock\Description
215
     * @uses \phpDocumentor\Reflection\DocBlock\Tags\Reference\Url
216
     * @uses \phpDocumentor\Reflection\Types\Context
217
     *
218
     * @covers ::create
219
     */
220
    public function testFactoryMethodWithSpaceBeforeClass() : void
221
    {
222
        $fqsenResolver      = new FqsenResolver();
223
        $tagFactory         = new StandardTagFactory($fqsenResolver);
224
        $descriptionFactory = new DescriptionFactory($tagFactory);
225
        $context            = new Context('');
226
227
        $fixture = Uses::create(
228
            ' Foo My Description ',
229
            $fqsenResolver,
230
            $descriptionFactory,
231
            $context
232
        );
233
234
        $this->assertSame('\ Foo My Description ', (string) $fixture);
235
        $this->assertInstanceOf(Fqsen::class, $fixture->getReference());
236
        $this->assertSame('\\', (string) $fixture->getReference());
237
        $this->assertSame('Foo My Description ', $fixture->getDescription() . '');
238
    }
239
240
    /**
241
     * @covers ::create
242
     */
243
    public function testFactoryMethodFailsIfBodyIsNotEmpty() : void
244
    {
245
        $this->expectException('InvalidArgumentException');
246
        $this->assertNull(Uses::create(''));
247
    }
248
249
    /**
250
     * @covers ::create
251
     */
252
    public function testFactoryMethodFailsIfResolverIsNull() : void
253
    {
254
        $this->expectException('InvalidArgumentException');
255
        Uses::create('body');
256
    }
257
258
    /**
259
     * @covers ::create
260
     */
261
    public function testFactoryMethodFailsIfDescriptionFactoryIsNull() : void
262
    {
263
        $this->expectException('InvalidArgumentException');
264
        Uses::create('body', new FqsenResolver());
265
    }
266
}
267