Skip to content

Commit 7afc215

Browse files
committed
test: add tests for Collection::toArray and Collection::toList
1 parent c883b50 commit 7afc215

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/Domain/Collection.php

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use InvalidArgumentException;
1212
use IteratorAggregate;
1313
use Traversable;
14+
1415
use function array_filter;
1516
use function array_map;
1617
use function array_reduce;

tests/Domain/CollectionTest.php

+29-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public function testAddMultiple(): void
156156
public function testFilter(): void
157157
{
158158
// Given
159-
$items = [1, 2, 3,4, 5, 6, 7, 8, 9, 10];
159+
$items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
160160
$collection = new Collection($items);
161161

162162
// When
@@ -584,4 +584,32 @@ public function testHasItems(): void
584584
$this->assertFalse((new Collection([]))->hasItems());
585585
$this->assertTrue((new Collection([1]))->hasItems());
586586
}
587+
588+
public function testToArray(): void
589+
{
590+
$this->assertSame([], (new Collection([]))->toArray());
591+
592+
$list = [1, 2, 3];
593+
$this->assertSame($list, (new Collection($list))->toArray());
594+
595+
$keyValue = ['foo' => 1, 'bar' => 2, 'baz' => 3];
596+
$this->assertSame($keyValue, (new Collection($keyValue))->toArray());
597+
598+
$numberIndexed = [1 => 1, 2 => 2, 3 => 3, 0 => 0];
599+
$this->assertSame($numberIndexed, (new Collection($numberIndexed))->toArray());
600+
}
601+
602+
public function testToList(): void
603+
{
604+
$this->assertSame([], (new Collection([]))->toList());
605+
606+
$list = [1, 2, 3];
607+
$this->assertSame($list, (new Collection($list))->toList());
608+
609+
$keyValue = ['foo' => 1, 'bar' => 2, 'baz' => 3];
610+
$this->assertSame([1, 2, 3], (new Collection($keyValue))->toList());
611+
612+
$numberIndexed = [1 => 1, 2 => 2, 3 => 3, 0 => 0];
613+
$this->assertSame([1, 2, 3, 0], (new Collection($numberIndexed))->toList());
614+
}
587615
}

0 commit comments

Comments
 (0)