Skip to content

Commit 056ede2

Browse files
test expand
1 parent 6784100 commit 056ede2

File tree

5 files changed

+94
-17
lines changed

5 files changed

+94
-17
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"src/Set.php",
3535
"src/Pipe.php",
3636
"src/Flatten.php",
37+
"src/Expand.php",
3738
"src/DotNotation.php",
3839
"src/Remove.php",
3940
"src/array_functions.php"

src/DotNotation.php

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,6 @@
33

44
namespace BrenoRoosevelt;
55

6-
/**
7-
* @param array $items
8-
* @param string $separator
9-
* @return array
10-
*/
11-
function expand(array $items, string $separator = '.'): array
12-
{
13-
$result = [];
14-
foreach ($items as $key => $value) {
15-
set_path($result, (string) $key, $value, $separator);
16-
}
17-
18-
return $result;
19-
}
20-
216
/**
227
* @param array $haystack
238
* @param string $path

src/Expand.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace BrenoRoosevelt;
5+
6+
/**
7+
* @param array $items
8+
* @param string $separator
9+
* @return array
10+
*/
11+
function expand(array $items, string $separator = '.'): array
12+
{
13+
$result = [];
14+
foreach ($items as $key => $value) {
15+
set_path($result, (string) $key, $value, $separator);
16+
}
17+
18+
return $result;
19+
}

tests/ExpandTest.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace BrenoRoosevelt\Tests;
5+
6+
use PHPUnit\Framework\TestCase;
7+
use function BrenoRoosevelt\expand;
8+
9+
class ExpandTest extends TestCase
10+
{
11+
public function expandProvider(): array
12+
{
13+
return [
14+
'case_1' => [
15+
['x' => 'a', 'y.0' => 'b', 'y.1.z' => 'c', 'y.2' => 'd'],
16+
'.',
17+
['x' => 'a', 'y' => ['b', ['z' => 'c'], 'd']],
18+
],
19+
'case_2' => [
20+
['x' => 'a', 'y\\0' => 'b', 'y\\1\\z' => 'c', 'y\\2' => 'd'],
21+
'\\',
22+
['x' => 'a', 'y' => ['b', ['z' => 'c'], 'd']],
23+
],
24+
'case_3' => [
25+
[
26+
'0.Post.id' => '1',
27+
'0.Post.author_id' => '1',
28+
'0.Post.title' => '1st post',
29+
'0.Author.id' => '1',
30+
'0.Author.user' => 'spencer',
31+
'0.Author.password' => 'pwd',
32+
'1.Post.id' => '2',
33+
'1.Post.author_id' => '3',
34+
'1.Post.title' => '2nd Post',
35+
'1.Post.body' => '2nd post body',
36+
'1.Author.id' => '3',
37+
'1.Author.user' => 'mary',
38+
'1.Author.password' => null,
39+
],
40+
'.',
41+
[
42+
[
43+
'Post' => ['id' => '1', 'author_id' => '1', 'title' => '1st post'],
44+
'Author' => ['id' => '1', 'user' => 'spencer', 'password' => 'pwd'],
45+
],
46+
[
47+
'Post' => ['id' => '2', 'author_id' => '3', 'title' => '2nd Post', 'body' => '2nd post body'],
48+
'Author' => ['id' => '3', 'user' => 'mary', 'password' => null],
49+
],
50+
],
51+
],
52+
'case_4' => [
53+
[],
54+
'.',
55+
[]
56+
],
57+
];
58+
}
59+
60+
/**
61+
* @param array $items
62+
* @param string|null $separator
63+
* @param array $expected
64+
* @return void
65+
* @dataProvider expandProvider
66+
*/
67+
public function testExpand(array $items, string $separator, array $expected): void
68+
{
69+
$result = expand($items, $separator);
70+
$this->assertEquals($expected, $result);
71+
}
72+
}

tests/FlattenTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
class FlattenTest extends TestCase
1010
{
11-
public function flattenProvider(): array
11+
public function dataProvider(): array
1212
{
1313
return [
1414
'case_1' => [
@@ -82,7 +82,7 @@ public function flattenProvider(): array
8282
* @param string|null $separator
8383
* @param array $expected
8484
* @return void
85-
* @dataProvider flattenProvider
85+
* @dataProvider dataProvider
8686
*/
8787
public function testFlatten(array $items, ?string $separator, array $expected): void
8888
{

0 commit comments

Comments
 (0)