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
|
|
|
use Tightenco\Collect\Support\Collection as LaravelCollection; |
10
|
|
|
|
11
|
|
|
class ExporterServiceTest extends TestCase |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
public function testExportFromObject() |
15
|
|
|
{ |
16
|
|
|
$this->assertEquals( |
17
|
|
|
collect(['foo' => 'testFoo', 'bar' => 'testBar']), |
18
|
|
|
$this->export(['foo', 'bar'], new Model(['foo' => 'testFoo', 'bar' => 'testBar', 'baz' => 'testBaz'])) |
19
|
|
|
); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
protected function export(array $what, $from): LaravelCollection |
23
|
|
|
{ |
24
|
|
|
return (new ExporterService($from))->export($what); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function testExportFromObjectWithGetter() |
28
|
|
|
{ |
29
|
|
|
$this->assertEquals( |
30
|
|
|
collect(['foo' => 'testFoo', 'otherProperty' => 'testOtherProperty']), |
31
|
|
|
$this->export(['foo', 'otherProperty'], new Model(['foo' => 'testFoo'], 'testOtherProperty')) |
32
|
|
|
); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function testExportFromArray() |
36
|
|
|
{ |
37
|
|
|
$this->assertEquals( |
38
|
|
|
collect(['foo' => 'testFoo', 'bar' => 'testBar']), |
39
|
|
|
$this->export(['foo', 'bar'], ['foo' => 'testFoo', 'bar' => 'testBar', 'baz' => 'testBaz']) |
40
|
|
|
); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testExportFromArrayAccess() |
44
|
|
|
{ |
45
|
|
|
$this->assertEquals( |
46
|
|
|
collect(['foo' => 'testFoo', 'bar' => 'testBar']), |
47
|
|
|
$this->export(['foo', 'bar'], new Collection([ |
48
|
|
|
'foo' => 'testFoo', |
49
|
|
|
'bar' => 'testBar', |
50
|
|
|
'baz' => 'testBaz', |
51
|
|
|
])) |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function testTryToExportPrivateProperty() |
56
|
|
|
{ |
57
|
|
|
$this->assertEquals( |
58
|
|
|
collect(['foo' => 'testFoo', 'bar' => null]), |
59
|
|
|
$this->export( |
60
|
|
|
['foo', 'bar'], |
61
|
|
|
new class |
62
|
|
|
{ |
63
|
|
|
public $foo = 'testFoo'; |
64
|
|
|
private $bar = 'testBar'; |
|
|
|
|
65
|
|
|
} |
66
|
|
|
) |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testExportFunction() |
71
|
|
|
{ |
72
|
|
|
$this->assertEquals( |
73
|
|
|
collect(['foo' => 'testFoo', 'bar' => 'testBar']), |
74
|
|
|
$this->export( |
75
|
|
|
['foo()', 'bar(testBar)'], |
76
|
|
|
new class |
77
|
|
|
{ |
78
|
|
|
public function foo(): string |
79
|
|
|
{ |
80
|
|
|
return 'testFoo'; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function bar($arg) |
84
|
|
|
{ |
85
|
|
|
return $arg; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
) |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function testExportNestedArray() |
93
|
|
|
{ |
94
|
|
|
$this->assertEquals( |
95
|
|
|
collect(['foo' => 'testFoo', 'bar' => collect(['bar2' => 'testBar2'])]), |
96
|
|
|
$this->export(['foo', 'bar' => ['bar2']], [ |
97
|
|
|
'foo' => 'testFoo', |
98
|
|
|
'bar' => ['bar1' => 'testBar1', 'bar2' => 'testBar2'], |
99
|
|
|
'baz' => 'testBaz', |
100
|
|
|
]) |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function testExportNestedArrayWithWildcard() |
105
|
|
|
{ |
106
|
|
|
$this->assertEquals( |
107
|
|
|
collect(['foo' => 'testFoo', 'bar' => collect([ |
108
|
|
|
collect(['bar2' => 'testBar02']), |
109
|
|
|
collect(['bar2' => 'testBar12']), |
110
|
|
|
])]), |
111
|
|
|
$this->export(['foo', 'bar' => ['*' => ['bar2']]], [ |
112
|
|
|
'foo' => 'testFoo', |
113
|
|
|
'bar' => [ |
114
|
|
|
['bar1' => 'testBar01', 'bar2' => 'testBar02'], |
115
|
|
|
['bar1' => 'testBar11', 'bar2' => 'testBar12'], |
116
|
|
|
], |
117
|
|
|
]) |
118
|
|
|
); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function testExportNestedAttribute() |
122
|
|
|
{ |
123
|
|
|
$this->assertEquals( |
124
|
|
|
collect(['foo' => 'testFoo', 'bar' => 'testBar2']), |
125
|
|
|
$this->export(['foo', 'bar' => 'bar2'], [ |
126
|
|
|
'foo' => 'testFoo', |
127
|
|
|
'bar' => ['bar1' => 'testBar1', 'bar2' => 'testBar2'], |
128
|
|
|
'baz' => 'testBaz', |
129
|
|
|
]) |
130
|
|
|
); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function testExportDotNotation() |
134
|
|
|
{ |
135
|
|
|
$this->assertEquals( |
136
|
|
|
collect(['foo' => 'testFoo', 'bar.bar2' => 'testBar2']), |
137
|
|
|
$this->export(['foo', 'bar.bar2'], [ |
138
|
|
|
'foo' => 'testFoo', |
139
|
|
|
'bar' => ['bar1' => 'testBar1', 'bar2' => 'testBar2'], |
140
|
|
|
'baz' => 'testBaz', |
141
|
|
|
]) |
142
|
|
|
); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function testExportDotNotationWithNested() |
146
|
|
|
{ |
147
|
|
|
$this->assertEquals( |
148
|
|
|
collect([ |
149
|
|
|
'foo.*' => null, |
150
|
|
|
'bar.*' => ['testBar1', 'testBar2'], |
151
|
|
|
'nested.*.nested1' => ['testNested1A', 'testNested1B'], |
152
|
|
|
]), |
153
|
|
|
$this->export(['foo.*', 'bar.*', 'nested.*.nested1'], [ |
154
|
|
|
'foo' => 'testFoo', |
155
|
|
|
'bar' => collect(['bar1' => 'testBar1', 'bar2' => 'testBar2']), |
156
|
|
|
'nested' => [ |
157
|
|
|
['nested1' => 'testNested1A', 'nested2' => 'testNested2A'], |
158
|
|
|
['nested1' => 'testNested1B', 'nested2' => 'testNested2B'], |
159
|
|
|
], |
160
|
|
|
]) |
161
|
|
|
); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function testExportUnknownProperty() |
165
|
|
|
{ |
166
|
|
|
$this->assertEquals( |
167
|
|
|
collect(['foo' => 'testFoo', 'bar' => null]), |
168
|
|
|
$this->export(['foo', 'bar'], new Model(['foo' => 'testFoo', 'baz' => 'testBaz'])) |
169
|
|
|
); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|