Completed
Push — master ( c19ab7...0a9b8e )
by Mike
09:32
created

testFactoryMethodWithEmojisToVerifyMultiByteBehaviour()   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 Mockery as m;
17
use phpDocumentor\Reflection\DocBlock\Description;
18
use phpDocumentor\Reflection\DocBlock\DescriptionFactory;
19
use phpDocumentor\Reflection\TypeResolver;
20
use phpDocumentor\Reflection\Types\Context;
21
use phpDocumentor\Reflection\Types\String_;
22
use PHPUnit\Framework\TestCase;
23
24
/**
25
 * @coversDefaultClass \phpDocumentor\Reflection\DocBlock\Tags\Return_
26
 * @covers ::<private>
27
 */
28
class ReturnTest extends TestCase
29
{
30
    /**
31
     * Call Mockery::close after each test.
32
     */
33
    public function tearDown() : void
34
    {
35
        m::close();
36
    }
37
38
    /**
39
     * @uses   \phpDocumentor\Reflection\DocBlock\Tags\Return_::__construct
40
     * @uses   \phpDocumentor\Reflection\DocBlock\Description
41
     *
42
     * @covers \phpDocumentor\Reflection\DocBlock\Tags\BaseTag::getName
43
     */
44
    public function testIfCorrectTagNameIsReturned() : void
45
    {
46
        $fixture = new Return_(new String_(), new Description('Description'));
47
48
        $this->assertSame('return', $fixture->getName());
49
    }
50
51
    /**
52
     * @uses   \phpDocumentor\Reflection\DocBlock\Tags\Return_::__construct
53
     * @uses   \phpDocumentor\Reflection\DocBlock\Tags\Return_::__toString
54
     * @uses   \phpDocumentor\Reflection\DocBlock\Tags\Formatter\PassthroughFormatter
55
     * @uses   \phpDocumentor\Reflection\DocBlock\Description
56
     *
57
     * @covers \phpDocumentor\Reflection\DocBlock\Tags\BaseTag::render
58
     * @covers \phpDocumentor\Reflection\DocBlock\Tags\BaseTag::getName
59
     */
60
    public function testIfTagCanBeRenderedUsingDefaultFormatter() : void
61
    {
62
        $fixture = new Return_(new String_(), new Description('Description'));
63
64
        $this->assertSame('@return string Description', $fixture->render());
65
    }
66
67
    /**
68
     * @uses   \phpDocumentor\Reflection\DocBlock\Tags\Return_::__construct
69
     * @uses   \phpDocumentor\Reflection\DocBlock\Description
70
     *
71
     * @covers \phpDocumentor\Reflection\DocBlock\Tags\BaseTag::render
72
     */
73
    public function testIfTagCanBeRenderedUsingSpecificFormatter() : void
74
    {
75
        $fixture = new Return_(new String_(), new Description('Description'));
76
77
        $formatter = m::mock(Formatter::class);
78
        $formatter->shouldReceive('format')->with($fixture)->andReturn('Rendered output');
79
80
        $this->assertSame('Rendered output', $fixture->render($formatter));
81
    }
82
83
    /**
84
     * @covers ::__construct
85
     * @covers ::getType
86
     */
87
    public function testHasType() : void
88
    {
89
        $expected = new String_();
90
91
        $fixture = new Return_($expected);
92
93
        $this->assertSame($expected, $fixture->getType());
94
    }
95
96
    /**
97
     * @uses   \phpDocumentor\Reflection\DocBlock\Description
98
     *
99
     * @covers ::__construct
100
     * @covers \phpDocumentor\Reflection\DocBlock\Tags\BaseTag::getDescription
101
     */
102
    public function testHasDescription() : void
103
    {
104
        $expected = new Description('Description');
105
106
        $fixture = new Return_(new String_(), $expected);
107
108
        $this->assertSame($expected, $fixture->getDescription());
109
    }
110
111
    /**
112
     * @uses   \phpDocumentor\Reflection\DocBlock\Description
113
     *
114
     * @covers ::__construct
115
     * @covers ::__toString
116
     */
117
    public function testStringRepresentationIsReturned() : void
118
    {
119
        $fixture = new Return_(new String_(), new Description('Description'));
120
121
        $this->assertSame('string Description', (string) $fixture);
122
    }
123
124
    /**
125
     * @uses \phpDocumentor\Reflection\DocBlock\Tags\Return_::<public>
126
     * @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory
127
     * @uses \phpDocumentor\Reflection\TypeResolver
128
     * @uses \phpDocumentor\Reflection\DocBlock\Description
129
     * @uses \phpDocumentor\Reflection\Types\String_
130
     * @uses \phpDocumentor\Reflection\Types\Context
131
     *
132
     * @covers ::create
133
     */
134
    public function testFactoryMethod() : void
135
    {
136
        $descriptionFactory = m::mock(DescriptionFactory::class);
137
        $resolver           = new TypeResolver();
138
        $context            = new Context('');
139
140
        $type        = new String_();
141
        $description = new Description('My Description');
142
        $descriptionFactory->shouldReceive('create')->with('My Description', $context)->andReturn($description);
143
144
        $fixture = Return_::create('string My Description', $resolver, $descriptionFactory, $context);
145
146
        $this->assertSame('string My Description', (string) $fixture);
147
        $this->assertEquals($type, $fixture->getType());
148
        $this->assertSame($description, $fixture->getDescription());
149
    }
150
151
    /**
152
     * This test checks whether a braces in a Type are allowed.
153
     *
154
     * 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
155
     * could purely rely on spaces to split the individual parts of the body of a tag; but when there is a type in play
156
     * we now need to check for braces.
157
     *
158
     * This test tests whether an error occurs demonstrating that the braces were taken into account; this test is still
159
     * expected to produce an exception because the TypeResolver does not support generics.
160
     *
161
     * @covers ::create
162
     * @uses \phpDocumentor\Reflection\DocBlock\Tags\Return_::<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
    public function testFactoryMethodWithGenericWithSpace()
170
    {
171
        $this->expectException(\InvalidArgumentException::class);
172
        $this->expectExceptionMessage('"\array<string, string>" is not a valid Fqsen.');
173
174
        $descriptionFactory = m::mock(DescriptionFactory::class);
175
        $resolver = new TypeResolver();
176
        $context = new Context('');
177
178
        $description = new Description('My Description');
179
        $descriptionFactory->shouldReceive('create')
180
            ->with('My Description', $context)
181
            ->andReturn($description);
182
183
        Return_::create('array<string, string> My Description', $resolver, $descriptionFactory, $context);
184
    }
185
186
    /**
187
     * @see self::testFactoryMethodWithGenericWithSpace()
188
     *
189
     * @covers ::create
190
     * @uses \phpDocumentor\Reflection\DocBlock\Tags\Return_::<public>
191
     * @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory
192
     * @uses \phpDocumentor\Reflection\TypeResolver
193
     * @uses \phpDocumentor\Reflection\DocBlock\Description
194
     * @uses \phpDocumentor\Reflection\Types\String_
195
     * @uses \phpDocumentor\Reflection\Types\Context
196
     */
197
    public function testFactoryMethodWithGenericWithSpaceAndAddedEmojisToVerifyMultiByteBehaviour()
198
    {
199
        $this->expectException(\InvalidArgumentException::class);
200
        $this->expectExceptionMessage('"\array😁<string,😁 😁string>" is not a valid Fqsen.');
201
202
        $descriptionFactory = m::mock(DescriptionFactory::class);
203
        $resolver = new TypeResolver();
204
        $context = new Context('');
205
206
        $description = new Description('My Description');
207
        $descriptionFactory->shouldReceive('create')
208
            ->with('My Description', $context)
209
            ->andReturn($description);
210
211
        Return_::create('array😁<string,😁 😁string> My Description', $resolver, $descriptionFactory, $context);
212
    }
213
214
    /**
215
     * @covers ::create
216
     * @uses \phpDocumentor\Reflection\DocBlock\Tags\Return_::<public>
217
     * @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory
218
     * @uses \phpDocumentor\Reflection\TypeResolver
219
     * @uses \phpDocumentor\Reflection\DocBlock\Description
220
     * @uses \phpDocumentor\Reflection\Types\String_
221
     * @uses \phpDocumentor\Reflection\Types\Context
222
     */
223
    public function testFactoryMethodWithEmojisToVerifyMultiByteBehaviour()
224
    {
225
        $descriptionFactory = m::mock(DescriptionFactory::class);
226
        $resolver = new TypeResolver();
227
        $context = new Context('');
228
229
        $description = new Description('My Description');
230
        $descriptionFactory->shouldReceive('create')
231
            ->with('My Description', $context)
232
            ->andReturn($description);
233
234
        $fixture = Return_::create('\My😁Class My Description', $resolver, $descriptionFactory, $context);
235
236
        $this->assertSame('\My😁Class My Description', (string) $fixture);
237
        $this->assertEquals('\My😁Class', $fixture->getType());
238
        $this->assertSame($description, $fixture->getDescription());
239
    }
240
241
    /**
242
     * @covers ::create
243
     */
244
    public function testFactoryMethodFailsIfBodyIsNotEmpty() : void
245
    {
246
        $this->expectException('InvalidArgumentException');
247
        $this->assertNull(Return_::create(''));
248
    }
249
250
    /**
251
     * @covers ::create
252
     */
253
    public function testFactoryMethodFailsIfResolverIsNull() : void
254
    {
255
        $this->expectException('InvalidArgumentException');
256
        Return_::create('body');
257
    }
258
259
    /**
260
     * @covers ::create
261
     */
262
    public function testFactoryMethodFailsIfDescriptionFactoryIsNull() : void
263
    {
264
        $this->expectException('InvalidArgumentException');
265
        Return_::create('body', new TypeResolver());
266
    }
267
}
268