PropertyDescriptorTest   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 367
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 0
Metric Value
dl 0
loc 367
rs 10
c 0
b 0
f 0
wmc 24
lcom 1
cbo 11

24 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A testSettingAndGettingWhetherPropertyIsStatic() 0 8 1
A testSettingAndGettingVisibility() 0 8 1
A testSetAndGetTypes() 0 9 1
A testGetTypesCollection() 0 9 1
A testSetAndGetTypesWhenVarIsPresent() 0 16 1
A testSetAndGetDefault() 0 8 1
A testRetrieveFileAssociatedWithAProperty() 0 12 1
A testSummaryInheritsWhenNoneIsPresent() 0 14 1
A testDescriptionInheritsWhenNoneIsPresent() 0 14 1
A testDescriptionInheritsWhenInheritDocIsPresent() 0 14 1
A testDescriptionIsAugmentedWhenInheritDocInlineTagIsPresent() 0 14 1
A testVarTagsInheritWhenNoneArePresent() 0 15 1
A testVarTagsWhenNoneArePresent() 0 7 1
A testAuthorTagsInheritWhenNoneArePresent() 0 15 1
A testVersionTagsInheritWhenNoneArePresent() 0 15 1
A testCopyrightTagsInheritWhenNoneArePresent() 0 15 1
A testFqsenHasDollarSignWhenParentIsSet() 0 5 1
A testSettingAndGettingAParent() 0 5 1
A testGettingAnInheritedElement() 0 8 1
A testGettingAnInheritedElementWhenThereIsNone() 0 4 1
A whenFixtureIsDirectlyRelatedToAFile() 0 6 1
A whenFixtureIsRelatedToAClassWithFile() 0 10 1
A whenFixtureHasPropertyInParentClassWithSameName() 0 15 1
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\VarDescriptor;
17
use \phpDocumentor\Descriptor\Tag\VersionDescriptor;
18
use phpDocumentor\Reflection\Types\Array_;
19
20
/**
21
 * Tests the functionality for the PropertyDescriptor class.
22
 * @coversDefaultClass \phpDocumentor\Descriptor\PropertyDescriptor
23
 */
