1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Cubiche package. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) Cubiche |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
namespace Cubiche\Core\Collections\Tests\Units; |
12
|
|
|
|
13
|
|
|
use Cubiche\Core\Collections\CollectionInterface; |
14
|
|
|
use Cubiche\Core\Comparable\Comparator; |
15
|
|
|
use Cubiche\Core\Specification\Criteria; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Collection Test Case class. |
19
|
|
|
* |
20
|
|
|
* @author Ivannis Suárez Jerez <[email protected]> |
21
|
|
|
* @author Karel Osorio Ramírez <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
abstract class CollectionTestCase extends TestCase |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @return \Cubiche\Core\Collections\CollectionInterface |
27
|
|
|
*/ |
28
|
|
|
protected function randomCollection($size = null) |
29
|
|
|
{ |
30
|
|
|
$collection = $this->emptyCollection(); |
31
|
|
|
$collection->addAll($this->randomValues($size)); |
32
|
|
|
|
33
|
|
|
return $collection; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param int $size |
38
|
|
|
* |
39
|
|
|
* @return mixed[] |
40
|
|
|
*/ |
41
|
|
|
protected function randomValues($size = null) |
42
|
|
|
{ |
43
|
|
|
$items = array(); |
44
|
|
|
if ($size === null) { |
45
|
|
|
$size = \rand(10, 20); |
46
|
|
|
} |
47
|
|
|
foreach (\range(0, $size - 1) as $value) { |
48
|
|
|
$items[$value] = $this->randomValue(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $items; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return CollectionInterface |
56
|
|
|
*/ |
57
|
|
|
abstract protected function emptyCollection(); |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return mixed |
61
|
|
|
*/ |
62
|
|
|
abstract protected function randomValue(); |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return mixed |
66
|
|
|
*/ |
67
|
|
|
abstract protected function uniqueValue(); |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return \Cubiche\Core\Comparable\ComparatorInterface |
71
|
|
|
*/ |
72
|
|
|
protected function comparator() |
73
|
|
|
{ |
74
|
|
|
return new Comparator(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Test class. |
79
|
|
|
*/ |
80
|
|
|
public function testClass() |
81
|
|
|
{ |
82
|
|
|
$this |
83
|
|
|
->testedClass |
84
|
|
|
->implements(CollectionInterface::class) |
85
|
|
|
; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Test add. |
90
|
|
|
*/ |
91
|
|
|
public function testAdd() |
92
|
|
|
{ |
93
|
|
|
$this |
94
|
|
|
->given($collection = $this->randomCollection()) |
95
|
|
|
->given($unique = $this->uniqueValue()) |
96
|
|
|
->let($count = $collection->count()) |
97
|
|
|
->when($collection->add($unique)) |
98
|
|
|
->collection($collection) |
99
|
|
|
->contains($unique) |
100
|
|
|
->size() |
101
|
|
|
->isEqualTo($count + 1) |
102
|
|
|
; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Test add. |
107
|
|
|
*/ |
108
|
|
|
public function testAddAll() |
109
|
|
|
{ |
110
|
|
|
$this |
111
|
|
|
->given($collection = $this->emptyCollection()) |
112
|
|
|
->given($items = $this->randomValues(10)) |
113
|
|
|
->when($collection->addAll($items)) |
114
|
|
|
->collection($collection) |
115
|
|
|
->containsValues($items) |
116
|
|
|
->size() |
117
|
|
|
->isEqualTo(\count($items)) |
118
|
|
|
; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Test remove. |
123
|
|
|
*/ |
124
|
|
|
public function testRemove() |
125
|
|
|
{ |
126
|
|
|
$this |
127
|
|
|
->given( |
128
|
|
|
$unique = $this->uniqueValue(), |
129
|
|
|
$emptyCollection = $this->emptyCollection() |
130
|
|
|
) |
131
|
|
|
->when($emptyCollection->add($unique)) |
132
|
|
|
->then() |
133
|
|
|
->collection($emptyCollection) |
134
|
|
|
->contains($unique) |
135
|
|
|
->and() |
136
|
|
|
->when($emptyCollection->remove($unique)) |
137
|
|
|
->then() |
138
|
|
|
->collection($emptyCollection) |
139
|
|
|
->notContains($unique) |
140
|
|
|
; |
141
|
|
|
|
142
|
|
|
$this |
143
|
|
|
->given( |
144
|
|
|
$unique = $this->uniqueValue(), |
145
|
|
|
$randomCollection = $this->randomCollection() |
146
|
|
|
) |
147
|
|
|
->when($randomCollection->add($unique)) |
148
|
|
|
->then() |
149
|
|
|
->collection($randomCollection) |
150
|
|
|
->contains($unique) |
151
|
|
|
->and() |
152
|
|
|
->when($randomCollection->remove($unique)) |
153
|
|
|
->then() |
154
|
|
|
->collection($randomCollection) |
155
|
|
|
->notContains($unique) |
156
|
|
|
; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Test clear. |
161
|
|
|
*/ |
162
|
|
|
public function testClear() |
163
|
|
|
{ |
164
|
|
|
$this |
165
|
|
|
->given($randomCollection = $this->randomCollection()) |
166
|
|
|
->then() |
167
|
|
|
->collection($randomCollection) |
168
|
|
|
->isNotEmpty() |
169
|
|
|
->and() |
170
|
|
|
->when($randomCollection->clear()) |
171
|
|
|
->then() |
172
|
|
|
->collection($randomCollection) |
173
|
|
|
->isEmpty() |
174
|
|
|
; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Test count. |
179
|
|
|
*/ |
180
|
|
|
public function testCount() |
181
|
|
|
{ |
182
|
|
|
$this |
183
|
|
|
->given($collection = $this->randomCollection(5)) |
184
|
|
|
->then() |
185
|
|
|
->collection($collection) |
186
|
|
|
->size() |
187
|
|
|
->isEqualTo(5) |
188
|
|
|
; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Test getIterator. |
193
|
|
|
*/ |
194
|
|
|
public function testGetIterator() |
195
|
|
|
{ |
196
|
|
|
$this |
197
|
|
|
->given($collection = $this->randomCollection()) |
198
|
|
|
->then |
199
|
|
|
->object($collection->getIterator()) |
200
|
|
|
->isInstanceOf(\Traversable::class) |
201
|
|
|
; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Test find. |
206
|
|
|
*/ |
207
|
|
|
public function testFind() |
208
|
|
|
{ |
209
|
|
|
$this |
210
|
|
|
->given( |
211
|
|
|
$unique = $this->uniqueValue(), |
212
|
|
|
$criteria = Criteria::eq($unique), |
213
|
|
|
$emptyCollection = $this->emptyCollection() |
214
|
|
|
) |
215
|
|
|
->when($findResult = $emptyCollection->find($criteria)) |
216
|
|
|
->then |
217
|
|
|
->collection($findResult) |
218
|
|
|
->isEmpty() |
219
|
|
|
->and() |
220
|
|
|
->when( |
221
|
|
|
$emptyCollection->add($unique), |
222
|
|
|
$findResult = $emptyCollection->find($criteria) |
223
|
|
|
) |
224
|
|
|
->then |
225
|
|
|
->collection($findResult) |
226
|
|
|
->contains($unique) |
227
|
|
|
; |
228
|
|
|
|
229
|
|
|
$this |
230
|
|
|
->given( |
231
|
|
|
$unique = $this->uniqueValue(), |
232
|
|
|
$criteria = Criteria::eq($unique), |
233
|
|
|
$randomCollection = $this->randomCollection() |
234
|
|
|
) |
235
|
|
|
->when($findResult = $randomCollection->find($criteria)) |
236
|
|
|
->then |
237
|
|
|
->collection($findResult) |
238
|
|
|
->isEmpty() |
239
|
|
|
->and() |
240
|
|
|
->when( |
241
|
|
|
$randomCollection->add($unique), |
242
|
|
|
$findResult = $randomCollection->find($criteria) |
243
|
|
|
) |
244
|
|
|
->then |
245
|
|
|
->collection($findResult) |
246
|
|
|
->contains($unique) |
247
|
|
|
; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Test findOne. |
252
|
|
|
*/ |
253
|
|
|
public function testFindOne() |
254
|
|
|
{ |
255
|
|
|
$this |
256
|
|
|
->given( |
257
|
|
|
$unique = $this->uniqueValue(), |
258
|
|
|
$criteria = Criteria::eq($unique), |
259
|
|
|
$emptyCollection = $this->emptyCollection() |
260
|
|
|
) |
261
|
|
|
->when($findResult = $emptyCollection->findOne($criteria)) |
262
|
|
|
->then |
263
|
|
|
->variable($findResult) |
264
|
|
|
->isNull() |
265
|
|
|
->and() |
266
|
|
|
->when( |
267
|
|
|
$emptyCollection->add($unique), |
268
|
|
|
$findResult = $emptyCollection->findOne($criteria) |
269
|
|
|
) |
270
|
|
|
->then() |
271
|
|
|
->variable($findResult) |
272
|
|
|
->isEqualTo($unique) |
273
|
|
|
; |
274
|
|
|
|
275
|
|
|
$this |
276
|
|
|
->given( |
277
|
|
|
$unique = $this->uniqueValue(), |
278
|
|
|
$criteria = Criteria::eq($unique), |
279
|
|
|
$randomCollection = $this->randomCollection() |
280
|
|
|
) |
281
|
|
|
->when($findResult = $randomCollection->findOne($criteria)) |
282
|
|
|
->then() |
283
|
|
|
->variable($findResult) |
284
|
|
|
->isNull() |
285
|
|
|
->and() |
286
|
|
|
->when( |
287
|
|
|
$randomCollection->add($unique), |
288
|
|
|
$findResult = $randomCollection->findOne($criteria) |
289
|
|
|
) |
290
|
|
|
->then() |
291
|
|
|
->variable($findResult) |
292
|
|
|
->isEqualTo($unique) |
293
|
|
|
; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* Test toArray. |
298
|
|
|
*/ |
299
|
|
View Code Duplication |
public function testToArray() |
|
|
|
|
300
|
|
|
{ |
301
|
|
|
$this |
302
|
|
|
->given($collection = $this->randomCollection()) |
303
|
|
|
->when($array = $collection->toArray()) |
304
|
|
|
->array($array) |
305
|
|
|
->isEqualTo(\iterator_to_array($collection->getIterator())); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* Test slice. |
310
|
|
|
*/ |
311
|
|
|
public function testSlice() |
312
|
|
|
{ |
313
|
|
|
$this |
314
|
|
|
->given($collection = $this->randomCollection()) |
315
|
|
|
->let($count = $collection->count()) |
316
|
|
|
->let($offset = rand(0, $count / 2)) |
317
|
|
|
->let($length = rand($count / 2, $count)) |
318
|
|
|
->let($maxCount = max([$count - $offset, 0])) |
319
|
|
|
->when($slice = $collection->slice($offset, $length)) |
320
|
|
|
->then() |
321
|
|
|
->collection($slice) |
322
|
|
|
->size() |
323
|
|
|
->isEqualTo(min($maxCount, $length)) |
324
|
|
|
; |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
/** |
328
|
|
|
* Test sorted. |
329
|
|
|
*/ |
330
|
|
|
public function testSorted() |
331
|
|
|
{ |
332
|
|
|
$this |
333
|
|
|
->given( |
334
|
|
|
$comparator = $this->comparator(), |
335
|
|
|
$reverseComparator = $comparator->reverse(), |
336
|
|
|
$collection = $this->randomCollection() |
337
|
|
|
) |
338
|
|
|
->when($sortedCollection = $collection->sorted($comparator)) |
339
|
|
|
->then() |
340
|
|
|
->collection($sortedCollection) |
341
|
|
|
->isSortedUsing($comparator) |
342
|
|
|
->collection($sortedCollection) |
343
|
|
|
->isNotSortedUsing($reverseComparator) |
344
|
|
|
; |
345
|
|
|
} |
346
|
|
|
} |
347
|
|
|
|
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.