|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright 2017, 2018 Alexey Kopytko <[email protected]> |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +declare(strict_types=1); |
| 20 | + |
| 21 | +namespace Tests\Pipeline; |
| 22 | + |
| 23 | +use ArrayIterator; |
| 24 | +use IteratorIterator; |
| 25 | +use PHPUnit\Framework\TestCase; |
| 26 | +use Iterator; |
| 27 | +use Pipeline\Helper\CursorIterator; |
| 28 | +use Pipeline\Standard; |
| 29 | + |
| 30 | +use function iterator_count; |
| 31 | +use function Pipeline\fromArray; |
| 32 | +use function Pipeline\map; |
| 33 | +use function Pipeline\take; |
| 34 | + |
| 35 | +/** |
| 36 | + * @covers \Pipeline\Standard::cursor |
| 37 | + * @covers \Pipeline\Helper\CursorIterator |
| 38 | + * |
| 39 | + * @internal |
| 40 | + */ |
| 41 | +final class CursorTest extends TestCase |
| 42 | +{ |
| 43 | + public static function provideIterables(): iterable |
| 44 | + { |
| 45 | + yield 'array' => [fromArray([1, 2, 3, 4, 5])]; |
| 46 | + |
| 47 | + yield 'ArrayIterator' => [take(new ArrayIterator([1, 2, 3, 4, 5]))]; |
| 48 | + |
| 49 | + yield 'IteratorIterator' => [take(new IteratorIterator(new ArrayIterator([1, 2, 3, 4, 5])))]; |
| 50 | + |
| 51 | + yield 'IteratorAggregate' => [take(new Standard(new IteratorIterator(new ArrayIterator([1, 2, 3, 4, 5]))))]; |
| 52 | + |
| 53 | + yield 'generator' => [map(fn() => yield from [1, 2, 3, 4, 5])]; |
| 54 | + |
| 55 | + yield 'stream' => [fromArray([1, 2, 3, 4, 5])->stream()]; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * @dataProvider provideIterables |
| 60 | + */ |
| 61 | + public function testCursorContinuesAfterBreak(Standard $pipeline): void |
| 62 | + { |
| 63 | + $cursor = $pipeline->cursor(); |
| 64 | + |
| 65 | + $collected = []; |
| 66 | + foreach ($cursor as $i) { |
| 67 | + $collected[] = $i; |
| 68 | + if (2 === $i) { |
| 69 | + break; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + $this->assertSame([1, 2], $collected); |
| 74 | + |
| 75 | + $remaining = []; |
| 76 | + foreach ($cursor as $i) { |
| 77 | + $remaining[] = $i; |
| 78 | + } |
| 79 | + |
| 80 | + // CursorIterator auto-advances past the break point |
| 81 | + $this->assertSame([3, 4, 5], $remaining); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * @dataProvider provideIterables |
| 86 | + */ |
| 87 | + public function testCursorWithTakeCount(Standard $pipeline): void |
| 88 | + { |
| 89 | + $cursor = $pipeline->cursor(); |
| 90 | + |
| 91 | + foreach ($cursor as $i) { |
| 92 | + if (2 === $i) { |
| 93 | + break; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + // 3 elements remain: 3, 4, 5 |
| 98 | + $this->assertSame(3, take($cursor)->count()); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * @dataProvider provideIterables |
| 103 | + */ |
| 104 | + public function testCursorWithSlice(Standard $pipeline): void |
| 105 | + { |
| 106 | + $cursor = $pipeline->cursor(); |
| 107 | + |
| 108 | + $this->assertSame([1, 2], take($cursor)->slice(0, 2)->toList()); |
| 109 | + |
| 110 | + // 3 elements remain: 3, 4, 5 |
| 111 | + $this->assertSame(3, take($cursor)->count()); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * @dataProvider provideIterables |
| 116 | + */ |
| 117 | + public function testCursorWithTakeReduce(Standard $pipeline): void |
| 118 | + { |
| 119 | + $cursor = $pipeline->cursor(); |
| 120 | + |
| 121 | + foreach ($cursor as $i) { |
| 122 | + if (2 === $i) { |
| 123 | + break; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + // Remaining: 3 + 4 + 5 = 12 |
| 128 | + $this->assertSame(12, take($cursor)->reduce()); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * @dataProvider provideIterables |
| 133 | + */ |
| 134 | + public function testExhaustedCursorReturnsEmpty(Standard $pipeline): void |
| 135 | + { |
| 136 | + $cursor = $pipeline->cursor(); |
| 137 | + |
| 138 | + // Consume all elements |
| 139 | + $this->assertSame(5, iterator_count($cursor)); |
| 140 | + |
| 141 | + $remaining = []; |
| 142 | + foreach ($cursor as $i) { |
| 143 | + $remaining[] = $i; |
| 144 | + } |
| 145 | + |
| 146 | + $this->assertSame([], $remaining); |
| 147 | + } |
| 148 | + |
| 149 | + public function testCursorReturnsIterator(): void |
| 150 | + { |
| 151 | + $pipeline = fromArray([1, 2, 3]); |
| 152 | + $cursor = $pipeline->cursor(); |
| 153 | + |
| 154 | + $this->assertInstanceOf(Iterator::class, $cursor); |
| 155 | + } |
| 156 | + |
| 157 | + public function testCursorAvoidDoubleWrapping(): void |
| 158 | + { |
| 159 | + $pipeline = fromArray([1, 2, 3]); |
| 160 | + |
| 161 | + $cursor1 = $pipeline->cursor(); |
| 162 | + $cursor2 = take($cursor1)->cursor(); |
| 163 | + |
| 164 | + // Should be the same instance (no double wrapping) |
| 165 | + $this->assertSame($cursor1, $cursor2); |
| 166 | + } |
| 167 | + |
| 168 | + public function testCursorWithEmptyPipeline(): void |
| 169 | + { |
| 170 | + $pipeline = fromArray([]); |
| 171 | + $cursor = $pipeline->cursor(); |
| 172 | + |
| 173 | + $this->assertSame([], take($cursor)->toList()); |
| 174 | + } |
| 175 | + |
| 176 | + public function testCursorPreservesKeys(): void |
| 177 | + { |
| 178 | + $pipeline = fromArray(['a' => 1, 'b' => 2, 'c' => 3]); |
| 179 | + $cursor = $pipeline->cursor(); |
| 180 | + |
| 181 | + $collected = []; |
| 182 | + foreach ($cursor as $key => $value) { |
| 183 | + $collected[$key] = $value; |
| 184 | + if ('a' === $key) { |
| 185 | + break; |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + $this->assertSame(['a' => 1], $collected); |
| 190 | + |
| 191 | + $remaining = []; |
| 192 | + foreach ($cursor as $key => $value) { |
| 193 | + $remaining[$key] = $value; |
| 194 | + } |
| 195 | + |
| 196 | + // CursorIterator auto-advances past 'a' |
| 197 | + $this->assertSame(['b' => 2, 'c' => 3], $remaining); |
| 198 | + } |
| 199 | + |
| 200 | + public function testCursorManualIteration(): void |
| 201 | + { |
| 202 | + $pipeline = fromArray([1, 2, 3]); |
| 203 | + $cursor = $pipeline->cursor(); |
| 204 | + |
| 205 | + $this->assertTrue($cursor->valid()); |
| 206 | + $this->assertSame(1, $cursor->current()); |
| 207 | + |
| 208 | + $cursor->next(); |
| 209 | + $this->assertSame(2, $cursor->current()); |
| 210 | + |
| 211 | + $cursor->next(); |
| 212 | + $this->assertSame(3, $cursor->current()); |
| 213 | + |
| 214 | + $cursor->next(); |
| 215 | + $this->assertFalse($cursor->valid()); |
| 216 | + } |
| 217 | +} |
0 commit comments