Skip to content

Commit 0cf8029

Browse files
committed
Add test to ensure merge is type safe
1 parent a708498 commit 0cf8029

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

tests/CollectionTest.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,22 +186,45 @@ public function testGetThrowsNotFound(): void
186186
$collection->get('invalid');
187187
}
188188

189-
public function testReduce()
189+
public function testReduce(): void
190190
{
191191
$collection = StringCollection::create(['foo', 'bar']);
192192

193193
$this->assertEquals('foobar', $collection->reduce(function (?string $total, $value) {
194194
return $total . $value;
195195
}));
196196
}
197+
198+
public function testMergeIsTypeSafe(): void
199+
{
200+
$this->assertInstanceOf(
201+
StringCollection::class,
202+
StringCollection::create(['foo'])->merge(StringCollection::create(['bar']))
203+
);
204+
205+
$this->expectException(InvalidArgumentException::class);
206+
StringCollection::create(['foo'])->merge(IntegerCollection::create([1]));
207+
}
197208
}
198209

199210
final class StringCollection extends ImmutableCollection
200211
{
201212
protected function validateItems(array $elements): void
202213
{
203214
foreach ($elements as $item) {
204-
if (!is_string($item)) {
215+
if (!\is_string($item)) {
216+
throw new InvalidArgumentException('Invalid value');
217+
}
218+
}
219+
}
220+
}
221+
222+
final class IntegerCollection extends ImmutableCollection
223+
{
224+
protected function validateItems(array $elements): void
225+
{
226+
foreach ($elements as $item) {
227+
if (!\is_int($item)) {
205228
throw new InvalidArgumentException('Invalid value');
206229
}
207230
}

0 commit comments

Comments
 (0)