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

ArgumentDescriptorTest   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 182
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

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

11 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A testSetAndGetTypes() 0 9 1
A testSetAndGetMethod() 0 9 1
A testSetAndGetDefault() 0 8 1
A testSetAndGetWhetherArgumentIsPassedByReference() 0 8 1
A testSetAndGetWhetherArgumentIsAVariadic() 0 8 1
A testDescriptionInheritsWhenNoneIsPresent() 0 13 1
A testDescriptionIsNotInheritedWhenPresent() 0 13 1
A testTypeIsInheritedWhenNoneIsPresent() 0 13 1
A testGetTheArgumentFromWhichThisArgumentInherits() 0 18 1
A whenFixtureHasMethodAndArgumentInParentClassWithSameName() 0 27 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 phpDocumentor\Reflection\Types\Integer;
15
use phpDocumentor\Reflection\Types\String_;
16
17
/**
18
 * @coversDefaultClass \phpDocumentor\Descriptor\ArgumentDescriptor
19
 */
20
class ArgumentDescriptorTest extends \Mockery\Adapter\Phpunit\MockeryTestCase
21
{
22
    /** @var ArgumentDescriptor $fixture */
23
    protected $fixture;
24
25
    /**
26
     * Creates a new (empty) fixture object.
27
     */
28
    protected function setUp()
29
    {
30
        $this->fixture = new ArgumentDescriptor();
31
    }
32
33
    /**
34
     * @covers ::getType
35
     * @covers ::setType
36
     */
37
    public function testSetAndGetTypes()
38
    {
39
        $this->assertSame(null, $this->fixture->getType());
40
41
        $type = new Integer();
42
        $this->fixture->setType($type);
43
44
        $this->assertSame($type, $this->fixture->getType());
45
    }
46
47
    /**
48
     * @covers ::getMethod
49
     * @covers ::setMethod
50
     */
51
    public function testSetAndGetMethod()
52
    {
53
        $this->assertSame(null, $this->fixture->getMethod());
54
55
        $method = new MethodDescriptor();
56
        $this->fixture->setMethod($method);
57
58
        $this->assertSame($method, $this->fixture->getMethod());
59
    }
60
61
    /**
62
     * @covers ::getDefault
63
     * @covers ::setDefault
64
     */
65
    public function testSetAndGetDefault()
66
    {
67
        $this->assertNull($this->fixture->getDefault());
68
69
        $this->fixture->setDefault('a');
70
71
        $this->assertSame('a', $this->fixture->getDefault());
72
    }
73
74
    /**
75
     * @covers ::isByReference
76
     * @covers ::setByReference
77
     */
78
    public function testSetAndGetWhetherArgumentIsPassedByReference()
79
    {
80
        $this->assertFalse($this->fixture->isByReference());
81
82
        $this->fixture->setByReference(true);
83
84
        $this->assertTrue($this->fixture->isByReference());
85
    }
86
87
    /**
88
     * @covers ::isVariadic
89
     * @covers ::setVariadic
90
     */
91
    public function testSetAndGetWhetherArgumentIsAVariadic()
92
    {
93
        $this->assertFalse($this->fixture->isVariadic());
94
95
        $this->fixture->setVariadic(true);
96
97
        $this->assertTrue($this->fixture->isVariadic());
98
    }
99
100
    /**
101
     * @covers ::getDescription
102
     */
103
    public function testDescriptionInheritsWhenNoneIsPresent()
104
    {
105
        // Arrange
106
        $description = 'This is a description';
107
        $this->fixture->setDescription(null);
108
        $parentArgument = $this->whenFixtureHasMethodAndArgumentInParentClassWithSameName('same_argument');
109
        $parentArgument->setDescription($description);
110
        // Act
111
        $result = $this->fixture->getDescription();
112
113
        // Assert
114
        $this->assertSame($description, $result);
115
    }
116
117
    /**
118
     * @covers ::getDescription
119
     */
120
    public function testDescriptionIsNotInheritedWhenPresent()
121
    {
122
        // Arrange
123
        $description = 'This is a description';
124
        $this->fixture->setDescription($description);
125
        $parentArgument = $this->whenFixtureHasMethodAndArgumentInParentClassWithSameName('same_argument');
126
        $parentArgument->setDescription('some random text');
127
        // Act
128
        $result = $this->fixture->getDescription();
129
130
        // Assert
131
        $this->assertSame($description, $result);
132
    }
133
134
    /**
135
     * @covers ::getType
136
     */
137
    public function testTypeIsInheritedWhenNoneIsPresent()
138
    {
139
        // Arrange
140
        $types = new String_();
141
        $this->fixture->setType(null);
142
        $parentArgument = $this->whenFixtureHasMethodAndArgumentInParentClassWithSameName('same_argument');
143
        $parentArgument->setType($types);
144
        // Act
145
        $result = $this->fixture->getType();
146
147
        // Assert
148
        $this->assertSame($types, $result);
149
    }
150
151
    /**
152
     * @covers ::setMethod
153
     * @covers ::getInheritedElement
154
     */
155
    public function testGetTheArgumentFromWhichThisArgumentInherits()
156
    {
157
        $this->assertNull(
158
            $this->fixture->getInheritedElement(),
159
            'By default, an argument does not have an inherited element'
160
        );
161
162
        $method = new MethodDescriptor();
163
        $method->setName('same');
164
        $method->addArgument('abc', $this->fixture);
165
        $this->fixture->setMethod($method);
166
167
        $this->assertNull($this->fixture->getInheritedElement());
168
169
        $this->whenFixtureHasMethodAndArgumentInParentClassWithSameName('abcd');
170
171
        $this->assertNotNull($this->fixture->getInheritedElement());
172
    }
173
174
    private function whenFixtureHasMethodAndArgumentInParentClassWithSameName(string $argumentName): ArgumentDescriptor
175
    {
176
        $this->fixture->setName($argumentName);
177
178
        $parentArgument = new ArgumentDescriptor();
179
        $parentArgument->setName($argumentName);
180
181
        $parentMethod = new MethodDescriptor();
182
        $parentMethod->setName('same');
183
        $parentMethod->addArgument($argumentName, $parentArgument);
184
185
        $method = new MethodDescriptor();
186
        $method->setName('same');
187
        $method->addArgument($argumentName, $this->fixture);
188
        $this->fixture->setMethod($method);
189
190
        $parent = new ClassDescriptor();
191
        $parent->getMethods()->set('same', $parentMethod);
192
        $parentMethod->setParent($parent);
193
194
        $class = new ClassDescriptor();
195
        $class->setParent($parent);
196
        $class->getMethods()->set('same', $method);
197
        $method->setParent($class);
198
199
        return $parentArgument;
200
    }
201
}
202