-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollectionMergeOperationTest.php
More file actions
116 lines (91 loc) · 4.03 KB
/
CollectionMergeOperationTest.php
File metadata and controls
116 lines (91 loc) · 4.03 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
<?php
declare(strict_types=1);
namespace Test\TinyBlocks\Collection\Internal\Operations\Write;
use PHPUnit\Framework\TestCase;
use TinyBlocks\Collection\Collection;
final class CollectionMergeOperationTest extends TestCase
{
public function testMergeEmptyCollections(): void
{
/** @Given two empty collections */
$collectionA = Collection::createFromEmpty();
$collectionB = Collection::createFromEmpty();
/** @When merging the two collections */
$actual = $collectionA->merge(other: $collectionB);
/** @Then the result should be an empty collection */
self::assertEmpty($actual->toArray());
}
public function testMergeIntoEmptyCollection(): void
{
/** @Given an empty collection and a non-empty collection */
$collectionA = Collection::createFromEmpty();
$collectionB = Collection::createFrom(elements: [4, 5, 6]);
/** @When merging the non-empty collection into the empty one */
$actual = $collectionA->merge(other: $collectionB);
/** @Then the result should contain only the elements from the non-empty collection */
self::assertSame([4, 5, 6], $actual->toArray());
}
public function testMergeWithEmptyCollection(): void
{
/** @Given a non-empty collection and an empty collection */
$collectionA = Collection::createFrom(elements: [1, 2, 3]);
$collectionB = Collection::createFromEmpty();
/** @When merging the empty collection into the non-empty one */
$actual = $collectionA->merge(other: $collectionB);
/** @Then the result should contain only the original elements */
self::assertSame([1, 2, 3], $actual->toArray());
}
public function testMergeTwoCollections(): void
{
/** @Given two collections with distinct elements */
$collectionA = Collection::createFrom(elements: [1, 2, 3]);
$collectionB = Collection::createFrom(elements: [4, 5, 6]);
/** @When merging collection B into collection A */
$actual = $collectionA->merge(other: $collectionB);
/** @Then the result should contain all elements in order */
self::assertSame([1, 2, 3, 4, 5, 6], $actual->toArray());
}
public function testMergePreservesLazyEvaluation(): void
{
/** @Given two collections created from generators */
$collectionA = Collection::createFrom(
elements: (static function () {
yield 1;
yield 2;
})()
);
$collectionB = Collection::createFrom(
elements: (static function () {
yield 3;
yield 4;
})()
);
/** @When merging and retrieving only the first element */
$actual = $collectionA->merge(other: $collectionB)->first();
/** @Then the first element should be from collection A without materializing all elements */
self::assertSame(1, $actual);
}
public function testMergeMultipleCollections(): void
{
/** @Given three collections */
$collectionA = Collection::createFrom(elements: [1, 2]);
$collectionB = Collection::createFrom(elements: [3, 4]);
$collectionC = Collection::createFrom(elements: [5, 6]);
/** @When chaining multiple merge operations */
$actual = $collectionA
->merge(other: $collectionB)
->merge(other: $collectionC);
/** @Then the result should contain all elements in order */
self::assertSame([1, 2, 3, 4, 5, 6], $actual->toArray());
}
public function testMergeWithDuplicateElements(): void
{
/** @Given two collections with overlapping elements */
$collectionA = Collection::createFrom(elements: [1, 2, 3]);
$collectionB = Collection::createFrom(elements: [3, 4, 5]);
/** @When merging the collections */
$actual = $collectionA->merge(other: $collectionB);
/** @Then the result should contain all elements including duplicates */
self::assertSame([1, 2, 3, 3, 4, 5], $actual->toArray());
}
}