Completed
Push — fix_acholi_language_code ( 7dbf83 )
by
unknown
21:30
created

DoctrineDatabaseTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 132
Duplicated Lines 12.88 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
dl 17
loc 132
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 8

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 8 1
A testInsert() 17 17 1
A testUpdateUserPreference() 0 17 1
A testCountUserPreferences() 0 6 1
A testLoadUserPreferences() 0 23 1
A getGateway() 0 6 1
A loadUserPreference() 0 16 1

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
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
declare(strict_types=1);
8
9
namespace eZ\Publish\Core\Persistence\Legacy\Tests\UserPreference\Gateway;
10
11
use Doctrine\DBAL\FetchMode;
12
use Doctrine\DBAL\ParameterType;
13
use eZ\Publish\Core\Persistence\Legacy\UserPreference\Gateway;
14
use eZ\Publish\Core\Persistence\Legacy\UserPreference\Gateway\DoctrineDatabase;
15
use eZ\Publish\Core\Persistence\Legacy\Tests\TestCase;
16
use eZ\Publish\SPI\Persistence\UserPreference\UserPreferenceSetStruct;
17
18
class DoctrineDatabaseTest extends TestCase
19
{
20
    const EXISTING_USER_PREFERENCE_ID = 1;
21
    const EXISTING_USER_PREFERENCE_DATA = [
22
        'id' => 1,
23
        'user_id' => 14,
24
        'name' => 'timezone',
25
        'value' => 'America/New_York',
26
    ];
27
28
    public function setUp()
29
    {
30
        parent::setUp();
31
32
        $this->insertDatabaseFixture(
33
            __DIR__ . '/../_fixtures/user_preferences.php'
34
        );
35
    }
36
37
    /**
38
     * @covers \eZ\Publish\Core\Persistence\Legacy\UserPreference\Gateway::setUserPreference()
39
     */
40 View Code Duplication
    public function testInsert()
41
    {
42
        $id = $this->getGateway()->setUserPreference(new UserPreferenceSetStruct([
43
            'userId' => 14,
44
            'name' => 'setting_3',
45
            'value' => 'value_3',
46
        ]));
47
48
        $data = $this->loadUserPreference($id);
49
50
        $this->assertEquals([
51
            'id' => $id,
52
            'user_id' => '14',
53
            'name' => 'setting_3',
54
            'value' => 'value_3',
55
        ], $data);
56
    }
57
58
    /**
59
     * @covers \eZ\Publish\Core\Persistence\Legacy\UserPreference\Gateway::setUserPreference()
60
     */
61
    public function testUpdateUserPreference()
62
    {
63
        $userPreference = new UserPreferenceSetStruct([
64
            'userId' => 14,
65
            'name' => 'timezone',
66
            'value' => 'Europe/Warsaw',
67
        ]);
68
69
        $this->getGateway()->setUserPreference($userPreference);
70
71
        $this->assertEquals([
72
            'id' => (string) self::EXISTING_USER_PREFERENCE_ID,
73
            'user_id' => '14',
74
            'name' => 'timezone',
75
            'value' => 'Europe/Warsaw',
76
        ], $this->loadUserPreference(self::EXISTING_USER_PREFERENCE_ID));
77
    }
78
79
    /**
80
     * @covers \eZ\Publish\Core\Persistence\Legacy\UserPreference\Gateway::countUserPreferences()
81
     */
82
    public function testCountUserPreferences()
83
    {
84
        $this->assertEquals(3, $this->getGateway()->countUserPreferences(
85
            self::EXISTING_USER_PREFERENCE_DATA['user_id']
86
        ));
87
    }
88
89
    /**
90
     * @covers \eZ\Publish\Core\Persistence\Legacy\UserPreference\Gateway::loadUserPreferences()
91
     */
92
    public function testLoadUserPreferences()
93
    {
94
        $userId = 14;
95
        $offset = 1;
96
        $limit = 2;
97
98
        $results = $this->getGateway()->loadUserPreferences($userId, $offset, $limit);
99
100
        $this->assertEquals([
101
            [
102
                'id' => '2',
103
                'user_id' => '14',
104
                'name' => 'setting_1',
105
                'value' => 'value_1',
106
            ],
107
            [
108
                'id' => '3',
109
                'user_id' => '14',
110
                'name' => 'setting_2',
111
                'value' => 'value_2',
112
            ],
113
        ], $results);
114
    }
115
116
    /**
117
     * Return a ready to test DoctrineStorage gateway.
118
     *
119
     * @return \eZ\Publish\Core\Persistence\Legacy\UserPreference\Gateway
120
     */
121
    protected function getGateway(): Gateway
122
    {
123
        return new DoctrineDatabase(
124
            $this->getDatabaseHandler()->getConnection()
125
        );
126
    }
127
128
    /**
129
     * @param int $id
130
     *
131
     * @return array
132
     */
133
    private function loadUserPreference(int $id): array
134
    {
135
        $queryBuilder = $this->connection->createQueryBuilder();
136
        $queryBuilder
137
            ->select('id', 'user_id', 'name', 'value')
138
            ->from('ezpreferences', 'p')
139
            ->where(
140
                $queryBuilder->expr()->eq(
141
                    'p.id',
142
                    $queryBuilder->createPositionalParameter($id, ParameterType::INTEGER)
143
                )
144
            );
145
        $result = $queryBuilder->execute()->fetchAll(FetchMode::ASSOCIATIVE);
146
147
        return reset($result);
148
    }
149
}
150