|
| 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 ArrayIterator; |
| 23 | +use PHPUnit\Framework\TestCase; |
| 24 | + |
| 25 | +use Pipeline\Standard; |
| 26 | +use IteratorIterator; |
| 27 | + |
| 28 | +use function Pipeline\fromArray; |
| 29 | +use function round; |
| 30 | +use function sqrt; |
| 31 | +use function Pipeline\take; |
| 32 | +use function Pipeline\map; |
| 33 | + |
| 34 | +/** |
| 35 | + * @covers \Pipeline\Standard |
| 36 | + * |
| 37 | + * @internal |
| 38 | + */ |
| 39 | +final class TuplesTest extends TestCase |
| 40 | +{ |
| 41 | + public static function provideArrays(): iterable |
| 42 | + { |
| 43 | + yield 'empty_array' => [[], []]; |
| 44 | + yield 'basic_key_values' => [['a' => 1, 'b' => 2, 'c' => 3], [['a', 1], ['b', 2], ['c', 3]]]; |
| 45 | + yield 'numeric_keys' => [[10, 20, 30], [[0, 10], [1, 20], [2, 30]]]; |
| 46 | + |
| 47 | + yield 'mixed_keys_values' => [ |
| 48 | + ['name' => 'Alice', 1 => 'Bob'], |
| 49 | + [['name', 'Alice'], [1, 'Bob']], |
| 50 | + ]; |
| 51 | + |
| 52 | + yield 'null_values' => [['x' => null, 'y' => null], [['x', null], ['y', null]]]; |
| 53 | + |
| 54 | + yield 'empty_string_key' => [['' => 'value'], [['', 'value']]]; |
| 55 | + |
| 56 | + yield 'nested_arrays' => [['outer' => ['inner1' => 'value1', 'inner2' => 'value2']], [['outer', ['inner1' => 'value1', 'inner2' => 'value2']]]]; |
| 57 | + } |
| 58 | + |
| 59 | + public static function provideIterables(): iterable |
| 60 | + { |
| 61 | + foreach (self::provideArrays() as $name => $item) { |
| 62 | + yield $item; |
| 63 | + |
| 64 | + $iteratorItem = $item; |
| 65 | + $iteratorItem[0] = new ArrayIterator($iteratorItem[0]); |
| 66 | + |
| 67 | + yield "$name(ArrayIterator)" => $iteratorItem; |
| 68 | + |
| 69 | + $iteratorItem = $item; |
| 70 | + $iteratorItem[0] = new IteratorIterator(new ArrayIterator($iteratorItem[0])); |
| 71 | + |
| 72 | + yield "$name(IteratorIterator)" => $iteratorItem; |
| 73 | + |
| 74 | + $iteratorItem = $item; |
| 75 | + $iteratorItem[0] = fromArray($iteratorItem[0]); |
| 76 | + |
| 77 | + yield "$name(Pipeline)" => $iteratorItem; |
| 78 | + } |
| 79 | + |
| 80 | + yield 'generator' => [map(function () { |
| 81 | + yield '1' => 2; |
| 82 | + yield '2' => 3; |
| 83 | + }), [['1', 2], ['2', 3]]]; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * @dataProvider provideIterables |
| 88 | + */ |
| 89 | + public function testTuples(iterable $input, array $expected, bool $preserve_keys = false): void |
| 90 | + { |
| 91 | + $pipeline = take($input); |
| 92 | + |
| 93 | + $pipeline->tuples(); |
| 94 | + |
| 95 | + $this->assertSame( |
| 96 | + $expected, |
| 97 | + $pipeline->toArray($preserve_keys) |
| 98 | + ); |
| 99 | + } |
| 100 | + |
| 101 | + public function testNoop(): void |
| 102 | + { |
| 103 | + $pipeline = new Standard(); |
| 104 | + |
| 105 | + $pipeline->tuples(); |
| 106 | + |
| 107 | + $this->assertSame([], $pipeline->toArray()); |
| 108 | + } |
| 109 | +} |
0 commit comments