|
| 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 ArgumentCountError; |
| 24 | +use LogicException; |
| 25 | +use PHPUnit\Framework\TestCase; |
| 26 | +use Pipeline\Standard; |
| 27 | +use SplQueue; |
| 28 | + |
| 29 | +use function Pipeline\fromArray; |
| 30 | +use function Pipeline\map; |
| 31 | +use function Pipeline\take; |
| 32 | + |
| 33 | +/** |
| 34 | + * @covers \Pipeline\Standard |
| 35 | + * |
| 36 | + * @internal |
| 37 | + */ |
| 38 | +final class TapTest extends TestCase |
| 39 | +{ |
| 40 | + private array $sideEffects; |
| 41 | + |
| 42 | + protected function setUp(): void |
| 43 | + { |
| 44 | + parent::setUp(); |
| 45 | + $this->sideEffects = []; |
| 46 | + } |
| 47 | + |
| 48 | + private function recordValue($value): void |
| 49 | + { |
| 50 | + $this->sideEffects[] = $value; |
| 51 | + } |
| 52 | + |
| 53 | + private function recordKeyValue($value, $key): void |
| 54 | + { |
| 55 | + $this->sideEffects[$key] = $value; |
| 56 | + } |
| 57 | + |
| 58 | + public function testUninitialized(): void |
| 59 | + { |
| 60 | + $pipeline = new Standard(); |
| 61 | + |
| 62 | + $result = $pipeline->tap($this->recordValue(...)); |
| 63 | + |
| 64 | + $this->assertSame([], $result->toList()); |
| 65 | + $this->assertSame([], $this->sideEffects); |
| 66 | + } |
| 67 | + |
| 68 | + public function testEmpty(): void |
| 69 | + { |
| 70 | + $pipeline = take([]); |
| 71 | + |
| 72 | + $result = $pipeline->tap($this->recordValue(...)); |
| 73 | + |
| 74 | + $this->assertSame([], $result->toList()); |
| 75 | + $this->assertSame([], $this->sideEffects); |
| 76 | + } |
| 77 | + |
| 78 | + public function testEmptyGenerator(): void |
| 79 | + { |
| 80 | + $pipeline = map(static fn() => yield from []); |
| 81 | + |
| 82 | + $result = $pipeline->tap($this->recordValue(...)); |
| 83 | + |
| 84 | + $this->assertSame([], $result->toList()); |
| 85 | + $this->assertSame([], $this->sideEffects); |
| 86 | + } |
| 87 | + |
| 88 | + public function testBasicTap(): void |
| 89 | + { |
| 90 | + $pipeline = take([1, 2, 3, 4]); |
| 91 | + |
| 92 | + $result = $pipeline->tap($this->recordValue(...))->toList(); |
| 93 | + |
| 94 | + $this->assertSame([1, 2, 3, 4], $result); |
| 95 | + $this->assertSame([1, 2, 3, 4], $this->sideEffects); |
| 96 | + } |
| 97 | + |
| 98 | + public function testTapWithKeys(): void |
| 99 | + { |
| 100 | + $pipeline = take(['a' => 1, 'b' => 2, 'c' => 3]); |
| 101 | + |
| 102 | + $result = $pipeline->tap($this->recordKeyValue(...))->toAssoc(); |
| 103 | + |
| 104 | + $this->assertSame(['a' => 1, 'b' => 2, 'c' => 3], $result); |
| 105 | + $this->assertSame(['a' => 1, 'b' => 2, 'c' => 3], $this->sideEffects); |
| 106 | + } |
| 107 | + |
| 108 | + public function testTapDoesNotChangeValues(): void |
| 109 | + { |
| 110 | + $pipeline = take([1, 2, 3]); |
| 111 | + |
| 112 | + $result = $pipeline |
| 113 | + ->tap(fn($value) => $value * 10) |
| 114 | + ->map(fn($value) => $value * 2) |
| 115 | + ->toList(); |
| 116 | + |
| 117 | + $this->assertSame([2, 4, 6], $result); |
| 118 | + } |
| 119 | + |
| 120 | + public function testTapChaining(): void |
| 121 | + { |
| 122 | + $result = take([1, 2, 3]) |
| 123 | + ->tap($this->recordValue(...)) |
| 124 | + ->cast(fn($value) => $value * 2) |
| 125 | + ->tap($this->recordValue(...)) |
| 126 | + ->toList(); |
| 127 | + |
| 128 | + $this->assertSame([2, 4, 6], $result); |
| 129 | + $this->assertSame([1, 2, 2, 4, 3, 6], $this->sideEffects); |
| 130 | + } |
| 131 | + |
| 132 | + public function testLaziness(): void |
| 133 | + { |
| 134 | + $pipeline = take([1, 2, 3, 4, 5]); |
| 135 | + |
| 136 | + $result = $pipeline |
| 137 | + ->tap($this->recordValue(...)) |
| 138 | + ->filter(fn($value) => $value <= 3); |
| 139 | + |
| 140 | + $this->assertSame([], $this->sideEffects, 'Tap should not execute until consumed'); |
| 141 | + |
| 142 | + $final = $result->toList(); |
| 143 | + |
| 144 | + $this->assertSame([1, 2, 3], $final); |
| 145 | + $this->assertSame([1, 2, 3, 4, 5], $this->sideEffects); |
| 146 | + } |
| 147 | + |
| 148 | + public function testTapWithException(): void |
| 149 | + { |
| 150 | + $pipeline = take([1, 2, 3]) |
| 151 | + ->tap(function ($value): void { |
| 152 | + $this->recordValue($value); |
| 153 | + if (2 === $value) { |
| 154 | + throw new LogicException('Test exception'); |
| 155 | + } |
| 156 | + }); |
| 157 | + |
| 158 | + $this->expectException(LogicException::class); |
| 159 | + |
| 160 | + $pipeline->toList(); |
| 161 | + } |
| 162 | + |
| 163 | + public function testTapStrictArity(): void |
| 164 | + { |
| 165 | + $queue = new SplQueue(); |
| 166 | + $pipeline = fromArray([1, 2, 3]); |
| 167 | + |
| 168 | + $result = $pipeline->tap($queue->enqueue(...))->toList(); |
| 169 | + |
| 170 | + $this->assertSame([1, 2, 3], $result); |
| 171 | + $this->assertSame([1, 2, 3], take($queue)->toList()); |
| 172 | + } |
| 173 | + |
| 174 | + public function testTapVariadicInternal(): void |
| 175 | + { |
| 176 | + $this->expectOutputString("123"); |
| 177 | + |
| 178 | + $pipeline = fromArray(['1', '2', '3']); |
| 179 | + $pipeline->tap(printf(...))->toList(); |
| 180 | + } |
| 181 | + |
| 182 | + public function testTapArgumentCountError(): void |
| 183 | + { |
| 184 | + $pipeline = fromArray(['1', '2', '3']) |
| 185 | + ->tap(static function ($a, $b, $c): void {}); |
| 186 | + |
| 187 | + $this->expectException(ArgumentCountError::class); |
| 188 | + $this->expectExceptionMessage('Too few arguments'); |
| 189 | + |
| 190 | + $pipeline->toList(); |
| 191 | + } |
| 192 | + |
| 193 | + public function testTapReturnsSameInstance(): void |
| 194 | + { |
| 195 | + $pipeline = take([1, 2, 3]); |
| 196 | + $result = $pipeline->tap($this->recordValue(...)); |
| 197 | + |
| 198 | + $this->assertSame($pipeline, $result); |
| 199 | + } |
| 200 | +} |
0 commit comments