Completed
Push — 6.12 ( 1cff2f )
by Łukasz
64:51
created

DoctrineDatabaseTest   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 531
Duplicated Lines 24.86 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 132
loc 531
rs 10
c 0
b 0
f 0
wmc 25
lcom 1
cbo 6

21 Methods

Rating   Name   Duplication   Size   Complexity  
A testConstructor() 0 11 1
A testLoadUrlaliasDataNonExistent() 0 9 1
B testLoadUrlaliasData() 35 35 1
B testLoadUrlaliasDataMultipleLanguages() 35 35 1
B providerForTestLoadPathData() 0 42 1
A testLoadPathData() 12 12 1
B providerForTestLoadPathDataMultipleLanguages() 0 39 1
A testLoadPathDataMultipleLanguages() 12 12 1
A providerForTestCleanupAfterPublishHistorize() 0 17 1
A providerForTestArchiveUrlAliasesForDeletedTranslations() 10 10 1
A testCleanupAfterPublishHistorize() 0 16 1
A providerForTestCleanupAfterPublishRemovesLanguage() 0 17 1
A testCleanupAfterPublishRemovesLanguage() 0 14 1
B testReparent() 0 24 1
A testRemove() 14 14 1
A testRemoveWithId() 14 14 1
A testRemoveCustomAlias() 0 12 1
A testRemoveCustomAliasFails() 0 10 1
A testGetNextId() 0 7 1
B testArchiveUrlAliasesForDeletedTranslations() 0 24 4
A getGateway() 0 17 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * File contains: eZ\Publish\Core\Persistence\Legacy\Tests\Content\UrlAlias\Gateway\DoctrineDatabaseTest class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\Core\Persistence\Legacy\Tests\Content\UrlAlias\Gateway;
10
11
use eZ\Publish\Core\Persistence\Legacy\Tests\TestCase;
12
use eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Gateway\DoctrineDatabase;
13
use eZ\Publish\Core\Persistence\Legacy\Content\Language\MaskGenerator as LanguageMaskGenerator;
14
use eZ\Publish\Core\Persistence\Legacy\Content\Language\Handler as LanguageHandler;
15
use eZ\Publish\Core\Persistence\Legacy\Content\Language\Mapper as LanguageMapper;
16
use eZ\Publish\Core\Persistence\Legacy\Content\Language\Gateway\DoctrineDatabase as LanguageGateway;
17
18
/**
19
 * Test case for eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Gateway\DoctrineDatabase.
20
 *
21
 * @group urlalias-gateway
22
 */
