Completed
Push — master ( 0a9b8e...b66889 )
by Mike
09:47
created

testFactoryMethodWithGenericWithSpace()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
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 InvalidArgumentException;
17
use Mockery as m;
18
use phpDocumentor\Reflection\DocBlock\Description;
19
use phpDocumentor\Reflection\DocBlock\DescriptionFactory;
20
use phpDocumentor\Reflection\TypeResolver;
21
use phpDocumentor\Reflection\Types\Context;
22
use phpDocumentor\Reflection\Types\String_;
23
use PHPUnit\Framework\TestCase;
24
25
/**
26
 * @coversDefaultClass \phpDocumentor\Reflection\DocBlock\Tags\Throws
27
 * @covers ::<private>
28
 */
29
class ThrowsTest 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\Throws::__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 Throws(new String_(), new Description('Description'));
48
49
        $this->assertSame('throws', $fixture->getName());
50
    }
51
52
    /**
53
     * @uses   \phpDocumentor\Reflection\DocBlock\Tags\Throws::__construct
54
     * @uses   \phpDocumentor\Reflection\DocBlock\Tags\Throws::__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 Throws(new String_(), new Description('Description'));
64
65
        $this->assertSame('@throws string Description', $fixture->render());
66
    }
67
68
    /**
69
     * @uses   \phpDocumentor\Reflection\DocBlock\Tags\Throws::__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 Throws(new String_(), 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 ::getType
87
     */
88
    public function testHasType() : void
