-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollectionIteratorTest.php
More file actions
63 lines (47 loc) · 2.24 KB
/
CollectionIteratorTest.php
File metadata and controls
63 lines (47 loc) · 2.24 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
<?php
declare(strict_types=1);
namespace Test\TinyBlocks\Collection;
use PHPUnit\Framework\TestCase;
use TinyBlocks\Collection\Collection;
final class CollectionIteratorTest extends TestCase
{
public function testIteratorShouldBeReusedIfNoModification(): void
{
/** @Given a collection with elements */
$collection = Collection::createFrom(elements: [1, 2, 3]);
/** @When retrieving the iterator for the first time */
$iterator = $collection->getIterator();
/** @And calling a method that does not modify the collection */
$count = $collection->count();
/** @Then the iterator should not be the same (due to lazy generation) */
self::assertSame(3, $count);
self::assertNotSame($iterator, $collection->getIterator());
/** @And the collection should remain unchanged */
self::assertSame([1, 2, 3], iterator_to_array($collection->getIterator()));
}
public function testIteratorShouldBeRecreatedAfterModification(): void
{
/** @Given a collection with elements */
$collection = Collection::createFrom(elements: [1, 2, 3]);
/** @When retrieving the iterator for the first time */
$iterator = $collection->getIterator();
/** @And modifying the collection */
$collection->add(elements: 4);
/** @Then the iterator should be recreated */
self::assertSame(4, $collection->count());
self::assertNotSame($iterator, $collection->getIterator());
/** @And the elements should be correct after modification */
self::assertSame([1, 2, 3, 4], iterator_to_array($collection->getIterator()));
}
public function testIteratorRemainsUnchangedWithUnrelatedOperations(): void
{
/** @Given a collection with elements */
$collection = Collection::createFrom(elements: [1, 2, 3]);
/** @When performing operations that do not modify the collection */
$firstIterator = $collection->getIterator();
/** @Then the iterator should remain unchanged */
self::assertFalse($collection->isEmpty());
self::assertSame([1, 2, 3], iterator_to_array($firstIterator));
self::assertSame([1, 2, 3], iterator_to_array($collection->getIterator()));
}
}