|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace MongoDB\Tests\Model; |
| 4 | + |
| 5 | +use MongoDB\Model\CachingIterator; |
| 6 | +use Exception; |
| 7 | + |
| 8 | +class CachingIteratorTest extends \PHPUnit_Framework_TestCase |
| 9 | +{ |
| 10 | + /** |
| 11 | + * Sanity check for all following tests. |
| 12 | + * |
| 13 | + * @expectedException \Exception |
| 14 | + * @expectedExceptionMessage Cannot traverse an already closed generator |
| 15 | + */ |
| 16 | + public function testTraversingGeneratorConsumesIt() |
| 17 | + { |
| 18 | + $iterator = $this->getTraversable([1, 2, 3]); |
| 19 | + $this->assertSame([1, 2, 3], iterator_to_array($iterator)); |
| 20 | + $this->assertSame([1, 2, 3], iterator_to_array($iterator)); |
| 21 | + } |
| 22 | + |
| 23 | + public function testConstructorRewinds() |
| 24 | + { |
| 25 | + $iterator = new CachingIterator($this->getTraversable([1, 2, 3])); |
| 26 | + |
| 27 | + $this->assertTrue($iterator->valid()); |
| 28 | + $this->assertSame(0, $iterator->key()); |
| 29 | + $this->assertSame(1, $iterator->current()); |
| 30 | + } |
| 31 | + |
| 32 | + public function testIteration() |
| 33 | + { |
| 34 | + $iterator = new CachingIterator($this->getTraversable([1, 2, 3])); |
| 35 | + |
| 36 | + $expectedKey = 0; |
| 37 | + $expectedItem = 1; |
| 38 | + |
| 39 | + foreach ($iterator as $key => $item) { |
| 40 | + $this->assertSame($expectedKey++, $key); |
| 41 | + $this->assertSame($expectedItem++, $item); |
| 42 | + } |
| 43 | + |
| 44 | + $this->assertFalse($iterator->valid()); |
| 45 | + } |
| 46 | + |
| 47 | + public function testIterationWithEmptySet() |
| 48 | + { |
| 49 | + $iterator = new CachingIterator($this->getTraversable([])); |
| 50 | + |
| 51 | + $iterator->rewind(); |
| 52 | + $this->assertFalse($iterator->valid()); |
| 53 | + } |
| 54 | + |
| 55 | + public function testPartialIterationDoesNotExhaust() |
| 56 | + { |
| 57 | + $traversable = $this->getTraversableThatThrows([1, 2, new Exception]); |
| 58 | + $iterator = new CachingIterator($traversable); |
| 59 | + |
| 60 | + $expectedKey = 0; |
| 61 | + $expectedItem = 1; |
| 62 | + |
| 63 | + foreach ($iterator as $key => $item) { |
| 64 | + $this->assertSame($expectedKey++, $key); |
| 65 | + $this->assertSame($expectedItem++, $item); |
| 66 | + |
| 67 | + if ($key === 1) { |
| 68 | + break; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + $this->assertTrue($iterator->valid()); |
| 73 | + } |
| 74 | + |
| 75 | + public function testRewindAfterPartialIteration() |
| 76 | + { |
| 77 | + $iterator = new CachingIterator($this->getTraversable([1, 2, 3])); |
| 78 | + |
| 79 | + $iterator->rewind(); |
| 80 | + $this->assertTrue($iterator->valid()); |
| 81 | + $this->assertSame(0, $iterator->key()); |
| 82 | + $this->assertSame(1, $iterator->current()); |
| 83 | + |
| 84 | + $iterator->next(); |
| 85 | + $this->assertSame([1, 2, 3], iterator_to_array($iterator)); |
| 86 | + } |
| 87 | + |
| 88 | + public function testCount() |
| 89 | + { |
| 90 | + $iterator = new CachingIterator($this->getTraversable([1, 2, 3])); |
| 91 | + $this->assertCount(3, $iterator); |
| 92 | + } |
| 93 | + |
| 94 | + public function testCountAfterPartialIteration() |
| 95 | + { |
| 96 | + $iterator = new CachingIterator($this->getTraversable([1, 2, 3])); |
| 97 | + |
| 98 | + $iterator->rewind(); |
| 99 | + $this->assertTrue($iterator->valid()); |
| 100 | + $this->assertSame(0, $iterator->key()); |
| 101 | + $this->assertSame(1, $iterator->current()); |
| 102 | + |
| 103 | + $iterator->next(); |
| 104 | + $this->assertCount(3, $iterator); |
| 105 | + } |
| 106 | + |
| 107 | + public function testCountWithEmptySet() |
| 108 | + { |
| 109 | + $iterator = new CachingIterator($this->getTraversable([])); |
| 110 | + $this->assertCount(0, $iterator); |
| 111 | + } |
| 112 | + |
| 113 | + private function getTraversable($items) |
| 114 | + { |
| 115 | + foreach ($items as $item) { |
| 116 | + yield $item; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + private function getTraversableThatThrows($items) |
| 121 | + { |
| 122 | + foreach ($items as $item) { |
| 123 | + if ($item instanceof Exception) { |
| 124 | + throw $item; |
| 125 | + } else { |
| 126 | + yield $item; |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | +} |
0 commit comments