89
    {
90
        $expected = new String_();
91
92
        $fixture = new Throws($expected);
93
94
        $this->assertSame($expected, $fixture->getType());
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 Throws(new String_(), $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 Throws(new String_(), new Description('Description'));
121
122
        $this->assertSame('string Description', (string) $fixture);
123
    }
124
125
    /**
126
     * @uses \phpDocumentor\Reflection\DocBlock\Tags\Throws::<public>
127
     * @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory
128
     * @uses \phpDocumentor\Reflection\TypeResolver
129
     * @uses \phpDocumentor\Reflection\DocBlock\Description
130
     * @uses \phpDocumentor\Reflection\Types\String_
131
     * @uses \phpDocumentor\Reflection\Types\Context
132
     *
133
     * @covers ::create
134
     */
135
    public function testFactoryMethod() : void
136
    {
137
        $descriptionFactory = m::mock(DescriptionFactory::class);
138
        $resolver           = new TypeResolver();
139
        $context            = new Context('');
140
141
        $type        = new String_();
142
        $description = new Description('My Description');
143
        $descriptionFactory->shouldReceive('create')->with('My Description', $context)->andReturn($description);
144
145
        $fixture = Throws::create('string My Description', $resolver, $descriptionFactory, $context);
146
147
        $this->assertSame('string My Description', (string) $fixture);
148
        $this->assertEquals($type, $fixture->getType());
149
        $this->assertSame($description, $fixture->getDescription());
150
    }
151
152
    /**
153
     * This test checks whether a braces in a Type are allowed.
154
     *
155
     * The advent of generics poses a few issues, one of them is that spaces can now be part of a type. In the past we
156
     * could purely rely on spaces to split the individual parts of the body of a tag; but when there is a type in play
157
     * we now need to check for braces.
158
     *
159
     * This test tests whether an error occurs demonstrating that the braces were taken into account; this test is still
160
     * expected to produce an exception because the TypeResolver does not support generics.
161
     *
162
     * @uses \phpDocumentor\Reflection\DocBlock\Tags\Throws::<public>
163
     * @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory
164
     * @uses \phpDocumentor\Reflection\TypeResolver
165
     * @uses \phpDocumentor\Reflection\DocBlock\Description
166
     * @uses \phpDocumentor\Reflection\Types\String_
167
     * @uses \phpDocumentor\Reflection\Types\Context
168
     *
169
     * @covers ::create
170
     */
171
    public function testFactoryMethodWithGenericWithSpace() : void
172
    {
173
        $descriptionFactory = m::mock(DescriptionFactory::class);
174
        $resolver           = new TypeResolver();
175
        $context            = new Context('');
176
177
        $description = new Description('My Description');
178
        $descriptionFactory->shouldReceive('create')
179
            ->with('My Description', $context)
180
            ->andReturn($description);
181
182
        $fixture = Throws::create('array<string, string> My Description', $resolver, $descriptionFactory, $context);
183
184
        $this->assertSame('array<string,string> My Description', (string) $fixture);
185
        $this->assertEquals('array<string,string>', $fixture->getType());
186
        $this->assertSame($description, $fixture->getDescription());
187
    }
188
189
    /**
190
     * @see  self::testFactoryMethodWithGenericWithSpace()
191
     * @uses \phpDocumentor\Reflection\DocBlock\Tags\Throws::<public>
192
     * @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory
193
     * @uses \phpDocumentor\Reflection\TypeResolver
194
     * @uses \phpDocumentor\Reflection\DocBlock\Description
195
     * @uses \phpDocumentor\Reflection\Types\String_
196
     * @uses \phpDocumentor\Reflection\Types\Context
197
     *
198
     * @covers ::create
199
     */
200
    public function testFactoryMethodWithGenericWithSpaceAndAddedEmojisToVerifyMultiByteBehaviour() : void
201
    {
202
        $this->markTestSkipped('A bug in the TypeResolver breaks this test');
203
        $this->expectException(InvalidArgumentException::class);
204
        $this->expectExceptionMessage('"\array😁<string,😁 😁string>" is not a valid Fqsen.');
205
206
        $descriptionFactory = m::mock(DescriptionFactory::class);
207
        $resolver           = new TypeResolver();
208
        $context            = new Context('');
209
210
        $description = new Description('My Description');
211
        $descriptionFactory->shouldReceive('create')
212
            ->with('My Description', $context)
213
            ->andReturn($description);
214
215
        Throws::create('array😁<string,😁 😁string> My Description', $resolver, $descriptionFactory, $context);
216
    }
217
218
    /**
219
     * @uses \phpDocumentor\Reflection\DocBlock\Tags\Throws::<public>
220
     * @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory
221
     * @uses \phpDocumentor\Reflection\TypeResolver
222
     * @uses \phpDocumentor\Reflection\DocBlock\Description
223
     * @uses \phpDocumentor\Reflection\Types\String_
224
     * @uses \phpDocumentor\Reflection\Types\Context
225
     *
226
     * @covers ::create
227
     */
228
    public function testFactoryMethodWithEmojisToVerifyMultiByteBehaviour() : void
229
    {
230
        $descriptionFactory = m::mock(DescriptionFactory::class);
231
        $resolver           = new TypeResolver();
232
        $context            = new Context('');
233
234
        $description = new Description('My Description');
235
        $descriptionFactory->shouldReceive('create')
236
            ->with('My Description', $context)
237
            ->andReturn($description);
238
239
        $fixture = Throws::create('\My😁Class My Description', $resolver, $descriptionFactory, $context);
240
241
        $this->assertSame('\My😁Class My Description', (string) $fixture);
242
        $this->assertEquals('\My😁Class', $fixture->getType());
243
        $this->assertSame($description, $fixture->getDescription());
244
    }
245
246
    /**
247
     * @covers ::create
248
     */
249
    public function testFactoryMethodFailsIfBodyIsNotEmpty() : void
250
    {
251
        $this->expectException('InvalidArgumentException');
252
        $this->assertNull(Throws::create(''));
253
    }
254
255
    /**
256
     * @covers ::create
257
     */
258
    public function testFactoryMethodFailsIfResolverIsNull() : void
259
    {
260
        $this->expectException('InvalidArgumentException');
261
        Throws::create('body');
262
    }
263
264
    /**
265
     * @covers ::create
266
     */
267
    public function testFactoryMethodFailsIfDescriptionFactoryIsNull() : void
268
    {
269
        $this->expectException('InvalidArgumentException');
270
        Throws::create('body', new TypeResolver());
271
    }
272
}
273