Completed
Push — develop ( 8eb671...133594 )
by Mike
19:30 queued 09:24
created

phpDocumentor/Descriptor/MethodDescriptorTest.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * phpDocumentor
4
 *
5
 * PHP Version 5.3
6
 *
7
 * @copyright 2010-2018 Mike van Riel / Naenius (http://www.naenius.com)
8
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
9
 * @link      http://phpdoc.org
10
 */
11
12
namespace phpDocumentor\Descriptor;
13
14
use \Mockery as m;
15
use phpDocumentor\Descriptor\Tag\AuthorDescriptor;
16
use phpDocumentor\Descriptor\Tag\ReturnDescriptor;
17
use phpDocumentor\Descriptor\Tag\VersionDescriptor;
18
use phpDocumentor\Reflection\Types\String_;
19
20
/**
21
 * Tests the functionality for the MethodDescriptor class.
22
 *
23
 * @coversDefaultClass \phpDocumentor\Descriptor\MethodDescriptor
24
 * @covers ::<private>
25
 * @covers ::<protected>
26
 */
27
class MethodDescriptorTest extends \Mockery\Adapter\Phpunit\MockeryTestCase
28
{
29
    /** @var MethodDescriptor $fixture */
30
    protected $fixture;
31
32
    /**
33
     * Creates a new (empty) fixture object.
34
     */
35
    protected function setUp()
36
    {
37
        $this->fixture = new MethodDescriptor();
38
        $this->fixture->setName('method');
39
    }
40
41
    /**
42
     * Tests whether all collection objects are properly initialized.
43
     *
44
     * @covers ::__construct
45
     */
46
    public function testInitialize()
47
    {
48
        $this->assertAttributeInstanceOf(Collection::class, 'arguments', $this->fixture);
49
    }
50
51
    /**
52
     * @covers ::setArguments
53
     * @covers ::getArguments
54
     */
55
    public function testSettingAndGettingArguments()
56
    {
57
        $this->assertInstanceOf(Collection::class, $this->fixture->getArguments());
58
59
        $mock = m::mock(Collection::class);
60
        $mock->shouldReceive('getIterator')->andReturn(new \ArrayIterator([]));
0 ignored issues
show
The method andReturn does only exist in Mockery\ExpectationInterface, but not in Mockery\HigherOrderMessage.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
61
62
        $this->fixture->setArguments($mock);
63
64
        $this->assertSame($mock, $this->fixture->getArguments());
65
    }
66
67
    /**
68
     * @covers ::isAbstract
69
     * @covers ::setAbstract
70
     */
71
    public function testSettingAndGettingWhetherMethodIsAbstract()
72
    {
73
        $this->assertFalse($this->fixture->isAbstract());
74
75
        $this->fixture->setAbstract(true);
76
77
        $this->assertTrue($this->fixture->isAbstract());
78
    }
79
80
    /**
81
     * @covers ::isFinal
82
     * @covers ::setFinal
83
     */
84
    public function testSettingAndGettingWhetherMethodIsFinal()
85
    {
86
        $this->assertFalse($this->fixture->isFinal());
87
88
        $this->fixture->setFinal(true);
89
90
        $this->assertTrue($this->fixture->isFinal());
91
    }
92
93
    /**
94
     * @covers ::isStatic
95
     * @covers ::setStatic
96
     */
97
    public function testSettingAndGettingWhetherMethodIsStatic()
98
    {
99
        $this->assertFalse($this->fixture->isStatic());
100
101
        $this->fixture->setStatic(true);
102
103
        $this->assertTrue($this->fixture->isStatic());
104
    }
105
106
    /**
107
     * @covers ::getVisibility
108
     * @covers ::setVisibility
109
     */
110
    public function testSettingAndGettingVisibility()
111
    {
112
        $this->assertEquals('public', $this->fixture->getVisibility());
113
114
        $this->fixture->setVisibility('private');
115
116
        $this->assertEquals('private', $this->fixture->getVisibility());
117
    }
118
119
    /**
120
     * @covers ::getResponse
121
     */
122
    public function testRetrieveReturnTagForResponse()
123
    {
124
        $mock = new ReturnDescriptor('return');
125
        $mock->setType(new String_());
126
127
        $this->assertNull($this->fixture->getResponse()->getType());
128
129
        $this->fixture->getTags()->set('return', new Collection([$mock]));
130
131
        $this->assertSame($mock, $this->fixture->getResponse());
132
    }
133
134
    /**
135
     * @covers ::setReturnType
136
     * @covers ::getResponse
137
     */
138
    public function testGetResponseReturnsReturnType()
139
    {
140
        $returnType = new String_();
141
        $this->fixture->setReturnType($returnType);
142
143
        $this->assertSame($returnType, $this->fixture->getResponse()->getType());
144
    }
145
146
    /**
147
     * @covers ::getFile
148
     */
149
    public function testRetrieveFileAssociatedWithAMethod()
150
    {
151
        // Arrange
152
        $file = $this->whenFixtureIsRelatedToAClassWithFile();
153
154
        // Act
155
        $result = $this->fixture->getFile();
156
157
        // Assert
158
        $this->assertAttributeSame(null, 'fileDescriptor', $this->fixture);
159
        $this->assertSame($file, $result);
160
    }
161
162
    /**
163
     * @covers \phpDocumentor\Descriptor\DescriptorAbstract::getSummary
164
     */
165
    public function testSummaryInheritsWhenNoneIsPresent()
166
    {
167
        // Arrange
168
        $summary = 'This is a summary';
169
        $this->fixture->setSummary(null);
170
        $parentMethod = $this->whenFixtureHasMethodInParentClassWithSameName($this->fixture->getName());
171
        $parentMethod->setSummary($summary);
172
173
        // Act
174
        $result = $this->fixture->getSummary();
175
176
        // Assert
177
        $this->assertSame($summary, $result);
178
    }
179
180
    /**
181
     * @covers ::getSummary
182
     */
183
    public function testSummaryInheritsFromImplementedInterfaceWhenNoneIsPresent()
184
    {
185
        // Arrange
186
        $summary = 'This is a summary';
187
        $this->fixture->setSummary(null);
188
        $parentMethod = $this->whenFixtureHasMethodInImplementedInterfaceWithSameName($this->fixture->getName());
189
        $parentMethod->setSummary($summary);
190
191
        // Act
192
        $result = $this->fixture->getSummary();
193
194
        // Assert
195
        $this->assertSame($summary, $result);
196
    }
197
198
    /**
199
     * @covers ::getDescription
200
     */
201
    public function testDescriptionInheritsWhenNoneIsPresent()
202
    {
203
        // Arrange
204
        $description = 'This is a description';
205
        $this->fixture->setDescription(null);
206
        $parentMethod = $this->whenFixtureHasMethodInParentClassWithSameName($this->fixture->getName());
207
        $parentMethod->setDescription($description);
208
209
        // Act
210
        $result = $this->fixture->getDescription();
211
212
        // Assert
213
        $this->assertSame($description, $result);
214
    }
215
216
    /**
217
     * @covers \phpDocumentor\Descriptor\DescriptorAbstract::getDescription
218
     */
219
    public function testDescriptionInheritsWhenInheritDocIsPresent()
220
    {
221
        // Arrange
222
        $description = 'This is a description';
223
        $this->fixture->setDescription('{@inheritDoc}');
224
        $parentMethod = $this->whenFixtureHasMethodInParentClassWithSameName($this->fixture->getName());
225
        $parentMethod->setDescription($description);
226
227
        // Act
228
        $result = $this->fixture->getDescription();
229
230
        // Assert
231
        $this->assertSame($description, $result);
232
    }
233
234
    /**
235
     * @covers \phpDocumentor\Descriptor\DescriptorAbstract::getDescription
236
     */
237
    public function testDescriptionIsAugmentedWhenInheritDocInlineTagIsPresent()
238
    {
239
        // Arrange
240
        $description = 'This is a description';
241
        $this->fixture->setDescription('Original description {@inheritDoc}');
242
        $parentMethod = $this->whenFixtureHasMethodInParentClassWithSameName($this->fixture->getName());
243
        $parentMethod->setDescription($description);
244
245
        // Act
246
        $result = $this->fixture->getDescription();
247
248
        // Assert
249
        $this->assertSame('Original description ' . $description, $result);
250
    }
251
252
    /**
253
     * @covers ::getReturn
254
     */
255
    public function testReturnTagsInheritWhenNoneArePresent()
256
    {
257
        // Arrange
258
        $returnTagDescriptor = new AuthorDescriptor('return');
259
        $returnCollection = new Collection([$returnTagDescriptor]);
260
        $this->fixture->getTags()->clear();
261
        $parentProperty = $this->whenFixtureHasMethodInParentClassWithSameName($this->fixture->getName());
262
        $parentProperty->getTags()->set('return', $returnCollection);
263
264
        // Act
265
        $result = $this->fixture->getReturn();
266
267
        // Assert
268
        $this->assertSame($returnCollection, $result);
269
    }
270
271
    /**
272
     * @covers ::getParam
273
     */
274
    public function testParamTagsInheritWhenNoneArePresent()
275
    {
276
        // Arrange
277
        $paramTagDescriptor = new AuthorDescriptor('param');
278
        $paramCollection = new Collection([$paramTagDescriptor]);
279
        $this->fixture->getTags()->clear();
280
        $parentProperty = $this->whenFixtureHasMethodInParentClassWithSameName($this->fixture->getName());
281
        $parentProperty->getTags()->set('param', $paramCollection);
282
283
        // Act
284
        $result = $this->fixture->getParam();
285
286
        // Assert
287
        $this->assertSame($paramCollection, $result);
288
    }
289
290
    /**
291
     * @covers ::getAuthor
292
     * @covers \phpDocumentor\Descriptor\DescriptorAbstract::getAuthor
293
     */
294
    public function testAuthorTagsInheritWhenNoneArePresent()
295
    {
296
        // Arrange
297
        $authorTagDescriptor = new AuthorDescriptor('author');
298
        $authorCollection = new Collection([$authorTagDescriptor]);
299
        $this->fixture->getTags()->clear();
300
        $parentProperty = $this->whenFixtureHasMethodInParentClassWithSameName($this->fixture->getName());
301
        $parentProperty->getTags()->set('author', $authorCollection);
302
303
        // Act
304
        $result = $this->fixture->getAuthor();
305
306
        // Assert
307
        $this->assertSame($authorCollection, $result);
308
    }
309
310
    /**
311
     * @covers ::getVersion
312
     * @covers \phpDocumentor\Descriptor\DescriptorAbstract::getVersion
313
     */
314
    public function testVersionTagsInheritWhenNoneArePresent()
315
    {
316
        // Arrange
317
        $versionTagDescriptor = new VersionDescriptor('version');
318
        $versionCollection = new Collection([$versionTagDescriptor]);
319
        $this->fixture->getTags()->clear();
320
        $parentProperty = $this->whenFixtureHasMethodInParentClassWithSameName($this->fixture->getName());
321
        $parentProperty->getTags()->set('version', $versionCollection);
322
323
        // Act
324
        $result = $this->fixture->getVersion();
325
326
        // Assert
327
        $this->assertSame($versionCollection, $result);
328
    }
329
330
    /**
331
     * @covers ::getCopyright
332
     * @covers \phpDocumentor\Descriptor\DescriptorAbstract::getCopyright
333
     */
334
    public function testCopyrightTagsInheritWhenNoneArePresent()
335
    {
336
        // Arrange
337
        $copyrightTagDescriptor = new TagDescriptor('copyright');
338
        $copyrightCollection = new Collection([$copyrightTagDescriptor]);
339
        $this->fixture->getTags()->clear();
340
        $parentProperty = $this->whenFixtureHasMethodInParentClassWithSameName($this->fixture->getName());
341
        $parentProperty->getTags()->set('copyright', $copyrightCollection);
342
343
        // Act
344
        $result = $this->fixture->getCopyright();
345
346
        // Assert
347
        $this->assertSame($copyrightCollection, $result);
348
    }
349
350
    /**
351
     * Sets up mocks as such that the fixture has a file.
352
     *
353
     * @return m\MockInterface|FileDescriptor
354
     */
355
    protected function whenFixtureIsDirectlyRelatedToAFile()
356
    {
357
        $file = m::mock(FileDescriptor::class);
358
        $this->fixture->setFile($file);
359
        return $file;
360
    }
361
362
    /**
363
     * Sets up mocks as such that the fixture has a parent class, with a file.
364
     *
365
     * @return m\MockInterface|FileDescriptor
366
     */
367
    protected function whenFixtureIsRelatedToAClassWithFile()
368
    {
369
        $file = m::mock(FileDescriptor::class);
370
        $parent = m::mock(ClassDescriptor::class);
371
        $parent->shouldReceive('getFile')->andReturn($file);
372
        $parent->shouldReceive('getFullyQualifiedStructuralElementName')->andReturn('Class1');
373
        $this->fixture->setParent($parent);
374
375
        return $file;
376
    }
377
378
    /**
379
     * @param string $name The name of the current method.
380
     */
381
    protected function whenFixtureHasMethodInParentClassWithSameName($name): MethodDescriptor
382
    {
383
        $result = new MethodDescriptor();
384
        $result->setName($name);
385
386
        $parent = new ClassDescriptor();
387
        $parent->getMethods()->set($name, $result);
388
389
        $class = new ClassDescriptor();
390
        $class->setParent($parent);
391
392
        $this->fixture->setParent($class);
393
394
        return $result;
395
    }
396
397
    /**
398
     * @param string $name The name of the current method.
399
     */
400
    protected function whenFixtureHasMethodInImplementedInterfaceWithSameName($name): MethodDescriptor
401
    {
402
        $result = new MethodDescriptor();
403
        $result->setName($name);
404
405
        $parent = new InterfaceDescriptor();
406
        $parent->getMethods()->set($name, $result);
407
408
        $class = new ClassDescriptor();
409
        $class->getInterfaces()->set('Implemented', $parent);
410
411
        $this->fixture->setParent($class);
412
413
        return $result;
414
    }
415
}
416