1+ <?php
2+
3+ namespace Camillebaronnet \ETL \Tests \Transformer ;
4+
5+ use Camillebaronnet \ETL \Transformer \Flatten ;
6+ use PHPUnit \Framework \TestCase ;
7+
8+ final class FlattenTest extends TestCase
9+ {
10+ /**
11+ * @var Flatten
12+ */
13+ private $ flatten ;
14+
15+ /**
16+ * @var array
17+ */
18+ private $ complexObject = [
19+ 'name ' => 'Bar ' ,
20+ 'address ' => [
21+ 'street ' => '1 road ' ,
22+ 'zip ' => 'xxx ' ,
23+ ],
24+ 'cards ' => [
25+ ['name ' => 'lo ' ],
26+ ['name ' => 'lol ' ],
27+ ['name ' => 'oll ' ],
28+ ],
29+ 'sub-tree ' => [
30+ 'sub-key ' => 'some value ' ,
31+ 're-sub-key ' => 'another value ' ,
32+ ],
33+ ];
34+
35+
36+ protected function setUp ()
37+ {
38+ $ this ->flatten = new Flatten ();
39+ }
40+
41+ public function testCanFlattenObjectWithDefaultContext ()
42+ {
43+ $ result = $ this ->flatten ->__invoke ($ this ->complexObject );
44+
45+ $ this ->assertEquals ($ result , [
46+ 'name ' => 'Bar ' ,
47+ 'address.street ' => '1 road ' ,
48+ 'address.zip ' => 'xxx ' ,
49+ 'cards.0.name ' => 'lo ' ,
50+ 'cards.1.name ' => 'lol ' ,
51+ 'cards.2.name ' => 'oll ' ,
52+ 'sub-tree.sub-key ' => 'some value ' ,
53+ 'sub-tree.re-sub-key ' => 'another value ' ,
54+ ]);
55+ }
56+
57+ public function testCanFlattenOnlySomeSegments ()
58+ {
59+ $ result = $ this ->flatten ->__invoke ($ this ->complexObject , [
60+ 'only ' => ['address ' , 'sub-tree ' ],
61+ ]);
62+
63+ $ this ->assertEquals ($ result , [
64+ 'name ' => 'Bar ' ,
65+ 'address.street ' => '1 road ' ,
66+ 'address.zip ' => 'xxx ' ,
67+ 'cards ' => [
68+ ['name ' => 'lo ' ],
69+ ['name ' => 'lol ' ],
70+ ['name ' => 'oll ' ],
71+ ],
72+ 'sub-tree.sub-key ' => 'some value ' ,
73+ 'sub-tree.re-sub-key ' => 'another value ' ,
74+ ]);
75+ }
76+
77+ public function testCanFlattenAllSegmentsExceptOne ()
78+ {
79+ $ result = $ this ->flatten ->__invoke ($ this ->complexObject , [
80+ 'ignore ' => ['address ' ],
81+ ]);
82+
83+ $ this ->assertEquals ($ result , [
84+ 'name ' => 'Bar ' ,
85+ 'address ' => [
86+ 'street ' => '1 road ' ,
87+ 'zip ' => 'xxx ' ,
88+ ],
89+ 'cards.0.name ' => 'lo ' ,
90+ 'cards.1.name ' => 'lol ' ,
91+ 'cards.2.name ' => 'oll ' ,
92+ 'sub-tree.sub-key ' => 'some value ' ,
93+ 'sub-tree.re-sub-key ' => 'another value ' ,
94+ ]);
95+ }
96+
97+ public function testCanSpecifyACustomRootKey ()
98+ {
99+ $ result = $ this ->flatten ->__invoke ($ this ->complexObject , [
100+ 'rootKey ' => '__ ' ,
101+ ]);
102+
103+ $ this ->assertEquals ($ result , [
104+ '__name ' => 'Bar ' ,
105+ '__address.street ' => '1 road ' ,
106+ '__address.zip ' => 'xxx ' ,
107+ '__cards.0.name ' => 'lo ' ,
108+ '__cards.1.name ' => 'lol ' ,
109+ '__cards.2.name ' => 'oll ' ,
110+ '__sub-tree.sub-key ' => 'some value ' ,
111+ '__sub-tree.re-sub-key ' => 'another value ' ,
112+ ]);
113+ }
114+
115+ public function testCanSpecifyACustomGlue ()
116+ {
117+ $ result = $ this ->flatten ->__invoke ($ this ->complexObject , [
118+ 'glue ' => '_ ' ,
119+ ]);
120+
121+ $ this ->assertEquals ($ result , [
122+ 'name ' => 'Bar ' ,
123+ 'address_street ' => '1 road ' ,
124+ 'address_zip ' => 'xxx ' ,
125+ 'cards_0_name ' => 'lo ' ,
126+ 'cards_1_name ' => 'lol ' ,
127+ 'cards_2_name ' => 'oll ' ,
128+ 'sub-tree_sub-key ' => 'some value ' ,
129+ 'sub-tree_re-sub-key ' => 'another value ' ,
130+ ]);
131+ }
132+ }
0 commit comments