23
class DoctrineDatabaseTest extends TestCase
24
{
25
    /**
26
     * Database gateway to test.
27
     *
28
     * @var \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Gateway
29
     */
30
    protected $gateway;
31
32
    /**
33
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Gateway\DoctrineDatabase::__construct
34
     */
35
    public function testConstructor()
36
    {
37
        $dbHandler = $this->getDatabaseHandler();
38
        $gateway = $this->getGateway();
39
40
        $this->assertAttributeSame(
41
            $dbHandler,
42
            'dbHandler',
43
            $gateway
44
        );
45
    }
46
47
    /**
48
     * Test for the loadUrlAliasData() method.
49
     *
50
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Gateway\DoctrineDatabase::loadUrlAliasData
51
     */
52
    public function testLoadUrlaliasDataNonExistent()
53
    {
54
        $this->insertDatabaseFixture(__DIR__ . '/_fixtures/urlaliases_simple.php');
55
        $gateway = $this->getGateway();
56
57
        $rows = $gateway->loadUrlAliasData(array(md5('tri')));
58
59
        self::assertEmpty($rows);
60
    }
61
62
    /**
63
     * Test for the loadUrlAliasData() method.
64
     *
65
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Gateway\DoctrineDatabase::loadUrlAliasData
66
     */
67 View Code Duplication
    public function testLoadUrlaliasData()
68
    {
69
        $this->insertDatabaseFixture(__DIR__ . '/_fixtures/urlaliases_simple.php');
70
        $gateway = $this->getGateway();
71
72
        $row = $gateway->loadUrlAliasData(array(md5('jedan'), md5('dva')));
73
74
        self::assertEquals(
75
            array(
76
                'ezurlalias_ml0_id' => '2',
77
                'ezurlalias_ml0_link' => '2',
78
                'ezurlalias_ml0_is_alias' => '0',
79
                'ezurlalias_ml0_alias_redirects' => '1',
80
                'ezurlalias_ml0_is_original' => '1',
81
                'ezurlalias_ml0_action' => 'eznode:314',
82
                'ezurlalias_ml0_action_type' => 'eznode',
83
                'ezurlalias_ml0_lang_mask' => '2',
84
                'ezurlalias_ml0_text' => 'jedan',
85
                'ezurlalias_ml0_parent' => '0',
86
                'ezurlalias_ml0_text_md5' => '6896260129051a949051c3847c34466f',
87
                'id' => '3',
88
                'link' => '3',
89
                'is_alias' => '0',
90
                'alias_redirects' => '1',
91
                'is_original' => '1',
92
                'action' => 'eznode:315',
93
                'action_type' => 'eznode',
94
                'lang_mask' => '3',
95
                'text' => 'dva',
96
                'parent' => '2',
97
                'text_md5' => 'c67ed9a09ab136fae610b6a087d82e21',
98
            ),
99
            $row
100
        );
101
    }
102
103
    /**
104
     * Test for the loadUrlAliasData() method.
105
     *
106
     * Test with fixture containing language mask with multiple languages.
107
     *
108
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Gateway\DoctrineDatabase::loadUrlAliasData
109
     */
110 View Code Duplication
    public function testLoadUrlaliasDataMultipleLanguages()
111
    {
112
        $this->insertDatabaseFixture(__DIR__ . '/_fixtures/urlaliases_multilang.php');
113
        $gateway = $this->getGateway();
114
115
        $row = $gateway->loadUrlAliasData(array(md5('jedan'), md5('dva')));
116
117
        self::assertEquals(
118
            array(
119
                'ezurlalias_ml0_id' => '2',
120
                'ezurlalias_ml0_link' => '2',
121
                'ezurlalias_ml0_is_alias' => '0',
122
                'ezurlalias_ml0_alias_redirects' => '1',
123
                'ezurlalias_ml0_is_original' => '1',
124
                'ezurlalias_ml0_action' => 'eznode:314',
125
                'ezurlalias_ml0_action_type' => 'eznode',
126
                'ezurlalias_ml0_lang_mask' => '3',
127
                'ezurlalias_ml0_text' => 'jedan',
128
                'ezurlalias_ml0_parent' => '0',
129
                'ezurlalias_ml0_text_md5' => '6896260129051a949051c3847c34466f',
130
                'id' => '3',
131
                'link' => '3',
132
                'is_alias' => '0',
133
                'alias_redirects' => '1',
134
                'is_original' => '1',
135
                'action' => 'eznode:315',
136
                'action_type' => 'eznode',
137
                'lang_mask' => '6',
138
                'text' => 'dva',
139
                'parent' => '2',
140
                'text_md5' => 'c67ed9a09ab136fae610b6a087d82e21',
141
            ),
142
            $row
143
        );
144
    }
145
146
    /**
147
     * @return array
148
     */
149
    public function providerForTestLoadPathData()
150
    {
151
        return array(
152
            array(
153
                2,
154
                array(
155
                    array(
156
                        array('parent' => '0', 'lang_mask' => '3', 'text' => 'jedan'),
157
                    ),
158
                ),
159
            ),
160
            array(
161
                3,
162
                array(
163
                    array(
164
                        array('parent' => '0', 'lang_mask' => '3', 'text' => 'jedan'),
165
                    ),
166
                    array(
167
                        array('parent' => '2', 'lang_mask' => '5', 'text' => 'two'),
168
                        array('parent' => '2', 'lang_mask' => '3', 'text' => 'dva'),
169
                    ),
170
                ),
171
            ),
172
            array(
173
                4,
174
                array(
175
                    array(
176
                        array('parent' => '0', 'lang_mask' => '3', 'text' => 'jedan'),
177
                    ),
178
                    array(
179
                        array('parent' => '2', 'lang_mask' => '5', 'text' => 'two'),
180
                        array('parent' => '2', 'lang_mask' => '3', 'text' => 'dva'),
181
                    ),
182
                    array(
183
                        array('parent' => '3', 'lang_mask' => '9', 'text' => 'drei'),
184
                        array('parent' => '3', 'lang_mask' => '5', 'text' => 'three'),
185
                        array('parent' => '3', 'lang_mask' => '3', 'text' => 'tri'),
186
                    ),
187
                ),
188
            ),
189
        );
190
    }
191
192
    /**
193
     * Test for the loadPathData() method.
194
     *
195
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Gateway\DoctrineDatabase::loadPathData
196
     * @dataProvider providerForTestLoadPathData
197
     */
198 View Code Duplication
    public function testLoadPathData($id, $pathData)
199
    {
200
        $this->insertDatabaseFixture(__DIR__ . '/_fixtures/urlaliases_fallback.php');
201
        $gateway = $this->getGateway();
202
203
        $loadedPathData = $gateway->loadPathData($id);
204
205
        self::assertEquals(
206
            $pathData,
207
            $loadedPathData
208
        );
209
    }
210
211
    /**
212
     * @return array
213
     */
214
    public function providerForTestLoadPathDataMultipleLanguages()
215
    {
216
        return array(
217
            array(
218
                2,
219
                array(
220
                    array(
221
                        array('parent' => '0', 'lang_mask' => '3', 'text' => 'jedan'),
222
                    ),
223
                ),
224
            ),
225
            array(
226
                3,
227
                array(
228
                    array(
229
                        array('parent' => '0', 'lang_mask' => '3', 'text' => 'jedan'),
230
                    ),
231
                    array(
232
                        array('parent' => '2', 'lang_mask' => '6', 'text' => 'dva'),
233
                    ),
234
                ),
235
            ),
236
            array(
237
                4,
238
                array(
239
                    array(
240
                        array('parent' => '0', 'lang_mask' => '3', 'text' => 'jedan'),
241
                    ),
242
                    array(
243
                        array('parent' => '2', 'lang_mask' => '6', 'text' => 'dva'),
244
                    ),
245
                    array(
246
                        array('parent' => '3', 'lang_mask' => '4', 'text' => 'three'),
247
                        array('parent' => '3', 'lang_mask' => '2', 'text' => 'tri'),
248
                    ),
249
                ),
250
            ),
251
        );
252
    }
253
254
    /**
255
     * Test for the loadPathData() method.
256
     *
257
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Gateway\DoctrineDatabase::loadPathData
258
     * @dataProvider providerForTestLoadPathDataMultipleLanguages
259
     */
260 View Code Duplication
    public function testLoadPathDataMultipleLanguages($id, $pathData)
261
    {
262
        $this->insertDatabaseFixture(__DIR__ . '/_fixtures/urlaliases_multilang.php');
263
        $gateway = $this->getGateway();
264
265
        $loadedPathData = $gateway->loadPathData($id);
266
267
        self::assertEquals(
268
            $pathData,
269
            $loadedPathData
270
        );
271
    }
272
273
    /**
274
     * @return array
275
     */
276
    public function providerForTestCleanupAfterPublishHistorize()
277
    {
278
        return array(
279
            array(
280
                'action' => 'eznode:314',
281
                'languageId' => 2,
282
                'parentId' => 0,
283
                'textMD5' => '6896260129051a949051c3847c34466f',
284
            ),
285
            array(
286
                'action' => 'eznode:315',
287
                'languageId' => 2,
288
                'parentId' => 0,
289
                'textMD5' => 'c67ed9a09ab136fae610b6a087d82e21',
290
            ),
291
        );
292
    }
293
294
    /**
295
     * Data provider for testArchiveUrlAliasesForDeletedTranslations.
296
     *
297
     * @see testArchiveUrlAliasesForDeletedTranslations
298
     *
299
     * @return array
300
     */
301 View Code Duplication
    public function providerForTestArchiveUrlAliasesForDeletedTranslations()
302
    {
303
        return [
304
            [314, [2]],
305
            [315, [4]],
306
            [316, [4]],
307
            [317, [2, 8]],
308
            [318, [2, 8]],
309
        ];
310
    }
311
312
    /**
313
     * Test for the cleanupAfterPublish() method.
314
     *
315
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Gateway\DoctrineDatabase::cleanupAfterPublish
316
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Gateway\DoctrineDatabase::historize
317
     * @dataProvider providerForTestCleanupAfterPublishHistorize
318
     */
319
    public function testCleanupAfterPublishHistorize($action, $languageId, $parentId, $textMD5)
320
    {
321
        $this->insertDatabaseFixture(__DIR__ . '/_fixtures/urlaliases_downgrade.php');
322
        $gateway = $this->getGateway();
323
324
        $loadedRow = $gateway->loadRow($parentId, $textMD5);
325
326
        $gateway->cleanupAfterPublish($action, $languageId, 42, $parentId, 'jabberwocky');
327
328
        $reloadedRow = $gateway->loadRow($parentId, $textMD5);
329
        $loadedRow['is_original'] = '0';
330
        $loadedRow['link'] = 42;
331
        $loadedRow['id'] = 6;
332
333
        self::assertEquals($reloadedRow, $loadedRow);
334
    }
335
336
    /**
337
     * @return array
338
     */
339
    public function providerForTestCleanupAfterPublishRemovesLanguage()
340
    {
341
        return array(
342
            array(
343
                'action' => 'eznode:316',
344
                'languageId' => 2,
345
                'parentId' => 0,
346
                'textMD5' => 'd2cfe69af2d64330670e08efb2c86df7',
347
            ),
348
            array(
349
                'action' => 'eznode:317',
350
                'languageId' => 2,
351
                'parentId' => 0,
352
                'textMD5' => '538dca05643d220317ad233cd7be7a0a',
353
            ),
354
        );
355
    }
356
357
    /**
358
     * Test for the cleanupAfterPublish() method.
359
     *
360
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Gateway\DoctrineDatabase::cleanupAfterPublish
361
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Gateway\DoctrineDatabase::removeTranslation
362
     * @dataProvider providerForTestCleanupAfterPublishRemovesLanguage
363
     */
364
    public function testCleanupAfterPublishRemovesLanguage($action, $languageId, $parentId, $textMD5)
365
    {
366
        $this->insertDatabaseFixture(__DIR__ . '/_fixtures/urlaliases_downgrade.php');
367
        $gateway = $this->getGateway();
368
369
        $loadedRow = $gateway->loadRow($parentId, $textMD5);
370
371
        $gateway->cleanupAfterPublish($action, $languageId, 42, $parentId, 'jabberwocky');
372
373
        $reloadedRow = $gateway->loadRow($parentId, $textMD5);
374
        $loadedRow['lang_mask'] = $loadedRow['lang_mask'] & ~$languageId;
375
376
        self::assertEquals($reloadedRow, $loadedRow);
377
    }
378
379
    /**
380
     * Test for the reparent() method.
381
     *
382
     * @todo document
383
     *
384
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Gateway\DoctrineDatabase::reparent
385
     */
386
    public function testReparent()
387
    {
388
        $this->insertDatabaseFixture(__DIR__ . '/_fixtures/urlaliases_simple.php');
389
        $gateway = $this->getGateway();
390
391
        $gateway->reparent(2, 42);
392
393
        self::assertEquals(
394
            array(
395
                'action' => 'eznode:315',
396
                'action_type' => 'eznode',
397
                'alias_redirects' => '1',
398
                'id' => '3',
399
                'is_alias' => '0',
400
                'is_original' => '1',
401
                'lang_mask' => '3',
402
                'link' => '3',
403
                'parent' => '42',
404
                'text' => 'dva',
405
                'text_md5' => 'c67ed9a09ab136fae610b6a087d82e21',
406
            ),
407
            $gateway->loadRow(42, 'c67ed9a09ab136fae610b6a087d82e21')
408
        );
409
    }
410
411
    /**
412
     * Test for the remove() method.
413
     *
414
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Gateway\DoctrineDatabase::remove
415
     */
416 View Code Duplication
    public function testRemove()
417
    {
418
        $this->insertDatabaseFixture(__DIR__ . '/_fixtures/urlaliases_remove.php');
419
        $gateway = $this->getGateway();
420
421
        $gateway->remove('eznode:314');
422
423
        self::assertEmpty($gateway->loadRow(0, 'd5189de027922f81005951e6efe0efd5'));
424
        self::assertEmpty($gateway->loadRow(0, 'a59d9f07e3d5fcf77911155650956a73'));
425
        self::assertEmpty($gateway->loadRow(0, '6449cba11bb134a57af94c8cb7f6c99c'));
426
        self::assertNotEmpty($gateway->loadRow(0, '0a06c09b6dd9a4606b4eb6d60ab188f0'));
427
        self::assertNotEmpty($gateway->loadRow(0, '82f2bce3283a0806a398fe78beda17d9'));
428
        self::assertNotEmpty($gateway->loadRow(0, '863d659d9fec68e5ab117b5f585a4ee7'));
429
    }
430
431
    /**
432
     * Test for the remove() method.
433
     *
434
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Gateway\DoctrineDatabase::remove
435
     */
436 View Code Duplication
    public function testRemoveWithId()
437
    {
438
        $this->insertDatabaseFixture(__DIR__ . '/_fixtures/urlaliases_remove.php');
439
        $gateway = $this->getGateway();
440
441
        $gateway->remove('eznode:315', 6);
442
443
        self::assertEmpty($gateway->loadRow(0, '0a06c09b6dd9a4606b4eb6d60ab188f0'));
444
        self::assertEmpty($gateway->loadRow(0, '82f2bce3283a0806a398fe78beda17d9'));
445
        self::assertNotEmpty($gateway->loadRow(0, '863d659d9fec68e5ab117b5f585a4ee7'));
446
        self::assertNotEmpty($gateway->loadRow(0, 'd5189de027922f81005951e6efe0efd5'));
447
        self::assertNotEmpty($gateway->loadRow(0, 'a59d9f07e3d5fcf77911155650956a73'));
448
        self::assertNotEmpty($gateway->loadRow(0, '6449cba11bb134a57af94c8cb7f6c99c'));
449
    }
450
451
    /**
452
     * Test for the removeCustomAlias() method.
453
     *
454
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Gateway\DoctrineDatabase::removeCustomAlias
455
     */
456
    public function testRemoveCustomAlias()
457
    {
458
        $this->insertDatabaseFixture(__DIR__ . '/_fixtures/urlaliases_remove.php');
459
        $gateway = $this->getGateway();
460
461
        $result = $gateway->removeCustomAlias(0, '6449cba11bb134a57af94c8cb7f6c99c');
462
463
        self::assertTrue($result);
464
        self::assertNotEmpty($gateway->loadRow(0, 'd5189de027922f81005951e6efe0efd5'));
465
        self::assertNotEmpty($gateway->loadRow(0, 'a59d9f07e3d5fcf77911155650956a73'));
466
        self::assertEmpty($gateway->loadRow(0, '6449cba11bb134a57af94c8cb7f6c99c'));
467
    }
468
469
    /**
470
     * Test for the removeByAction() method.
471
     *
472
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Gateway\DoctrineDatabase::removeCustomAlias
473
     */
474
    public function testRemoveCustomAliasFails()
475
    {
476
        $this->insertDatabaseFixture(__DIR__ . '/_fixtures/urlaliases_remove.php');
477
        $gateway = $this->getGateway();
478
479
        $result = $gateway->removeCustomAlias(0, 'd5189de027922f81005951e6efe0efd5');
480
481
        self::assertFalse($result);
482
        self::assertNotEmpty($gateway->loadRow(0, 'd5189de027922f81005951e6efe0efd5'));
483
    }
484
485
    /**
486
     * Test for the getNextId() method.
487
     *
488
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Gateway\DoctrineDatabase::getNextId
489
     */
490
    public function testGetNextId()
491
    {
492
        $gateway = $this->getGateway();
493
494
        self::assertEquals(1, $gateway->getNextId());
495
        self::assertEquals(2, $gateway->getNextId());
496
    }
497
498
    /**
499
     * @dataProvider providerForTestArchiveUrlAliasesForDeletedTranslations
500
     *
501
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Gateway\DoctrineDatabase::archiveUrlAliasesForDeletedTranslations
502
     *
503
     * @param int $locationId
504
     * @param int[] $removedLanguageIds
505
     */
506
    public function testArchiveUrlAliasesForDeletedTranslations($locationId, array $removedLanguageIds)
507
    {
508
        $this->insertDatabaseFixture(__DIR__ . '/_fixtures/urlaliases_multilang.php');
509
        $gateway = $this->getGateway();
510
511
        foreach ($gateway->loadLocationEntries($locationId) as $row) {
512
            $gateway->archiveUrlAliasesForDeletedTranslations(
513
                $locationId,
514
                (int) $row['parent'],
515
                $removedLanguageIds
516
            );
517
        }
518
519
        // check results
520
        $languageMask = 0;
521
        foreach ($removedLanguageIds as $languageId) {
522
            $languageMask |= $languageId;
523
        }
524
        foreach ($gateway->loadLocationEntries($locationId) as $row) {
525
            self::assertNotEquals(0, (int) $row['lang_mask']);
526
            self::assertNotEquals(1, (int) $row['lang_mask']);
527
            self::assertEquals(0, (int) $row['lang_mask'] & $languageMask);
528
        }
529
    }
530
531
    /**
532
     * Returns the DoctrineDatabase gateway to test.
533
     *
534
     * @return \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Gateway\DoctrineDatabase
535
     */
536
    protected function getGateway()
537
    {
538
        if (!isset($this->gateway)) {
539
            $languageHandler = new LanguageHandler(
540
                new LanguageGateway(
541
                    $this->getDatabaseHandler()
542
                ),
543
                new LanguageMapper()
544
            );
545
            $this->gateway = new DoctrineDatabase(
546
                $this->getDatabaseHandler(),
547
                new LanguageMaskGenerator($languageHandler)
548
            );
549
        }
550
551
        return $this->gateway;
552
    }
553
}
554