diff --git a/packages/seal/src/Search/Result.php b/packages/seal/src/Search/Result.php index 7210a396..7dc5abab 100644 --- a/packages/seal/src/Search/Result.php +++ b/packages/seal/src/Search/Result.php @@ -16,7 +16,7 @@ /** * @extends \IteratorIterator, \Generator> */ -class Result extends \IteratorIterator +final class Result extends \IteratorIterator { /** * @param \Generator> $documents @@ -32,4 +32,11 @@ public function total(): int { return $this->total; } + + public static function createEmpty(): static + { + return new self((static function (): \Generator { + yield from []; + })(), 0); + } } diff --git a/packages/seal/tests/Search/ResultTest.php b/packages/seal/tests/Search/ResultTest.php new file mode 100644 index 00000000..cbf16e78 --- /dev/null +++ b/packages/seal/tests/Search/ResultTest.php @@ -0,0 +1,37 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace CmsIg\Seal\Tests\Search; + +use CmsIg\Seal\Search\Result; +use PHPUnit\Framework\TestCase; + +class ResultTest extends TestCase +{ + public function testIteratingResult(): void + { + $result = new Result((static function (): \Generator { + yield ['id' => 42]; + })(), 1); + + $this->assertSame(1, $result->total()); + $this->assertSame([['id' => 42]], \iterator_to_array($result)); + } + + public function testCreateEmptyResult(): void + { + $result = Result::createEmpty(); + $this->assertSame(0, $result->total()); + $this->assertSame([], \iterator_to_array($result)); + } +}