Completed
Push — develop ( f937fe...60406c )
by Mike
14s
created

testGetTheArgumentFromWhichThisArgumentInherits()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 0
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * phpDocumentor
4
 *
5
 * PHP Version 5.3
6
 *
7
 * @copyright 2010-2013 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(array(), $this->fixture->getTypes());
37
38
        $this->fixture->setTypes(array(1));
0 ignored issues
show
Documentation introduced by
array(1) is of type array<integer,integer,{"0":"integer"}>, 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...
39
40
        $this->assertSame(array(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
    /**
71
     * @covers ::isVariadic
72
     * @covers ::setVariadic
73
     */
74
    public function testSetAndGetWhetherArgumentIsAVariadic()
75
    {
76
        $this->assertFalse($this->fixture->isVariadic());
77
78
        $this->fixture->setVariadic(true);
79
80
        $this->assertTrue($this->fixture->isVariadic());
81
    }
82
83
    /**
84
     * @covers ::getDescription
85
     */
86
    public function testDescriptionInheritsWhenNoneIsPresent()
87
    {
88
        // Arrange
89
        $description = 'This is a description';
90
        $this->fixture->setDescription(null);
91
        $parentArgument = $this->whenFixtureHasMethodAndArgumentInParentClassWithSameName('same_argument');
92
        $parentArgument->setDescription($description);
93
        // Act
94
        $result = $this->fixture->getDescription();
95
96
        // Assert
97
        $this->assertSame($description, $result);
98
    }
99
100
    /**
101
     * @covers ::getDescription
102
     */
103
    public function testDescriptionIsNotInheritedWhenPresent()
104
    {
105
        // Arrange
106
        $description = 'This is a description';
107
        $this->fixture->setDescription($description);
108
        $parentArgument = $this->whenFixtureHasMethodAndArgumentInParentClassWithSameName('same_argument');
109
        $parentArgument->setDescription('some random text');
110
        // Act
111
        $result = $this->fixture->getDescription();
112
113
        // Assert
114
        $this->assertSame($description, $result);
115
    }
116
117
    /**
118
     * @covers ::getTypes
119
     */
120
    public function testTypeIsInheritedWhenNoneIsPresent()
121
    {
122
        // Arrange
123
        $types = array('string');
124
        $this->fixture->setTypes(null);
0 ignored issues
show
Documentation introduced by
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...
125
        $parentArgument = $this->whenFixtureHasMethodAndArgumentInParentClassWithSameName('same_argument');
126
        $parentArgument->setTypes($types);
0 ignored issues
show
Documentation introduced by
$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...
127
        // Act
128
        $result = $this->fixture->getTypes();
129
130
        // Assert
131
        $this->assertSame($types, $result);
132
    }
133
134
135
    /**
136
     * @covers ::setMethod
137
     * @covers ::getInheritedElement
138
     */
139
    public function testGetTheArgumentFromWhichThisArgumentInherits()
140
    {
141
        $this->assertNull(
142
            $this->fixture->getInheritedElement(),
143
            'By default, an argument does not have an inherited element'
144
        );
145
146
        $method = new MethodDescriptor;
147
        $method->setName('same');
148
        $method->addArgument('abc', $this->fixture);
149
        $this->fixture->setMethod($method);
150
151
        $this->assertNull($this->fixture->getInheritedElement());
152
153
        $this->whenFixtureHasMethodAndArgumentInParentClassWithSameName('abcd');
154
155
        $this->assertNotNull($this->fixture->getInheritedElement());
156
    }
157
158
    private function whenFixtureHasMethodAndArgumentInParentClassWithSameName(string $argumentName): ArgumentDescriptor
159
    {
160
        $this->fixture->setName($argumentName);
161
162
        $parentArgument = new ArgumentDescriptor();
163
        $parentArgument->setName($argumentName);
164
165
        $parentMethod = new MethodDescriptor();
166
        $parentMethod->setName('same');
167
        $parentMethod->addArgument($argumentName, $parentArgument);
168
169
        $method = new MethodDescriptor;
170
        $method->setName('same');
171
        $method->addArgument($argumentName, $this->fixture);
172
        $this->fixture->setMethod($method);
173
174
        $parent = new ClassDescriptor();
175
        $parent->getMethods()->set('same', $parentMethod);
176
        $parentMethod->setParent($parent);
177
178
        $class  = new ClassDescriptor();
179
        $class->setParent($parent);
180
        $class->getMethods()->set('same', $method);
181
        $method->setParent($class);
182
183
        return $parentArgument;
184
    }
185
}
186