Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions src/Primitives/Traits/Collection/GetIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -18,13 +16,12 @@ trait GetIterator
/**
* Returns an iterator for the elements.
*
* @throws ThrowableInterface
* @return \Traversable<array-key, mixed>
*
* @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();
}
}
63 changes: 63 additions & 0 deletions tests/Unit/Primitives/Traits/Collection/GetIteratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

namespace Atournayre\Tests\Unit\Primitives\Traits\Collection;

use Atournayre\Primitives\Collection;
use PHPUnit\Framework\TestCase;

class GetIteratorTest extends TestCase
{
public function testGetIteratorReturnsTraversable(): void
{
$collection = Collection::of([1, 2, 3]);

self::assertInstanceOf(\Traversable::class, $collection->getIterator());

Check failure on line 16 in tests/Unit/Primitives/Traits/Collection/GetIteratorTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2)

Call to static method PHPUnit\Framework\Assert::assertInstanceOf() with 'Traversable' and Traversable will always evaluate to true.

Check failure on line 16 in tests/Unit/Primitives/Traits/Collection/GetIteratorTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2)

Call to static method PHPUnit\Framework\Assert::assertInstanceOf() with 'Traversable' and Traversable will always evaluate to true.
}

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) {

Check failure on line 33 in tests/Unit/Primitives/Traits/Collection/GetIteratorTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2)

Argument of an invalid type Atournayre\Primitives\Collection supplied for foreach, only iterables are supported.

Check failure on line 33 in tests/Unit/Primitives/Traits/Collection/GetIteratorTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2)

Argument of an invalid type Atournayre\Primitives\Collection supplied for foreach, only iterables are supported.
$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);
}
}
Loading