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\ListInterface; |
14
|
|
|
use Cubiche\Core\Specification\Criteria; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* ListTestCase class. |
18
|
|
|
* |
19
|
|
|
* @method protected ListInterface emptyCollection() |
20
|
|
|
* |
21
|
|
|
* @author Ivannis Suárez Jerez <[email protected]> |
22
|
|
|
* @author Karel Osorio Ramírez <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
abstract class ListTestCase extends CollectionTestCase |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @param int $size |
28
|
|
|
* |
29
|
|
|
* @return ListInterface |
30
|
|
|
*/ |
31
|
|
|
protected function randomCollection($size = null) |
32
|
|
|
{ |
33
|
|
|
$collection = $this->emptyCollection(); |
34
|
|
|
$collection->addAll($this->randomValues($size)); |
35
|
|
|
|
36
|
|
|
return $collection; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Test class. |
41
|
|
|
*/ |
42
|
|
|
public function testClass() |
43
|
|
|
{ |
44
|
|
|
$this |
45
|
|
|
->testedClass |
46
|
|
|
->implements(ListInterface::class) |
47
|
|
|
; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Test add. |
52
|
|
|
*/ |
53
|
|
View Code Duplication |
public function testAdd() |
|
|
|
|
54
|
|
|
{ |
55
|
|
|
$this |
56
|
|
|
->given($collection = $this->randomCollection()) |
57
|
|
|
->and($unique = $this->uniqueValue()) |
58
|
|
|
->and($count = $collection->count()) |
59
|
|
|
->when($collection->add($unique)) |
60
|
|
|
->then() |
61
|
|
|
->list($collection) |
62
|
|
|
->contains($unique) |
63
|
|
|
->size() |
64
|
|
|
->isEqualTo($count + 1) |
65
|
|
|
; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Test add. |
70
|
|
|
*/ |
71
|
|
View Code Duplication |
public function testAddAll() |
|
|
|
|
72
|
|
|
{ |
73
|
|
|
$this |
74
|
|
|
->given($collection = $this->emptyCollection()) |
75
|
|
|
->and($items = $this->randomValues(10)) |
76
|
|
|
->when($collection->addAll($items)) |
77
|
|
|
->then() |
78
|
|
|
->list($collection) |
79
|
|
|
->containsValues($items) |
80
|
|
|
->size() |
81
|
|
|
->isEqualTo(\count($items)) |
82
|
|
|
->and() |
83
|
|
|
->then() |
84
|
|
|
->exception(function () use ($collection) { |
85
|
|
|
$collection->addAll(100); |
|
|
|
|
86
|
|
|
})->isInstanceOf(\InvalidArgumentException::class) |
87
|
|
|
; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Test remove. |
92
|
|
|
*/ |
93
|
|
View Code Duplication |
public function testRemove() |
|
|
|
|
94
|
|
|
{ |
95
|
|
|
$this |
96
|
|
|
->given( |
97
|
|
|
$unique = $this->uniqueValue(), |
98
|
|
|
$emptyCollection = $this->emptyCollection() |
99
|
|
|
) |
100
|
|
|
->when($emptyCollection->add($unique)) |
101
|
|
|
->then() |
102
|
|
|
->list($emptyCollection) |
103
|
|
|
->contains($unique) |
104
|
|
|
->and() |
105
|
|
|
->when($result = $emptyCollection->remove($unique)) |
106
|
|
|
->then() |
107
|
|
|
->list($emptyCollection) |
108
|
|
|
->notContains($unique) |
109
|
|
|
->boolean($result) |
110
|
|
|
->isTrue() |
111
|
|
|
; |
112
|
|
|
|
113
|
|
|
$this |
114
|
|
|
->given( |
115
|
|
|
$unique = $this->uniqueValue(), |
116
|
|
|
$randomCollection = $this->randomCollection() |
117
|
|
|
) |
118
|
|
|
->when($randomCollection->add($unique)) |
119
|
|
|
->then() |
120
|
|
|
->list($randomCollection) |
121
|
|
|
->contains($unique) |
122
|
|
|
->and() |
123
|
|
|
->when($randomCollection->remove($unique)) |
124
|
|
|
->then() |
125
|
|
|
->list($randomCollection) |
126
|
|
|
->notContains($unique) |
127
|
|
|
->and() |
128
|
|
|
->when($result = $randomCollection->remove('foo')) |
129
|
|
|
->then() |
130
|
|
|
->boolean($result) |
131
|
|
|
->isFalse() |
132
|
|
|
; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/* |
136
|
|
|
* Test removeAll. |
137
|
|
|
*/ |
138
|
|
View Code Duplication |
public function testRemoveAll() |
|
|
|
|
139
|
|
|
{ |
140
|
|
|
$this |
141
|
|
|
->given( |
142
|
|
|
$unique = $this->uniqueValue(), |
143
|
|
|
$random = $this->randomValue(), |
144
|
|
|
$collection = $this->emptyCollection() |
145
|
|
|
) |
146
|
|
|
->and($collection->addAll([$unique, $random])) |
147
|
|
|
->then() |
148
|
|
|
->list($collection) |
149
|
|
|
->contains($unique) |
150
|
|
|
->list($collection) |
151
|
|
|
->contains($random) |
152
|
|
|
->and() |
153
|
|
|
->when($collection->removeAll([$unique, $random])) |
154
|
|
|
->then() |
155
|
|
|
->boolean($collection->isEmpty()) |
156
|
|
|
->isTrue() |
157
|
|
|
; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Test find. |
162
|
|
|
*/ |
163
|
|
View Code Duplication |
public function testFind() |
|
|
|
|
164
|
|
|
{ |
165
|
|
|
parent::testFind(); |
166
|
|
|
|
167
|
|
|
$this |
168
|
|
|
->given( |
169
|
|
|
$unique = $this->uniqueValue(), |
170
|
|
|
$criteria = Criteria::same($unique), |
171
|
|
|
$emptyCollection = $this->emptyCollection() |
172
|
|
|
) |
173
|
|
|
->when($emptyCollection->add($unique)) |
174
|
|
|
->and($findResult = $emptyCollection->find($criteria)) |
175
|
|
|
->then() |
176
|
|
|
->list($findResult) |
177
|
|
|
->size() |
178
|
|
|
->isEqualTo(1) |
179
|
|
|
->array($findResult->toArray()) |
180
|
|
|
->contains($unique) |
181
|
|
|
; |
182
|
|
|
|
183
|
|
|
$this |
184
|
|
|
->given( |
185
|
|
|
$unique = $this->uniqueValue(), |
186
|
|
|
$criteria = Criteria::same($unique), |
187
|
|
|
$randomCollection = $this->randomCollection() |
188
|
|
|
) |
189
|
|
|
->when($randomCollection->add($unique)) |
190
|
|
|
->and($findResult = $randomCollection->find($criteria)) |
191
|
|
|
->then() |
192
|
|
|
->array($findResult->toArray()) |
193
|
|
|
->contains($unique) |
194
|
|
|
; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Test findOne. |
199
|
|
|
*/ |
200
|
|
View Code Duplication |
public function testFindOne() |
|
|
|
|
201
|
|
|
{ |
202
|
|
|
$this |
203
|
|
|
->given( |
204
|
|
|
$unique = $this->uniqueValue(), |
205
|
|
|
$criteria = Criteria::eq($unique), |
206
|
|
|
$emptyCollection = $this->emptyCollection() |
207
|
|
|
) |
208
|
|
|
->when($findResult = $emptyCollection->findOne($criteria)) |
209
|
|
|
->then() |
210
|
|
|
->variable($findResult) |
211
|
|
|
->isNull() |
212
|
|
|
->and() |
213
|
|
|
->when( |
214
|
|
|
$emptyCollection->add($unique), |
215
|
|
|
$findResult = $emptyCollection->findOne($criteria) |
216
|
|
|
) |
217
|
|
|
->then() |
218
|
|
|
->variable($findResult) |
219
|
|
|
->isEqualTo($unique) |
220
|
|
|
; |
221
|
|
|
|
222
|
|
|
$this |
223
|
|
|
->given( |
224
|
|
|
$unique = $this->uniqueValue(), |
225
|
|
|
$criteria = Criteria::eq($unique), |
226
|
|
|
$randomCollection = $this->randomCollection() |
227
|
|
|
) |
228
|
|
|
->when($findResult = $randomCollection->findOne($criteria)) |
229
|
|
|
->then() |
230
|
|
|
->variable($findResult) |
231
|
|
|
->isNull() |
232
|
|
|
->and() |
233
|
|
|
->when( |
234
|
|
|
$randomCollection->add($unique), |
235
|
|
|
$findResult = $randomCollection->findOne($criteria) |
236
|
|
|
) |
237
|
|
|
->then() |
238
|
|
|
->variable($findResult) |
239
|
|
|
->isEqualTo($unique) |
240
|
|
|
; |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|
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.