-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathExporterServiceTest.php
More file actions
330 lines (299 loc) · 11 KB
/
Copy pathExporterServiceTest.php
File metadata and controls
330 lines (299 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<?php
declare(strict_types=1);
namespace MathieuTu\Exporter\Tests;
use Illuminate\Support\Collection as SupportCollection;
use MathieuTu\Exporter\ExporterService;
use MathieuTu\Exporter\NotFoundException;
use MathieuTu\Exporter\Tests\Fixtures\Collection;
use MathieuTu\Exporter\Tests\Fixtures\Model;
use PHPUnit\Framework\TestCase;
class ExporterServiceTest extends TestCase
{
public function testExportFromObject()
{
$this->assertEquals(
collect(['foo' => 'testFoo', 'bar' => 'testBar']),
$this->export(['foo', 'bar'], new Model(['foo' => 'testFoo', 'bar' => 'testBar', 'baz' => 'testBaz']))
);
}
protected function export($what, $from)
{
return (new ExporterService($from))->export($what);
}
public function testExportFromObjectWithGetter()
{
$this->assertEquals(
collect(['foo' => 'testFoo', 'otherProperty' => 'testOtherProperty']),
$this->export(['foo', 'otherProperty'], new Model(['foo' => 'testFoo'], 'testOtherProperty')),
);
}
public function testExportFromObjectWithGetterChangingCase()
{
$this->assertEquals(
collect(['other_property' => 'testOtherProperty']),
$this->export(['other_property'], new Model([], 'testOtherProperty')),
);
}
public function testExportFromArray()
{
$this->assertEquals(
collect(['foo' => 'testFoo', 'bar' => 'testBar']),
$this->export(['foo', 'bar'], ['foo' => 'testFoo', 'bar' => 'testBar', 'baz' => 'testBaz']),
);
}
public function testExportFromArrayAccess()
{
$this->assertEquals(
collect(['foo' => 'testFoo', 'bar' => 'testBar']),
$this->export(['foo', 'bar'], new Collection([
'foo' => 'testFoo',
'bar' => 'testBar',
'baz' => 'testBaz',
])),
);
}
public function testTryToExportPrivateProperty()
{
$this->assertEquals(
collect(['foo' => 'testFoo', 'bar' => null]),
$this->export(
['foo', 'bar'],
new class {
public $foo = 'testFoo';
private $bar = 'testBar';
},
),
);
}
public function testExportFunction()
{
$this->assertEquals(
collect([
'foo' => 'testFoo', 'bar' => 'testBar',
'nested' => 'nested:plop',
'nestedCollect' => collect(['fnct' => 'nested:plop']),
'nestedDot' => 'nested:plop',
]),
$this->export(
[
'foo()', 'bar(testBar)',
'nested' => 'fnct(plop)',
'nested as nestedCollect' => ['fnct(plop)'],
'nested.fnct(plop) as nestedDot'
],
new class {
public $nested;
public function __construct()
{
$this->nested = new class {
public function fnct($arg)
{
return 'nested:' . $arg;
}
};
}
public function foo(): string
{
return 'testFoo';
}
public function bar($arg)
{
return $arg;
}
},
),
);
}
public function testExportNestedArray()
{
$this->assertEquals(
collect(['foo' => 'testFoo', 'bar' => collect(['bar2' => 'testBar2'])]),
$this->export(['foo', 'bar' => ['bar2']], [
'foo' => 'testFoo',
'bar' => ['bar1' => 'testBar1', 'bar2' => 'testBar2'],
'baz' => 'testBaz',
]),
);
}
public function testExportNestedArrayWithWildcard()
{
$this->assertEquals(
collect([
'foo' => 'testFoo',
'bar' => collect([
collect(['bar2' => 'testBar02']),
collect(['bar2' => 'testBar12']),
]),
]),
$this->export(['foo', 'bar' => ['*' => ['bar2']]], [
'foo' => 'testFoo',
'bar' => collect([
collect(['bar1' => 'testBar01', 'bar2' => 'testBar02']),
['bar1' => 'testBar11', 'bar2' => 'testBar12'],
]),
]),
);
}
public function testExportNestedAttribute()
{
$this->assertEquals(
collect(['foo' => 'testFoo', 'bar' => 'testBar2']),
$this->export(['foo', 'bar' => 'bar2'], [
'foo' => 'testFoo',
'bar' => ['bar1' => 'testBar1', 'bar2' => 'testBar2'],
'baz' => 'testBaz',
]),
);
}
public function testExportNestedAttributeWithWildcard()
{
$this->assertEquals(
collect([
'nested' => collect(['testNested1A', 'testNested1B']),
]),
$this->export(['nested' => ['*' => 'nested1']], [
'foo' => 'testFoo',
'bar' => ['bar1' => 'testBar1', 'bar2' => 'testBar2'],
'nested' => [
collect(['nested1' => 'testNested1A', 'nested2' => 'testNested2A']),
['nested1' => 'testNested1B', 'nested2' => 'testNested2B'],
],
]),
);
}
public function testExportDotNotation()
{
$this->assertEquals(
collect(['foo' => 'testFoo', 'bar.bar2' => 'testBar2']),
$this->export(['foo', 'bar.bar2'], [
'foo' => 'testFoo',
'bar' => ['bar1' => 'testBar1', 'bar2' => 'testBar2'],
'baz' => 'testBaz',
]),
);
}
public function testExportDotNotationWithNested()
{
$this->assertEquals(
collect([
'foo.*' => null,
'bar.*' => ['testBar1', 'testBar2'],
'nested.*.nested1' => ['testNested1A', 'testNested1B'],
]),
$this->export(['foo.*', 'bar.*', 'nested.*.nested1'], [
'foo' => 'testFoo',
'bar' => collect(['bar1' => 'testBar1', 'bar2' => 'testBar2']),
'nested' => [
['nested1' => 'testNested1A', 'nested2' => 'testNested2A'],
['nested1' => 'testNested1B', 'nested2' => 'testNested2B'],
],
]),
);
}
public function testExportAliases()
{
$this->assertEquals(
collect([
'foo' => 'testFoo', 'barKey' => 'testBar2', 'bazKey' => 'testBaz',
'nestedBar' => collect(['a' => 'testA', 'cbis' => 'testC']), 'barB' => 'testB'
]),
$this->export([
'foo', 'bar.bar2 as barKey', 'baz as bazKey',
'bar.bar3 as nestedBar' => ['a', 'c as cbis'], 'bar as barB' => 'bar3.b'
], [
'foo' => 'testFoo',
'bar' => ['bar1' => 'testBar1', 'bar2' => 'testBar2', 'bar3' => ['a' => 'testA', 'b' => 'testB', 'c' => 'testC']],
'baz' => 'testBaz',
]),
);
}
public function testExportDotNotationWithNestedAndAliases()
{
$this->assertEquals(
collect([
'foos' => null,
'bars' => ['testBar1', 'testBar2'],
'nestedOnes' => ['testNested1A', 'testNested1B'],
]),
$this->export(['foo.* as foos', 'bar.* as bars', 'nested.*.nested1 as nestedOnes'], [
'foo' => 'testFoo',
'bar' => collect(['bar1' => 'testBar1', 'bar2' => 'testBar2']),
'nested' => [
['nested1' => 'testNested1A', 'nested2' => 'testNested2A'],
['nested1' => 'testNested1B', 'nested2' => 'testNested2B'],
],
]),
);
}
public function testExportingUnknowPropertyThrowsIfStrictnessEnabled()
{
ExporterService::$strict = true;
try {
$this->export(['foo', 'bar'], new SupportCollection(['foo' => 'testFoo', 'baz' => 'testBaz']));
$this->fail('An NotFoundException should have been thrown when trying to export an unknown property with strictness enabled');
} catch (NotFoundException $e) {
$this->addToAssertionCount(1);
$this->assertEquals('bar can\'t be found in {"foo":"testFoo","baz":"testBaz"}', $e->getMessage());
} finally {
ExporterService::$strict = false;
}
}
public function testExportingUnknownPropertyReturnsNullIfStrictnessDisabled()
{
$this->assertEquals(
collect(['foo' => 'testFoo', 'bar' => null]),
$this->export(['foo', 'bar'], new Model(['foo' => 'testFoo', 'baz' => 'testBaz'])),
);
}
public function testExportUnknownOrNullPropertyNested()
{
$this->assertEquals(
collect(['foo' => 'testFoo', 'bar' => collect(['not_existing' => collect(['nested' => null])])]),
$this->export(['foo', 'bar' => ['not_existing' => ['nested']]], new Model(['foo' => 'testFoo', 'baz' => 'testBaz'])),
);
$this->assertEquals(
collect(['foo' => 'testFoo', 'bar' => collect(['not_existing' => collect(['nested' => null])])]),
$this->export(['foo', 'bar' => ['not_existing' => ['nested']]], new Model(['foo' => 'testFoo', 'bar' => null])),
);
}
public function testExportOnlyOneAttributeValue()
{
$this->assertEquals(
'testFoo',
$this->export('foo', new Model(['foo' => 'testFoo', 'baz' => 'testBaz'])),
);
}
public function testExportOnlyOneNestedAttributeValue()
{
$this->assertEquals(
'testBar2',
$this->export('bar.bar2', [
'foo' => 'testFoo',
'bar' => ['bar1' => 'testBar1', 'bar2' => 'testBar2'],
'baz' => 'testBaz',
]),
);
}
public function testPassingUnknownTypeToExportThrowAnException()
{
$this->expectException(\TypeError::class);
$this->expectExceptionMessage('Argument #1 ($attributes) must be of type array|string|int');
$this->export(
new \stdClass(),
['foo' => 'testFoo', 'bar' => 'testBar', 'baz' => 'testBaz'],
);
}
public function testExportDirectlyWildcardFromIterable()
{
$this->assertEquals(
collect([
collect(['bar2' => 'testBar02']),
collect(['bar2' => 'testBar12']),
]),
$this->export(['*' => ['bar2']], [
['bar1' => 'testBar01', 'bar2' => 'testBar02'],
['bar1' => 'testBar11', 'bar2' => 'testBar12'],
]),
);
}
}