Skip to content
Merged
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
3 changes: 1 addition & 2 deletions .github/workflows/Tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
11 changes: 11 additions & 0 deletions src/NullSpecification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php declare(strict_types=1);

namespace Star\Component\Specification;

final class NullSpecification implements Specification
{
public function applySpecification(SpecificationPlatform $platform): void
{
// do nothing
}
}
8 changes: 8 additions & 0 deletions src/Result/ArrayResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Star\Component\Specification\Platform\InMemoryPlatform;
use Star\Component\Specification\Specification;
use Star\Component\Type\Value;
use Traversable;
use function array_key_exists;
use function array_map;

Expand Down Expand Up @@ -69,6 +70,13 @@ public function isEmpty(): bool
return $this->count() === 0;
}

public function getIterator(): Traversable
{
foreach ($this->rows as $row) {
yield $row;
}
}

/**
* @param callable $callback
* @return mixed[]
Expand Down
6 changes: 5 additions & 1 deletion src/Result/ResultSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
namespace Star\Component\Specification\Result;

use Countable;
use IteratorAggregate;
use Star\Component\Type\Value;

interface ResultSet extends Countable
/**
* @extends IteratorAggregate<int, ResultRow>
*/
interface ResultSet extends Countable, IteratorAggregate
{
public function getRow(int $row): ResultRow;
public function getValue(int $row, string $column): Value;
Expand Down
6 changes: 6 additions & 0 deletions src/Result/StreamedResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Star\Component\Specification\Result;

use Star\Component\Type\Value;
use Traversable;

final class StreamedResult implements ResultSet
{
Expand All @@ -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.');
}
}
24 changes: 24 additions & 0 deletions tests/Platform/DoctrineDBALTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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());
}
}
18 changes: 18 additions & 0 deletions tests/Result/ArrayResultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
}
}