Completed
Push — master ( f7313f...f3d09b )
by Karel
03:49
created

ConfigurationTest   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 300
Duplicated Lines 8 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 14
c 0
b 0
f 0
lcom 1
cbo 3
dl 24
loc 300
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A getConfigs() 0 6 1
A testUnconfiguredConfiguration() 0 10 1
B testLogging() 0 29 1
A testSlashIsAddedAtTheEndOfServerUrl() 11 11 1
A testClientConfigurationNoUrl() 13 13 1
B testClientConfiguration() 0 37 1
B testTypeConfig() 0 45 1
A testUnconfiguredType() 0 17 1
B testNestedProperties() 0 44 1
A testCompressionConfig() 0 16 1
A testCompressionDefaultConfig() 0 10 1
A testTimeoutConfig() 0 18 1
A testAWSConfig() 0 19 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
namespace FOS\ElasticaBundle\Tests\Resetter\DependencyInjection;
4
5
use FOS\ElasticaBundle\DependencyInjection\Configuration;
6
use Symfony\Component\Config\Definition\Processor;
7
8
/**
9
 * ConfigurationTest.
10
 */
11
class ConfigurationTest extends \PHPUnit_Framework_TestCase
12
{
13
    /**
14
     * @var Processor
15
     */
16
    private $processor;
17
18
    public function setUp()
19
    {
20
        $this->processor = new Processor();
21
    }
22
23
    private function getConfigs(array $configArray)
24
    {
25
        $configuration = new Configuration(true);
26
27
        return $this->processor->processConfiguration($configuration, array($configArray));
28
    }
29
30
    public function testUnconfiguredConfiguration()
31
    {
32
        $configuration = $this->getConfigs(array());
33
34
        $this->assertSame(array(
35
            'clients' => array(),
36
            'indexes' => array(),
37
            'default_manager' => 'orm',
38
        ), $configuration);
39
    }
40
41
    public function testClientConfiguration()
42
    {
43
        $configuration = $this->getConfigs(array(
44
            'clients' => array(
45
                'default' => array(
46
                    'url' => 'http://localhost:9200',
47
                    'retryOnConflict' => 5,
48
                ),
49
                'clustered' => array(
50
                    'connections' => array(
51
                        array(
52
                            'url' => 'http://es1:9200',
53
                            'headers' => array(
54
                                'Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==',
55
                            ),
56
                        ),
57
                        array(
58
                            'url' => 'http://es2:9200',
59
                            'headers' => array(
60
                                'Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==',
61
                            ),
62
                        ),
63
                    ),
64
                ),
65
            ),
66
        ));
67
68
        $this->assertCount(2, $configuration['clients']);
69
        $this->assertCount(1, $configuration['clients']['default']['connections']);
70
        $this->assertCount(0, $configuration['clients']['default']['connections'][0]['headers']);
71
        $this->assertEquals(5, $configuration['clients']['default']['connections'][0]['retryOnConflict']);
72
73
        $this->assertCount(2, $configuration['clients']['clustered']['connections']);
74
        $this->assertEquals('http://es2:9200/', $configuration['clients']['clustered']['connections'][1]['url']);
75
        $this->assertCount(1, $configuration['clients']['clustered']['connections'][1]['headers']);
76
        $this->assertEquals('Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==', $configuration['clients']['clustered']['connections'][0]['headers'][0]);
77
    }
78
79
    public function testLogging()
80
    {
81
        $configuration = $this->getConfigs(array(
82
            'clients' => array(
83
                'logging_enabled' => array(
84
                    'url' => 'http://localhost:9200',
85
                    'logger' => true,
86
                ),
87
                'logging_disabled' => array(
88
                    'url' => 'http://localhost:9200',
89
                    'logger' => false,
90
                ),
91
                'logging_not_mentioned' => array(
92
                    'url' => 'http://localhost:9200',
93
                ),
94
                'logging_custom' => array(
95
                    'url' => 'http://localhost:9200',
96
                    'logger' => 'custom.service',
97
                ),
98
            ),
99
        ));
100
101
        $this->assertCount(4, $configuration['clients']);
102
103
        $this->assertEquals('fos_elastica.logger', $configuration['clients']['logging_enabled']['connections'][0]['logger']);
104
        $this->assertFalse($configuration['clients']['logging_disabled']['connections'][0]['logger']);
105
        $this->assertEquals('fos_elastica.logger', $configuration['clients']['logging_not_mentioned']['connections'][0]['logger']);
106
        $this->assertEquals('custom.service', $configuration['clients']['logging_custom']['connections'][0]['logger']);
107
    }
108
109 View Code Duplication
    public function testSlashIsAddedAtTheEndOfServerUrl()
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...
110
    {
111
        $config = array(
112
            'clients' => array(
113
                'default' => array('url' => 'http://www.github.com'),
114
            ),
115
        );
116
        $configuration = $this->getConfigs($config);
117
118
        $this->assertEquals('http://www.github.com/', $configuration['clients']['default']['connections'][0]['url']);
119
    }
120
121
    public function testTypeConfig()
122
    {
123
        $this->getConfigs(array(
124
            'clients' => array(
125
                'default' => array('url' => 'http://localhost:9200'),
126
            ),
127
            'indexes' => array(
128
                'test' => array(
129
                    'type_prototype' => array(
130
                        'analyzer' => 'custom_analyzer',
131
                        'persistence' => array(
132
                            'identifier' => 'ID',
133
                        ),
134
                        'serializer' => array(
135
                            'groups' => array('Search'),
136
                            'version' => 1,
137
                            'serialize_null' => false,
138
                        ),
139
                    ),
140
                    'types' => array(
141
                        'test' => array(
142
                            'properties' => array(
143
                                'title' => array(),
144
                                'published' => array('type' => 'datetime'),
145
                                'body' => null,
146
                            ),
147
                            'persistence' => array(
148
                                'listener' => array(
149
                                    'logger' => true,
150
                                ),
151
                            ),
152
                        ),
153
                        'test2' => array(
154
                            'properties' => array(
155
                                'title' => null,
156
                                'children' => array(
157
                                    'type' => 'nested',
158
                                ),
159
                            ),
160
                        ),
161
                    ),
162
                ),
163
            ),
164
        ));
165
    }
166
167 View Code Duplication
    public function testClientConfigurationNoUrl()
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...
168
    {
169
        $configuration = $this->getConfigs(array(
170
            'clients' => array(
171
                'default' => array(
172
                    'host' => 'localhost',
173
                    'port' => 9200,
174
                ),
175
            ),
176
        ));
177
178
        $this->assertTrue(empty($configuration['clients']['default']['connections'][0]['url']));
179
    }
180
181
    public function testUnconfiguredType()
182
    {
183
        $configuration = $this->getConfigs(array(
184
                'clients' => array(
185
                    'default' => array('url' => 'http://localhost:9200'),
186
                ),
187
                'indexes' => array(
188
                    'test' => array(
189
                        'types' => array(
190
                            'test' => null,
191
                        ),
192
                    ),
193
                ),
194
            ));
195
196
        $this->assertArrayHasKey('properties', $configuration['indexes']['test']['types']['test']);
197
    }
198
199
    public function testNestedProperties()
200
    {
201
        $this->getConfigs(array(
202
            'clients' => array(
203
                'default' => array('url' => 'http://localhost:9200'),
204
            ),
205
            'indexes' => array(
206
                'test' => array(
207
                    'types' => array(
208
                        'user' => array(
209
                            'properties' => array(
210
                                'field1' => array(),
211
                            ),
212
                            'persistence' => array(),
213
                        ),
214
                        'user_profile' => array(
215
                            '_parent' => array(
216
                                'type' => 'user',
217
                            ),
218
                            'properties' => array(
219
                                'field1' => array(),
220
                                'field2' => array(
221
                                    'type' => 'nested',
222
                                    'properties' => array(
223
                                        'nested_field1' => array(
224
                                            'type' => 'integer',
225
                                        ),
226
                                        'nested_field2' => array(
227
                                            'type' => 'object',
228
                                            'properties' => array(
229
                                                'id' => array(
230
                                                    'type' => 'integer',
231
                                                ),
232
                                            ),
233
                                        ),
234
                                    ),
235
                                ),
236
                            ),
237
                        ),
238
                    ),
239
                ),
240
            ),
241
        ));
242
    }
243
244
    public function testCompressionConfig()
245
    {
246
        $configuration = $this->getConfigs(array(
247
            'clients' => array(
248
                'compression_enabled' => array(
249
                    'compression' => true,
250
                ),
251
                'compression_disabled' => array(
252
                    'compression' => false,
253
                ),
254
            ),
255
        ));
256
257
        $this->assertTrue($configuration['clients']['compression_enabled']['connections'][0]['compression']);
258
        $this->assertFalse($configuration['clients']['compression_disabled']['connections'][0]['compression']);
259
    }
260
261
    public function testCompressionDefaultConfig()
262
    {
263
        $configuration = $this->getConfigs(array(
264
            'clients' => array(
265
                'default' => array(),
266
            ),
267
        ));
268
269
        $this->assertFalse($configuration['clients']['default']['connections'][0]['compression']);
270
    }
271
272
    public function testTimeoutConfig()
273
    {
274
        $configuration = $this->getConfigs(array(
275
            'clients' => array(
276
                'simple_timeout'       => array(
277
                    'url'    => 'http://localhost:9200',
278
                    'timeout' => 123,
279
                ),
280
                'connect_timeout'      => array(
281
                    'url'    => 'http://localhost:9200',
282
                    'connectTimeout' => 234,
283
                ),
284
            ),
285
        ));
286
287
        $this->assertEquals(123, $configuration['clients']['simple_timeout']['connections'][0]['timeout']);
288
        $this->assertEquals(234, $configuration['clients']['connect_timeout']['connections'][0]['connectTimeout']);
289
    }
290
291
    public function testAWSConfig()
292
    {
293
        $configuration = $this->getConfigs(array(
294
            'clients' => array(
295
                'default' => array(
296
                    'aws_access_key_id'     => 'AWS_KEY',
297
                    'aws_secret_access_key' => 'AWS_SECRET',
298
                    'aws_region'            => 'AWS_REGION',
299
                    'aws_session_token'     => 'AWS_SESSION_TOKEN',
300
                ),
301
            ),
302
        ));
303
304
        $connection = $configuration['clients']['default']['connections'][0];
305
        $this->assertEquals('AWS_KEY', $connection['aws_access_key_id']);
306
        $this->assertEquals('AWS_SECRET', $connection['aws_secret_access_key']);
307
        $this->assertEquals('AWS_REGION', $connection['aws_region']);
308
        $this->assertEquals('AWS_SESSION_TOKEN', $connection['aws_session_token']);
309
    }
310
}
311