Completed
Pull Request — master (#10)
by Mathieu
08:43
created

ExporterServiceTest::testExportDotNotation()

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
eloc 6
nc 1
nop 0
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
                    public $foo = 'testFoo';
63
                    private $bar = 'testBar';
64
                }
65
            )
66
        );
67
    }
68
69
    public function testExportFunction()
70
    {
71
        $this->assertEquals(
72
            collect(['foo' => 'testFoo', 'bar' => 'testBar']),
73
            $this->export(
74
                ['foo()', 'bar(testBar)'],
75
                new class {
76
                    public function foo(): string
77
                    {
78
                        return 'testFoo';
79
                    }
80
81
                    public function bar($arg)
82
                    {
83
                        return $arg;
84
                    }
85
                }
86
            )
87
        );
88
    }
89
90
    public function testExportNestedArray()
91
    {
92
        $this->assertEquals(
93
            collect(['foo' => 'testFoo', 'bar' => collect(['bar2' => 'testBar2'])]),
94
            $this->export(['foo', 'bar' => ['bar2']], [
95
                'foo' => 'testFoo',
96
                'bar' => ['bar1' => 'testBar1', 'bar2' => 'testBar2'],
97
                'baz' => 'testBaz',
98
            ])
99
        );
100
    }
101
102
    public function testExportNestedArrayWithWildcard()
103
    {
104
        $this->assertEquals(
105
            collect(['foo' => 'testFoo', 'bar' => collect([
106
                collect(['bar2' => 'testBar02']),
107
                collect(['bar2' => 'testBar12']),
108
            ])]),
109
            $this->export(['foo', 'bar' => ['*' => ['bar2']]], [
110
                'foo' => 'testFoo',
111
                'bar' => [
112
                    ['bar1' => 'testBar01', 'bar2' => 'testBar02'],
113
                    ['bar1' => 'testBar11', 'bar2' => 'testBar12'],
114
                ],
115
            ])
116
        );
117
    }
118
119
    public function testExportNestedAttribute()
120
    {
121
        $this->assertEquals(
122
            collect(['foo' => 'testFoo', 'bar' => 'testBar2']),
123
            $this->export(['foo', 'bar' => 'bar2'], [
124
                'foo' => 'testFoo',
125
                'bar' => ['bar1' => 'testBar1', 'bar2' => 'testBar2'],
126
                'baz' => 'testBaz',
127
            ])
128
        );
129
    }
130
131
    public function testExportDotNotation()
132
    {
133
        $this->assertEquals(
134
            collect(['foo' => 'testFoo', 'bar.bar2' => 'testBar2']),
135
            $this->export(['foo', 'bar.bar2'], [
136
                'foo' => 'testFoo',
137
                'bar' => ['bar1' => 'testBar1', 'bar2' => 'testBar2'],
138
                'baz' => 'testBaz',
139
            ])
140
        );
141
    }
142
143
    public function testExportDotNotationWithNested()
144
    {
145
        $this->assertEquals(
146
            collect([
147
                'foo.*' => null,
148
                'bar.*' => ['testBar1', 'testBar2'],
149
                'nested.*.nested1' => ['testNested1A', 'testNested1B'],
150
            ]),
151
            $this->export(['foo.*', 'bar.*', 'nested.*.nested1'], [
152
                'foo' => 'testFoo',
153
                'bar' => collect(['bar1' => 'testBar1', 'bar2' => 'testBar2']),
154
                'nested' => [
155
                    ['nested1' => 'testNested1A', 'nested2' => 'testNested2A'],
156
                    ['nested1' => 'testNested1B', 'nested2' => 'testNested2B'],
157
                ],
158
            ])
159
        );
160
    }
161
162
163
    public function testExportAliases()
164
    {
165
        $this->assertEquals(
166
            collect(['foo' => 'testFoo', 'barKey' => 'testBar2', 'bazKey' => 'testBaz']),
167
            $this->export(['foo', 'bar.bar2 as barKey', 'baz as bazKey'], [
168
                'foo' => 'testFoo',
169
                'bar' => ['bar1' => 'testBar1', 'bar2' => 'testBar2'],
170
                'baz' => 'testBaz',
171
            ])
172
        );
173
    }
174
175
    public function testExportUnknownProperty()
176
    {
177
        $this->assertEquals(
178
            collect(['foo' => 'testFoo', 'bar' => null]),
179
            $this->export(['foo', 'bar'], new Model(['foo' => 'testFoo', 'baz' => 'testBaz']))
180
        );
181
    }
182
}
183