Skip to content

Commit ffcf38c

Browse files
committed
Test TraversalsToArray
1 parent 9eeb107 commit ffcf38c

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

test/FlattenTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,6 @@ private function flattenToArray(
227227
$prefix = Flatten::DEFAULT_PREFIX,
228228
$flags = Flatten::DEFAULT_FLAGS
229229
) {
230-
return TraversableToArray::toArray((new Flatten($separator, $prefix, $flags))->flatten($input));
230+
return (new Flatten($separator, $prefix, $flags))->flattenToArray($input);
231231
}
232232
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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

Comments
 (0)