ProcessorTest::testThreeLevelFeed()   B
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 95

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 95
c 0
b 0
f 0
rs 8.109
cc 2
nc 1
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;
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 ProcessorTest extends \PHPUnit_Framework_TestCase
13
{
14
    /**
15
     * @param UnitBagInterface $bag
16
     * @return Processor
17
     */
18
    public function getShaper(UnitBagInterface $bag)
19
    {
20
        return $this->getMockBuilder('Maketok\DataMigration\Input\Shaper\Processor')->setConstructorArgs([
21
            $bag,
22
            new ArrayMap(),
23
            new LanguageAdapter(new ExpressionLanguage())
24
        ])->getMockForAbstractClass();
25
    }
26
27
    public function testSimpleBagFeed()
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->feed($row));
42
        }
43
        $this->assertFalse($shaper->feed([]));
44
    }
45
46
    public function testTwoLevelFeed()
47
    {
48
        $unit1 = new Unit('customer');
49
        $unit1->setIsEntityCondition(function ($map, $odlmap) {
50
            return ($odlmap['email'] != $map['email']) && !empty($map['email']);
51
        });
52
        $unit2 = new Unit('address');
53
        $unit2->setParent($unit1);
54
55
        $unit3 = new Unit('address_data');
56
        $unit3->addSibling($unit2);
57
58
        $bag = new SimpleBag();
59
        $bag->addSet([$unit1, $unit2, $unit3]);
60
61
        $shaper = $this->getShaper($bag);
62
63
        $rows = [
64
            ['email' => '[email protected]', 'name' => 'bob', 'street' => 'charity str.'],
65
            ['email' => '[email protected]', 'name' => 'paul', 'street' => 'buckingham ave.'],
66
            ['email' => null, 'name' => null, 'street' => 'mirabelle str.'],
67
        ];
68
        $this->assertFalse($shaper->feed($rows[0]));
69
        $this->assertSame([
70
            'email' => '[email protected]',
71
            'name' => 'bob',
72
            'street' => 'charity str.',
73
            'address' => [
74
                [
75
                    'email' => '[email protected]',
76
                    'name' => 'bob',
77
                    'street' => 'charity str.',
78
                ],
79
            ]
80
        ], $shaper->feed($rows[1]));
81
        $this->assertFalse($shaper->feed($rows[2]));
82
        $this->assertSame([
83
            'email' => '[email protected]',
84
            'name' => 'paul',
85
            'street' => 'buckingham ave.',
86
            'address' => [
87
                [
88
                    'email' => '[email protected]',
89
                    'name' => 'paul',
90
                    'street' => 'buckingham ave.',
91
                ],
92
                [
93
                    'email' => null,
94
                    'name' => null,
95
                    'street' => 'mirabelle str.',
96
                ],
97
            ]
98
        ], $shaper->feed([]));
99
        $this->assertFalse($shaper->feed([]));
100
    }
101
102
    public function testThreeLevelFeed()
103
    {
104
        $unit1 = new Unit('customer');
105
        $unit1->setIsEntityCondition(function ($map, $odlmap) {
106
            return ($odlmap['email'] != $map['email']) && !empty($map['email']);
107
        });
108
        $unit2 = new Unit('address');
109
        $unit2->setParent($unit1);
110
        $unit2->setIsEntityCondition(function ($map, $oldmap) {
111
            return $map['street'] != $oldmap['street'];
112
        });
113
114
        $unit3 = new Unit('address_data');
115
        $unit3->setParent($unit2);
116
117
        $bag = new SimpleBag();
118
        $bag->addSet([$unit1, $unit2, $unit3]);
119
120
        $shaper = $this->getShaper($bag);
121
122
        $rows = [
123
            ['email' => '[email protected]', 'name' => 'bob', 'street' => 'charity str.', 'phone' => '123'],
124
            ['email' => null, 'name' => null, 'street' => 'charity str.', 'phone' => '432'],
125
            ['email' => '[email protected]', 'name' => 'paul', 'street' => 'buckingham ave.', 'phone' => '222'],
126
            ['email' => null, 'name' => null, 'street' => 'mirabelle str.', 'phone' => '323'],
127
        ];
128
        $this->assertFalse($shaper->feed($rows[0]));
129
        $this->assertFalse($shaper->feed($rows[1]));
130
        $this->assertSame([
131
            'email' => '[email protected]',
132
            'name' => 'bob',
133
            'street' => 'charity str.',
134
            'phone' => '123',
135
            'address' => [
136
                [
137
                    'email' => '[email protected]',
138
                    'name' => 'bob',
139
                    'street' => 'charity str.',
140
                    'phone' => '123',
141
                    'address_data' => [
142
                        [
143
                            'email' => '[email protected]',
144
                            'name' => 'bob',
145
                            'street' => 'charity str.',
146
                            'phone' => '123',
147
                        ],
148
                        [
149
                            'email' => null,
150
                            'name' => null,
151
                            'street' => 'charity str.',
152
                            'phone' => '432',
153
                        ]
154
                    ],
155
                ],
156
            ]
157
        ], $shaper->feed($rows[2]));
158
        $this->assertFalse($shaper->feed($rows[3]));
159
        $this->assertSame([
160
            'email' => '[email protected]',
161
            'name' => 'paul',
162
            'street' => 'buckingham ave.',
163
            'phone' => '222',
164
            'address' => [
165
                [
166
                    'email' => '[email protected]',
167
                    'name' => 'paul',
168
                    'street' => 'buckingham ave.',
169
                    'phone' => '222',
170
                    'address_data' => [
171
                        [
172
                            'email' => '[email protected]',
173
                            'name' => 'paul',
174
                            'street' => 'buckingham ave.',
175
                            'phone' => '222',
176
                        ]
177
                    ],
178
                ],
179
                [
180
                    'email' => null,
181
                    'name' => null,
182
                    'street' => 'mirabelle str.',
183
                    'phone' => '323',
184
                    'address_data' => [
185
                        [
186
                            'email' => null,
187
                            'name' => null,
188
                            'street' => 'mirabelle str.',
189
                            'phone' => '323',
190
                        ]
191
                    ],
192
                ],
193
            ]
194
        ], $shaper->feed([]));
195
        $this->assertFalse($shaper->feed([]));
196
    }
197
}
198