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