Completed
Push — develop ( 48a3ec...fa4cb4 )
by Mike
05:54
created

testSettingAndGettingAParent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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