diff --git a/.github/workflows/Tests.yml b/.github/workflows/Tests.yml index 0689841..1f346b3 100644 --- a/.github/workflows/Tests.yml +++ b/.github/workflows/Tests.yml @@ -12,10 +12,9 @@ permissions: jobs: build: - runs-on: ${{ matrix.operating-system }} + runs-on: ubuntu-latest strategy: matrix: - operating-system: ['ubuntu-20.04', 'ubuntu-latest'] php-versions: ['7.0', '7.1', '7.2', '7.3', '7.4', '8.0', '8.1', '8.2', '8.3'] steps: diff --git a/src/NullSpecification.php b/src/NullSpecification.php new file mode 100644 index 0000000..ac6824e --- /dev/null +++ b/src/NullSpecification.php @@ -0,0 +1,11 @@ +count() === 0; } + public function getIterator(): Traversable + { + foreach ($this->rows as $row) { + yield $row; + } + } + /** * @param callable $callback * @return mixed[] diff --git a/src/Result/ResultSet.php b/src/Result/ResultSet.php index 2824037..6375bde 100644 --- a/src/Result/ResultSet.php +++ b/src/Result/ResultSet.php @@ -3,9 +3,13 @@ namespace Star\Component\Specification\Result; use Countable; +use IteratorAggregate; use Star\Component\Type\Value; -interface ResultSet extends Countable +/** + * @extends IteratorAggregate + */ +interface ResultSet extends Countable, IteratorAggregate { public function getRow(int $row): ResultRow; public function getValue(int $row, string $column): Value; diff --git a/src/Result/StreamedResult.php b/src/Result/StreamedResult.php index 3836158..3d814ea 100644 --- a/src/Result/StreamedResult.php +++ b/src/Result/StreamedResult.php @@ -3,6 +3,7 @@ namespace Star\Component\Specification\Result; use Star\Component\Type\Value; +use Traversable; final class StreamedResult implements ResultSet { @@ -25,4 +26,9 @@ public function isEmpty(): bool { throw new \RuntimeException(__METHOD__ . ' not implemented yet.'); } + + public function getIterator(): Traversable + { + throw new \RuntimeException(__METHOD__ . ' not implemented yet.'); + } } diff --git a/tests/Platform/DoctrineDBALTest.php b/tests/Platform/DoctrineDBALTest.php index d322338..db9089b 100644 --- a/tests/Platform/DoctrineDBALTest.php +++ b/tests/Platform/DoctrineDBALTest.php @@ -21,10 +21,13 @@ use Star\Component\Specification\IsNull; use Star\Component\Specification\Lower; use Star\Component\Specification\LowerEquals; +use Star\Component\Specification\NullSpecification; use Star\Component\Specification\OrderBy; use Star\Component\Specification\OrX; use Star\Component\Specification\Platform\DoctrineDBALPlatform; +use Star\Component\Specification\Result\ResultRow; use Star\Component\Specification\StartsWith; +use function iterator_to_array; final class DoctrineDBALTest extends TestCase { @@ -635,4 +638,25 @@ public function test_it_should_allow_empty_alias(): void self::assertSame('Robert Ludlum', $rows->getValue(1, 'name')->toString()); self::assertSame('Stephen King', $rows->getValue(2, 'name')->toString()); } + + public function test_it_should_iterate_on_result(): void + { + $qb = $this->connection->createQueryBuilder(); + $qb + ->select('*') + ->from(self::TABLE_AUTHOR); + $platform = new DoctrineDBALPlatform($qb); + + /** + * @var ResultRow[] $rows + */ + $rows = iterator_to_array($platform->fetchAll(new NullSpecification())); + + self::assertCount(5, $rows); + self::assertSame('William Shakespeare', $rows[0]->getValue('name')->toString()); + self::assertSame('JRR Tolkien', $rows[1]->getValue('name')->toString()); + self::assertSame('JK. Rowling', $rows[2]->getValue('name')->toString()); + self::assertSame('Robert Ludlum', $rows[3]->getValue('name')->toString()); + self::assertSame('Stephen King', $rows[4]->getValue('name')->toString()); + } } diff --git a/tests/Result/ArrayResultTest.php b/tests/Result/ArrayResultTest.php index 5e3e7c3..4d06972 100644 --- a/tests/Result/ArrayResultTest.php +++ b/tests/Result/ArrayResultTest.php @@ -8,6 +8,7 @@ use PHPUnit\Framework\TestCase; use Star\Component\Specification\Result\NotUniqueResult; use Star\Component\Type\Value; +use function iterator_to_array; final class ArrayResultTest extends TestCase { @@ -156,4 +157,21 @@ public function test_it_should_throw_exception_when_more_than_one_row_returned_f $this->expectExceptionMessage('Query was expected to return 0-1 row, "2" rows returned.'); $result->fetchOne(EqualsTo::booleanValue('alias', 'is_active', true)); } + + public function test_it_should_be_iterator(): void + { + $result = ArrayResult::fromRowsOfMixed( + [ + 'id' => 1, + ], + [ + 'id' => 2, + ], + [ + 'id' => 3, + ], + ); + $rows = iterator_to_array($result); + self::assertCount(3, $rows); + } }