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

Descriptor/ConstantDescriptorTest.php (7 issues)

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\VarDescriptor;
17
use phpDocumentor\Descriptor\Tag\VersionDescriptor;
18
use phpDocumentor\Reflection\Type;
19
use phpDocumentor\Reflection\Types\Array_;
20
use phpDocumentor\Reflection\Types\String_;
21
22
/**
23
 * Tests the functionality for the ConstantDescriptor class.
24
 */
25
class ConstantDescriptorTest extends \Mockery\Adapter\Phpunit\MockeryTestCase
26
{
27
    /** @var ConstantDescriptor $fixture */
28
    protected $fixture;
29
30
    /**
31
     * Creates a new (empty) fixture object.
32
     */
33
    protected function setUp()
34
    {
35
        $this->fixture = new ConstantDescriptor();
36
        $this->fixture->setName('CONSTANT');
37
    }
38
39
    /**
40
     * @covers \phpDocumentor\Descriptor\ConstantDescriptor::getParent
41
     * @covers \phpDocumentor\Descriptor\ConstantDescriptor::setParent
42
     */
43
    public function testSetAndGetParentClass()
44
    {
45
        $this->assertNull($this->fixture->getParent());
46
47
        $parentMock = m::mock('phpDocumentor\Descriptor\ClassDescriptor');
48
        $parentMock->shouldReceive('getFullyQualifiedStructuralElementName')->andReturn('TestClass');
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...
49
50
        $this->fixture->setParent($parentMock);
51
52
        $this->assertSame($parentMock, $this->fixture->getParent());
53
    }
54
55
    /**
56
     * @covers \phpDocumentor\Descriptor\ConstantDescriptor::setParent
57
     * @expectedException \InvalidArgumentException
58
     */
59
    public function testSettingAParentFailsWhenInputIsNotNullClassOrInterface()
60
    {
61
        $this->fixture->setParent('string');
0 ignored issues
show
'string' is of type string, but the function expects a object<phpDocumentor\Des...terfaceDescriptor>|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
62
    }
63
64
    /**
65
     * @covers \phpDocumentor\Descriptor\ConstantDescriptor::getParent
66
     * @covers \phpDocumentor\Descriptor\ConstantDescriptor::setParent
67
     */
68
    public function testSetAndGetParentInterface()
69
    {
70
        $this->assertNull($this->fixture->getParent());
71
72
        $parentMock = m::mock('phpDocumentor\Descriptor\InterfaceDescriptor');
73
        $parentMock->shouldReceive('getFullyQualifiedStructuralElementName')->andReturn('TestInterface');
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...
74
        $this->fixture->setParent($parentMock);
75
76
        $this->assertSame($parentMock, $this->fixture->getParent());
77
    }
78
79
    /**
80
     * @covers \phpDocumentor\Descriptor\ConstantDescriptor::getTypes
81
     * @covers \phpDocumentor\Descriptor\ConstantDescriptor::setTypes
82
     */
83
    public function testSetAndGetTypes()
84
    {
85
        $this->assertEquals(null, $this->fixture->getType());
86
        $expected = new Array_();
87
88
        $this->fixture->setTypes($expected);
89
90
        $this->assertSame($expected, $this->fixture->getType());
91
    }
92
93
    /**
94
     * @covers \phpDocumentor\Descriptor\ConstantDescriptor::getTypes
95
     * @covers \phpDocumentor\Descriptor\ConstantDescriptor::getVar
96
     */
97
    public function testTestgetTypesDerivedFromVarTag()
98
    {
99
        $expected = new String_();
100
101
        $varTag = new VarDescriptor('var');
102
        $varTag->setType($expected);
103
104
        $this->fixture->getTags()->set('var', new Collection([$varTag]));
105
106
        $this->assertEquals($expected, $this->fixture->getType());
107
    }
108
109
    /**
110
     * @covers \phpDocumentor\Descriptor\ConstantDescriptor::getTypes
111
     * @covers \phpDocumentor\Descriptor\ConstantDescriptor::getVar
112
     */
113
    public function testGetTypesUsingInheritanceOfVarTag()
114
    {
115
        $expected = new String_();
116
117
        $constantName = 'CONSTANT';
118
        $this->fixture->setName($constantName);
119
        $parentClass = $this->createParentClassWithSuperClassAndConstant($expected, $constantName);
120
121
        // Attempt to get the types; which come from the superclass' constants
122
        $this->fixture->setParent($parentClass);
0 ignored issues
show
It seems like $parentClass defined by $this->createParentClass...xpected, $constantName) on line 119 can also be of type object<Mockery\MockInterface>; however, phpDocumentor\Descriptor...Descriptor::setParent() does only seem to accept object<phpDocumentor\Des...terfaceDescriptor>|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
123
        $types = $this->fixture->getType();
124
125
        $this->assertSame($expected, $types);
126
    }
