DuplicatesTest   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 225
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
dl 0
loc 225
c 0
b 0
f 0
wmc 9
lcom 1
cbo 7
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getShaper() 0 8 1
A testOneLevelParse() 0 17 2
A testTwoLevelParse() 0 54 2
B testTwoLevelParse2() 0 59 2
B testThreeLevelParse() 0 76 2
1
<?php
2
3
namespace Maketok\DataMigration\Input\Shaper\Processor;
4
5
use Maketok\DataMigration\ArrayMap;
6
use Maketok\DataMigration\Expression\LanguageAdapter;
7
use Maketok\DataMigration\Unit\SimpleBag;
8
use Maketok\DataMigration\Unit\Type\Unit;
9
use Maketok\DataMigration\Unit\UnitBagInterface;
10
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
11
12
class DuplicatesTest extends \PHPUnit_Framework_TestCase
13
{
14
    /**
15
     * @param UnitBagInterface $bag
16
     * @return Duplicates
17
     */
18
    public function getShaper(UnitBagInterface $bag)
19
    {
20
        return new Duplicates(
21
            $bag,
22
            new ArrayMap(),
23
            new LanguageAdapter(new ExpressionLanguage())
24
        );
25
    }
26
27
    public function testOneLevelParse()
28
    {
29
        $unit = new Unit('test');
30
        $bag = new SimpleBag();
31
        $bag->add($unit);
32
33
        $rows = [
34
            ['code' => 'test', 'id' => 1, 'name' => 'bar'],
35
            ['code' => 'test2', 'id' => 11, 'name' => 'baz'],
36
        ];
37
38
        $shaper = $this->getShaper($bag);
39
40
        foreach ($rows as $row) {
41
            $this->assertSame([$row], $shaper->parse($row));
42
        }
43
    }
44
45
    public function testTwoLevelParse()
46
    {
47
        $unit1 = new Unit('customer');
48
        $unit1->setIsEntityCondition(function ($map) {
49
            return !empty($map['email']);
50
        });
51
        $unit2 = new Unit('address');
52
        $unit2->setParent($unit1);
53
54
        $unit3 = new Unit('address_data');
55
        $unit3->addSibling($unit2);
56
57
        $bag = new SimpleBag();
58
        $bag->addSet([$unit1, $unit2, $unit3]);
59
60
        $entities = [
61
            [
62
                'email' => '[email protected]',
63
                'name' => 'bob',
64
                'address' => [
65
                    [
66
                        'street' => 'charity str.',
67
                    ],
68
                ]
69
            ],
70
            [
71
                'email' => '[email protected]',
72
                'name' => 'paul',
73
                'address' => [
74
                    [
75
                        'street' => 'buckingham ave.',
76
                    ],
77
                    [
78
                        'street' => 'mirabelle str.',
79
                    ],
80
                ]
81
            ],
82
        ];
83
        $expected = [
84
            [
85
                ['email' => '[email protected]', 'name' => 'bob', 'street' => 'charity str.'],
86
            ],
87
            [
88
                ['email' => '[email protected]', 'name' => 'paul', 'street' => 'buckingham ave.'],
89
                ['email' => '[email protected]', 'name' => 'paul', 'street' => 'mirabelle str.'],
90
            ]
91
        ];
92
93
        $shaper = $this->getShaper($bag);
94
95
        for ($i = 0; $i<count($entities); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
96
            $this->assertSame($expected[$i], $shaper->parse($entities[$i]));
97
        }
98
    }
99
100
    public function testTwoLevelParse2()
101
    {
102
        $unit1 = new Unit('customer');
103
        $unit1->setIsEntityCondition(function ($map) {
104
            return !empty($map['email']);
105
        });
106
        $unit2 = new Unit('address');
107
        $unit2->setParent($unit1);
108
109
        $bag = new SimpleBag();
110
        $bag->addSet([$unit1, $unit2]);
111
112
        $entities = [
113
            [
114
                'email' => '[email protected]',
115
                'name' => 'bob',
116
                'address' => [
117
                    [
118
                        'addr_name' => 'billy',
119
                        'street' => 'charity str.',
120
                    ],
121
                ]
122
            ],
123
            [
124
                'email' => '[email protected]',
125
                'name' => 'paul',
126
                'address' => [
127
                    [
128
                        'addr_name' => 'paul',
129
                        'street' => 'buckingham ave.',
130
                    ],
131
                    [
132
                        'addr_name' => 'megan',
133
                        'street' => 'mirabelle str.',
134
                    ],
135
                    [
136
                        'addr_name' => 'tiffany',
137
                        'street' => 'mirabelle str.',
138
                    ],
139
                ]
140
            ],
141
        ];
142
        $expected = [
143
            [
144
                ['email' => '[email protected]', 'name' => 'bob', 'addr_name' => 'billy', 'street' => 'charity str.'],
145
            ],
146
            [
147
                ['email' => '[email protected]', 'name' => 'paul', 'addr_name' => 'paul', 'street' => 'buckingham ave.'],
148
                ['email' => '[email protected]', 'name' => 'paul', 'addr_name' => 'megan', 'street' => 'mirabelle str.'],
149
                ['email' => '[email protected]', 'name' => 'paul', 'addr_name' => 'tiffany', 'street' => 'mirabelle str.'],
150
            ]
151
        ];
152
153
        $shaper = $this->getShaper($bag);
154
155
        for ($i = 0; $i<count($entities); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
156
            $this->assertSame($expected[$i], $shaper->parse($entities[$i]));
157
        }
158
    }
159
160
    public function testThreeLevelParse()
161
    {
162
        $unit1 = new Unit('customer');
163
        $unit1->setIsEntityCondition(function ($map) {
164
            return !empty($map['email']);
165
        });
166
        $unit2 = new Unit('address');
167
        $unit2->setParent($unit1);
168
        $unit2->setIsEntityCondition(function ($map, $oldmap) {
169
            return $map['street'] != $oldmap['street'];
170
        });
171
172
        $unit3 = new Unit('address_data');
173
        $unit3->setParent($unit2);
174
175
        $bag = new SimpleBag();
176
        $bag->addSet([$unit1, $unit2, $unit3]);
177
178
        $entities = [
179
            [
180
                'email' => '[email protected]',
181
                'name' => 'bob',
182
                'address' => [
183
                    [
184
                        'street' => 'charity str.',
185
                        'address_data' => [
186
                            [
187
                                'phone' => '123',
188
                            ],
189
                            [
190
                                'phone' => '432',
191
                            ]
192
                        ],
193
                    ],
194
                ]
195
            ],
196
            [
197
                'email' => '[email protected]',
198
                'name' => 'paul',
199
                'address' => [
200
                    [
201
                        'street' => 'buckingham ave.',
202
                        'address_data' => [
203
                            [
204
                                'phone' => '222',
205
                            ]
206
                        ],
207
                    ],
208
                    [
209
                        'street' => 'mirabelle str.',
210
                        'address_data' => [
211
                            [
212
                                'phone' => '323',
213
                            ]
214
                        ],
215
                    ],
216
                ]
217
            ],
218
        ];
219
        $expected = [
220
            [
221
                ['email' => '[email protected]', 'name' => 'bob', 'street' => 'charity str.', 'phone' => '123'],
222
                ['email' => '[email protected]', 'name' => 'bob', 'street' => 'charity str.', 'phone' => '432'],
223
            ],
224
            [
225
                ['email' => '[email protected]', 'name' => 'paul', 'street' => 'buckingham ave.', 'phone' => '222'],
226
                ['email' => '[email protected]', 'name' => 'paul', 'street' => 'mirabelle str.', 'phone' => '323'],
227
            ]
228
        ];
229
230
        $shaper = $this->getShaper($bag);
231
232
        for ($i = 0; $i<count($entities); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
233
            $this->assertSame($expected[$i], $shaper->parse($entities[$i]));
234
        }
235
    }
236
}
237