These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace spec\DusanKasan\Knapsack; |
||
4 | |||
5 | use ArrayIterator; |
||
6 | use DOMXPath; |
||
7 | use DusanKasan\Knapsack\Collection; |
||
8 | use DusanKasan\Knapsack\Exceptions\InvalidArgument; |
||
9 | use DusanKasan\Knapsack\Exceptions\ItemNotFound; |
||
10 | use DusanKasan\Knapsack\Exceptions\NoMoreItems; |
||
11 | use DusanKasan\Knapsack\Tests\Helpers\PlusOneAdder; |
||
12 | use Iterator; |
||
13 | use IteratorAggregate; |
||
14 | use PhpSpec\ObjectBehavior; |
||
15 | use function DusanKasan\Knapsack\reverse; |
||
16 | |||
17 | /** |
||
18 | * @mixin Collection |
||
19 | */ |
||
20 | class CollectionSpec extends ObjectBehavior |
||
21 | { |
||
22 | function let() |
||
23 | { |
||
24 | $this->beConstructedWith([1, 3, 3, 2,]); |
||
25 | } |
||
26 | |||
27 | function it_is_initializable() |
||
28 | { |
||
29 | $this->shouldHaveType(Collection::class); |
||
30 | $this->shouldHaveType(Iterator::class); |
||
31 | } |
||
32 | |||
33 | function it_can_be_instantiated_from_iterator() |
||
34 | { |
||
35 | $iterator = new ArrayIterator([1, 2]); |
||
36 | $this->beConstructedWith($iterator); |
||
37 | $this->toArray()->shouldReturn([1, 2]); |
||
38 | } |
||
39 | |||
40 | function it_can_be_instantiated_from_iterator_aggregate(IteratorAggregate $iteratorAggregate) |
||
41 | { |
||
42 | $iterator = new ArrayIterator([1, 2]); |
||
43 | $iteratorAggregate->getIterator()->willReturn($iterator); |
||
44 | $this->beConstructedWith($iteratorAggregate); |
||
45 | $this->toArray()->shouldReturn([1, 2]); |
||
46 | } |
||
47 | |||
48 | function it_can_be_instantiated_from_array() |
||
49 | { |
||
50 | $this->beConstructedWith([1, 2, 3]); |
||
51 | $this->toArray()->shouldReturn([1, 2, 3]); |
||
52 | } |
||
53 | |||
54 | function it_will_throw_when_passed_something_other_than_array_or_traversable() |
||
55 | { |
||
56 | $this->shouldThrow(InvalidArgument::class)->during('__construct', [1]); |
||
57 | } |
||
58 | |||
59 | function it_can_be_created_statically() |
||
60 | { |
||
61 | $this->beConstructedThrough('from', [[1, 2]]); |
||
62 | $this->toArray()->shouldReturn([1, 2]); |
||
63 | } |
||
64 | |||
65 | function it_can_be_created_to_iterate_over_function_infinitely() |
||
66 | { |
||
67 | $this->beConstructedThrough('iterate', [1, function($i) {return $i+1;}]); |
||
68 | $this->take(2)->toArray()->shouldReturn([1, 2]); |
||
69 | } |
||
70 | |||
71 | function it_can_be_created_to_iterate_over_function_non_infinitely() |
||
72 | { |
||
73 | $this->beConstructedThrough( |
||
74 | 'iterate', |
||
75 | [ |
||
76 | 1, |
||
77 | function($i) { |
||
78 | if ($i > 3) { |
||
79 | throw new NoMoreItems; |
||
80 | } |
||
81 | |||
82 | return $i+1; |
||
83 | } |
||
84 | ] |
||
85 | ); |
||
86 | $this->toArray()->shouldReturn([1, 2, 3, 4]); |
||
87 | } |
||
88 | |||
89 | function it_can_be_created_to_repeat_a_value_infinite_times() |
||
90 | { |
||
91 | $this->beConstructedThrough('repeat', [1]); |
||
92 | $this->take(2)->toArray()->shouldReturn([1, 1]); |
||
93 | } |
||
94 | |||
95 | function it_can_filter() |
||
96 | { |
||
97 | $this |
||
98 | ->filter(function ($item) { |
||
99 | return $item > 2; |
||
100 | }) |
||
101 | ->toArray() |
||
102 | ->shouldReturn([1 => 3, 2 => 3]); |
||
103 | |||
104 | $this |
||
105 | ->filter(function ($item, $key) { |
||
106 | return $key > 2 && $item < 3; |
||
107 | }) |
||
108 | ->toArray() |
||
109 | ->shouldReturn([3 => 2]); |
||
110 | } |
||
111 | |||
112 | function it_can_distinct() |
||
113 | { |
||
114 | $this |
||
115 | ->distinct() |
||
116 | ->toArray() |
||
117 | ->shouldReturn([1, 3, 3 => 2]); |
||
118 | } |
||
119 | |||
120 | function it_can_concat() |
||
121 | { |
||
122 | $c1 = $this->concat([4, 5]); |
||
123 | $c1->toArray()->shouldReturn([4, 5, 3, 2]); |
||
124 | $c1->size()->shouldReturn(6); |
||
125 | } |
||
126 | |||
127 | function it_can_map() |
||
128 | { |
||
129 | $this |
||
130 | ->map(function ($item) { |
||
131 | return $item + 1; |
||
132 | }) |
||
133 | ->toArray() |
||
134 | ->shouldReturn([2, 4, 4, 3]); |
||
135 | } |
||
136 | |||
137 | View Code Duplication | function it_can_reduce() |
|
138 | { |
||
139 | $this |
||
140 | ->reduce( |
||
141 | function ($temp, $item) { |
||
142 | return $temp + $item; |
||
143 | }, |
||
144 | 0 |
||
145 | ) |
||
146 | ->shouldReturn(9); |
||
147 | |||
148 | $this |
||
149 | ->reduce( |
||
150 | function ($temp, $item, $key) { |
||
151 | return $temp + $key + $item; |
||
152 | }, |
||
153 | 0 |
||
154 | ) |
||
155 | ->shouldReturn(15); |
||
156 | |||
157 | $this |
||
158 | ->reduce( |
||
159 | function (Collection $temp, $item) { |
||
160 | return $temp->append($item); |
||
161 | }, |
||
162 | new Collection([]) |
||
163 | ) |
||
164 | ->toArray() |
||
165 | ->shouldReturn([1, 3, 3, 2]); |
||
166 | } |
||
167 | |||
168 | function it_can_flatten() |
||
169 | { |
||
170 | $this->beConstructedWith([1, [2, [3]]]); |
||
171 | $this->flatten()->values()->toArray()->shouldReturn([1, 2, 3]); |
||
172 | $this->flatten(1)->values()->toArray()->shouldReturn([1, 2, [3]]); |
||
173 | } |
||
174 | |||
175 | function it_can_sort() |
||
176 | { |
||
177 | $this->beConstructedWith([3, 1, 2]); |
||
178 | |||
179 | $this |
||
180 | ->sort(function ($a, $b) { |
||
181 | return $a > $b; |
||
182 | }) |
||
183 | ->toArray() |
||
184 | ->shouldReturn([1 => 1, 2 => 2, 0 => 3]); |
||
185 | |||
186 | $this |
||
187 | ->sort(function ($v1, $v2, $k1, $k2) { |
||
188 | return $k1 < $k2 || $v1 == $v2; |
||
189 | }) |
||
190 | ->toArray() |
||
191 | ->shouldReturn([2 => 2, 1 => 1, 0 => 3]); |
||
192 | } |
||
193 | |||
194 | function it_can_slice() |
||
195 | { |
||
196 | $this->beConstructedWith([1, 2, 3, 4, 5]); |
||
197 | |||
198 | $this |
||
199 | ->slice(2, 4) |
||
200 | ->toArray() |
||
201 | ->shouldReturn([2 => 3, 3 => 4]); |
||
202 | |||
203 | $this |
||
204 | ->slice(4) |
||
205 | ->toArray() |
||
206 | ->shouldReturn([4 => 5]); |
||
207 | } |
||
208 | |||
209 | function it_can_group_by() |
||
210 | { |
||
211 | $this->beConstructedWith([1, 2, 3, 4, 5]); |
||
212 | |||
213 | $collection = $this->groupBy(function ($i) { |
||
214 | return $i % 2; |
||
215 | }); |
||
216 | |||
217 | $collection->get(0)->toArray()->shouldReturn([2, 4]); |
||
218 | $collection->get(1)->toArray()->shouldReturn([1, 3, 5]); |
||
219 | |||
220 | $collection = $this->groupBy(function ($k, $i) { |
||
221 | return ($k + $i) % 3; |
||
222 | }); |
||
223 | $collection->get(0)->toArray()->shouldReturn([2, 5]); |
||
224 | $collection->get(1)->toArray()->shouldReturn([1, 4]); |
||
225 | $collection->get(2)->toArray()->shouldReturn([3]); |
||
226 | } |
||
227 | |||
228 | function it_can_execute_callback_for_each_item(DOMXPath $a) |
||
229 | { |
||
230 | $a->query('asd')->shouldBeCalled(); |
||
231 | $this->beConstructedWith([$a]); |
||
232 | |||
233 | $this |
||
234 | ->each(function (DOMXPath $i) { |
||
235 | $i->query('asd'); |
||
236 | }) |
||
237 | ->toArray() |
||
238 | ->shouldReturn([$a]); |
||
239 | } |
||
240 | |||
241 | function it_can_get_size() |
||
242 | { |
||
243 | $this->size()->shouldReturn(4); |
||
244 | } |
||
245 | |||
246 | function it_can_get_item_by_key() |
||
247 | { |
||
248 | $this->beConstructedWith([1, [2], 3]); |
||
249 | $this->get(0)->shouldReturn(1); |
||
250 | $this->get(1, true)->first()->shouldReturn(2); |
||
251 | $this->get(1)->shouldReturn([2]); |
||
252 | $this->shouldThrow(new ItemNotFound)->during('get', [5]); |
||
253 | } |
||
254 | |||
255 | function it_can_get_item_by_key_or_return_default() |
||
256 | { |
||
257 | $this->beConstructedWith([1, [2], 3]); |
||
258 | $this->getOrDefault(0)->shouldReturn(1); |
||
259 | $this->getOrDefault(1, null, true)->first()->shouldReturn(2); |
||
260 | $this->getOrDefault(1, null)->shouldReturn([2]); |
||
261 | $this->getOrDefault(5)->shouldReturn(null); |
||
262 | $this->getOrDefault(5, 'not found')->shouldReturn('not found'); |
||
263 | } |
||
264 | |||
265 | function it_can_get_nth_item() |
||
266 | { |
||
267 | $this->beConstructedWith([1, [2], 3]); |
||
268 | $this->getNth(0)->shouldReturn(1); |
||
269 | $this->getNth(1, true)->first()->shouldReturn(2); |
||
270 | $this->getNth(1)->shouldReturn([2]); |
||
271 | } |
||
272 | |||
273 | function it_can_find() |
||
274 | { |
||
275 | $this->beConstructedWith([1, 3, 3, 2, [5]]); |
||
276 | |||
277 | $this |
||
278 | ->find(function ($v) { |
||
279 | return $v < 3; |
||
280 | }) |
||
281 | ->shouldReturn(1); |
||
282 | |||
283 | $this |
||
284 | ->find(function ($v, $k) { |
||
285 | return $v < 3 && $k > 1; |
||
286 | }) |
||
287 | ->shouldReturn(2); |
||
288 | |||
289 | $this |
||
290 | ->find(function ($v) { |
||
291 | return $v < 0; |
||
292 | }) |
||
293 | ->shouldReturn(null); |
||
294 | |||
295 | $this |
||
296 | ->find( |
||
297 | function ($v) { |
||
298 | return $v < 0; |
||
299 | }, |
||
300 | 'not found' |
||
301 | ) |
||
302 | ->shouldReturn('not found'); |
||
303 | |||
304 | $this->find('\DusanKasan\Knapsack\isCollection', null, true)->first()->shouldReturn(5); |
||
305 | $this->find('\DusanKasan\Knapsack\isCollection')->shouldReturn([5]); |
||
306 | } |
||
307 | |||
308 | function it_can_count_by() |
||
309 | { |
||
310 | $this |
||
311 | ->countBy(function ($v) { |
||
312 | return $v % 2 == 0 ? 'even' : 'odd'; |
||
313 | }) |
||
314 | ->toArray() |
||
315 | ->shouldReturn(['odd' => 3, 'even' => 1]); |
||
316 | |||
317 | $this |
||
318 | ->countBy(function ($k, $v) { |
||
319 | return ($k + $v) % 2 == 0 ? 'even' : 'odd'; |
||
320 | }) |
||
321 | ->toArray() |
||
322 | ->shouldReturn(['odd' => 3, 'even' => 1]); |
||
323 | } |
||
324 | |||
325 | function it_can_index_by() |
||
326 | { |
||
327 | $this |
||
328 | ->indexBy(function ($v) { |
||
329 | return $v; |
||
330 | }) |
||
331 | ->toArray() |
||
332 | ->shouldReturn([1 => 1, 3 => 3, 2 => 2]); |
||
333 | |||
334 | $this |
||
335 | ->indexBy(function ($v, $k) { |
||
336 | return $k . $v; |
||
337 | }) |
||
338 | ->toArray() |
||
339 | ->shouldReturn(['01' => 1, '13' => 3, '23' => 3, '32' => 2]); |
||
340 | } |
||
341 | |||
342 | function it_can_check_if_every_item_passes_predicament_test() |
||
343 | { |
||
344 | $this |
||
345 | ->every(function ($v) { |
||
346 | return $v > 0; |
||
347 | }) |
||
348 | ->shouldReturn(true); |
||
349 | |||
350 | $this |
||
351 | ->every(function ($v) { |
||
352 | return $v > 1; |
||
353 | }) |
||
354 | ->shouldReturn(false); |
||
355 | |||
356 | $this |
||
357 | ->every(function ($v, $k) { |
||
358 | return $v > 0 && $k >= 0; |
||
359 | }) |
||
360 | ->shouldReturn(true); |
||
361 | |||
362 | $this |
||
363 | ->every(function ($v, $k) { |
||
364 | return $v > 0 && $k > 0; |
||
365 | }) |
||
366 | ->shouldReturn(false); |
||
367 | } |
||
368 | |||
369 | function it_can_check_if_some_items_pass_predicament_test() |
||
370 | { |
||
371 | $this |
||
372 | ->some(function ($v) { |
||
373 | return $v < -1; |
||
374 | }) |
||
375 | ->shouldReturn(false); |
||
376 | |||
377 | $this |
||
378 | ->some(function ($v, $k) { |
||
379 | return $v > 0 && $k < -1; |
||
380 | }) |
||
381 | ->shouldReturn(false); |
||
382 | |||
383 | $this |
||
384 | ->some(function ($v) { |
||
385 | return $v < 2; |
||
386 | }) |
||
387 | ->shouldReturn(true); |
||
388 | |||
389 | $this |
||
390 | ->some(function ($v, $k) { |
||
391 | return $v > 0 && $k > 0; |
||
392 | }) |
||
393 | ->shouldReturn(true); |
||
394 | } |
||
395 | |||
396 | function it_can_check_if_it_contains_a_value() |
||
397 | { |
||
398 | $this |
||
399 | ->contains(3) |
||
400 | ->shouldReturn(true); |
||
401 | |||
402 | $this |
||
403 | ->contains(true) |
||
404 | ->shouldReturn(false); |
||
405 | } |
||
406 | |||
407 | function it_can_reverse() |
||
408 | { |
||
409 | $it = $this->reverse(); |
||
410 | |||
411 | $it->rewind(); |
||
412 | $it->valid()->shouldReturn(true); |
||
413 | $it->key()->shouldReturn(3); |
||
414 | $it->current()->shouldReturn(2); |
||
415 | $it->next(); |
||
416 | $it->valid()->shouldReturn(true); |
||
417 | $it->key()->shouldReturn(2); |
||
418 | $it->current()->shouldReturn(3); |
||
419 | $it->next(); |
||
420 | $it->valid()->shouldReturn(true); |
||
421 | $it->key()->shouldReturn(1); |
||
422 | $it->current()->shouldReturn(3); |
||
423 | $it->next(); |
||
424 | $it->valid()->shouldReturn(true); |
||
425 | $it->key()->shouldReturn(0); |
||
426 | $it->current()->shouldReturn(1); |
||
427 | $it->next(); |
||
428 | $it->valid()->shouldReturn(false); |
||
429 | } |
||
430 | |||
431 | View Code Duplication | function it_can_reduce_from_right() |
|
432 | { |
||
433 | $this |
||
434 | ->reduceRight( |
||
435 | function ($temp, $e) { |
||
436 | return $temp . $e; |
||
437 | }, |
||
438 | 0 |
||
439 | ) |
||
440 | ->shouldReturn('02331'); |
||
441 | |||
442 | $this |
||
443 | ->reduceRight( |
||
444 | function ($temp, $key, $item) { |
||
445 | return $temp + $key + $item; |
||
446 | }, |
||
447 | 0 |
||
448 | ) |
||
449 | ->shouldReturn(15); |
||
450 | |||
451 | $this |
||
452 | ->reduceRight( |
||
453 | function (Collection $temp, $item) { |
||
454 | return $temp->append($item); |
||
455 | }, |
||
456 | new Collection([]) |
||
457 | ) |
||
458 | ->toArray() |
||
459 | ->shouldReturn([2, 3, 3, 1]); |
||
460 | } |
||
461 | |||
462 | function it_can_return_only_first_x_elements() |
||
463 | { |
||
464 | $this->take(2) |
||
465 | ->toArray() |
||
466 | ->shouldReturn([1, 3]); |
||
467 | } |
||
468 | |||
469 | function it_can_skip_first_x_elements() |
||
470 | { |
||
471 | $this->drop(2) |
||
472 | ->toArray() |
||
473 | ->shouldReturn([2 => 3, 3 => 2]); |
||
474 | } |
||
475 | |||
476 | function it_can_return_values() |
||
477 | { |
||
478 | $this->beConstructedWith(['a' => 1, 'b' => 2]); |
||
479 | $this->values()->toArray()->shouldReturn([1, 2]); |
||
480 | } |
||
481 | |||
482 | |||
483 | function it_can_reject_elements_from_collection() |
||
484 | { |
||
485 | $this |
||
1 ignored issue
–
show
|
|||
486 | ->reject(function ($v) { |
||
487 | return $v == 3; |
||
488 | }) |
||
489 | ->toArray() |
||
490 | ->shouldReturn([1, 3 => 2]); |
||
491 | |||
492 | $this |
||
1 ignored issue
–
show
The method
reject() does not exist on spec\DusanKasan\Knapsack\CollectionSpec . Did you maybe mean it_can_reject_elements_from_collection() ?
This check marks calls to methods that do not seem to exist on an object. This is most likely the result of a method being renamed without all references to it being renamed likewise. ![]() |
|||
493 | ->reject(function ($v, $k) { |
||
494 | return $k == 2 && $v == 3; |
||
495 | }) |
||
496 | ->toArray() |
||
497 | ->shouldReturn([1, 3, 3 => 2]); |
||
498 | } |
||
499 | |||
500 | function it_can_get_keys() |
||
501 | { |
||
502 | $this |
||
503 | ->keys() |
||
504 | ->toArray() |
||
505 | ->shouldReturn([0, 1, 2, 3]); |
||
506 | } |
||
507 | |||
508 | View Code Duplication | function it_can_interpose() |
|
509 | { |
||
510 | $this |
||
511 | ->interpose('a') |
||
512 | ->values() |
||
513 | ->toArray() |
||
514 | ->shouldReturn([1, 'a', 3, 'a', 3, 'a', 2]); |
||
515 | } |
||
516 | |||
517 | function it_can_drop_elements_from_end_of_the_collection() |
||
518 | { |
||
519 | $this |
||
520 | ->dropLast() |
||
521 | ->toArray() |
||
522 | ->shouldReturn([1, 3, 3]); |
||
523 | |||
524 | $this |
||
525 | ->dropLast(2) |
||
526 | ->toArray() |
||
527 | ->shouldReturn([1, 3]); |
||
528 | } |
||
529 | |||
530 | function it_can_interleave_elements() |
||
531 | { |
||
532 | $this |
||
533 | ->interleave(['a', 'b', 'c', 'd', 'e']) |
||
534 | ->values() |
||
535 | ->toArray() |
||
536 | ->shouldReturn([1, 'a', 3, 'b', 3, 'c', 2, 'd', 'e']); |
||
537 | } |
||
538 | |||
539 | View Code Duplication | function it_can_repeat_items_of_collection_infinitely() |
|
540 | { |
||
541 | $this |
||
542 | ->cycle() |
||
543 | ->take(8) |
||
544 | ->values() |
||
545 | ->toArray() |
||
546 | ->shouldReturn([1, 3, 3, 2, 1, 3, 3, 2]); |
||
547 | } |
||
548 | |||
549 | function it_can_prepend_item() |
||
550 | { |
||
551 | $this |
||
552 | ->prepend(1) |
||
553 | ->values() |
||
554 | ->toArray() |
||
555 | ->shouldReturn([1, 1, 3, 3, 2]); |
||
556 | } |
||
557 | |||
558 | function it_can_prepend_item_with_key() |
||
559 | { |
||
560 | $this |
||
561 | ->prepend(1, 'a') |
||
562 | ->toArray() |
||
563 | ->shouldReturn(['a' => 1, 0 => 1, 1 => 3, 2 => 3, 3 => 2]); |
||
564 | } |
||
565 | |||
566 | function it_can_append_item() |
||
567 | { |
||
568 | $this |
||
569 | ->append(1) |
||
570 | ->values() |
||
571 | ->toArray() |
||
572 | ->shouldReturn([1, 3, 3, 2, 1]); |
||
573 | } |
||
574 | |||
575 | function it_can_append_item_with_key() |
||
576 | { |
||
577 | $this |
||
578 | ->append(1, 'a') |
||
579 | ->toArray() |
||
580 | ->shouldReturn([1, 3, 3, 2, 'a' => 1]); |
||
581 | } |
||
582 | |||
583 | function it_can_drop_items_while_predicament_is_true() |
||
584 | { |
||
585 | $this |
||
586 | ->dropWhile(function ($v) { |
||
587 | return $v < 3; |
||
588 | }) |
||
589 | ->toArray() |
||
590 | ->shouldReturn([1 => 3, 2 => 3, 3 => 2]); |
||
591 | |||
592 | $this |
||
593 | ->dropWhile(function ($v, $k) { |
||
594 | return $k < 2 && $v < 3; |
||
595 | }) |
||
596 | ->toArray() |
||
597 | ->shouldReturn([1 => 3, 2 => 3, 3 => 2]); |
||
598 | } |
||
599 | |||
600 | function it_can_map_and_then_concatenate_a_collection() |
||
601 | { |
||
602 | $this |
||
603 | ->mapcat(function ($v) { |
||
604 | return [[$v]]; |
||
605 | }) |
||
606 | ->values() |
||
607 | ->toArray() |
||
608 | ->shouldReturn([[1], [3], [3], [2]]); |
||
609 | |||
610 | $this |
||
611 | ->mapcat(function ($v, $k) { |
||
612 | return [[$k + $v]]; |
||
613 | }) |
||
614 | ->values() |
||
615 | ->toArray() |
||
616 | ->shouldReturn([[1], [4], [5], [5]]); |
||
617 | } |
||
618 | |||
619 | function it_can_take_items_while_predicament_is_true() |
||
620 | { |
||
621 | $this |
||
622 | ->takeWhile(function ($v) { |
||
623 | return $v < 3; |
||
624 | }) |
||
625 | ->toArray() |
||
626 | ->shouldReturn([1]); |
||
627 | |||
628 | $this |
||
629 | ->takeWhile(function ($v, $k) { |
||
630 | return $k < 2 && $v < 3; |
||
631 | }) |
||
632 | ->toArray() |
||
633 | ->shouldReturn([1]); |
||
634 | } |
||
635 | |||
636 | function it_can_split_the_collection_at_nth_item() |
||
637 | { |
||
638 | $this->splitAt(2)->size()->shouldBe(2); |
||
639 | $this->splitAt(2)->first()->toArray()->shouldBeLike([1, 3]); |
||
640 | $this->splitAt(2)->getNth(1)->toArray()->shouldBeLike([2 => 3, 3 => 2]); |
||
641 | } |
||
642 | |||
643 | function it_can_split_the_collection_with_predicament() |
||
644 | { |
||
645 | $s1 = $this->splitWith(function ($v) { |
||
646 | return $v < 3; |
||
647 | }); |
||
648 | |||
649 | $s1->size()->shouldBe(2); |
||
650 | $s1->first()->toArray()->shouldBe([1]); |
||
651 | $s1->getNth(1)->toArray()->shouldBe([1 => 3, 2 => 3, 3 => 2]); |
||
652 | |||
653 | $s2 = $this->splitWith(function ($v, $k) { |
||
654 | return $v < 2 && $k < 3; |
||
655 | }); |
||
656 | |||
657 | $s2->size()->shouldBe(2); |
||
658 | $s2->first()->toArray()->shouldBe([1]); |
||
659 | $s2->getNth(1)->toArray()->shouldBe([1 => 3, 2 => 3, 3 => 2]); |
||
660 | } |
||
661 | |||
662 | function it_can_replace_items_by_items_from_another_collection() |
||
663 | { |
||
664 | $this |
||
665 | ->replace([3 => 'a']) |
||
666 | ->toArray() |
||
667 | ->shouldReturn([1, 'a', 'a', 2]); |
||
668 | } |
||
669 | |||
670 | function it_can_get_reduction_steps() |
||
671 | { |
||
672 | $this |
||
673 | ->reductions( |
||
674 | function ($tmp, $i) { |
||
675 | return $tmp + $i; |
||
676 | }, |
||
677 | 0 |
||
678 | ) |
||
679 | ->toArray() |
||
680 | ->shouldReturn([0, 1, 4, 7, 9]); |
||
681 | } |
||
682 | |||
683 | function it_can_return_every_nth_item() |
||
684 | { |
||
685 | $this->takeNth(2) |
||
686 | ->toArray() |
||
687 | ->shouldReturn([1, 2 => 3]); |
||
688 | } |
||
689 | |||
690 | function it_can_shuffle_itself() |
||
691 | { |
||
692 | $this |
||
693 | ->shuffle() |
||
694 | ->reduce( |
||
695 | function ($tmp, $i) { |
||
696 | return $tmp + $i; |
||
697 | }, |
||
698 | 0 |
||
699 | ) |
||
700 | ->shouldReturn(9); |
||
701 | } |
||
702 | |||
703 | function it_can_partition() |
||
704 | { |
||
705 | $s1 = $this->partition(3, 2, [0, 1]); |
||
706 | $s1->size()->shouldBe(2); |
||
707 | $s1->first()->toArray()->shouldBe([1, 3, 3]); |
||
708 | $s1->getNth(1)->toArray()->shouldBe([2 => 3, 3 => 2, 0 => 0]); |
||
709 | |||
710 | $s2 = $this->partition(3, 2); |
||
711 | $s2->size()->shouldBe(2); |
||
712 | $s2->first()->toArray()->shouldBe([1, 3, 3]); |
||
713 | $s2->getNth(1)->toArray()->shouldBe([2 => 3, 3 => 2]); |
||
714 | |||
715 | $s3 = $this->partition(3); |
||
716 | $s3->size()->shouldBe(2); |
||
717 | $s3->first()->toArray()->shouldBe([1, 3, 3]); |
||
718 | $s3->getNth(1)->toArray()->shouldBe([3 => 2]); |
||
719 | |||
720 | $s4 = $this->partition(1, 3); |
||
721 | $s4->size()->shouldBe(2); |
||
722 | $s4->first()->toArray()->shouldBe([1,]); |
||
723 | $s4->getNth(1)->toArray()->shouldBe([3 => 2]); |
||
724 | } |
||
725 | |||
726 | function it_can_partition_by() |
||
727 | { |
||
728 | $this->beConstructedWith([1, 3, 3, 2]); |
||
729 | |||
730 | $s1 = $this->partitionBy(function ($v) { |
||
731 | return $v % 3 == 0; |
||
732 | }); |
||
733 | $s1->size()->shouldBe(3); |
||
734 | $s1->first()->toArray()->shouldBe([1]); |
||
735 | $s1->getNth(1)->toArray()->shouldBe([1 => 3, 2 => 3]); |
||
736 | $s1->getNth(2)->toArray()->shouldBe([3 => 2]); |
||
737 | |||
738 | |||
739 | $s2 = $this->partitionBy(function ($v, $k) { |
||
740 | return $k - $v; |
||
741 | }); |
||
742 | $s2->size()->shouldBe(4); |
||
743 | $s2->first()->toArray()->shouldBe([1]); |
||
744 | $s2->getNth(1)->toArray()->shouldBe([1 => 3]); |
||
745 | $s2->getNth(2)->toArray()->shouldBe([2 => 3]); |
||
746 | $s2->getNth(3)->toArray()->shouldBe([3 => 2]); |
||
747 | } |
||
748 | |||
749 | function it_can_get_nth_value() |
||
750 | { |
||
751 | $this->getNth(0)->shouldReturn(1); |
||
752 | $this->getNth(3)->shouldReturn(2); |
||
753 | } |
||
754 | |||
755 | function it_can_pluck() |
||
756 | { |
||
757 | $this->beConstructedWith([['a' => 1], ['a' => 2, 'b' => 3]]); |
||
758 | $this->pluck('a')->values()->toArray()->shouldReturn([1, 2]); |
||
759 | } |
||
760 | |||
761 | function it_can_create_infinite_collection_of_repeated_values() |
||
762 | { |
||
763 | $this->beConstructedThrough('repeat', [1]); |
||
764 | $this->take(3)->toArray()->shouldReturn([1, 1, 1]); |
||
765 | } |
||
766 | |||
767 | function it_can_create_finite_collection_of_repeated_values() |
||
768 | { |
||
769 | $this->beConstructedThrough('repeat', [1, 1]); |
||
770 | $this->toArray()->shouldReturn([1]); |
||
771 | } |
||
772 | |||
773 | function it_can_create_range_from_value_to_infinity() |
||
774 | { |
||
775 | $this->beConstructedThrough('range', [5]); |
||
776 | $this->take(2)->toArray()->shouldReturn([5, 6]); |
||
777 | } |
||
778 | |||
779 | function it_can_create_range_from_value_to_another_value() |
||
780 | { |
||
781 | $this->beConstructedThrough('range', [5, 6]); |
||
782 | $this->take(4)->toArray()->shouldReturn([5, 6]); |
||
783 | } |
||
784 | |||
785 | function it_can_check_if_it_is_not_empty() |
||
786 | { |
||
787 | $this->isEmpty()->shouldReturn(false); |
||
788 | $this->isNotEmpty()->shouldReturn(true); |
||
789 | } |
||
790 | |||
791 | function it_can_check_if_it_is_empty() |
||
792 | { |
||
793 | $this->beConstructedWith([]); |
||
794 | |||
795 | $this->isEmpty()->shouldReturn(true); |
||
796 | $this->isNotEmpty()->shouldReturn(false); |
||
797 | } |
||
798 | |||
799 | function it_can_check_frequency_of_distinct_items() |
||
800 | { |
||
801 | $this |
||
802 | ->frequencies() |
||
803 | ->toArray() |
||
804 | ->shouldReturn([1 => 1, 3 => 2, 2 => 1]); |
||
805 | } |
||
806 | |||
807 | function it_can_get_first_item() |
||
808 | { |
||
809 | $this->beConstructedWith([1, [2], 3]); |
||
810 | $this->first()->shouldReturn(1); |
||
811 | $this->drop(1)->first()->toArray()->shouldReturn([2]); |
||
812 | } |
||
813 | |||
814 | function it_will_throw_when_trying_to_get_first_item_of_empty_collection() |
||
815 | { |
||
816 | $this->beConstructedWith([]); |
||
817 | $this->shouldThrow(ItemNotFound::class)->during('first'); |
||
818 | } |
||
819 | |||
820 | function it_can_get_last_item() |
||
821 | { |
||
822 | $this->beConstructedWith([1, [2], 3]); |
||
823 | $this->last()->shouldReturn(3); |
||
824 | $this->take(2)->last()->toArray()->shouldReturn([2]); |
||
825 | } |
||
826 | |||
827 | function it_will_throw_when_trying_to_get_last_item_of_empty_collection() |
||
828 | { |
||
829 | $this->beConstructedWith([]); |
||
830 | $this->shouldThrow(ItemNotFound::class)->during('last'); |
||
831 | } |
||
832 | |||
833 | function it_can_realize_the_collection(PlusOneAdder $adder) |
||
834 | { |
||
835 | $adder->dynamicMethod(1)->willReturn(2); |
||
836 | $adder->dynamicMethod(2)->willReturn(3); |
||
837 | |||
838 | $this->beConstructedWith(function () use ($adder) { |
||
839 | yield $adder->dynamicMethod(1); |
||
840 | yield $adder->dynamicMethod(2); |
||
841 | }); |
||
842 | |||
843 | $this->realize(); |
||
844 | } |
||
845 | } |
||
846 |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.