diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index 2ef7976..1e55d8a 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -17,5 +17,8 @@ jobs: - name: Install Composer run: composer install + - name: Include files for autoloading + run: composer dump-autoload + - name: Run PHPUnit tests run: ./vendor/bin/phpunit tests diff --git a/composer.json b/composer.json index 78da7e5..e4b3c7b 100644 --- a/composer.json +++ b/composer.json @@ -6,5 +6,10 @@ "vimeo/psalm": "^4.8", "phpmd/phpmd": "^2.10", "friendsofphp/php-cs-fixer": "^3.0" + }, + "autoload": { + "psr-4": { + "MaxGoryunov\\SavingIterator\\": "" + } } } diff --git a/tests/src/SavingIteratorTest.php b/tests/src/SavingIteratorTest.php index 553e623..d483269 100644 --- a/tests/src/SavingIteratorTest.php +++ b/tests/src/SavingIteratorTest.php @@ -3,6 +3,7 @@ namespace MaxGoryunov\SavingIterator\Tests\Src; use ArrayIterator; +use Generator; use MaxGoryunov\SavingIterator\Fakes\TimesCalled; use MaxGoryunov\SavingIterator\Fakes\TransparentIterator; use MaxGoryunov\SavingIterator\Src\SavingIterator; @@ -71,6 +72,66 @@ public function testDoesNotCallOriginIfValuesAreInCache(): void $this->assertEquals(count($input), $called->value()); } + /** + * @covers ::__construct + * @covers ::rewind + * @covers ::valid + * @covers ::current + * @covers ::key + * @covers ::next + * + * @small + * + * @return void + */ + public function testWorksWithGenerator(): void + { + $limit = 6; + $this->assertEquals( + range(0, $limit), + iterator_to_array( + new SavingIterator( + ( + function () use ($limit): Generator + { + for ($i = 0; $i <= $limit; $i++) { + yield $i; + } + } + )() + ) + ) + ); + } + + /** + * @covers ::__construct + * @covers ::rewind + * @covers ::valid + * @covers ::current + * @covers ::key + * @covers ::next + * + * @small + * + * @return void + */ + public function testWorksWithGeneratorMultipleTimes(): void + { + $iterator = new SavingIterator( + (function (): Generator + { + for ($i = 0; $i < 10; $i++) { + yield $i; + } + })() + ); + $this->assertEquals( + iterator_to_array($iterator), + iterator_to_array($iterator) + ); + } + /** * @covers ::__construct * @covers ::rewind