1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MathieuTu\Exporter\Tests; |
4
|
|
|
|
5
|
|
|
use MathieuTu\Exporter\ExporterService; |
6
|
|
|
use MathieuTu\Exporter\Tests\Fixtures\Collection; |
7
|
|
|
use MathieuTu\Exporter\Tests\Fixtures\Model; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
|
10
|
|
|
class ExporterServiceTest extends TestCase |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
public function testExportFromObject() |
14
|
|
|
{ |
15
|
|
|
$this->assertEquals( |
16
|
|
|
collect(['foo' => 'testFoo', 'bar' => 'testBar']), |
17
|
|
|
$this->export(['foo', 'bar'], new Model(['foo' => 'testFoo', 'bar' => 'testBar', 'baz' => 'testBaz'])) |
18
|
|
|
); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
protected function export($what, $from) |
22
|
|
|
{ |
23
|
|
|
return (new ExporterService($from))->export($what); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function testExportFromObjectWithGetter() |
27
|
|
|
{ |
28
|
|
|
$this->assertEquals( |
29
|
|
|
collect(['foo' => 'testFoo', 'otherProperty' => 'testOtherProperty']), |
30
|
|
|
$this->export(['foo', 'otherProperty'], new Model(['foo' => 'testFoo'], 'testOtherProperty')) |
31
|
|
|
); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function testExportFromArray() |
35
|
|
|
{ |
36
|
|
|
$this->assertEquals( |
37
|
|
|
collect(['foo' => 'testFoo', 'bar' => 'testBar']), |
38
|
|
|
$this->export(['foo', 'bar'], ['foo' => 'testFoo', 'bar' => 'testBar', 'baz' => 'testBaz']) |
39
|
|
|
); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function testExportFromArrayAccess() |
43
|
|
|
{ |
44
|
|
|
$this->assertEquals( |
45
|
|
|
collect(['foo' => 'testFoo', 'bar' => 'testBar']), |
46
|
|
|
$this->export(['foo', 'bar'], new Collection([ |
47
|
|
|
'foo' => 'testFoo', |
48
|
|
|
'bar' => 'testBar', |
49
|
|
|
'baz' => 'testBaz', |
50
|
|
|
])) |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testTryToExportPrivateProperty() |
55
|
|
|
{ |
56
|
|
|
$this->assertEquals( |
57
|
|
|
collect(['foo' => 'testFoo', 'bar' => null]), |
58
|
|
|
$this->export( |
59
|
|
|
['foo', 'bar'], |
60
|
|
|
new class { |
61
|
|
|
public $foo = 'testFoo'; |
62
|
|
|
private $bar = 'testBar'; |
63
|
|
|
} |
64
|
|
|
) |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function testExportFunction() |
69
|
|
|
{ |
70
|
|
|
$this->assertEquals( |
71
|
|
|
collect(['foo' => 'testFoo', 'bar' => 'testBar']), |
72
|
|
|
$this->export( |
73
|
|
|
['foo()', 'bar(testBar)'], |
74
|
|
|
new class { |
75
|
|
|
public function foo(): string |
76
|
|
|
{ |
77
|
|
|
return 'testFoo'; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function bar($arg) |
81
|
|
|
{ |
82
|
|
|
return $arg; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
) |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function testExportNestedArray() |
90
|
|
|
{ |
91
|
|
|
$this->assertEquals( |
92
|
|
|
collect(['foo' => 'testFoo', 'bar' => collect(['bar2' => 'testBar2'])]), |
93
|
|
|
$this->export(['foo', 'bar' => ['bar2']], [ |
94
|
|
|
'foo' => 'testFoo', |
95
|
|
|
'bar' => ['bar1' => 'testBar1', 'bar2' => 'testBar2'], |
96
|
|
|
'baz' => 'testBaz', |
97
|
|
|
]) |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function testExportNestedArrayWithWildcard() |
102
|
|
|
{ |
103
|
|
|
$this->assertEquals( |
104
|
|
|
collect(['foo' => 'testFoo', 'bar' => collect([ |
105
|
|
|
collect(['bar2' => 'testBar02']), |
106
|
|
|
collect(['bar2' => 'testBar12']), |
107
|
|
|
])]), |
108
|
|
|
$this->export(['foo', 'bar' => ['*' => ['bar2']]], [ |
109
|
|
|
'foo' => 'testFoo', |
110
|
|
|
'bar' => [ |
111
|
|
|
['bar1' => 'testBar01', 'bar2' => 'testBar02'], |
112
|
|
|
['bar1' => 'testBar11', 'bar2' => 'testBar12'], |
113
|
|
|
], |
114
|
|
|
]) |
115
|
|
|
); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function testExportNestedAttribute() |
119
|
|
|
{ |
120
|
|
|
$this->assertEquals( |
121
|
|
|
collect(['foo' => 'testFoo', 'bar' => 'testBar2']), |
122
|
|
|
$this->export(['foo', 'bar' => 'bar2'], [ |
123
|
|
|
'foo' => 'testFoo', |
124
|
|
|
'bar' => ['bar1' => 'testBar1', 'bar2' => 'testBar2'], |
125
|
|
|
'baz' => 'testBaz', |
126
|
|
|
]) |
127
|
|
|
); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function testExportNestedAttributeWithWildcard() |
131
|
|
|
{ |
132
|
|
|
$this->assertEquals( |
133
|
|
|
collect([ |
134
|
|
|
'nested' => collect(['testNested1A', 'testNested1B']), |
135
|
|
|
]), |
136
|
|
|
$this->export(['nested' => ['*' => 'nested1']], [ |
137
|
|
|
'foo' => 'testFoo', |
138
|
|
|
'bar' => ['bar1' => 'testBar1', 'bar2' => 'testBar2'], |
139
|
|
|
'nested' => [ |
140
|
|
|
['nested1' => 'testNested1A', 'nested2' => 'testNested2A'], |
141
|
|
|
['nested1' => 'testNested1B', 'nested2' => 'testNested2B'], |
142
|
|
|
], |
143
|
|
|
]) |
144
|
|
|
); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function testExportDotNotation() |
148
|
|
|
{ |
149
|
|
|
$this->assertEquals( |
150
|
|
|
collect(['foo' => 'testFoo', 'bar.bar2' => 'testBar2']), |
151
|
|
|
$this->export(['foo', 'bar.bar2'], [ |
152
|
|
|
'foo' => 'testFoo', |
153
|
|
|
'bar' => ['bar1' => 'testBar1', 'bar2' => 'testBar2'], |
154
|
|
|
'baz' => 'testBaz', |
155
|
|
|
]) |
156
|
|
|
); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function testExportDotNotationWithNested() |
160
|
|
|
{ |
161
|
|
|
$this->assertEquals( |
162
|
|
|
collect([ |
163
|
|
|
'foo.*' => null, |
164
|
|
|
'bar.*' => ['testBar1', 'testBar2'], |
165
|
|
|
'nested.*.nested1' => ['testNested1A', 'testNested1B'], |
166
|
|
|
]), |
167
|
|
|
$this->export(['foo.*', 'bar.*', 'nested.*.nested1'], [ |
168
|
|
|
'foo' => 'testFoo', |
169
|
|
|
'bar' => collect(['bar1' => 'testBar1', 'bar2' => 'testBar2']), |
170
|
|
|
'nested' => [ |
171
|
|
|
['nested1' => 'testNested1A', 'nested2' => 'testNested2A'], |
172
|
|
|
['nested1' => 'testNested1B', 'nested2' => 'testNested2B'], |
173
|
|
|
], |
174
|
|
|
]) |
175
|
|
|
); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
|
179
|
|
|
public function testExportAliases() |
180
|
|
|
{ |
181
|
|
|
$this->assertEquals( |
182
|
|
|
collect(['foo' => 'testFoo', 'barKey' => 'testBar2', 'bazKey' => 'testBaz']), |
183
|
|
|
$this->export(['foo', 'bar.bar2 as barKey', 'baz as bazKey'], [ |
184
|
|
|
'foo' => 'testFoo', |
185
|
|
|
'bar' => ['bar1' => 'testBar1', 'bar2' => 'testBar2'], |
186
|
|
|
'baz' => 'testBaz', |
187
|
|
|
]) |
188
|
|
|
); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function testExportDotNotationWithNestedAndAliases() |
192
|
|
|
{ |
193
|
|
|
$this->assertEquals( |
194
|
|
|
collect([ |
195
|
|
|
'foos' => null, |
196
|
|
|
'bars' => ['testBar1', 'testBar2'], |
197
|
|
|
'nestedOnes' => ['testNested1A', 'testNested1B'], |
198
|
|
|
]), |
199
|
|
|
$this->export(['foo.* as foos', 'bar.* as bars', 'nested.*.nested1 as nestedOnes'], [ |
200
|
|
|
'foo' => 'testFoo', |
201
|
|
|
'bar' => collect(['bar1' => 'testBar1', 'bar2' => 'testBar2']), |
202
|
|
|
'nested' => [ |
203
|
|
|
['nested1' => 'testNested1A', 'nested2' => 'testNested2A'], |
204
|
|
|
['nested1' => 'testNested1B', 'nested2' => 'testNested2B'], |
205
|
|
|
], |
206
|
|
|
]) |
207
|
|
|
); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
public function testExportUnknownProperty() |
211
|
|
|
{ |
212
|
|
|
$this->assertEquals( |
213
|
|
|
collect(['foo' => 'testFoo', 'bar' => null]), |
214
|
|
|
$this->export(['foo', 'bar'], new Model(['foo' => 'testFoo', 'baz' => 'testBaz'])) |
215
|
|
|
); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
public function testExportOnlyOneAttributeValue() |
219
|
|
|
{ |
220
|
|
|
$this->assertEquals( |
221
|
|
|
'testFoo', |
222
|
|
|
$this->export('foo', new Model(['foo' => 'testFoo', 'baz' => 'testBaz'])) |
223
|
|
|
); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
public function testExportOnlyOneNestedAttributeValue() |
227
|
|
|
{ |
228
|
|
|
$this->assertEquals( |
229
|
|
|
'testBar2', |
230
|
|
|
$this->export('bar.bar2', [ |
231
|
|
|
'foo' => 'testFoo', |
232
|
|
|
'bar' => ['bar1' => 'testBar1', 'bar2' => 'testBar2'], |
233
|
|
|
'baz' => 'testBaz', |
234
|
|
|
]) |
235
|
|
|
); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
public function testPassingUnknownTypeToExportThrowAnException() |
239
|
|
|
{ |
240
|
|
|
$this->expectException(\InvalidArgumentException::class); |
241
|
|
|
$this->expectExceptionMessage("Exporter only accept array, string or int attribute. 'object' passed."); |
242
|
|
|
|
243
|
|
|
$this->export( |
244
|
|
|
new \stdClass(), |
245
|
|
|
['foo' => 'testFoo', 'bar' => 'testBar', 'baz' => 'testBaz'], |
246
|
|
|
); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
public function testExportDirectlyWildcardFromIterable() |
250
|
|
|
{ |
251
|
|
|
$this->assertEquals( |
252
|
|
|
collect([ |
253
|
|
|
collect(['bar2' => 'testBar02']), |
254
|
|
|
collect(['bar2' => 'testBar12']), |
255
|
|
|
]), |
256
|
|
|
$this->export(['*' => ['bar2']], [ |
257
|
|
|
['bar1' => 'testBar01', 'bar2' => 'testBar02'], |
258
|
|
|
['bar1' => 'testBar11', 'bar2' => 'testBar12'], |
259
|
|
|
]) |
260
|
|
|
); |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
|