Completed
Push — develop ( a89061...54507c )
by Jaap
08:53
created

Descriptor/ArgumentDescriptorTest.php (2 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
/**
15
 * @coversDefaultClass \phpDocumentor\Descriptor\ArgumentDescriptor
16
 */
17
class ArgumentDescriptorTest extends \Mockery\Adapter\Phpunit\MockeryTestCase
18
{
19
    /** @var ArgumentDescriptor $fixture */
20
    protected $fixture;
21
22
    /**
23
     * Creates a new (empty) fixture object.
24
     */
25
    protected function setUp()
26
    {
27
        $this->fixture = new ArgumentDescriptor();
28
    }
29
30
    /**
31
     * @covers ::getTypes
32
     * @covers ::setTypes
33
     */
34
    public function testSetAndGetTypes()
35
    {
36
        $this->assertSame([], $this->fixture->getTypes());
37
38
        $this->fixture->setTypes([1]);
39
40
        $this->assertSame([1], $this->fixture->getTypes());
41
    }
42
43
    /**
44
     * @covers ::getDefault
45
     * @covers ::setDefault
46
     */
47
    public function testSetAndGetDefault()
48
    {
49
        $this->assertNull($this->fixture->getDefault());
50
51
        $this->fixture->setDefault('a');
52
53
        $this->assertSame('a', $this->fixture->getDefault());
54
    }
55
56
    /**
57
     * @covers ::isByReference
58
     * @covers ::setByReference
59
     */
60
    public function testSetAndGetWhetherArgumentIsPassedByReference()
61
    {
62
        $this->assertFalse($this->fixture->isByReference());
63
64
        $this->fixture->setByReference(true);
65
66
        $this->assertTrue($this->fixture->isByReference());
67
    }
68
69
    /**
70
     * @covers ::isVariadic
71
     * @covers ::setVariadic
72
     */
73
    public function testSetAndGetWhetherArgumentIsAVariadic()
74
    {
75
        $this->assertFalse($this->fixture->isVariadic());
76
77
        $this->fixture->setVariadic(true);
78
79
        $this->assertTrue($this->fixture->isVariadic());
80
    }
81
82
    /**
83
     * @covers ::getDescription
84
     */
85
    public function testDescriptionInheritsWhenNoneIsPresent()
86
    {
87
        // Arrange
88
        $description = 'This is a description';
89
        $this->fixture->setDescription(null);
90
        $parentArgument = $this->whenFixtureHasMethodAndArgumentInParentClassWithSameName('same_argument');
91
        $parentArgument->setDescription($description);
92
        // Act
93
        $result = $this->fixture->getDescription();
94
95
        // Assert
96
        $this->assertSame($description, $result);
97
    }
98
99
    /**
100
     * @covers ::getDescription
101
     */
102
    public function testDescriptionIsNotInheritedWhenPresent()
103
    {
104
        // Arrange
105
        $description = 'This is a description';
106
        $this->fixture->setDescription($description);
107
        $parentArgument = $this->whenFixtureHasMethodAndArgumentInParentClassWithSameName('same_argument');
108
        $parentArgument->setDescription('some random text');
109
        // Act
110
        $result = $this->fixture->getDescription();
111
112
        // Assert
113
        $this->assertSame($description, $result);
114
    }
115
116
    /**
117
     * @covers ::getTypes
118
     */
119
    public function testTypeIsInheritedWhenNoneIsPresent()
120
    {
121
        // Arrange
122
        $types = ['string'];
123
        $this->fixture->setTypes(null);
0 ignored issues
show
null is of type null, but the function expects a object<phpDocumentor\Descriptor\Collection>.

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...
124
        $parentArgument = $this->whenFixtureHasMethodAndArgumentInParentClassWithSameName('same_argument');
125
        $parentArgument->setTypes($types);
0 ignored issues
show
$types is of type array<integer,string,{"0":"string"}>, but the function expects a object<phpDocumentor\Descriptor\Collection>.

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...
126
        // Act
127
        $result = $this->fixture->getTypes();
128
129
        // Assert
130
        $this->assertSame($types, $result);
131
    }
132
133
    /**
134
     * @covers ::setMethod
135
     * @covers ::getInheritedElement
136
     */
137
    public function testGetTheArgumentFromWhichThisArgumentInherits()
138
    {
139
        $this->assertNull(
140
            $this->fixture->getInheritedElement(),
141
            'By default, an argument does not have an inherited element'
142
        );
143
144
        $method = new MethodDescriptor();
145
        $method->setName('same');
146
        $method->addArgument('abc', $this->fixture);
147
        $this->fixture->setMethod($method);
148
149
        $this->assertNull($this->fixture->getInheritedElement());
150
151
        $this->whenFixtureHasMethodAndArgumentInParentClassWithSameName('abcd');
152
153
        $this->assertNotNull($this->fixture->getInheritedElement());
154
    }
155
156
    private function whenFixtureHasMethodAndArgumentInParentClassWithSameName(string $argumentName): ArgumentDescriptor
157
    {
158
        $this->fixture->setName($argumentName);
159
160
        $parentArgument = new ArgumentDescriptor();
161
        $parentArgument->setName($argumentName);
162
163
        $parentMethod = new MethodDescriptor();
164
        $parentMethod->setName('same');
165
        $parentMethod->addArgument($argumentName, $parentArgument);
166
167
        $method = new MethodDescriptor();
168
        $method->setName('same');
169
        $method->addArgument($argumentName, $this->fixture);
170
        $this->fixture->setMethod($method);
171
172
        $parent = new ClassDescriptor();
173
        $parent->getMethods()->set('same', $parentMethod);
174
        $parentMethod->setParent($parent);
175
176
        $class = new ClassDescriptor();
177
        $class->setParent($parent);
178
        $class->getMethods()->set('same', $method);
179
        $method->setParent($class);
180
181
        return $parentArgument;
182
    }
183
}
184