127
128
    /**
129
     * @covers \phpDocumentor\Descriptor\ConstantDescriptor::getValue
130
     * @covers \phpDocumentor\Descriptor\ConstantDescriptor::setValue
131
     */
132
    public function testSetAndGetValue()
133
    {
134
        $this->assertNull($this->fixture->getValue());
135
136
        $this->fixture->setValue('a');
137
138
        $this->assertSame('a', $this->fixture->getValue());
139
    }
140
141
    /**
142
     * @covers \phpDocumentor\Descriptor\ConstantDescriptor::getFile
143
     */
144
    public function testRetrieveFileAssociatedWithAGlobalConstant()
145
    {
146
        // Arrange
147
        $file = $this->whenFixtureIsDirectlyRelatedToAFile();
148
149
        // Act
150
        $result = $this->fixture->getFile();
151
152
        // Assert
153
        $this->assertSame($file, $result);
154
    }
155
156
    /**
157
     * @covers \phpDocumentor\Descriptor\ConstantDescriptor::getFile
158
     */
159
    public function testRetrieveFileAssociatedWithAClassConstant()
160
    {
161
        // Arrange
162
        $file = $this->whenFixtureIsRelatedToAClassWithFile();
163
164
        // Act
165
        $result = $this->fixture->getFile();
166
167
        // Assert
168
        $this->assertAttributeSame(null, 'fileDescriptor', $this->fixture);
169
        $this->assertSame($file, $result);
170
    }
171
172
    /**
173
     * @covers \phpDocumentor\Descriptor\DescriptorAbstract::getSummary
174
     */
175
    public function testSummaryInheritsWhenNoneIsPresent()
176
    {
177
        // Arrange
178
        $summary = 'This is a summary';
179
        $this->fixture->setSummary(null);
180
        $parentConstant = $this->whenFixtureHasConstantInParentClassWithSameName($this->fixture->getName());
181
        $parentConstant->setSummary($summary);
182
183
        // Act
184
        $result = $this->fixture->getSummary();
185
186
        // Assert
187
        $this->assertSame($summary, $result);
188
    }
189
190
    /**
191
     * @covers \phpDocumentor\Descriptor\DescriptorAbstract::getDescription
192
     */
193
    public function testDescriptionInheritsWhenNoneIsPresent()
194
    {
195
        // Arrange
196
        $description = 'This is a description';
197
        $this->fixture->setDescription(null);
198
        $parentConstant = $this->whenFixtureHasConstantInParentClassWithSameName($this->fixture->getName());
199
        $parentConstant->setDescription($description);
200
201
        // Act
202
        $result = $this->fixture->getDescription();
203
204
        // Assert
205
        $this->assertSame($description, $result);
206
    }
207
208
    /**
209
     * @covers \phpDocumentor\Descriptor\DescriptorAbstract::getDescription
210
     */
211
    public function testDescriptionInheritsWhenInheritDocIsPresent()
212
    {
213
        // Arrange
214
        $description = 'This is a description';
215
        $this->fixture->setDescription('{@inheritDoc}');
216
        $parentConstant = $this->whenFixtureHasConstantInParentClassWithSameName($this->fixture->getName());
217
        $parentConstant->setDescription($description);
218
219
        // Act
220
        $result = $this->fixture->getDescription();
221
222
        // Assert
223
        $this->assertSame($description, $result);
224
    }
