-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJoinTest.php
105 lines (94 loc) · 2.75 KB
/
JoinTest.php
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
<?php
declare(strict_types=1);
namespace BenCondaTest\Collection\Modifier;
use BenConda\Collection\Collection;
use PHPUnit\Framework\TestCase;
/**
* @covers \BenConda\Collection\Modifier\Join
*/
final class JoinTest extends TestCase
{
/** @var Collection<int, Item> */
private Collection $itemCollection;
/** @var Collection<int, Person> */
private Collection $personCollection;
/** @var Collection<int, Cart> */
private Collection $cartCollection;
protected function setUp(): void
{
$this->itemCollection = Collection::from([
new Item(0, 'Banana'),
new Item(1, 'Peach'),
new Item(2, 'Peanuts'),
]);
$this->personCollection = Collection::from([
new Person(0, 'Person 1'),
new Person(1, 'Person 2'),
new Person(2, 'Person 3'),
]);
$this->cartCollection = Collection::from([
new Cart(personId: 0, itemId: 0),
new Cart(personId: 0, itemId: 2),
new Cart(personId: 0, itemId: 1),
new Cart(personId: 1, itemId: 1),
]);
}
public function testMapWithModifier(): void
{
$personsWithCartItems = $this->personCollection
->join(
$this->cartCollection,
on: fn (Person $person, Cart $cart): bool => $person->id === $cart->personId,
select: fn (Person $person, Collection $cart): PersonWithCartItems => new PersonWithCartItems(
$person,
$cart->join(
$this->itemCollection,
on: fn (Cart $cart, Item $item): bool => $cart->itemId === $item->id,
select: fn (Cart $cart, Collection $item) => $item->first()
)
),
);
foreach ($personsWithCartItems as $item) {
self::assertInstanceOf(PersonWithCartItems::class, $item);
self::assertInstanceOf(Collection::class, $item->cartItems);
foreach ($item->cartItems as $cartItem) {
self::assertInstanceOf(Item::class, $cartItem);
}
}
}
}
class Item
{
public function __construct(
public readonly int $id,
public readonly string $name
) {
}
}
class Person
{
public function __construct(
public readonly int $id,
public readonly string $name
) {
}
}
class Cart
{
public function __construct(
public readonly int $personId,
public readonly int $itemId
) {
}
}
class PersonWithCartItems
{
/**
* @param List<?Item> $cartItems
*/
public function __construct(
public readonly Person $person,
public readonly iterable $cartItems
) {
}
}