NullsTest::testTwoLevelParse2()   B
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 62

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 62
c 0
b 0
f 0
rs 8.829
cc 2
nc 2
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 NullsTest extends \PHPUnit_Framework_TestCase
13
{
14
    /**
15
     * @param UnitBagInterface $bag
16
     * @return Nulls
17
     */
18
    public function getShaper(UnitBagInterface $bag)
19
    {
20
        return new Nulls(
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' => null, 'name' => null, '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
        $unit3 = new Unit('address_data');
110
        $unit3->addSibling($unit2);
111
112
        $bag = new SimpleBag();
113
        $bag->addSet([$unit1, $unit2, $unit3]);
114
115
        $entities = [
116
            [
117
                'email' => '[email protected]',
118
                'name' => 'bob',
119
                'address' => [
120
                    [
121
                        'addr_name' => 'billy',
122
                        'street' => 'charity str.',
123
                    ],
124
                ]
125
            ],
126
            [
127
                'email' => '[email protected]',
128
                'name' => 'paul',
129
                'address' => [
130
                    [
131
                        'addr_name' => 'billy',
132
                        'street' => 'buckingham ave.',
133
                    ],
134
                    [
135
                        'addr_name' => 'paul',
136
                        'street' => 'mirabelle str.',
137
                    ],
138
                    [
139
                        'addr_name' => 'megan',
140
                        'street' => 'mirabelle str.',
141
                    ],
142
                ]
143
            ],
144
        ];
145
        $expected = [
146
            [
147
                ['email' => '[email protected]', 'name' => 'bob', 'addr_name' => 'billy', 'street' => 'charity str.'],
148
            ],
149
            [
150
                ['email' => '[email protected]', 'name' => 'paul', 'addr_name' => 'billy', 'street' => 'buckingham ave.'],
151
                ['email' => null, 'name' => null, 'addr_name' => 'paul', 'street' => 'mirabelle str.'],
152
                ['email' => null, 'name' => null, 'addr_name' => 'megan', 'street' => 'mirabelle str.'],
153
            ]
154
        ];
155
156
        $shaper = $this->getShaper($bag);
157
158
        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...
159
            $this->assertSame($expected[$i], $shaper->parse($entities[$i]));
160
        }
161
    }
162
163
    public function testThreeLevelParse()
164
    {
165
        $unit1 = new Unit('customer');
166
        $unit1->setIsEntityCondition(function ($map) {
167
            return !empty($map['email']);
168
        });
169
        $unit2 = new Unit('address');
170
        $unit2->setParent($unit1);
171
        $unit2->setIsEntityCondition(function ($map, $oldmap) {
172
            return $map['street'] != $oldmap['street'];
173
        });
174
175
        $unit3 = new Unit('address_data');
176
        $unit3->setParent($unit2);
177
178
        $bag = new SimpleBag();
179
        $bag->addSet([$unit1, $unit2, $unit3]);
180
181
        $entities = [
182
            [
183
                'email' => '[email protected]',
184
                'name' => 'bob',
185
                'address' => [
186
                    [
187
                        'street' => 'charity str.',
188
                        'addr_name' => 'bob',
189
                        'address_data' => [
190
                            [
191
                                'phone' => '123',
192
                                'fax' => '244',
193
                            ],
194
                            [
195
                                'phone' => '432',
196
                                'fax' => '6766',
197
                            ]
198
                        ],
199
                    ],
200
                ]
201
            ],
202
            [
203
                'email' => '[email protected]',
204
                'name' => 'paul',
205
                'address' => [
206
                    [
207
                        'street' => 'buckingham ave.',
208
                        'addr_name' => 'collin',
209
                        'address_data' => [
210
                            [
211
                                'phone' => '222',
212
                                'fax' => '333',
213
                            ]
214
                        ],
215
                    ],
216
                    [
217
                        'street' => 'mirabelle str.',
218
                        'addr_name' => 'rich',
219
                        'address_data' => [
220
                            [
221
                                'phone' => '323',
222
                                'fax' => '24234',
223
                            ]
224
                        ],
225
                    ],
226
                ]
227
            ],
228
        ];
229
        $expected = [
230
            [
231
                ['email' => '[email protected]', 'name' => 'bob', 'street' => 'charity str.', 'addr_name' => 'bob', 'phone' => '123', 'fax' => '244'],
232
                ['email' => null, 'name' => null, 'street' => null, 'addr_name' => null, 'phone' => '432', 'fax' => '6766'],
233
            ],
234
            [
235
                ['email' => '[email protected]', 'name' => 'paul', 'street' => 'buckingham ave.', 'addr_name' => 'collin', 'phone' => '222', 'fax' => '333'],
236
                ['email' => null, 'name' => null, 'street' => 'mirabelle str.', 'addr_name' => 'rich', 'phone' => '323', 'fax' => '24234'],
237
            ]
238
        ];
239
240
        $shaper = $this->getShaper($bag);
241
242
        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...
243
            $this->assertSame($expected[$i], $shaper->parse($entities[$i]));
244
        }
245
    }
246
}
247