225
226
    /**
227
     * @covers \phpDocumentor\Descriptor\DescriptorAbstract::getDescription
228
     */
229
    public function testDescriptionIsAugmentedWhenInheritDocInlineTagIsPresent()
230
    {
231
        // Arrange
232
        $description = 'This is a description';
233
        $this->fixture->setDescription('Original description {@inheritDoc}');
234
        $parentConstant = $this->whenFixtureHasConstantInParentClassWithSameName($this->fixture->getName());
235
        $parentConstant->setDescription($description);
236
237
        // Act
238
        $result = $this->fixture->getDescription();
239
240
        // Assert
241
        $this->assertSame('Original description ' . $description, $result);
242
    }
243
244
    /**
245
     * @covers \phpDocumentor\Descriptor\ConstantDescriptor::getVar
246
     */
247
    public function testVarTagsInheritWhenNoneArePresent()
248
    {
249
        // Arrange
250
        $varTagDescriptor = new VarDescriptor('var');
251
        $varCollection = new Collection([$varTagDescriptor]);
252
        $this->fixture->getTags()->clear();
253
        $parentProperty = $this->whenFixtureHasConstantInParentClassWithSameName($this->fixture->getName());
254
        $parentProperty->getTags()->set('var', $varCollection);
255
256
        // Act
257
        $result = $this->fixture->getVar();
258
259
        // Assert
260
        $this->assertSame($varCollection, $result);
261
    }
262
263
    /**
264
     * @covers \phpDocumentor\Descriptor\DescriptorAbstract::getPackage
265
     */
266
    public function testPackageInheritWhenNoneArePresent()
267
    {
268
        // Arrange
269
        $packageTagDescriptor = new PackageDescriptor();
270
        $this->fixture->setPackage('');
0 ignored issues
show
'' is of type string, but the function expects a object<phpDocumentor\Des...ptor\PackageDescriptor>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
271
        $parentProperty = $this->whenFixtureHasConstantInParentClassWithSameName($this->fixture->getName());
272
        $parentProperty->setPackage($packageTagDescriptor);
273
274
        // Act
275
        $result = $this->fixture->getPackage();
276
277
        // Assert
278
        $this->assertSame($packageTagDescriptor, $result);
279
    }
280
281
    /**
282
     * @covers \phpDocumentor\Descriptor\DescriptorAbstract::getAuthor
283
     */
284
    public function testAuthorTagsInheritWhenNoneArePresent()
285
    {
286
        // Arrange
287
        $authorTagDescriptor = new AuthorDescriptor('author');
288
        $authorCollection = new Collection([$authorTagDescriptor]);
289
        $this->fixture->getTags()->clear();
290
        $parentProperty = $this->whenFixtureHasConstantInParentClassWithSameName($this->fixture->getName());
291
        $parentProperty->getTags()->set('author', $authorCollection);
292
293
        // Act
294
        $result = $this->fixture->getAuthor();
295
296
        // Assert
297
        $this->assertSame($authorCollection, $result);
298
    }
299
300
    /**
301
     * @covers \phpDocumentor\Descriptor\DescriptorAbstract::getVersion
302
     */
303
    public function testVersionTagsInheritWhenNoneArePresent()
304
    {
305
        // Arrange
306
        $versionTagDescriptor = new VersionDescriptor('version');
307
        $versionCollection = new Collection([$versionTagDescriptor]);
308
        $this->fixture->getTags()->clear();
309
        $parentProperty = $this->whenFixtureHasConstantInParentClassWithSameName($this->fixture->getName());
310
        $parentProperty->getTags()->set('version', $versionCollection);
311
312
        // Act
313
        $result = $this->fixture->getVersion();
314
315
        // Assert
316
        $this->assertSame($versionCollection, $result);
317
    }
318
319
    /**
320
     * @covers \phpDocumentor\Descriptor\DescriptorAbstract::getCopyright
321
     */
