diff --git a/src/Primitives/Traits/Collection/GetIterator.php b/src/Primitives/Traits/Collection/GetIterator.php index 5f8abfe..f4aab76 100644 --- a/src/Primitives/Traits/Collection/GetIterator.php +++ b/src/Primitives/Traits/Collection/GetIterator.php @@ -4,9 +4,7 @@ namespace Atournayre\Primitives\Traits\Collection; -use Atournayre\Common\Exception\RuntimeException; use Atournayre\Contracts\Collection\GetIteratorInterface; -use Atournayre\Contracts\Exception\ThrowableInterface; /** * Trait GetIterator. @@ -18,13 +16,12 @@ trait GetIterator /** * Returns an iterator for the elements. * - * @throws ThrowableInterface + * @return \Traversable * * @api */ - // @phpstan-ignore-next-line Remove this line when the method is implemented - public function getIterator() + public function getIterator(): \Traversable { - RuntimeException::new('Not implemented yet!')->throw(); + return $this->collection->getIterator(); } } diff --git a/tests/Unit/Primitives/Traits/Collection/GetIteratorTest.php b/tests/Unit/Primitives/Traits/Collection/GetIteratorTest.php new file mode 100644 index 0000000..eace32c --- /dev/null +++ b/tests/Unit/Primitives/Traits/Collection/GetIteratorTest.php @@ -0,0 +1,63 @@ +getIterator()); + } + + public function testGetIteratorAllowsForeach(): void + { + $collection = Collection::of([1, 2, 3]); + + $result = iterator_to_array($collection->getIterator(), false); + + self::assertSame([1, 2, 3], $result); + } + + public function testGetIteratorWithEmptyCollection(): void + { + $collection = Collection::of([]); + $count = 0; + + foreach ($collection as $value) { + $count++; + } + + self::assertSame(0, $count); + } + + public function testGetIteratorPreservesKeys(): void + { + $collection = Collection::of(['a' => 1, 'b' => 2, 'c' => 3]); + + $result = iterator_to_array($collection->getIterator(), true); + + self::assertSame(['a' => 1, 'b' => 2, 'c' => 3], $result); + } + + public function testGetIteratorWithNestedArrays(): void + { + $collection = Collection::of([ + 'user1' => ['name' => 'Alice'], + 'user2' => ['name' => 'Bob'], + ]); + + $result = iterator_to_array($collection->getIterator(), true); + + self::assertSame([ + 'user1' => ['name' => 'Alice'], + 'user2' => ['name' => 'Bob'], + ], $result); + } +}