Test Failed
Push — master ( 50dc03...2f2ddd )
by Petr
02:45
created

FillerTest::testChildParse1()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 61
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 44
c 2
b 0
f 0
dl 0
loc 61
rs 9.216
cc 3
nc 3
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 SearchTests;
4
5
6
use CommonTestClass;
7
use kalanis\kw_mapper\Interfaces\IDriverSources;
8
use kalanis\kw_mapper\Interfaces\IEntryType;
9
use kalanis\kw_mapper\MapperException;
10
use kalanis\kw_mapper\Mappers\Database\ADatabase;
11
use kalanis\kw_mapper\Records\ASimpleRecord;
12
use kalanis\kw_mapper\Search\Connector\Database\Filler;
13
use kalanis\kw_mapper\Search\Connector\Database\RecordsInJoin;
14
use kalanis\kw_mapper\Storage;
15
use kalanis\kw_mapper\Storage\Database;
16
17
18
class FillerTest extends CommonTestClass
19
{
20
    protected function setUp(): void
21
    {
22
        Database\ConfigStorage::getInstance()->addConfig(
23
            Database\Config::init()->setTarget(
24
                IDriverSources::TYPE_PDO_MYSQL,
25
                'testing',
26
                'localhost',
27
                3306,
28
                'kwdeploy',
29
                'testingpass',
30
                'kw_deploy'
31
            )
32
        );
33
    }
34
35
    /**
36
     * @throws MapperException
37
     */
38
    public function testSimpleFill(): void
39
    {
40
        $record = new XRecordChild();
41
        $record2 = new XRecordParent();
42
        $lib = new Filler($record);
43
        $records = [ // you must define all wanted records with their aliases used for join
44
            (new RecordsInJoin())->setData($record, $record->getMapper()->getAlias(), null, ''), // primary record has its alias as key and must have empty parent
45
            (new RecordsInJoin())->setData($record2, 'as_is', $record->getMapper()->getAlias(), 'prt'), // other records has aliases defined by their parents or by custom value
46
        ];
47
        $lib->initTreeSolver($records);
48
        // more than once - ignore this one
49
        $join2 = new Storage\Shared\QueryBuilder\Join();
50
        $join2->setData(
51
            'as_is', // alias from join
52
            'kw_mapper_parent_testing', // parent
53
            'kmpt_id', // colum in parent
54
            'kw_mapper_child_testing', // child which want join
55
            'kmpt_id', // column in child
56
            'not need here', // for join query itself
57
            '' // referred in query
58
        );
59
        $struct = $lib->getColumns([$this->basicJoin(), $join2]);
60
        $this->assertEquals([ // tables
61
            'kw_mapper_child_testing',
62
            'kw_mapper_child_testing',
63
            'kw_mapper_child_testing',
64
            'as_is',
65
            'as_is',
66
        ], array_column($struct, 0));
67
        $this->assertEquals([ // columns
68
            'kmct_id',
69
            'kmct_name',
70
            'kmpt_id',
71
            'kmpt_id',
72
            'kmpt_name',
73
        ], array_column($struct, 1));
74
        $this->assertEquals([ // aliases
75
            'kw_mapper_child_testing____kmct_id',
76
            'kw_mapper_child_testing____kmct_name',
77
            'kw_mapper_child_testing____kmpt_id',
78
            'as_is____kmpt_id',
79
            'as_is____kmpt_name',
80
        ], array_column($struct, 2));
81
    }
82
83
    /**
84
     * @throws MapperException
85
     */
86
    public function testSimpleParse1(): void
87
    {
88
        $record = new XRecordChild();
89
        $lib = new Filler($record);
90
91
        $wantedRecords = [ // you must define all wanted records with their aliases used for join
92
            (new RecordsInJoin())->setData($record, $record->getMapper()->getAlias(), null, ''), // primary record has its alias as key and must have empty parent
93
        ];
94
        $lib->initTreeSolver($wantedRecords);
95
        $records = $lib->fillResults($this->resultData1());
96
97
        // check records - four found
98
        /** @var XRecordChild[] $records */
99
        $this->assertEquals(4, count($records));
100
        $child = reset($records);
101
        $this->assertEquals('1', $child->id);
102
        $this->assertEquals('abc', $child->name);
103
        $child = next($records);
104
        $this->assertEquals('2', $child->id);
105
        $this->assertEquals('ghi', $child->name);
106
        $child = next($records);
107
        $this->assertEquals('3', $child->id);
108
        $this->assertEquals('ijk', $child->name);
109
        $child = next($records);
110
        $this->assertEquals('4', $child->id);
111
        $this->assertEquals('lmn', $child->name);
112
    }
113
114
    /**
115
     * @throws MapperException
116
     */
117
    public function testSimpleParse2(): void
118
    {
119
        $record = new XRecordChild();
120
        $lib = new Filler($record);
121
122
        $wantedRecords = [ // you must define all wanted records with their aliases used for join
123
            (new RecordsInJoin())->setData($record, $record->getMapper()->getAlias(), null, ''), // primary record has its alias as key and must have empty parent
124
        ];
125
        $lib->initTreeSolver($wantedRecords);
126
        $records = $lib->fillResults($this->resultData2());
127
128
        // check records - four found
129
        /** @var XRecordChild[] $records */
130
        $this->assertEquals(4, count($records));
131
        $child = reset($records);
132
        $this->assertEquals('1', $child->id);
133
        $this->assertEquals('abc', $child->name);
134
        $child = next($records);
135
        $this->assertEquals('2', $child->id);
136
        $this->assertEquals('ghi', $child->name);
137
        $child = next($records);
138
        $this->assertEquals('3', $child->id);
139
        $this->assertEquals('ijk', $child->name);
140
        $child = next($records);
141
        $this->assertEquals('4', $child->id);
142
        $this->assertEquals('lmn', $child->name);
143
    }
144
145
    /**
146
     * @throws MapperException
147
     */
148
    public function testChildParse1(): void
149
    {
150
        $record = new XRecordChild();
151
        $record2 = new XRecordParent();
152
        $lib = new Filler($record);
153
154
        $wantedRecords = [ // you must define all wanted records with their aliases used for join
155
            (new RecordsInJoin())->setData($record, $record->getMapper()->getAlias(), null, ''), // primary record has its alias as key and must have empty parent
156
            (new RecordsInJoin())->setData($record2, 'as_is', $record->getMapper()->getAlias(), 'prt'), // other records has aliases defined by their parents or by custom value
157
        ];
158
        // more than once - ignore this one
159
        $join2 = new Storage\Shared\QueryBuilder\Join();
160
        $join2->setData(
161
            'as_is', // alias from join
162
            'kw_mapper_parent_testing', // parent
163
            'kmpt_id', // colum in parent
164
            'kw_mapper_child_testing', // child which want join
165
            'kmpt_id', // column in child
166
            'not need here', // for join query itself
167
            '' // referred in query
168
        );
169
170
        $lib->initTreeSolver($wantedRecords);
171
        $records = $lib->fillResults($this->resultData3());
172
173
        // check records - four found
174
        /** @var XRecordChild[] $records */
175
        $this->assertEquals(4, count($records));
176
        $child = reset($records);
177
        $this->assertEquals('1', $child->id);
178
        $this->assertEquals('abc', $child->name);
179
        $child = next($records);
180
        $this->assertEquals('2', $child->id);
181
        $this->assertEquals('ghi', $child->name);
182
        $child = next($records);
183
        $this->assertEquals('3', $child->id);
184
        $this->assertEquals('ijk', $child->name);
185
        $child = next($records);
186
        $this->assertEquals('4', $child->id);
187
        $this->assertEquals('lmn', $child->name);
188
189
        // check parents - only 2 get
190
        /** @var XRecordParent[] $parents */
191
        $parents = [];
192
        foreach ($records as $record) {
193
            $subs = $record->offsetGet('prt');
194
            /** @var XRecordParent[] $subs */
195
            $sub = reset($subs);
196
            $key = strval($sub->offsetGet('id'));
197
            if (empty($parents[$key])) {
198
                $parents[$key] = $sub;
199
            }
200
        }
201
        $this->assertEquals(2, count($parents));
202
203
        $parent = reset($parents);
204
        $this->assertEquals('1', $parent->id);
205
        $this->assertEquals('def', $parent->name);
206
        $parent = next($parents);
207
        $this->assertEquals('2', $parent->id);
208
        $this->assertEquals('opq', $parent->name);
209
    }
210
211
    /**
212
     * @throws MapperException
213
     */
214
    public function testChildParse2(): void
215
    {
216
        $record = new XRecordChild();
217
        $record2 = new XRecordParent();
218
        $lib = new Filler($record);
219
220
        $wantedRecords = [ // you must define all wanted records with their aliases used for join
221
            (new RecordsInJoin())->setData($record, $record->getMapper()->getAlias(), null, ''), // primary record has its alias as key and must have empty parent
222
            (new RecordsInJoin())->setData($record2, 'as_is', $record->getMapper()->getAlias(), 'prt'), // other records has aliases defined by their parents or by custom value
223
        ];
224
225
        $lib->initTreeSolver($wantedRecords);
226
        $records = $lib->fillResults($this->resultData4());
227
228
        // check records
229
        /** @var XRecordChild[] $records */
230
        /** @var XRecordChild $child */
231
        $this->assertEquals(6, count($records));
232
        $child = reset($records);
233
234
        $this->assertEquals('1', $child->id);
235
        $this->assertEquals('abc', $child->name);
236
        $this->assertEquals(1, count($child->prt));
237
        $inner1 = $child->prt;
238
        $inner1 = reset($inner1);
239
        $this->assertEquals('1', $inner1->id);
240
        $this->assertEquals('def', $inner1->name);
241
242
        $child = next($records);
243
        $this->assertEquals('1', $child->id);
244
        $this->assertEquals('abc', $child->name);
245
        $this->assertEquals(1, count($child->prt));
246
        $inner2 = $child->prt;
247
        $inner2 = reset($inner2);
248
        $this->assertEquals('2', $inner2->id);
249
        $this->assertEquals('opq', $inner2->name);
250
251
        $child = next($records);
252
        $this->assertEquals('2', $child->id);
253
        $this->assertEquals('ghi', $child->name);
254
        $this->assertEquals(1, count($child->prt));
255
        $inner3 = $child->prt;
256
        $inner3 = reset($inner3);
257
        $this->assertEquals('1', $inner3->id);
258
        $this->assertEquals('def', $inner3->name);
259
260
        $child = next($records);
261
        $this->assertEquals('2', $child->id);
262
        $this->assertEquals('ghi', $child->name);
263
        $this->assertEquals(1, count($child->prt));
264
        $inner4 = $child->prt;
265
        $inner4 = reset($inner4);
266
        $this->assertEquals('2', $inner4->id);
267
        $this->assertEquals('opq', $inner4->name);
268
269
        $child = next($records);
270
        $this->assertEquals('3', $child->id);
271
        $this->assertEquals('jkl', $child->name);
272
        $this->assertEquals(0, count($child->prt));
273
274
        $child = next($records);
275
        $this->assertEquals('4', $child->id);
276
        $this->assertEquals('mno', $child->name);
277
        $this->assertEquals(1, count($child->prt));
278
        $inner5 = $child->prt;
279
        $inner5 = reset($inner5);
280
        $this->assertEquals('3', $inner5->id);
281
        $this->assertEquals('uhb', $inner5->name);
282
283
        $this->assertEquals($inner1, $inner3);
284
        $this->assertEquals($inner2, $inner4);
285
        $this->assertNotEquals($inner1, $inner5);
286
        $this->assertNotEquals($inner2, $inner5);
287
    }
288
289
    /**
290
     * @throws MapperException
291
     */
292
    public function testFailedRecordNotExists(): void
293
    {
294
        $record = new XRecordChild();
295
        $record2 = new XRecordParent();
296
        $lib = new Filler($record);
297
298
        $wantedRecords = [ // you must define all wanted records with their aliases used for join
299
            (new RecordsInJoin())->setData($record, $record->getMapper()->getAlias(), null, ''), // primary record has its alias as key and must have empty parent
300
            (new RecordsInJoin())->setData($record2, 'not_known', $record->getMapper()->getAlias(), 'prt'), // other records has aliases defined by their parents or by custom value
301
        ];
302
        $lib->initTreeSolver($wantedRecords);
303
        $this->expectException(MapperException::class);
304
        $lib->fillResults($this->resultData3());
305
    }
306
307
    /**
308
     * @throws MapperException
309
     */
310
    public function testFailedRootRecordNotExists(): void
311
    {
312
        $record = new XRecordChild();
313
        $record2 = new XRecordParent();
314
        $lib = new Filler($record);
315
316
        $wantedRecords = [ // you must define all wanted records with their aliases used for join
317
            (new RecordsInJoin())->setData($record, $record->getMapper()->getAlias(), 'not_known', ''), // primary record has its alias as key and must have empty parent
318
            (new RecordsInJoin())->setData($record2, 'as_is', 'not_known', 'prt'), // other records has aliases defined by their parents or by custom value
319
        ];
320
        $lib->initTreeSolver($wantedRecords);
321
        $this->expectException(MapperException::class);
322
        $this->expectExceptionMessage('No root record found.');
323
        $lib->fillResults($this->resultData3());
324
    }
325
326
    protected function basicJoin(): Storage\Shared\QueryBuilder\Join
327
    {
328
        $join = new Storage\Shared\QueryBuilder\Join();
329
        $join->setData(
330
            'prt', // alias from join
331
            'kw_mapper_parent_testing', // parent
332
            'kmpt_id', // column in parent
333
            'kw_mapper_child_testing', // child which want join
334
            'kmpt_id', // column in child
335
            'not need here', // for join query itself
336
            'as_is' // referred in query
337
        );
338
        return $join;
339
    }
340
341
    /**
342
     * Result array as it come from database
343
     * @return string[][]|int[][]
344
     */
345
    protected function resultData1(): array
346
    {
347
        return [
348
            [
349
                'kmct_id' => 1,
350
                'kmct_name' => 'abc',
351
                'kmpt_id' => 1,
352
            ],
353
            [
354
                'kmct_id' => 2,
355
                'kmct_name' => 'ghi',
356
                'kmpt_id' => 1,
357
            ],
358
            [
359
                'kmct_id' => 3,
360
                'kmct_name' => 'ijk',
361
                'kmpt_id' => 1,
362
            ],
363
            [
364
                'kmct_id' => 4,
365
                'kmct_name' => 'lmn',
366
                'kmpt_id' => 2,
367
            ],
368
        ];
369
    }
370
371
    /**
372
     * Result array as it come from database
373
     * @return string[][]|int[][]
374
     */
375
    protected function resultData2(): array
376
    {
377
        return [
378
            [
379
                'kw_mapper_child_testing.kmct_id' => 1,
380
                'kw_mapper_child_testing.kmct_name' => 'abc',
381
                'kw_mapper_child_testing.kmpt_id' => 1,
382
            ],
383
            [
384
                'kw_mapper_child_testing.kmct_id' => 2,
385
                'kw_mapper_child_testing.kmct_name' => 'ghi',
386
                'kw_mapper_child_testing.kmpt_id' => 1,
387
            ],
388
            [
389
                'kw_mapper_child_testing.kmct_id' => 3,
390
                'kw_mapper_child_testing.kmct_name' => 'ijk',
391
                'kw_mapper_child_testing.kmpt_id' => 1,
392
            ],
393
            [
394
                'kw_mapper_child_testing.kmct_id' => 4,
395
                'kw_mapper_child_testing.kmct_name' => 'lmn',
396
                'kw_mapper_child_testing.kmpt_id' => 2,
397
            ],
398
        ];
399
    }
400
401
    /**
402
     * Result array as it come from database
403
     * @return string[][]|int[][]
404
     */
405
    protected function resultData3(): array
406
    {
407
        return [
408
            [
409
                'kw_mapper_child_testing____kmct_id' => 1,
410
                'kw_mapper_child_testing____kmct_name' => 'abc',
411
                'kw_mapper_child_testing____kmpt_id' => 1,
412
                'as_is____kmpt_id' => 1,
413
                'as_is____kmpt_name' => 'def',
414
            ],
415
            [
416
                'kw_mapper_child_testing____kmct_id' => 2,
417
                'kw_mapper_child_testing____kmct_name' => 'ghi',
418
                'kw_mapper_child_testing____kmpt_id' => 1,
419
                'as_is____kmpt_id' => 1,
420
                'as_is____kmpt_name' => 'def',
421
            ],
422
            [
423
                'kw_mapper_child_testing____kmct_id' => 3,
424
                'kw_mapper_child_testing____kmct_name' => 'ijk',
425
                'kw_mapper_child_testing____kmpt_id' => 1,
426
                'as_is____kmpt_id' => 1,
427
                'as_is____kmpt_name' => 'def',
428
            ],
429
            [
430
                'kw_mapper_child_testing____kmct_id' => 4,
431
                'kw_mapper_child_testing____kmct_name' => 'lmn',
432
                'kw_mapper_child_testing____kmpt_id' => 2,
433
                'as_is____kmpt_id' => 2,
434
                'as_is____kmpt_name' => 'opq',
435
            ],
436
        ];
437
    }
438
439
    /**
440
     * Result array as it come from database
441
     * @return string[][]|int[][]
442
     */
443
    protected function resultData4(): array
444
    {
445
        return [
446
            [ // 4 with cross references
447
                'kw_mapper_child_testing____kmct_id' => 1,
448
                'kw_mapper_child_testing____kmct_name' => 'abc',
449
                'kw_mapper_child_testing____kmpt_id' => 1,
450
                'as_is____kmpt_id' => 1,
451
                'as_is____kmpt_name' => 'def',
452
            ],
453
            [
454
                'kw_mapper_child_testing____kmct_id' => 1,
455
                'kw_mapper_child_testing____kmct_name' => 'abc',
456
                'kw_mapper_child_testing____kmpt_id' => 2,
457
                'as_is____kmpt_id' => 2,
458
                'as_is____kmpt_name' => 'opq',
459
            ],
460
            [
461
                'kw_mapper_child_testing____kmct_id' => 2,
462
                'kw_mapper_child_testing____kmct_name' => 'ghi',
463
                'kw_mapper_child_testing____kmpt_id' => 1,
464
                'as_is____kmpt_id' => 1,
465
                'as_is____kmpt_name' => 'def',
466
            ],
467
            [
468
                'kw_mapper_child_testing____kmct_id' => 2,
469
                'kw_mapper_child_testing____kmct_name' => 'ghi',
470
                'kw_mapper_child_testing____kmpt_id' => 2,
471
                'as_is____kmpt_id' => 2,
472
                'as_is____kmpt_name' => 'opq',
473
            ],
474
            [ // no child
475
                'kw_mapper_child_testing____kmct_id' => 3,
476
                'kw_mapper_child_testing____kmct_name' => 'jkl',
477
                'kw_mapper_child_testing____kmpt_id' => null,
478
                'as_is____kmpt_id' => null,
479
                'as_is____kmpt_name' => null,
480
            ],
481
            [ // one separated, no another reference
482
                'kw_mapper_child_testing____kmct_id' => 4,
483
                'kw_mapper_child_testing____kmct_name' => 'mno',
484
                'kw_mapper_child_testing____kmpt_id' => 3,
485
                'as_is____kmpt_id' => 3,
486
                'as_is____kmpt_name' => 'uhb',
487
            ],
488
        ];
489
    }
490
}
491
492
493
/**
494
 * Class XRecordParent
495
 * @package SearchTests
496
 * @property int $id
497
 * @property string $name
498
 * @property XRecordChild[] $chld
499
 */
