Skip to content

Commit 3fbcc51

Browse files
Added getCache method
1 parent 38e3a94 commit 3fbcc51

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

src/CachedGenerator.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ public function getInnerIterator(): Generator
5252
return $this->generator;
5353
}
5454

55+
public function getCache(): array
56+
{
57+
return $this->cache;
58+
}
59+
5560
private function addCurrentToCache(): void
5661
{
5762
if ($this->generator->valid()) {

tests/CachedGeneratorTest.php

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77

88
class CachedGeneratorTest extends TestCase
99
{
10-
public function testGenerates() {
10+
public function testGenerates()
11+
{
1112
$generator = function () {
1213
foreach (range(0, 2) as $value) {
1314
yield $value;
@@ -19,7 +20,8 @@ public function testGenerates() {
1920
$this->assertEquals(range(0, 2), $results);
2021
}
2122

22-
public function testWorksWithEmptyGenerator() {
23+
public function testWorksWithEmptyGenerator()
24+
{
2325
$generator = function () {
2426
if (false) yield 0;
2527
};
@@ -29,7 +31,8 @@ public function testWorksWithEmptyGenerator() {
2931
$this->assertEquals([], $results);
3032
}
3133

32-
public function testWorksTwice() {
34+
public function testWorksTwice()
35+
{
3336
$generator = function () {
3437
foreach (range(0, 2) as $value) {
3538
yield $value;
@@ -42,4 +45,32 @@ public function testWorksTwice() {
4245
$results = iterator_to_array($cachedGenerator);
4346
$this->assertEquals(range(0, 2), $results);
4447
}
48+
49+
public function testExposesInnerGenerator()
50+
{
51+
$generator = function () {
52+
foreach (range(0, 2) as $value) {
53+
yield $value;
54+
}
55+
};
56+
57+
$generatorInstance = $generator();
58+
$cachedGenerator = new CachedGenerator($generatorInstance);
59+
60+
$this->assertEquals($generatorInstance, $cachedGenerator->getInnerIterator());
61+
}
62+
63+
public function testExposesCache()
64+
{
65+
$generator = function () {
66+
foreach (range(0, 2) as $value) {
67+
yield $value;
68+
}
69+
};
70+
$cachedGenerator = new CachedGenerator($generator());
71+
72+
$results = iterator_to_array($cachedGenerator);
73+
74+
$this->assertEquals($results, $cachedGenerator->getCache());
75+
}
4576
}

0 commit comments

Comments
 (0)