24
class PropertyDescriptorTest extends \Mockery\Adapter\Phpunit\MockeryTestCase
25
{
26
    /** @var PropertyDescriptor $fixture */
27
    protected $fixture;
28
29
    /**
30
     * Creates a new (empty) fixture object.
31
     */
32
    protected function setUp()
33
    {
34
        $this->fixture = new PropertyDescriptor();
35
        $this->fixture->setName('property');
36
    }
37
38
    /**
39
     * @covers ::isStatic
40
     * @covers ::setStatic
41
     */
42
    public function testSettingAndGettingWhetherPropertyIsStatic()
43
    {
44
        $this->assertFalse($this->fixture->isStatic());
45
46
        $this->fixture->setStatic(true);
47
48
        $this->assertTrue($this->fixture->isStatic());
49
    }
50
51
    /**
52
     * @covers ::getVisibility
53
     * @covers ::setVisibility
54
     */
55
    public function testSettingAndGettingVisibility()
56
    {
57
        $this->assertEquals('public', $this->fixture->getVisibility());
58
59
        $this->fixture->setVisibility('private');
60
61
        $this->assertEquals('private', $this->fixture->getVisibility());
62
    }
63
64
    /**
65
     * @covers ::getType
66
     * @covers ::setType
67
     */
68
    public function testSetAndGetTypes()
69
    {
70
        $this->assertEquals(null, $this->fixture->getType());
71
        $expected = new Array_();
72
73
        $this->fixture->setType($expected);
74
75
        $this->assertSame($expected, $this->fixture->getType());
76
    }
77
78
    /**
79
     * @covers ::getTypes
80
     */
81
    public function testGetTypesCollection()
82
    {
83
        $this->assertSame([], $this->fixture->getTypes());
84
        $expected = new Array_();
85
86
        $this->fixture->setType($expected);
87
88
        $this->assertSame(['array'], $this->fixture->getTypes());
89
    }
90
91
    /**
92
     * @covers ::getType
93
     * @covers ::setType
94
     */
95
    public function testSetAndGetTypesWhenVarIsPresent()
96
    {
97
        // Arrange
98
        $typesCollection = new Array_();
99
        $varTagDescriptor = new VarDescriptor('var');
100
        $varTagDescriptor->setType($typesCollection);
101
        $varCollection = new Collection([$varTagDescriptor]);
102
        $this->fixture->getTags()->clear();
103
        $this->fixture->getTags()->set('var', $varCollection);
104
105
        // Act
106
        $result = $this->fixture->getType();
107
108
        // Assert
109
        $this->assertSame($typesCollection, $result);
110
    }
111
112
    /**
113
     * @covers ::getDefault
114
     * @covers ::setDefault
115
     */
116
    public function testSetAndGetDefault()
117
    {
118
        $this->assertNull($this->fixture->getDefault());
119
120
        $this->fixture->setDefault('a');
121
122
        $this->assertSame('a', $this->fixture->getDefault());
123
    }
124
125
    /**
126
     * @covers ::getFile
127
     */
128
    public function testRetrieveFileAssociatedWithAProperty()
129
    {
130
        // Arrange
131
        $file = $this->whenFixtureIsRelatedToAClassWithFile();
132
133
        // Act
134
        $result = $this->fixture->getFile();
135
136
        // Assert
137
        $this->assertAttributeSame(null, 'fileDescriptor', $this->fixture);
138
        $this->assertSame($file, $result);
139
    }
140
141
    /**
142
     * @covers \phpDocumentor\Descriptor\DescriptorAbstract::getSummary
143
     */
144
    public function testSummaryInheritsWhenNoneIsPresent()
145
    {
146
        // Arrange
147
        $summary = 'This is a summary';
148
        $this->fixture->setSummary(null);
149
        $parentProperty = $this->whenFixtureHasPropertyInParentClassWithSameName($this->fixture->getName());
150
        $parentProperty->setSummary($summary);
151
152
        // Act
153
        $result = $this->fixture->getSummary();
154
155
        // Assert
156
        $this->assertSame($summary, $result);
157
    }
158
159
    /**
160
     * @covers \phpDocumentor\Descriptor\DescriptorAbstract::getDescription
161
     */
162
    public function testDescriptionInheritsWhenNoneIsPresent()
163
    {
164
        // Arrange
165
        $description = 'This is a description';
166
        $this->fixture->setDescription(null);
167
        $parentProperty = $this->whenFixtureHasPropertyInParentClassWithSameName($this->fixture->getName());
168
        $parentProperty->setDescription($description);
169
170
        // Act
171
        $result = $this->fixture->getDescription();
172
173
        // Assert
174
        $this->assertSame($description, $result);
175
    }
176
177
    /**
178
     * @covers \phpDocumentor\Descriptor\DescriptorAbstract::getDescription
179
     */
180
    public function testDescriptionInheritsWhenInheritDocIsPresent()
181
    {
182
        // Arrange
183
        $description = 'This is a description';
184
        $this->fixture->setDescription('{@inheritDoc}');
185
        $parentProperty = $this->whenFixtureHasPropertyInParentClassWithSameName($this->fixture->getName());
186
        $parentProperty->setDescription($description);
187
188
        // Act
189
        $result = $this->fixture->getDescription();
190
191
        // Assert
192
        $this->assertSame($description, $result);
193
    }
194
195
    /**
196
     * @covers \phpDocumentor\Descriptor\DescriptorAbstract::getDescription
197
     */
198
    public function testDescriptionIsAugmentedWhenInheritDocInlineTagIsPresent()
199
    {
200
        // Arrange
201
        $description = 'This is a description';
202
        $this->fixture->setDescription('Original description {@inheritDoc}');
203
        $parentProperty = $this->whenFixtureHasPropertyInParentClassWithSameName($this->fixture->getName());
204
        $parentProperty->setDescription($description);
205
206
        // Act
207
        $result = $this->fixture->getDescription();
208
209
        // Assert
210
        $this->assertSame('Original description ' . $description, $result);
211
    }
212
213
    /**
214
     * @covers ::getVar
215
     */
216
    public function testVarTagsInheritWhenNoneArePresent()
217
    {
218
        // Arrange
219
        $varTagDescriptor = new VarDescriptor('var');
220
        $varCollection = new Collection([$varTagDescriptor]);
221
        $this->fixture->getTags()->clear();
222
        $parentProperty = $this->whenFixtureHasPropertyInParentClassWithSameName($this->fixture->getName());
223
        $parentProperty->getTags()->set('var', $varCollection);
224
225
        // Act
226
        $result = $this->fixture->getVar();
227
228
        // Assert
229
        $this->assertSame($varCollection, $result);
230
    }
231
232
    /**
233
     * @covers ::getVar
234
     */
235
    public function testVarTagsWhenNoneArePresent()
236
    {
237
        $varCollection = new Collection();
238
        $result = $this->fixture->getVar();
239
240
        $this->assertEquals($varCollection, $result);
241
    }
242
243
    /**
244
     * @covers ::getAuthor
245
     * @covers \phpDocumentor\Descriptor\DescriptorAbstract::getAuthor
246
     */
247
    public function testAuthorTagsInheritWhenNoneArePresent()
248
    {
249
        // Arrange
250
        $authorTagDescriptor = new AuthorDescriptor('author');
251
        $authorCollection = new Collection([$authorTagDescriptor]);
252
        $this->fixture->getTags()->clear();
253
        $parentProperty = $this->whenFixtureHasPropertyInParentClassWithSameName($this->fixture->getName());
254
        $parentProperty->getTags()->set('author', $authorCollection);
255
256
        // Act
257
        $result = $this->fixture->getAuthor();
258
259
        // Assert
260
        $this->assertSame($authorCollection, $result);
261
    }
262
263
    /**
264
     * @covers ::getVersion
265
     * @covers \phpDocumentor\Descriptor\DescriptorAbstract::getVersion
266
     */
267
    public function testVersionTagsInheritWhenNoneArePresent()
268
    {
269
        // Arrange
270
        $versionTagDescriptor = new VersionDescriptor('version');
271
        $versionCollection = new Collection([$versionTagDescriptor]);
272
        $this->fixture->getTags()->clear();
273
        $parentProperty = $this->whenFixtureHasPropertyInParentClassWithSameName($this->fixture->getName());
274
        $parentProperty->getTags()->set('version', $versionCollection);
275
276
        // Act
277
        $result = $this->fixture->getVersion();
278
279
        // Assert
280
        $this->assertSame($versionCollection, $result);
281
    }
282
283
    /**
284
     * @covers ::getCopyright
285
     * @covers \phpDocumentor\Descriptor\DescriptorAbstract::getCopyright
286
     */
287
    public function testCopyrightTagsInheritWhenNoneArePresent()
288
    {
289
        // Arrange
290
        $copyrightTagDescriptor = new TagDescriptor('copyright');
291
        $copyrightCollection = new Collection([$copyrightTagDescriptor]);
292
        $this->fixture->getTags()->clear();
293
        $parentProperty = $this->whenFixtureHasPropertyInParentClassWithSameName($this->fixture->getName());
294
        $parentProperty->getTags()->set('copyright', $copyrightCollection);
295
296
        // Act
297
        $result = $this->fixture->getCopyright();
298
299
        // Assert
300
        $this->assertSame($copyrightCollection, $result);
301
    }
302
303
    /**
304
     * @covers ::setParent
305
     */
306
    public function testFqsenHasDollarSignWhenParentIsSet()
307
    {
308
        $this->whenFixtureHasPropertyInParentClassWithSameName($this->fixture->getName());
309
        $this->assertSame('::$property', $this->fixture->getFullyQualifiedStructuralElementName());
310
    }
311
312
    /**
313
     * @covers ::setParent
314
     * @covers ::getParent
315
     */
316
    public function testSettingAndGettingAParent()
317
    {
318
        $this->whenFixtureHasPropertyInParentClassWithSameName($this->fixture->getName());
319
        $this->assertInstanceOf('\phpDocumentor\Descriptor\ClassDescriptor', $this->fixture->getParent());
320
    }
321
322
    /**
323
     * @covers ::getInheritedElement
324
     */
325
    public function testGettingAnInheritedElement()
326
    {
327
        $this->whenFixtureHasPropertyInParentClassWithSameName($this->fixture->getName());
328
329
        $inheritedProperty = $this->fixture->getInheritedElement();
330
331
        $this->assertSame($inheritedProperty->getName(), $this->fixture->getName());
332
    }
333
334
    /**
335
     * @covers ::getInheritedElement
336
     */
337
    public function testGettingAnInheritedElementWhenThereIsNone()
338
    {
339
        $this->assertNull($this->fixture->getInheritedElement());
340
    }
341
342
    /**
343
     * Sets up mocks as such that the fixture has a file.
344
     *
345
     * @return m\MockInterface|FileDescriptor
346
     */
347
    protected function whenFixtureIsDirectlyRelatedToAFile()
348
    {
349
        $file = m::mock('\phpDocumentor\Descriptor\FileDescriptor');
350
        $this->fixture->setFile($file);
351
        return $file;
352
    }
353
354
    /**
355
     * Sets up mocks as such that the fixture has a parent class, with a file.
356
     *
357
     * @return m\MockInterface|FileDescriptor
358
     */
359
    protected function whenFixtureIsRelatedToAClassWithFile()
360
    {
361
        $file = m::mock('\phpDocumentor\Descriptor\FileDescriptor');
362
        $parent = m::mock('\phpDocumentor\Descriptor\ClassDescriptor');
363
        $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...
364
        $parent->shouldReceive('getFullyQualifiedStructuralElementName')->andReturn('Class1');
365
        $this->fixture->setParent($parent);
366
367
        return $file;
368
    }
369
370
    /**
371
     * @param string $name The name of the current property.
372
     *
373
     * @return PropertyDescriptor
374
     */
375
    protected function whenFixtureHasPropertyInParentClassWithSameName($name)
376
    {
377
        $result = new PropertyDescriptor();
378
        $result->setName($name);
379
380
        $parent = new ClassDescriptor();
381
        $parent->getProperties()->set($name, $result);
382
383
        $class = new ClassDescriptor();
384
        $class->setParent($parent);
385
386
        $this->fixture->setParent($class);
387
388
        return $result;
389
    }
390
}
391