Skip to content

Commit 4ce169e

Browse files
committed
Add tests for AggregateStore::getItems()
1 parent 456dab0 commit 4ce169e

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

tests/Stache/AggregateStoreTest.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,54 @@ public function it_gets_and_sets_child_stores()
3434
$this->assertInstanceOf(ChildStore::class, $childOne);
3535
$this->assertEquals(['one' => $childOne, 'two' => $childTwo], $this->store->stores()->all());
3636
}
37+
38+
#[Test]
39+
public function it_gets_items_from_multiple_child_stores()
40+
{
41+
$this->store->store('one')->setItems([
42+
'a' => ['id' => 'a', 'title' => 'Item A'],
43+
'b' => ['id' => 'b', 'title' => 'Item B'],
44+
]);
45+
46+
$this->store->store('two')->setItems([
47+
'c' => ['id' => 'c', 'title' => 'Item C'],
48+
'd' => ['id' => 'd', 'title' => 'Item D'],
49+
]);
50+
51+
$items = $this->store->getItems(['one::a', 'two::c', 'one::b']);
52+
53+
$this->assertCount(3, $items);
54+
$this->assertEquals('a', $items[0]['id']);
55+
$this->assertEquals('c', $items[1]['id']);
56+
$this->assertEquals('b', $items[2]['id']);
57+
}
58+
59+
#[Test]
60+
public function it_gets_items_preserving_order_across_stores()
61+
{
62+
$this->store->store('one')->setItems([
63+
'a' => ['id' => 'a'],
64+
'b' => ['id' => 'b'],
65+
]);
66+
67+
$this->store->store('two')->setItems([
68+
'c' => ['id' => 'c'],
69+
'd' => ['id' => 'd'],
70+
]);
71+
72+
// Request in mixed order
73+
$items = $this->store->getItems(['two::d', 'one::a', 'two::c', 'one::b']);
74+
75+
$this->assertEquals(['d', 'a', 'c', 'b'], $items->pluck('id')->all());
76+
}
77+
78+
#[Test]
79+
public function it_returns_empty_collection_for_empty_keys()
80+
{
81+
$items = $this->store->getItems([]);
82+
83+
$this->assertCount(0, $items);
84+
}
3785
}
3886

3987
class TestAggregateStore extends AggregateStore
@@ -53,4 +101,20 @@ public function discoverStores()
53101

54102
class TestChildStore extends ChildStore
55103
{
104+
protected $items = [];
105+
106+
public function setItems(array $items)
107+
{
108+
$this->items = $items;
109+
}
110+
111+
public function getItem($key)
112+
{
113+
return $this->items[$key] ?? null;
114+
}
115+
116+
public function getItems($keys)
117+
{
118+
return collect($keys)->map(fn ($key) => $this->getItem($key));
119+
}
56120
}

0 commit comments

Comments
 (0)