|
20 | 20 |
|
21 | 21 | namespace Tests\Pipeline; |
22 | 22 |
|
| 23 | +use ArgumentCountError; |
23 | 24 | use LogicException; |
24 | 25 | use PHPUnit\Framework\TestCase; |
25 | 26 | use Pipeline\Standard; |
26 | 27 | use ArrayIterator; |
| 28 | +use SplQueue; |
| 29 | +use Tests\Pipeline\Fixtures\CallableThrower; |
27 | 30 |
|
28 | 31 | use function Pipeline\map; |
29 | 32 | use function Pipeline\take; |
@@ -158,4 +161,58 @@ public function testNotVoidReturn(): void |
158 | 161 |
|
159 | 162 | $this->addToAssertionCount(1); |
160 | 163 | } |
| 164 | + |
| 165 | + public function testStrictArity(): void |
| 166 | + { |
| 167 | + $queue = new SplQueue(); |
| 168 | + $pipeline = fromArray([1, 2, 3]); |
| 169 | + $pipeline->each($queue->enqueue(...)); |
| 170 | + |
| 171 | + $this->assertSame([1, 2, 3], take($queue)->toList()); |
| 172 | + } |
| 173 | + |
| 174 | + public function testVariadicInternal(): void |
| 175 | + { |
| 176 | + $this->expectOutputString("123"); |
| 177 | + |
| 178 | + $pipeline = fromArray(['1', '2', '3']); |
| 179 | + $pipeline->each(printf(...)); |
| 180 | + } |
| 181 | + |
| 182 | + public function testVariadicInternalOnIterator(): void |
| 183 | + { |
| 184 | + $this->expectOutputString("123"); |
| 185 | + |
| 186 | + $pipeline = take(new ArrayIterator(['1', '2', '3'])); |
| 187 | + $pipeline->each(printf(...)); |
| 188 | + } |
| 189 | + |
| 190 | + public function testArgumentCountError(): void |
| 191 | + { |
| 192 | + $pipeline = fromArray(['1', '2', '3']); |
| 193 | + |
| 194 | + $this->expectException(ArgumentCountError::class); |
| 195 | + $this->expectExceptionMessage('Too few arguments'); |
| 196 | + $pipeline->each(static function ($a, $b, $c): void {}); |
| 197 | + } |
| 198 | + |
| 199 | + /** |
| 200 | + * Test that the reassignment of the callable inside the loop will affect all iterations. |
| 201 | + */ |
| 202 | + public function testCallableReassigned(): void |
| 203 | + { |
| 204 | + $callback = new CallableThrower(); |
| 205 | + |
| 206 | + $pipeline = fromArray(['1', '2', '3']); |
| 207 | + $pipeline->each($callback); |
| 208 | + |
| 209 | + $this->assertSame(4, $callback->callCount, 'Expected 1 initial call that throws + 3 successful calls after wrapping'); |
| 210 | + |
| 211 | + $this->assertSame([ |
| 212 | + ['1', 0], |
| 213 | + ['1'], |
| 214 | + ['2'], |
| 215 | + ['3'], |
| 216 | + ], $callback->args); |
| 217 | + } |
161 | 218 | } |
0 commit comments