Passed
Push — master ( c9a540...1f886f )
by Mathieu
02:20
created

ExporterServiceTest::testExportFromArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
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 testExportFunction()
56
    {
57
        $this->assertEquals(
58
            collect(['foo' => 'testFoo', 'bar' => 'testBar']),
59
            $this->export(
60
                ['foo()', 'bar(testBar)'],
61
                new class
62
                {
63
                    public function foo(): string
64
                    {
65
                        return 'testFoo';
66
                    }
67
68
                    public function bar($arg)
69
                    {
70
                        return $arg;
71
                    }
72
                }
73
            )
74
        );
75
    }
76
77
    public function testExportNestedArray()
78
    {
79
        $this->assertEquals(
80
            collect(['foo' => 'testFoo', 'bar' => collect(['bar2' => 'testBar2'])]),
81
            $this->export(['foo', 'bar' => ['bar2']], [
82
                'foo' => 'testFoo',
83
                'bar' => ['bar1' => 'testBar1', 'bar2' => 'testBar2'],
84
                'baz' => 'testBaz',
85
            ])
86
        );
87
    }
88
89
    public function testExportNestedArrayWithWildcard()
90
    {
91
        $this->assertEquals(
92
            collect(['foo' => 'testFoo', 'bar' => collect([
93
                collect(['bar2' => 'testBar02']),
94
                collect(['bar2' => 'testBar12']),
95
            ])]),
96
            $this->export(['foo', 'bar' => ['*' => ['bar2']]], [
97
                'foo' => 'testFoo',
98
                'bar' => [
99
                    ['bar1' => 'testBar01', 'bar2' => 'testBar02'],
100
                    ['bar1' => 'testBar11', 'bar2' => 'testBar12'],
101
                ],
102
            ])
103
        );
104
    }
105
106
    public function testExportNestedAttribute()
107
    {
108
        $this->assertEquals(
109
            collect(['foo' => 'testFoo', 'bar' => 'testBar2']),
110
            $this->export(['foo', 'bar' => 'bar2'], [
111
                'foo' => 'testFoo',
112
                'bar' => ['bar1' => 'testBar1', 'bar2' => 'testBar2'],
113
                'baz' => 'testBaz',
114
            ])
115
        );
116
    }
117
118
    public function testExportDotNotation()
119
    {
120
        $this->assertEquals(
121
            collect(['foo' => 'testFoo', 'bar.bar2' => '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 testExportDotNotationWithNested()
131
    {
132
        $this->assertEquals(
133
            collect([
134
                'foo.*'            => null,
135
                'bar.*'            => ['testBar1', 'testBar2'],
136
                'nested.*.nested1' => ['testNested1A', 'testNested1B'],
137
            ]),
138
            $this->export(['foo.*', 'bar.*', 'nested.*.nested1'], [
139
                'foo'    => 'testFoo',
140
                'bar'    => collect(['bar1' => 'testBar1', 'bar2' => 'testBar2']),
141
                'nested' => [
142
                    ['nested1' => 'testNested1A', 'nested2' => 'testNested2A'],
143
                    ['nested1' => 'testNested1B', 'nested2' => 'testNested2B'],
144
                ],
145
            ])
146
        );
147
    }
148
149
    public function testExportUnknownProperty()
150
    {
151
        $this->assertEquals(
152
            collect(['foo' => 'testFoo', 'bar' => null]),
153
            $this->export(['foo', 'bar'], new Model(['foo' => 'testFoo', 'baz' => 'testBaz']))
154
        );
155
    }
156
}
157