CreateTmpFilesTest::testProcess2()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 107

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 107
c 0
b 0
f 0
rs 8
cc 1
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\Action\Type;
4
5
use Maketok\DataMigration\ArrayMap;
6
use Maketok\DataMigration\Expression\LanguageAdapter;
7
use Maketok\DataMigration\Hashmap\ArrayHashmap;
8
use Maketok\DataMigration\Input\InputResourceInterface;
9
use Maketok\DataMigration\MapInterface;
10
use Maketok\DataMigration\Storage\Db\ResourceHelperInterface;
11
use Maketok\DataMigration\Storage\Filesystem\ResourceInterface;
12
use Maketok\DataMigration\Unit\Type\Unit;
13
use Maketok\DataMigration\Workflow\Result;
14
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
15
16
class CreateTmpFilesTest extends \PHPUnit_Framework_TestCase
17
{
18
    use ServiceGetterTrait;
19
20
    public function testGetCode()
21
    {
22
        $action = new CreateTmpFiles(
23
            $this->getUnitBag(),
24
            $this->getConfig(),
25
            new LanguageAdapter(),
26
            $this->getInputResource(),
27
            new ArrayMap(),
28
            $this->getResourceHelper()
29
        );
30
        $this->assertEquals('create_tmp_files', $action->getCode());
31
    }
32
33
    /**
34
     * @return ResourceHelperInterface
35
     */
36
    protected function getResourceHelper()
37
    {
38
        $helper = $this->getMockBuilder('\Maketok\DataMigration\Storage\Db\ResourceHelperInterface')
39
            ->getMock();
40
        return $helper;
41
    }
42
43
    /**
44
     * @param string $code
45
     * @return Unit
46
     */
47
    public function getUnit($code)
48
    {
49
        $unit = new Unit($code);
50
        $dummy = function () {
51
            return true;
52
        };
53
        // all add
54
        $unit->addWriteCondition($dummy);
55
        // all valid
56
        $unit->addValidationRule($dummy);
57
        $unit->addHashmap(new ArrayHashmap('test'));
58
        return $unit;
59
    }
60
61
    /**
62
     * @param array $toReturn
63
     * @return InputResourceInterface
64
     */
65
    protected function getInputResource(array $toReturn = [])
66
    {
67
        $input = $this->getMockBuilder('\Maketok\DataMigration\Input\InputResourceInterface')
68
            ->getMock();
69
        $method = $input->expects($this->any())
70
            ->method('get');
71
        call_user_func_array([$method, 'willReturnOnConsecutiveCalls'], $toReturn);
72
        return $input;
73
    }
74
75
    /**
76
     * @param array $with
77
     * @return ResourceInterface
78
     */
79
    protected function getFS($with = [])
80
    {
81
        $filesystem = $this->getMockBuilder('\Maketok\DataMigration\Storage\Filesystem\ResourceInterface')
82
            ->getMock();
83
        $method = $filesystem->expects($this->exactly(count($with)))->method('writeRow');
84
        call_user_func_array([$method, 'withConsecutive'], $with);
85
        return $filesystem;
86
    }
87
88
    /**
89
     * input type
90
     * - customer,address1,address2
91
     * - customer,address1,address2
92
     * ...
93
     */
94
    public function testProcess()
95
    {
96
        $inputs = [
97
            ['email' => '[email protected]', 'name' => 'Olaf Stone', 'age' => 30, 'addr1_city' => 'Chicago',
98
                'addr1_street' => '4100 Marine dr. App. 54', 'addr2_city' => 'New York',
99
                'addr2_street' => '3300 St. George, Suite 300'],
100
            ['email' => '[email protected]', 'name' => 'Peter Ostridge', 'age' => 33, 'addr1_city' => 'Chicago',
101
                'addr1_street' => '5011 Sunnyside ave', 'addr2_city' => 'Chicago',
102
                'addr2_street' => '111 W Jackson'],
103
            false
104
        ];
105
        $unit1 = $this->getUnit('customer');
106
        $unit1->setMapping([
107
            'id' => 'map.id',
108
            'fname' => function ($row) {
109
                list($fname) = explode(" ", $row['name']);
110
                return $fname;
111
            },
112
            'lname' => function ($row) {
113
                list(, $lname) = explode(" ", $row['name']);
114
                return $lname;
115
            },
116
            'email' => 'map.email',
117
            'age' => 'map.age',
118
        ]);
119
        $unit1->addContribution(function (MapInterface $map) {
120
            $map->incr('id', 1);
121
        });
122
        $unit1->setFilesystem($this->getFS(
123
            [
124
                [[1, 'Olaf', 'Stone', '[email protected]', 30]],
125
                [[2, 'Peter', 'Ostridge', '[email protected]', 33]],
126
            ]
127
        ));
128
        $unit2 = $this->getUnit('address1');
129
        $unit2->setMapping([
130
            'id' => 'map.addr_id',
131
            'street' => 'map.addr1_street',
132
            'city' => 'map.addr1_city',
133
            'parent_id' => 'map.id',
134
        ]);
135
        $unit2->addContribution(function (MapInterface $map) {
136
            $map->incr('addr_id', 1);
137
        });
138
        $unit2->setFilesystem($this->getFS(
139
            [
140
                [[1, '4100 Marine dr. App. 54', 'Chicago', 1]],
141
                [[3, '5011 Sunnyside ave', 'Chicago', 2]],
142
            ]
143
        ));
144
        $unit3 = $this->getUnit('address2');
145
        $unit3->setMapping([
146
            'id' => 'map.addr_id',
147
            'street' => 'map.addr2_street',
148
            'city' => 'map.addr2_city',
149
            'parent_id' => 'map.id',
150
        ]);
151
        $unit3->addContribution(function (MapInterface $map) {
152
            $map->incr('addr_id', 1);
153
        });
154
        $unit3->setFilesystem($this->getFS(
155
            [
156
                [[2, '3300 St. George, Suite 300', 'New York', 1]],
157
                [[4, '111 W Jackson', 'Chicago', 2]],
158
            ]
159
        ));
160
161
        $action = new CreateTmpFiles(
162
            $this->getUnitBag([$unit1, $unit2, $unit3]),
163
            $this->getConfig(),
164
            new LanguageAdapter(new ExpressionLanguage()),
165
            $this->getInputResource($inputs),
166
            new ArrayMap(),
167
            $this->getResourceHelper()
168
        );
169
        $action->process($this->getResultMock());
170
171
        $this->assertEquals('/tmp/customer.csv',
172
            $unit1->getTmpFileName());
173
        $this->assertEquals('/tmp/address1.csv',
174
            $unit2->getTmpFileName());
175
        $this->assertEquals('/tmp/address2.csv',
176
            $unit3->getTmpFileName());
177
    }
178
179
    /**
180
     * input type
181
     * - customer1,address1
182
     * - customer1,address2
183
     * - customer2,address1
184
     * ...
185
     */
186
    public function testProcess2()
187
    {
188
        $inputs = [
189
            [
190
                'email' => '[email protected]',
191
                'name' => 'Olaf Stone',
192
                'age' => 30,
193
                'addr_city' => 'Chicago',
194
                'addr_street' => '4100 Marine dr. App. 54',
195
                'address' => [
196
                    [
197
                        'email' => '[email protected]',
198
                        'name' => 'Olaf Stone',
199
                        'age' => 30,
200
                        'addr_city' => 'Chicago',
201
                        'addr_street' => '4100 Marine dr. App. 54',
202
                    ],
203
                    [
204
                        'email' => '[email protected]',
205
                        'name' => 'Olaf Stone',
206
                        'age' => 30,
207
                        'addr_city' => 'New York',
208
                        'addr_street' => '3300 St. George, Suite 300',
209
                    ],
210
                ]
211
            ],
212
            [
213
                'email' => '[email protected]',
214
                'name' => 'Peter Ostridge',
215
                'age' => 33,
216
                'addr_city' => 'Chicago',
217
                'addr_street' => '111 W Jackson',
218
                'address' => [
219
                    [
220
                        'email' => '[email protected]',
221
                        'name' => 'Peter Ostridge',
222
                        'age' => 33,
223
                        'addr_city' => 'Chicago',
224
                        'addr_street' => '111 W Jackson',
225
                    ]
226
                ]
227
            ],
228
            false
229
        ];
230
        $unit1 = $this->getUnit('customer');
231
        $unit1->setMapping([
232
            'id' => 'map.id',
233
            'fname' => function ($map) {
234
                list($fname) = explode(" ", $map['name']);
235
                return $fname;
236
            },
237
            'lname' => function ($map) {
238
                list(, $lname) = explode(" ", $map['name']);
239
                return $lname;
240
            },
241
            'email' => 'map.email',
242
            'age' => 'map.age',
243
        ]);
244
        $unit1->addContribution(function (MapInterface $map) {
245
            $map->frozenIncr('id', 1);
246
        });
247
        $unit1->setFilesystem($this->getFS(
248
            [
249
                [[1, 'Olaf', 'Stone', '[email protected]', 30]],
250
                [[2, 'Peter', 'Ostridge', '[email protected]', 33]],
251
            ]
252
        ));
253
        $unit1->setIsEntityCondition(function (
254
            MapInterface $map,
255
            MapInterface $oldmap
256
        ) {
257
            return $oldmap->offsetGet('email') != $map->offsetGet('email');
258
        });
259
        $unit2 = $this->getUnit('address');
260
        $unit2->setMapping([
261
            'id' => 'map.addr_id',
262
            'street' => 'map.addr_street',
263
            'city' => 'map.addr_city',
264
            'parent_id' => 'map.id',
265
        ]);
266
        $unit2->addContribution(function (MapInterface $map) {
267
            $map->incr('addr_id', 1);
268
        });
269
        $unit2->setFilesystem($this->getFS(
270
            [
271
                [[1, '4100 Marine dr. App. 54', 'Chicago', 1]],
272
                [[2, '3300 St. George, Suite 300', 'New York', 1]],
273
                [[3, '111 W Jackson', 'Chicago', 2]],
274
            ]
275
        ));
276
        $unit2->setParent($unit1);
277
278
        $action = new CreateTmpFiles(
279
            $this->getUnitBag([$unit1, $unit2]),
280
            $this->getConfig(),
281
            new LanguageAdapter(new ExpressionLanguage()),
282
            $this->getInputResource($inputs),
283
            new ArrayMap(),
284
            $this->getResourceHelper()
285
        );
286
        $action->process($this->getResultMock());
287
288
        $this->assertEquals('/tmp/customer.csv',
289
            $unit1->getTmpFileName());
290
        $this->assertEquals('/tmp/address.csv',
291
            $unit2->getTmpFileName());
292
    }
293
294
    public function testFailedWrite()
295
    {
296
        $unit = $this->getUnit('test');
297
        $filesystem = $this->getMockBuilder('\Maketok\DataMigration\Storage\Filesystem\ResourceInterface')
298
            ->getMock();
299
        $filesystem->expects($this->any())->method('writeRow')->willReturn(false);
300
        $input = ['name' => 'Oleg', 'address' => 'Galaxy'];
301
        /** @var ResourceInterface $filesystem */
302
        $unit->setFilesystem($filesystem);
303
        $unit->setMapping([
304
            'name' => 'map.name',
305
        ]);
306
        $action = new CreateTmpFiles(
307
            $this->getUnitBag([$unit]),
308
            $this->getConfig(),
309
            new LanguageAdapter(new ExpressionLanguage()),
310
            $this->getInputResource([$input, false]),
311
            new ArrayMap(),
312
            $this->getResourceHelper()
313
        );
314
        $result = new Result();
315
        $action->process($result);
316
317
        $this->assertCount(1, $result->getAllErrors());
318
        $this->assertEquals(0, $result->getTotalRowsProcessed());
319
    }
320
321
    public function testInvalidRow()
322
    {
323
        $unit = $this->getUnit('test');
324
        $input = ['name' => 'Oleg', 'address' => 'Galaxy'];
325
        $unit->addValidationRule(function (ArrayMap $map) {
326
            return $map->name != 'Oleg';
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<Maketok\DataMigration\ArrayMap>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
327
        });
328
        $action = new CreateTmpFiles(
329
            $this->getUnitBag([$unit]),
330
            $this->getConfig(),
331
            new LanguageAdapter(new ExpressionLanguage()),
332
            $this->getInputResource([$input, false]),
333
            new ArrayMap(),
334
            $this->getResourceHelper()
335
        );
336
        $result = new Result();
337
        $action->process($result);
338
339
        $this->assertCount(1, $result->getAllErrors());
340
        $this->assertEquals(0, $result->getTotalRowsProcessed());
341
    }
342
}
343