-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathCollectionTest.php
More file actions
385 lines (329 loc) · 11.1 KB
/
Copy pathCollectionTest.php
File metadata and controls
385 lines (329 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
<?php
namespace Bow\Tests\Support;
use Bow\Support\Collection;
use Generator as PHPGenerator;
class CollectionTest extends \PHPUnit\Framework\TestCase
{
public function test_get_instance()
{
$collection = new Collection(range(1, 10));
$this->assertInstanceOf(Collection::class, $collection);
return $collection;
}
/**
* @param Collection $collection
* @depends test_get_instance
*/
public function test_sum(Collection $collection)
{
$this->assertEquals(array_sum(range(1, 10)), $collection->sum());
}
/**
* @param Collection $collection
* @depends test_get_instance
*/
public function test_max(Collection $collection)
{
$this->assertEquals(max(range(1, 10)), $collection->max());
}
/**
* @param Collection $collection
* @depends test_get_instance
*/
public function test_min(Collection $collection)
{
$this->assertEquals(min(range(1, 10)), $collection->min());
}
/**
* @param Collection $collection
* @depends test_get_instance
*/
public function test_count(Collection $collection)
{
// Create fresh collection to avoid mutations from previous tests
$collection = new Collection(range(1, 10));
$this->assertEquals(count(range(1, 10)), $collection->count());
}
public function test_pop()
{
$collection = new Collection(range(1, 10));
$this->assertEquals(10, $collection->pop());
}
public function test_shift()
{
$collection = new Collection(range(1, 10));
$this->assertEquals(1, $collection->shift());
}
public function test_reserve()
{
$collection = new Collection(range(1, 10));
$this->assertEquals(array_reverse(range(1, 10)), $collection->reverse()->toArray());
}
public function test_generator()
{
$collection = new Collection(range(1, 10));
$gen = $collection->yieldify();
$this->assertInstanceOf(PHPGenerator::class, $gen);
}
public function test_json()
{
$collection = new Collection(range(1, 10));
$this->assertJson($collection->toJson());
}
public function test_excepts()
{
$collection = new Collection(range(1, 10));
// excepts([0, 1]) keeps only items at indices 0 and 1, which are values 1 and 2
$result = $collection->excepts([0, 1])->toArray();
$this->assertEquals([0 => 1, 1 => 2], $result);
}
public function test_push()
{
$collection = new Collection(range(1, 9));
$collection->push(10);
$this->assertEquals(range(1, 10), $collection->toArray());
}
public function test_first()
{
$collection = new Collection([1, 2, 3, 4, 5]);
$this->assertEquals(1, $collection->first());
}
public function test_last()
{
$collection = new Collection([1, 2, 3, 4, 5]);
$this->assertEquals(5, $collection->last());
}
public function test_is_empty()
{
$collection = new Collection();
$this->assertTrue($collection->isEmpty());
$collection->push(1);
$this->assertFalse($collection->isEmpty());
}
public function test_length()
{
$collection = new Collection([1, 2, 3]);
$this->assertEquals(3, $collection->length());
}
public function test_values()
{
$collection = new Collection(['a' => 1, 'b' => 2, 'c' => 3]);
$values = $collection->values();
$this->assertInstanceOf(Collection::class, $values);
$this->assertEquals([1, 2, 3], $values->toArray());
}
public function test_keys()
{
$collection = new Collection(['a' => 1, 'b' => 2, 'c' => 3]);
$keys = $collection->keys();
$this->assertInstanceOf(Collection::class, $keys);
$this->assertEquals(['a', 'b', 'c'], $keys->toArray());
}
public function test_chunk()
{
$collection = new Collection([1, 2, 3, 4, 5, 6]);
$chunked = $collection->chunk(2);
$expected = [[1, 2], [3, 4], [5, 6]];
$this->assertEquals($expected, $chunked->all());
}
public function test_collectify()
{
$collection = new Collection(['items' => [1, 2, 3], 'count' => 3]);
$items = $collection->collectify('items');
$this->assertInstanceOf(Collection::class, $items);
$this->assertEquals([1, 2, 3], $items->toArray());
}
public function test_has()
{
$collection = new Collection(['name' => 'John', 'age' => 30]);
$this->assertTrue($collection->has('name'));
$this->assertFalse($collection->has('email'));
$this->assertTrue($collection->has('age', true));
}
public function test_each()
{
$collection = new Collection([1, 2, 3]);
$sum = 0;
$collection->each(function ($value) use (&$sum) {
$sum += $value;
});
$this->assertEquals(6, $sum);
}
public function test_merge()
{
$collection = new Collection([1, 2, 3]);
$merged = $collection->merge([4, 5, 6]);
$this->assertEquals([1, 2, 3, 4, 5, 6], $merged->toArray());
}
public function test_merge_with_collection()
{
$collection1 = new Collection([1, 2, 3]);
$collection2 = new Collection([4, 5, 6]);
$merged = $collection1->merge($collection2);
$this->assertEquals([1, 2, 3, 4, 5, 6], $merged->toArray());
}
public function test_map()
{
$collection = new Collection([1, 2, 3]);
$mapped = $collection->map(function ($value) {
return $value * 2;
});
$this->assertEquals([2, 4, 6], $mapped->toArray());
}
public function test_filter()
{
$collection = new Collection([1, 2, 3, 4, 5]);
$filtered = $collection->filter(function ($value) {
return $value > 3;
});
$this->assertEquals([4, 5], $filtered->toArray());
}
public function test_fill()
{
$collection = new Collection([1, 2, 3]);
$old = $collection->fill('x', 2);
$this->assertEquals([1, 2, 3], $old);
$this->assertEquals([1, 2, 3, 'x', 'x'], $collection->toArray());
}
public function test_reduce()
{
$collection = new Collection([1, 2, 3, 4]);
$result = $collection->reduce(function ($carry, $item) {
return $carry + $item;
}, 0);
$this->assertInstanceOf(Collection::class, $result);
}
public function test_implode()
{
$collection = new Collection(['a', 'b', 'c']);
$this->assertEquals('a,b,c', $collection->implode(','));
}
public function test_ignores()
{
$collection = new Collection(['a' => 1, 'b' => 2, 'c' => 3]);
$ignored = $collection->ignores(['b']);
$this->assertInstanceOf(Collection::class, $ignored);
$this->assertEquals(['a' => 1, 'c' => 3], $ignored->toArray());
}
public function test_update()
{
$collection = new Collection(['name' => 'John', 'age' => 30]);
$result = $collection->update('name', 'Jane');
$this->assertTrue($result);
$this->assertEquals('Jane', $collection->get('name'));
}
public function test_update_non_existing()
{
$collection = new Collection(['name' => 'John']);
$result = $collection->update('email', 'john@example.com');
$this->assertFalse($result);
}
public function test_all()
{
$data = ['a' => 1, 'b' => 2];
$collection = new Collection($data);
$this->assertEquals($data, $collection->all());
}
public function test_get()
{
$collection = new Collection(['name' => 'John', 'age' => 30]);
$this->assertEquals('John', $collection->get('name'));
$this->assertEquals('default', $collection->get('email', 'default'));
}
public function test_get_with_callback()
{
$collection = new Collection(['name' => 'John']);
$result = $collection->get('email', function () {
return 'no-email@example.com';
});
$this->assertEquals('no-email@example.com', $result);
}
public function test_set()
{
$collection = new Collection(['name' => 'John']);
$old = $collection->set('name', 'Jane');
$this->assertEquals('John', $old);
$this->assertEquals('Jane', $collection->get('name'));
}
public function test_set_new_key()
{
$collection = new Collection();
$old = $collection->set('name', 'John');
$this->assertNull($old);
$this->assertEquals('John', $collection->get('name'));
}
public function test_remove()
{
$collection = new Collection(['name' => 'John', 'age' => 30]);
$result = $collection->remove('name');
$this->assertInstanceOf(Collection::class, $result);
$this->assertFalse($collection->has('name'));
}
public function test_magic_get()
{
$collection = new Collection(['name' => 'John']);
$this->assertEquals('John', $collection->name);
}
public function test_magic_set()
{
$collection = new Collection();
$collection->name = 'John';
$this->assertEquals('John', $collection->get('name'));
}
public function test_magic_isset()
{
$collection = new Collection(['name' => 'John']);
$this->assertTrue(isset($collection->name));
$this->assertFalse(isset($collection->email));
}
public function test_magic_unset()
{
$collection = new Collection(['name' => 'John', 'age' => 30]);
unset($collection->name);
$this->assertFalse($collection->has('name'));
}
public function test_array_access_exists()
{
$collection = new Collection(['name' => 'John']);
$this->assertTrue(isset($collection['name']));
}
public function test_array_access_get()
{
$collection = new Collection(['name' => 'John']);
$this->assertEquals('John', $collection['name']);
}
public function test_array_access_set()
{
$collection = new Collection();
$collection['name'] = 'John';
$this->assertEquals('John', $collection->get('name'));
}
public function test_array_access_unset()
{
$collection = new Collection(['name' => 'John', 'age' => 30]);
unset($collection['name']);
$this->assertFalse($collection->has('name'));
}
public function test_iterator()
{
$data = ['a' => 1, 'b' => 2, 'c' => 3];
$collection = new Collection($data);
$result = [];
foreach ($collection as $key => $value) {
$result[$key] = $value;
}
$this->assertEquals($data, $result);
}
public function test_to_string()
{
$collection = new Collection(['name' => 'John', 'age' => 30]);
$json = (string)$collection;
$this->assertJson($json);
$this->assertEquals(['name' => 'John', 'age' => 30], json_decode($json, true));
}
public function test_json_serialize()
{
$collection = new Collection(['name' => 'John', 'age' => 30]);
$this->assertEquals(['name' => 'John', 'age' => 30], $collection->jsonSerialize());
}
}