500
class XRecordParent extends ASimpleRecord
501
{
502
    protected function addEntries(): void
503
    {
504
        $this->addEntry('id', IEntryType::TYPE_INTEGER, 512);
505
        $this->addEntry('name', IEntryType::TYPE_STRING, 512);
506
        $this->addEntry('chld', IEntryType::TYPE_ARRAY); // FK - makes the array of entries every time
507
        $this->setMapper('\SearchTests\XMapperParent');
508
    }
509
}
510
511
512
/**
513
 * Class XRecordChild
514
 * @package SearchTests
515
 * @property int $id
516
 * @property string $name
517
 * @property int $prtId
518
 * @property XRecordParent[] $prt
519
 */
520
class XRecordChild extends ASimpleRecord
521
{
522
    protected function addEntries(): void
523
    {
524
        $this->addEntry('id', IEntryType::TYPE_INTEGER, 512);
525
        $this->addEntry('name', IEntryType::TYPE_STRING, 512);
526
        $this->addEntry('prtId', IEntryType::TYPE_INTEGER, 64); // ID of remote
527
        $this->addEntry('prt', IEntryType::TYPE_ARRAY); // FK - makes the array of entries every time
528
        $this->setMapper('\SearchTests\XMapperChild');
529
    }
530
}
531
532
533
class XMapperParent extends ADatabase
534
{
535
    public function setMap(): void
536
    {
537
        $this->setSource('testing');
538
        $this->setTable('kw_mapper_parent_testing');
539
        $this->setRelation('id', 'kmpt_id');
540
        $this->setRelation('name', 'kmpt_name');
541
        $this->addPrimaryKey('id');
542
        $this->addForeignKey('chld', '\SearchTests\XRecordChild', 'chldId', 'id');
543
    }
544
}
545
546
547
class XMapperChild extends ADatabase
548
{
549
    public function setMap(): void
550
    {
551
        $this->setSource('testing');
552
        $this->setTable('kw_mapper_child_testing');
553
        $this->setRelation('id', 'kmct_id');
554
        $this->setRelation('name', 'kmct_name');
555
        $this->setRelation('prtId', 'kmpt_id');
556
        $this->addPrimaryKey('id');
557
        $this->addForeignKey('prt', '\SearchTests\XRecordParent', 'prtId', 'id');
558
    }
559
}
560