1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace PhpValueObjects\Tests\Collection; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use PhpValueObjects\Collection\Exception\InvalidCollectionObjectException; |
8
|
|
|
use PhpValueObjects\Tests\BaseUnitTestCase; |
9
|
|
|
use stdClass; |
10
|
|
|
|
11
|
|
|
final class CollectionTest extends BaseUnitTestCase |
12
|
|
|
{ |
13
|
|
|
private $objects; |
14
|
|
|
private $collection; |
15
|
|
|
|
16
|
|
|
protected function setUp() |
17
|
|
|
{ |
18
|
|
|
parent::setUp(); |
19
|
|
|
$this->objects = [ |
20
|
|
|
new ObjectForTest(1), |
21
|
|
|
new ObjectForTest(2), |
22
|
|
|
new ObjectForTest(3), |
23
|
|
|
new ObjectForTest(4), |
24
|
|
|
new ObjectForTest(5), |
25
|
|
|
]; |
26
|
|
|
$this->collection = new Collection($this->objects); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
protected function tearDown() |
30
|
|
|
{ |
31
|
|
|
parent::tearDown(); |
32
|
|
|
$this->objects = null; |
33
|
|
|
$this->collection = null; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @test |
38
|
|
|
*/ |
39
|
|
|
public function itShouldThrowInvalidCollectionObjectException(): void |
40
|
|
|
{ |
41
|
|
|
$this->expectException(InvalidCollectionObjectException::class); |
42
|
|
|
|
43
|
|
|
new Collection([new stdClass(), new stdClass()]); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @test |
48
|
|
|
*/ |
49
|
|
|
public function itShouldReturnCollection(): void |
50
|
|
|
{ |
51
|
|
|
$this->assertSame($this->objects, $this->collection->getCollection()); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @test |
56
|
|
|
*/ |
57
|
|
|
public function itShouldReturnEmptyCollection(): void |
58
|
|
|
{ |
59
|
|
|
$collection = new Collection([]); |
60
|
|
|
|
61
|
|
|
$this->assertEmpty($collection->getCollection()); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @test |
66
|
|
|
*/ |
67
|
|
|
public function itShouldReturnFirst(): void |
68
|
|
|
{ |
69
|
|
|
/** @var ObjectForTest $object */ |
70
|
|
|
$object = $this->collection->first(); |
71
|
|
|
$this->assertSame(1, $object->getId()); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @test |
76
|
|
|
*/ |
77
|
|
|
public function itShouldReturnLast(): void |
78
|
|
|
{ |
79
|
|
|
/** @var ObjectForTest $object */ |
80
|
|
|
$object = $this->collection->last(); |
81
|
|
|
$this->assertSame(5, $object->getId()); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @test |
86
|
|
|
*/ |
87
|
|
|
public function itShouldReturnKey(): void |
88
|
|
|
{ |
89
|
|
|
|
90
|
|
|
$key = $this->collection->key(); |
91
|
|
|
$this->assertSame(0, $key); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @test |
96
|
|
|
*/ |
97
|
|
View Code Duplication |
public function itShouldReturnNext(): void |
|
|
|
|
98
|
|
|
{ |
99
|
|
|
$first = $this->collection->first(); |
100
|
|
|
$this->assertSame(1, $first->getId()); |
101
|
|
|
$next = $this->collection->next(); |
102
|
|
|
$this->assertSame(2, $next->getId()); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @test |
107
|
|
|
*/ |
108
|
|
View Code Duplication |
public function itShouldReturnCurrent(): void |
|
|
|
|
109
|
|
|
{ |
110
|
|
|
$this->collection->first(); |
111
|
|
|
$this->collection->next(); |
112
|
|
|
$current = $this->collection->current(); |
113
|
|
|
$this->assertSame(2, $current->getId()); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @test |
118
|
|
|
*/ |
119
|
|
|
public function itShouldRemoveKey(): void |
120
|
|
|
{ |
121
|
|
|
$this->collection->remove(4); |
122
|
|
|
$this->assertFalse(array_key_exists(4, $this->collection->getCollection())); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @test |
127
|
|
|
*/ |
128
|
|
|
public function itShouldRemoveElement(): void |
129
|
|
|
{ |
130
|
|
|
$element = $this->collection->first(); |
131
|
|
|
$response = $this->collection->removeElement($element); |
132
|
|
|
$this->assertTrue($response); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @test |
137
|
|
|
*/ |
138
|
|
|
public function itShouldReturnContains(): void |
139
|
|
|
{ |
140
|
|
|
$element = $this->collection->last(); |
141
|
|
|
$this->assertTrue($this->collection->contains($element)); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @test |
146
|
|
|
*/ |
147
|
|
|
public function itShouldReturnElementByKey(): void |
148
|
|
|
{ |
149
|
|
|
$this->assertInstanceOf(ObjectForTest::class, $this->collection->get(3)); |
150
|
|
|
$this->assertNull($this->collection->get(25)); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @test |
155
|
|
|
*/ |
156
|
|
|
public function itShouldReturnKeys(): void |
157
|
|
|
{ |
158
|
|
|
$keys = $this->collection->getKeys(); |
159
|
|
|
$this->assertNotEmpty($keys); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @test |
164
|
|
|
*/ |
165
|
|
|
public function itShouldReturnValues(): void |
166
|
|
|
{ |
167
|
|
|
$values = $this->collection->getValues(); |
168
|
|
|
$this->assertNotEmpty($values); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @test |
173
|
|
|
*/ |
174
|
|
|
public function itShouldReturnCount(): void |
175
|
|
|
{ |
176
|
|
|
$this->assertSame(5, $this->collection->count()); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @test |
181
|
|
|
*/ |
182
|
|
|
public function itShouldSetObject(): void |
183
|
|
|
{ |
184
|
|
|
$new = new ObjectForTest(7); |
185
|
|
|
$this->collection->set(3, $new); |
186
|
|
|
$this->assertSame($this->collection->get(3)->getId(), $new->getId()); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @test |
191
|
|
|
*/ |
192
|
|
|
public function itShouldAddObject(): void |
193
|
|
|
{ |
194
|
|
|
$new = new ObjectForTest(25); |
195
|
|
|
$this->collection->add($new); |
196
|
|
|
$this->assertSame($new->getId(), $this->collection->last()->getId()); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @test |
201
|
|
|
*/ |
202
|
|
|
public function itShouldClearObjects(): void |
203
|
|
|
{ |
204
|
|
|
$this->assertFalse($this->collection->isEmpty()); |
205
|
|
|
$this->collection->clear(); |
206
|
|
|
$this->assertTrue($this->collection->isEmpty()); |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.