From 92522cd61670c100a8f956bba31398b5ed4f5ca5 Mon Sep 17 00:00:00 2001 From: Max Goryunov Date: Sat, 10 Jul 2021 15:23:55 +0300 Subject: [PATCH 1/3] #5: added Generator tests; SavingIterator does not work when rewound --- tests/src/SavingIteratorTest.php | 61 ++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/tests/src/SavingIteratorTest.php b/tests/src/SavingIteratorTest.php index 48c3873..35e3f19 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 IteratorIterator; use MaxGoryunov\SavingIterator\Fakes\TimesCalled; use MaxGoryunov\SavingIterator\Fakes\TransparentIterator; @@ -69,4 +70,64 @@ 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) + ); + } } From f369d40df5c9c8c6b8604dd7736c0184e2bfc21a Mon Sep 17 00:00:00 2001 From: Max Goryunov Date: Sat, 10 Jul 2021 22:48:36 +0300 Subject: [PATCH 2/3] #5: tests pass after merge; added own prefix autoloading in composer.json; this commit solves #5 --- composer.json | 5 +++++ 1 file changed, 5 insertions(+) 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\\": "" + } } } From 3f03defde4c263bc2cd1f3673d4680542cf29272 Mon Sep 17 00:00:00 2001 From: Max Goryunov Date: Sun, 11 Jul 2021 13:38:18 +0300 Subject: [PATCH 3/3] #5: changed CI workflow --- .github/workflows/php.yml | 3 +++ 1 file changed, 3 insertions(+) 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