322
    public function testCopyrightTagsInheritWhenNoneArePresent()
323
    {
324
        // Arrange
325
        $copyrightTagDescriptor = new TagDescriptor('copyright');
326
        $copyrightCollection = new Collection([$copyrightTagDescriptor]);
327
        $this->fixture->getTags()->clear();
328
        $parentProperty = $this->whenFixtureHasConstantInParentClassWithSameName($this->fixture->getName());
329
        $parentProperty->getTags()->set('copyright', $copyrightCollection);
330
331
        // Act
332
        $result = $this->fixture->getCopyright();
333
334
        // Assert
335
        $this->assertSame($copyrightCollection, $result);
336
    }
337
338
    /**
339
     * Creates a parentClass for a Constant with a SuperClass, which in turn has a constant exposing the given types.
340
     *
341
     * The created ParentClass can be used to test the inheritance of properties of a constant descriptor, such as
342
     * inheriting type information.
343
     *
344
     * @param Type $type
345
     * @param string $constantName
346
     *
347
     * @return m\MockInterface|ClassDescriptor
348
     */
349
    protected function createParentClassWithSuperClassAndConstant(Type $type, $constantName)
350
    {
351
        // construct the to-be-inherited constant and its @var tag
352
        $varTag = m::mock('phpDocumentor\Descriptor\Tag\VarDescriptor');
353
        $varTag->shouldReceive('getType')->andReturn($type);
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...
354
355
        $parentConstant = m::mock('\phpDocumentor\Descriptor\ConstantDescriptor');
356
        $parentConstant->shouldReceive('getVar')->andReturn(new Collection([$varTag]));
357
358
        // create SuperClassMock and add a Constant collection with out to-be-inherited constant
359
        $superClass = m::mock('phpDocumentor\Descriptor\ClassDescriptor');
360
        $superClass->shouldReceive('getConstants')->andReturn(
361
            new Collection(
362
                [$constantName => $parentConstant]
363
            )
364
        );
365
366
        // create and set the parent class for our fixture
367
        $parentClass = m::mock('phpDocumentor\Descriptor\ClassDescriptor');
368
        $parentClass->shouldReceive('getFullyQualifiedStructuralElementName')->andReturn('TestClass');
369
        $parentClass->shouldReceive('getParent')->andReturn($superClass);
370
371
        return $parentClass;
372
    }
373
374
    /**
375
     * Sets up mocks as such that the fixture has a file.
376
     *
377
     * @return m\MockInterface|FileDescriptor
378
     */
379
    protected function whenFixtureIsDirectlyRelatedToAFile()
380
    {
381
        $file = m::mock('phpDocumentor\Descriptor\FileDescriptor');
382
        $this->fixture->setFile($file);
383
        return $file;
384
    }
385
386
    /**
387
     * Sets up mocks as such that the fixture has a parent class, with a file.
388
     *
389
     * @return m\MockInterface|FileDescriptor
390
     */
391
    protected function whenFixtureIsRelatedToAClassWithFile()
392
    {
393
        $file = m::mock('phpDocumentor\Descriptor\FileDescriptor');
394
        $parent = m::mock('phpDocumentor\Descriptor\ClassDescriptor');
395
        $parent->shouldReceive('getFile')->andReturn($file);
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...
396
        $parent->shouldReceive('getFullyQualifiedStructuralElementName')->andReturn('Class1');
397
        $this->fixture->setParent($parent);
398
399
        return $file;
400
    }
401
402
    /**
403
     * @param string $name The name of the current constant.
404
     *
405
     * @return ConstantDescriptor
406
     */
407
    protected function whenFixtureHasConstantInParentClassWithSameName($name)
408
    {
409
        $result = new ConstantDescriptor();
410
        $result->setName($name);
411
412
        $parent = new ClassDescriptor();
413
        $parent->getConstants()->set($name, $result);
414
415
        $class = new ClassDescriptor();
416
        $class->setParent($parent);
417
418
        $this->fixture->setParent($class);
419
420
        return $result;
421
    }
422
}
423