|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Sarhan\Flatten\Test; |
| 4 | + |
| 5 | +use PHPUnit\Framework\TestCase; |
| 6 | +use Sarhan\Flatten\Util\TraversableToArray; |
| 7 | + |
| 8 | +class TraversableToArrayTest extends TestCase |
| 9 | +{ |
| 10 | + public function traversablesProvider() |
| 11 | + { |
| 12 | + // Iterator example |
| 13 | + $iteratorsInput = new \AppendIterator(); |
| 14 | + $iteratorsInput->append(new \ArrayIterator([ |
| 15 | + 'a', |
| 16 | + 'b', |
| 17 | + 100 => 'c', |
| 18 | + 'd' => [1, 2, 3], |
| 19 | + 'e' |
| 20 | + ])); |
| 21 | + $iteratorsInput->append(new \ArrayIterator([ |
| 22 | + 'A', |
| 23 | + new \ArrayIterator(['B', 'bB']), |
| 24 | + 100 => ['d', 'e', 'f'], |
| 25 | + 'E', |
| 26 | + 'D' => 'd' |
| 27 | + ])); |
| 28 | + |
| 29 | + // Generator example |
| 30 | + $generatorInput = (function () { |
| 31 | + yield 0 => 'a'; |
| 32 | + yield 0 => 'A'; |
| 33 | + yield 1 => 'b'; |
| 34 | + yield 1 => (function () { |
| 35 | + yield 0 => 'B'; |
| 36 | + yield 1 => 'bB'; |
| 37 | + })(); |
| 38 | + yield 100 => 'c'; |
| 39 | + yield 100 => ['d', 'e', 'f']; |
| 40 | + yield 'd' => [1, 2, 3]; |
| 41 | + yield 101 => 'e'; |
| 42 | + yield 101 => 'E'; |
| 43 | + yield 'D' => 'd'; |
| 44 | + })(); |
| 45 | + |
| 46 | + $expectedOutput = [ |
| 47 | + [['a', 'A']], |
| 48 | + [['b', 'B'], 'bB'], |
| 49 | + 100 => [['c', 'd'], 'e', 'f'], |
| 50 | + 'd' => [1, 2, 3], |
| 51 | + [['e', 'E']], |
| 52 | + 'D' => 'd' |
| 53 | + ]; |
| 54 | + |
| 55 | + return [ |
| 56 | + 'Iterator' => [$iteratorsInput, $expectedOutput], |
| 57 | + 'Generator' => [$generatorInput, $expectedOutput] |
| 58 | + ]; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @dataProvider traversablesProvider |
| 63 | + */ |
| 64 | + public function testTraversableToArray($input, $expectedOutput) |
| 65 | + { |
| 66 | + $output = TraversableToArray::toArray($input); |
| 67 | + $this->assertEquals($expectedOutput, $output); |
| 68 | + } |
| 69 | +} |
0 commit comments