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

unit/phpDocumentor/Descriptor/CollectionTest.php (1 issue)

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
 * Tests the functionality for the Collection class.
16
 */
17
class CollectionTest extends \Mockery\Adapter\Phpunit\MockeryTestCase
18
{
19
    /** @var Collection $fixture */
20
    protected $fixture;
21
22
    /**
23
     * Creates a new (empty) fixture object.
24
     */
25
    protected function setUp()
26
    {
27
        $this->fixture = new Collection();
28
    }
29
30
    /**
31
     * @covers phpDocumentor\Descriptor\Collection::__construct
32
     */
33
    public function testInitialize()
34
    {
35
        $fixture = new Collection();
36
37
        $this->assertAttributeEquals([], 'items', $fixture);
38
    }
39
40
    /**
41
     * @covers phpDocumentor\Descriptor\Collection::__construct
42
     */
43
    public function testInitializeWithExistingArray()
44
    {
45
        $expected = [1, 2];
46
        $fixture = new Collection($expected);
47
48
        $this->assertAttributeEquals($expected, 'items', $fixture);
49
    }
50
51
    /**
52
     * @covers phpDocumentor\Descriptor\Collection::add
53
     */
54
    public function testAddNewItem()
55
    {
56
        $expected = ['abc'];
57
        $expectedSecondRun = ['abc', 'def'];
58
59
        $this->assertAttributeEquals([], 'items', $this->fixture);
60
61
        $this->fixture->add('abc');
62
63
        $this->assertAttributeEquals($expected, 'items', $this->fixture);
64
65
        $this->fixture->add('def');
66
67
        $this->assertAttributeEquals($expectedSecondRun, 'items', $this->fixture);
68
    }
69
70
    /**
71
     * @covers phpDocumentor\Descriptor\Collection::set
72
     * @covers phpDocumentor\Descriptor\Collection::offsetSet
73
     */
74
    public function testSetItemsWithKey()
75
    {
76
        $expected = ['z' => 'abc'];
77
        $expectedSecondRun = ['z' => 'abc', 'y' => 'def'];
78
79
        $this->assertAttributeEquals([], 'items', $this->fixture);
80
81
        $this->fixture->set('z', 'abc');
82
83
        $this->assertAttributeEquals($expected, 'items', $this->fixture);
84
85
        $this->fixture->set('y', 'def');
86
87
        $this->assertAttributeEquals($expectedSecondRun, 'items', $this->fixture);
88
    }
89
90
    /**
91
     * @covers phpDocumentor\Descriptor\Collection::set
92
     * @expectedException \InvalidArgumentException
93
     */
94
    public function testSetItemsWithEmptyKeyShouldThrowException()
95
    {
96
        $this->fixture->set('', 'abc');
97
    }
98
99
    /**
100
     * @covers phpDocumentor\Descriptor\Collection::offsetSet
101
     * @expectedException \InvalidArgumentException
102
     */
103
    public function testSetItemsUsingOffsetSetWithEmptyKeyShouldThrowException()
104
    {
105
        $this->fixture->offsetSet('', 'abc');
106
    }
107
108
    /**
109
     * @covers phpDocumentor\Descriptor\Collection::get
110
     * @covers phpDocumentor\Descriptor\Collection::__get
111
     * @covers phpDocumentor\Descriptor\Collection::offsetGet
112
     */
113
    public function testRetrievalOfItems()
114
    {
115
        $this->fixture['a'] = 'abc';
116
        $this->assertEquals('abc', $this->fixture->__get('a'));
117
        $this->assertEquals('abc', $this->fixture['a']);
118
        $this->assertEquals('abc', $this->fixture->get('a'));
119
        $this->assertCount(1, $this->fixture);
120
121
        $this->assertEquals('def', $this->fixture->get(1, 'def'));
122
        $this->assertCount(2, $this->fixture);
123
    }
124
125
    /**
126
     * @covers phpDocumentor\Descriptor\Collection::getAll
127
     */
128
    public function testRetrieveAllItems()
129
    {
130
        $this->fixture['a'] = 'abc';
131
        $this->assertSame(['a' => 'abc'], $this->fixture->getAll());
132
    }
133
134
    /**
135
     * @covers phpDocumentor\Descriptor\Collection::getIterator
136
     */
137
    public function testGetIterator()
138
    {
139
        $this->fixture['a'] = 'abc';
140
        $this->assertInstanceOf('ArrayIterator', $this->fixture->getIterator());
141
        $this->assertSame(['a' => 'abc'], $this->fixture->getIterator()->getArrayCopy());
142
    }
143
144
    /**
145
     * @covers phpDocumentor\Descriptor\Collection::count
146
     * @covers phpDocumentor\Descriptor\Collection::offsetUnset
147
     */
148
    public function testCountReturnsTheNumberOfElements()
149
    {
150
        $this->assertCount(0, $this->fixture);
151
        $this->assertEquals(0, $this->fixture->count());
152
153
        $this->fixture[0] = 'abc';
154
155
        $this->assertCount(1, $this->fixture);
156
        $this->assertEquals(1, $this->fixture->count());
157
158
        $this->fixture[1] = 'def';
159
160
        $this->assertCount(2, $this->fixture);
161
        $this->assertEquals(2, $this->fixture->count());
162
163
        unset($this->fixture[0]);
164
165
        $this->assertCount(1, $this->fixture);
166
        $this->assertEquals(1, $this->fixture->count());
167
    }
168
169
    /**
170
     * @covers phpDocumentor\Descriptor\Collection::clear
171
     */
172
    public function testClearingTheCollection()
173
    {
174
        $this->fixture[1] = 'a';
175
        $this->fixture[2] = 'b';
176
177
        $this->assertCount(2, $this->fixture);
178
179
        $this->fixture->clear();
180
181
        $this->assertCount(0, $this->fixture);
182
    }
183
184
    /**
185
     * @covers phpDocumentor\Descriptor\Collection::offsetExists
186
     */
187
    public function testIfExistingElementsAreDetected()
188
    {
189
        $this->assertArrayNotHasKey(0, $this->fixture);
190
        $this->assertFalse($this->fixture->offsetExists(0));
191
192
        $this->fixture[0] = 'abc';
193
194
        $this->assertArrayHasKey(0, $this->fixture);
195
        $this->assertTrue($this->fixture->offsetExists(0));
196
    }
197
198
    /**
199
     * @covers phpDocumentor\Descriptor\Collection::merge
200
     */
201
    public function testIfAfterMergeCollectionContainsAllItems()
202
    {
203
        $expected = [0 => 'a', 1 => 'b', 2 => 'c'];
204
        $this->fixture[1] = 'a';
205
        $this->fixture[2] = 'b';
206
207
        $collection2 = new Collection();
208
        $collection2[4] = 'c';
209
210
        $result = $this->fixture->merge($collection2);
0 ignored issues
show
$collection2 is of type object<phpDocumentor\Descriptor\Collection>, but the function expects a object<self>.

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...
211
212
        $this->assertSame($expected, $result->getAll());
213
    }
214
}
215