ArrayUtilsTraitTest::testAssembleResolve()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Maketok\DataMigration;
4
5
class ArrayUtilsTraitTest extends \PHPUnit_Framework_TestCase
6
{
7
    /**
8
     * @var ArrayUtilsTrait
9
     */
10
    protected $trait;
11
12
    /**
13
     * set up trait
14
     */
15
    public function setUp()
16
    {
17
        $this->trait = $this->getMockBuilder('Maketok\DataMigration\ArrayUtilsTrait')->getMockForTrait();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getMockBuilder('M...it')->getMockForTrait() of type object<PHPUnit_Framework_MockObject_MockObject> is incompatible with the declared type object<Maketok\DataMigration\ArrayUtilsTrait> of property $trait.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
18
    }
19
    /**
20
     * @param array $data
21
     * @param bool $expected
22
     * @dataProvider isEmptyDataProvider
23
     */
24
    public function testIsEmptyData(array $data, $expected)
25
    {
26
        $this->assertEquals($expected, $this->trait->isEmptyData($data));
27
    }
28
29
    /**
30
     * @return array
31
     */
32
    public function isEmptyDataProvider()
33
    {
34
        return [
35
            [[], true],
36
            [
37
                [
38
                    [],
39
                ],
40
                true,
41
            ],
42
            [
43
                [
44
                    ['some' => null],
45
                ],
46
                true,
47
            ],
48
            [
49
                [
50
                    ['some'],
51
                ],
52
                false,
53
            ],
54
        ];
55
    }
56
57
    /**
58
     * @dataProvider rowProvider
59
     * @param array $row
60
     * @param $expected
61
     */
62
    public function normalize(array $row, $expected)
63
    {
64
        $this->assertSame($expected, $this->trait->normalize($row));
65
    }
66
67
    /**
68
     * @expectedException \Maketok\DataMigration\Action\Exception\NormalizationException
69
     * @expectedExceptionMessage Can not extract values: uneven data for row
70
     */
71
    public function normalizeError()
72
    {
73
        $row = ['str', [1,2], 'boo'];
74
        $this->trait->normalize($row);
75
    }
76
77
    /**
78
     * @return array
79
     */
80
    public function rowProvider()
81
    {
82
        return array(
83
            array(
84
                ['str', 1, 'boo'], [['str', 1, 'boo']]
85
            ),
86
            array(
87
                [['str', 'baz'], [1,2], ['boo', 'bar']], [['str', 1, 'boo'], ['baz', 2, 'bar']]
88
            ),
89
            array(
90
                [], []
91
            ),
92
        );
93
    }
94
95
    /**
96
     * @param $data
97
     * @param $expectedRow
98
     * @dataProvider tmpUnitsProvider
99
     */
100
    public function testAssemble($data, $expectedRow)
101
    {
102
        $this->assertEquals($expectedRow, $this->trait->assemble($data));
103
    }
104
105
    /**
106
     * @expectedException \Maketok\DataMigration\Action\Exception\ConflictException
107
     */
108
    public function testAssembleConflict()
109
    {
110
        $data = [
111
            'unit1' => [
112
                'id' => 1,
113
                'name' => 'u1',
114
            ],
115
            'unit2' => [
116
                'id' => 1,
117
                'name' => 'u2',
118
            ],
119
        ];
120
        $this->trait->assemble($data);
121
    }
122
123
    /**
124
     * @expectedException \Maketok\DataMigration\Action\Exception\ConflictException
125
     */
126
    public function testAssembleConflict2()
127
    {
128
        $data = [
129
            'unit2' => [
130
                'id' => 1,
131
                'name' => 'u2',
132
            ],
133
            'unit3' => [
134
                'id' => 2,
135
            ],
136
        ];
137
        $this->trait->assemble($data);
138
    }
139
140
    /**
141
     * @expectedException \Maketok\DataMigration\Action\Exception\ConflictException
142
     */
143
    public function testAssembleConflict3()
144
    {
145
        $data = [
146
            'unit1' => [
147
                'name' => 'u1',
148
                'id' => 3,
149
            ],
150
            'unit2' => [
151
                'id' => 1,
152
                'name' => 'u2',
153
            ],
154
        ];
155
        $this->trait->assemble($data);
156
    }
157
158
    /**
159
     * @expectedException \Maketok\DataMigration\Action\Exception\ConflictException
160
     * @dataProvider tmpUnitConflictProvider
161
     */
162
    public function testAssembleConflict4($units)
163
    {
164
        $this->trait->assemble($units);
165
    }
166
167
    /**
168
     * @return array
169
     */
170
    public function tmpUnitsProvider()
171
    {
172
        return [
173
            // simple merge
174
            [
175
                [
176
                    'unit1' => [
177
                        'id' => 1,
178
                        'name' => 'tmp1',
179
                    ],
180
                    'unit2' => [
181
                        'code' => 't1',
182
                    ],
183
                ],
184
                [
185
                    'id' => 1,
186
                    'name' => 'tmp1',
187
                    'code' => 't1',
188
                ],
189
            ],
190
            // merge with equal keys
191
            [
192
                [
193
                    'unit1' => [
194
                        'id' => 1,
195
                        'name' => 'tmp1',
196
                    ],
197
                    'unit2' => [
198
                        'id' => 1,
199
                        'code' => 't1',
200
                    ],
201
                ],
202
                [
203
                    'id' => 1,
204
                    'name' => 'tmp1',
205
                    'code' => 't1',
206
                ],
207
            ],
208
            // merge one unit
209
            [
210
                [
211
                    'unit1' => [
212
                        'id' => 1,
213
                        'name' => 'tmp1',
214
                    ],
215
                ],
216
                [
217
                    'id' => 1,
218
                    'name' => 'tmp1',
219
                ],
220
            ],
221
        ];
222
    }
223
224
    /**
225
     * @param array $data
226
     * @param array $expected
227
     * @dataProvider tmpUnitConflictProvider
228
     */
229
    public function testAssembleResolve($data, $expected)
230
    {
231
        $this->assertEquals($expected, $this->trait->assembleResolve($data));
232
    }
233
234
    /**
235
     * @param array $data
236
     * @param array $expected
237
     * @dataProvider tmpUnitsProvider
238
     */
239
    public function testAssembleResolve2($data, $expected)
240
    {
241
        $this->assertEquals($expected, $this->trait->assembleResolve($data));
242
    }
243
244
    /**
245
     * @return array
246
     */
247
    public function tmpUnitConflictProvider()
248
    {
249
        $res = [
250
            [
251
                [
252
                    'unit1' => [
253
                        'id' => 1,
254
                        'name' => 'u1',
255
                    ],
256
                    'unit2' => [
257
                        'id' => 1,
258
                        'name' => 'u2',
259
                    ],
260
                ],
261
                [
262
                    'id' => 1,
263
                    'unit1_name' => 'u1',
264
                    'unit2_name' => 'u2',
265
                ],
266
            ],
267
            [
268
                [
269
                    'unit2' => [
270
                        'id' => 1,
271
                        'name' => 'u2',
272
                    ],
273
                    'unit3' => [
274
                        'id' => 2,
275
                    ],
276
                ],
277
                [
278
                    'unit2_id' => 1,
279
                    'unit3_id' => 2,
280
                    'name' => 'u2',
281
                ],
282
            ],
283
            [
284
                [
285
                    'u1' => [
286
                        'key1' => 'Bob',
287
                        'key2' => 'Rob',
288
                        'id' => 123123,
289
                        'code' => 'muse',
290
                    ],
291
                    'u2' => [
292
                        'key3' => 'John',
293
                        'key4' => 'Doe',
294
                    ],
295
                    'u3' => [
296
                        'key3' => 'Bird',
297
                        'key4' => 'Doe',
298
                    ],
299
                ],
300
                [
301
                    'key1' => 'Bob',
302
                    'key2' => 'Rob',
303
                    'id' => 123123,
304
                    'code' => 'muse',
305
                    'u2_key3' => 'John',
306
                    'u3_key3' => 'Bird',
307
                    'key4' => 'Doe',
308
                ],
309
            ],
310
        ];
311
        return $res;
312
    }
313
}
314