DsnTest   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 253
Duplicated Lines 8.7 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 18
lcom 0
cbo 1
dl 22
loc 253
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A hostValues() 0 21 1
A testHost() 11 11 3
A portValues() 0 22 1
A testPort() 11 11 3
A databaseValues() 0 23 1
A testDatabase() 0 5 1
A passwordValues() 0 22 1
A testPassword() 0 11 2
A isValidValues() 0 21 1
A testIsValid() 0 5 1
A parameterValues() 0 14 1
A testParameterValues() 0 7 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
 * This file is part of php-cache organization.
5
 *
6
 * (c) 2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Cache\AdapterBundle\Tests\Unit;
13
14
use Cache\AdapterBundle\DSN;
15
use PHPUnit\Framework\TestCase;
16
17
/**
18
 * DsnTest.
19
 */
20
class DsnTest extends TestCase
21
{
22
    /**
23
     * @static
24
     *
25
     * @return array
26
     */
27
    public static function hostValues()
28
    {
29
        return [
30
            ['redis://localhost', 'localhost'],
31
            ['redis://localhost/1', 'localhost'],
32
            ['redis://localhost:63790', 'localhost'],
33
            ['redis://localhost:63790/10', 'localhost'],
34
            ['redis://pw@localhost:63790/10', 'localhost'],
35
            ['redis://127.0.0.1', '127.0.0.1'],
36
            ['redis://127.0.0.1/1', '127.0.0.1'],
37
            ['redis://127.0.0.1:63790', '127.0.0.1'],
38
            ['redis://127.0.0.1:63790/10', '127.0.0.1'],
39
            ['redis://[email protected]:63790/10', '127.0.0.1'],
40
            ['mongodb://localhost', 'localhost'],
41
            ['mongodb://127.0.0.1', '127.0.0.1'],
42
            ['mongodb://dev:[email protected]', '127.0.0.1'],
43
            ['mongodb://dev:[email protected]:27371', '127.0.0.1'],
44
            ['mongodb://dev:[email protected]:27371/database', '127.0.0.1'],
45
            ['mongodb://dev:[email protected],192.168.1.1:27371/database', ['127.0.0.1', '192.168.1.1']],
46
        ];
47
    }
48
49
    /**
50
     * @param string $dsn  DSN
51
     * @param string $host Host
52
     *
53
     * @dataProvider hostValues
54
     */
55 View Code Duplication
    public function testHost($dsn, $host)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56
    {
57
        $dsn = new DSN($dsn);
58
        if (is_array($host)) {
59
            foreach ($dsn->getHosts() as $index => $h) {
60
                $this->assertEquals($host[$index], $h['host']);
61
            }
62
        } else {
63
            $this->assertEquals($host, $dsn->getFirstHost());
64
        }
65
    }
66
67
    /**
68
     * @static
69
     *
70
     * @return array
71
     */
72
    public static function portValues()
73
    {
74
        return [
75
            ['redis://localhost', 6379],
76
            ['tcp://localhost', 6379],
77
            ['redis://localhost/1', 6379],
78
            ['redis://localhost:63790', 63790],
79
            ['redis://localhost:63790/10', 63790],
80
            ['redis://pw@localhost:63790/10', 63790],
81
            ['redis://127.0.0.1', 6379],
82
            ['redis://127.0.0.1/1', 6379],
83
            ['redis://127.0.0.1:63790', 63790],
84
            ['redis://127.0.0.1:63790/10', 63790],
85
            ['redis://[email protected]:63790/10', 63790],
86
            ['mongodb://localhost', 27017],
87
            ['mongodb://127.0.0.1', 27017],
88
            ['mongodb://dev:[email protected]', 27017],
89
            ['mongodb://dev:[email protected]:27371', 27371],
90
            ['mongodb://dev:[email protected]:27371/database', 27371],
91
            ['mongodb://dev:[email protected],192.168.1.1:27371/database', [27017, 27371]],
92
        ];
93
    }
94
95
    /**
96
     * @param string $dsn  DSN
97
     * @param int    $port Port
98
     *
99
     * @dataProvider portValues
100
     */
101 View Code Duplication
    public function testPort($dsn, $port)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
102
    {
103
        $dsn = new DSN($dsn);
104
        if (is_array($port)) {
105
            foreach ($dsn->getHosts() as $index => $host) {
106
                $this->assertEquals($port[$index], $host['port']);
107
            }
108
        } else {
109
            $this->assertEquals($port, $dsn->getFirstPort());
110
        }
111
    }
112
113
    /**
114
     * @static
115
     *
116
     * @return array
117
     */
118
    public static function databaseValues()
119
    {
120
        return [
121
            ['redis://localhost', null],
122
            ['redis://localhost/0', 0],
123
            ['redis://localhost/1', 1],
124
            ['redis://localhost:63790', null],
125
            ['redis://localhost:63790/10', 10],
126
            ['redis://pw@localhost:63790/10', 10],
127
            ['redis://127.0.0.1', null],
128
            ['redis://127.0.0.1/0', 0],
129
            ['redis://127.0.0.1/1', 1],
130
            ['redis://127.0.0.1:63790', null],
131
            ['redis://127.0.0.1:63790/10', 10],
132
            ['redis://[email protected]:63790/10', 10],
133
            ['mongodb://localhost', null],
134
            ['mongodb://127.0.0.1', null],
135
            ['mongodb://dev:[email protected]', null],
136
            ['mongodb://dev:[email protected]:27371', null],
137
            ['mongodb://dev:[email protected]:27371/database', 'database'],
138
            ['mongodb://dev:[email protected],192.168.1.1:27371/database', 'database'],
139
        ];
140
    }
141
142
    /**
143
     * @param string $dsn      DSN
144
     * @param int    $database Database
145
     *
146
     * @dataProvider databaseValues
147
     */
148
    public function testDatabase($dsn, $database)
149
    {
150
        $dsn = new DSN($dsn);
151
        $this->assertEquals($database, $dsn->getDatabase());
152
    }
153
154
    /**
155
     * @static
156
     *
157
     * @return array
158
     */
159
    public static function passwordValues()
160
    {
161
        return [
162
            ['redis://localhost', null],
163
            ['redis://localhost/1', null],
164
            ['redis://user:pass@localhost:63790/10', ['user', 'pass']],
165
            ['redis://pw@localhost:63790/10', 'pw'],
166
            ['redis://p\@w@localhost:63790/10', 'p@w'],
167
            ['redis://mB(.z9},6o?zl>v!LM76A]lCg77,;.@localhost:63790/10', 'mB(.z9},6o?zl>v!LM76A]lCg77,;.'],
168
            ['redis://127.0.0.1', null],
169
            ['redis://127.0.0.1/1', null],
170
            ['redis://[email protected]:63790/10', 'pw'],
171
            ['redis://p\@[email protected]:63790/10', 'p@w'],
172
            ['redis://mB(.z9},6o?zl>v!LM76A]lCg77,;[email protected]:63790/10', 'mB(.z9},6o?zl>v!LM76A]lCg77,;.'],
173
            ['mongodb://localhost', null],
174
            ['mongodb://127.0.0.1', null],
175
            ['mongodb://dev:[email protected]', ['dev', 'pass']],
176
            ['mongodb://dev:[email protected]:27371', ['dev', 'pass']],
177
            ['mongodb://dev:[email protected]:27371/database', ['dev', 'pass']],
178
            ['mongodb://dev:[email protected],192.168.1.1:27371/database', ['dev', 'pass']],
179
        ];
180
    }
181
182
    /**
183
     * @param string $dsn      DSN
184
     * @param string $password Password
185
     *
186
     * @dataProvider passwordValues
187
     */
188
    public function testPassword($dsn, $password)
189
    {
190
        $dsn = new DSN($dsn);
191
192
        if (is_array($password)) {
193
            $this->assertEquals($password[0], $dsn->getUsername());
194
            $this->assertEquals($password[1], $dsn->getPassword());
195
        } else {
196
            $this->assertEquals($password, $dsn->getPassword());
197
        }
198
    }
199
200
    /**
201
     * @static
202
     *
203
     * @return array
204
     */
205
    public static function isValidValues()
206
    {
207
        return [
208
            ['redis://localhost', true],
209
            ['redis://localhost/1', true],
210
            ['redis://pw@localhost:63790/10', true],
211
            ['redis://127.0.0.1', true],
212
            ['redis://127.0.0.1/1', true],
213
            ['redis://[email protected]:63790/10', true],
214
            ['mongodb://localhost', true],
215
            ['mongodb://127.0.0.1', true],
216
            ['mongodb://dev:[email protected]', true],
217
            ['mongodb://dev:[email protected]:27371', true],
218
            ['mongodb://dev:[email protected]:27371/database', true],
219
            ['mongodb://dev:[email protected],192.168.1.1:27371/database', true],
220
            ['mongo://localhost', false],
221
            ['localhost', false],
222
            ['localhost/1', false],
223
            ['pw@localhost:63790/10', false],
224
        ];
225
    }
226
227
    /**
228
     * @param string $dsn   DSN
229
     * @param bool   $valid Valid
230
     *
231
     * @dataProvider isValidValues
232
     */
233
    public function testIsValid($dsn, $valid)
234
    {
235
        $dsn = new DSN($dsn);
236
        $this->assertEquals($valid, $dsn->isValid(), 'Failed validating: '.$dsn->getDsn());
237
    }
238
239
    /**
240
     * @static
241
     *
242
     * @return array
243
     */
244
    public static function parameterValues()
245
    {
246
        return [
247
            ['redis://localhost', []],
248
            ['redis://localhost/1?weight=1&alias=master', ['weight' => 1, 'alias' => 'master']],
249
            ['redis://pw@localhost:63790/10?alias=master&weight=2', ['weight' => 2, 'alias' => 'master']],
250
            ['redis://127.0.0.1?weight=3', ['weight' => 3]],
251
            ['redis://127.0.0.1/1?alias=master&weight=4', ['weight' => 4, 'alias' => 'master']],
252
            ['redis://[email protected]:63790/10?weight=5&alias=master', ['weight' => 5, 'alias' => 'master']],
253
            ['redis://localhost?alias=master', ['alias' => 'master']],
254
            ['mongodb://dev:[email protected],192.168.1.1:27371/database?replicaSet=test', ['replicaSet' => 'test']],
255
            ['mongodb://dev:[email protected],192.168.1.1:27371/database?test', ['test' => null]],
256
        ];
257
    }
258
259
    /**
260
     * @param string $dsn
261
     * @param array  $parameters
262
     *
263
     * @dataProvider parameterValues
264
     */
265
    public function testParameterValues($dsn, $parameters)
266
    {
267
        $dsn = new DSN($dsn);
268
        foreach ($parameters as $key => $value) {
269
            $this->assertEquals($value, $dsn->getParameters()[$key]);
270
        }
271
    }
272
}
273