|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2017, 2018 Alexey Kopytko <[email protected]> |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +declare(strict_types=1); |
| 19 | + |
| 20 | +namespace Tests\Pipeline; |
| 21 | + |
| 22 | +use LogicException; |
| 23 | +use PHPUnit\Framework\TestCase; |
| 24 | +use Pipeline\Standard; |
| 25 | + |
| 26 | +use ArrayIterator; |
| 27 | + |
| 28 | +use function Pipeline\map; |
| 29 | +use function Pipeline\take; |
| 30 | +use function Pipeline\fromArray; |
| 31 | + |
| 32 | +/** |
| 33 | + * @covers \Pipeline\Standard |
| 34 | + * |
| 35 | + * @internal |
| 36 | + */ |
| 37 | +final class EachTest extends TestCase |
| 38 | +{ |
| 39 | + private array $output; |
| 40 | + |
| 41 | + protected function setUp(): void |
| 42 | + { |
| 43 | + parent::setUp(); |
| 44 | + $this->output = []; |
| 45 | + } |
| 46 | + |
| 47 | + protected function observeValue($value): void |
| 48 | + { |
| 49 | + $this->output[] = $value; |
| 50 | + } |
| 51 | + |
| 52 | + protected function observeKeyValue($key, $value): void |
| 53 | + { |
| 54 | + $this->output[$key] = $value; |
| 55 | + } |
| 56 | + |
| 57 | + public function testUninitialized(): void |
| 58 | + { |
| 59 | + $pipeline = new Standard(); |
| 60 | + |
| 61 | + $pipeline->each(fn ($value) => $this->observeValue($value)); |
| 62 | + |
| 63 | + $this->assertSame([], $this->output); |
| 64 | + } |
| 65 | + |
| 66 | + public function testEmpty(): void |
| 67 | + { |
| 68 | + $pipeline = take([]); |
| 69 | + |
| 70 | + $pipeline->each(fn ($value) => $this->observeValue($value)); |
| 71 | + |
| 72 | + $this->assertSame([], $this->output); |
| 73 | + } |
| 74 | + |
| 75 | + public function testEmptyGenerator(): void |
| 76 | + { |
| 77 | + $pipeline = map(static fn () => yield from []); |
| 78 | + |
| 79 | + $pipeline->each(fn ($value) => $this->observeValue($value)); |
| 80 | + |
| 81 | + $this->assertSame([], $this->output); |
| 82 | + } |
| 83 | + |
| 84 | + public function testNotEmpty(): void |
| 85 | + { |
| 86 | + $pipeline = take([1, 2, 3, 4]); |
| 87 | + |
| 88 | + $pipeline->each(fn (int $value) => $this->observeValue($value)); |
| 89 | + |
| 90 | + $this->assertSame([1, 2, 3, 4], $this->output); |
| 91 | + } |
| 92 | + |
| 93 | + public function testKeyValue(): void |
| 94 | + { |
| 95 | + $pipeline = take([5 => 1, 2, 3, 4]); |
| 96 | + |
| 97 | + $pipeline->each(fn (int $value, int $key) => $this->observeKeyValue($key, $value)); |
| 98 | + |
| 99 | + $this->assertSame([5 => 1, 2, 3, 4], $this->output); |
| 100 | + } |
| 101 | + |
| 102 | + |
| 103 | + public static function provideInterrupt(): iterable |
| 104 | + { |
| 105 | + yield [map(fn () => yield from [1, 2, 3, 4])]; |
| 106 | + yield [take(new ArrayIterator([1, 2, 3, 4]))]; |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * @dataProvider provideInterrupt |
| 111 | + */ |
| 112 | + public function testInterrupt(Standard $pipeline): void |
| 113 | + { |
| 114 | + $pipeline->cast(function (int $value) { |
| 115 | + if (3 === $value) { |
| 116 | + throw new LogicException(); |
| 117 | + } |
| 118 | + |
| 119 | + return $value; |
| 120 | + }); |
| 121 | + |
| 122 | + try { |
| 123 | + $pipeline->each(function (int $value): void { |
| 124 | + $this->observeValue($value); |
| 125 | + }); |
| 126 | + } catch (LogicException $_) { |
| 127 | + $this->assertSame([1, 2], $this->output); |
| 128 | + } |
| 129 | + |
| 130 | + $pipeline->each(function (int $value): void { |
| 131 | + $this->fail(); |
| 132 | + }); |
| 133 | + |
| 134 | + $this->assertSame([1, 2], $this->output); |
| 135 | + $this->assertSame([], $pipeline->toArray()); |
| 136 | + } |
| 137 | + |
| 138 | + public function testNoDiscard(): void |
| 139 | + { |
| 140 | + $pipeline = fromArray([1, 2, 3]); |
| 141 | + |
| 142 | + $pipeline->each(function (int $value): void { |
| 143 | + $this->observeValue($value); |
| 144 | + }, false); |
| 145 | + |
| 146 | + $pipeline->each(function (int $value): void { |
| 147 | + $this->observeValue($value); |
| 148 | + }); |
| 149 | + |
| 150 | + $this->assertSame([1, 2, 3, 1, 2, 3], $this->output); |
| 151 | + $this->assertSame([], $pipeline->toArray()); |
| 152 | + } |
| 153 | + |
| 154 | +} |
0 commit comments