Completed
Pull Request — master (#917)
by Dmitry
08:52
created

ConfigurationTest   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 356
Duplicated Lines 6.74 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 5
Bugs 0 Features 2
Metric Value
wmc 15
c 5
b 0
f 2
lcom 1
cbo 3
dl 24
loc 356
rs 10

15 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A getConfigs() 0 6 1
A testUnconfiguredConfiguration() 0 11 1
B testClientConfiguration() 0 37 1
B testLogging() 0 29 1
A testSlashIsAddedAtTheEndOfServerUrl() 11 11 1
B testTypeConfig() 0 45 1
A testClientConfigurationNoUrl() 13 13 1
A testMappingsRenamedToProperties() 0 23 1
A testUnconfiguredType() 0 17 1
B testNestedProperties() 0 45 1
A testCompressionConfig() 0 16 1
A testCompressionDefaultConfig() 0 10 1
A testTimeoutConfig() 0 18 1
A testIndexTemplates() 0 49 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
            'index_templates' => array(),
38
            'default_manager' => 'orm',
39
        ), $configuration);
40
    }
41
42
    public function testClientConfiguration()
43
    {
44
        $configuration = $this->getConfigs(array(
45
            'clients' => array(
46
                'default' => array(
47
                    'url' => 'http://localhost:9200',
48
                    'retryOnConflict' => 5,
49
                ),
50
                'clustered' => array(
51
                    'connections' => array(
52
                        array(
53
                            'url' => 'http://es1:9200',
54
                            'headers' => array(
55
                                'Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==',
56
                            ),
57
                        ),
58
                        array(
59
                            'url' => 'http://es2:9200',
60
                            'headers' => array(
61
                                'Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==',
62
                            ),
63
                        ),
64
                    ),
65
                ),
66
            ),
67
        ));
68
69
        $this->assertCount(2, $configuration['clients']);
70
        $this->assertCount(1, $configuration['clients']['default']['connections']);
71
        $this->assertCount(0, $configuration['clients']['default']['connections'][0]['headers']);
72
        $this->assertEquals(5, $configuration['clients']['default']['connections'][0]['retryOnConflict']);
73
74
        $this->assertCount(2, $configuration['clients']['clustered']['connections']);
75
        $this->assertEquals('http://es2:9200/', $configuration['clients']['clustered']['connections'][1]['url']);
76
        $this->assertCount(1, $configuration['clients']['clustered']['connections'][1]['headers']);
77
        $this->assertEquals('Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==', $configuration['clients']['clustered']['connections'][0]['headers'][0]);
78
    }
79
80
    public function testLogging()
81
    {
82
        $configuration = $this->getConfigs(array(
83
            'clients' => array(
84
                'logging_enabled' => array(
85
                    'url' => 'http://localhost:9200',
86
                    'logger' => true,
87
                ),
88
                'logging_disabled' => array(
89
                    'url' => 'http://localhost:9200',
90
                    'logger' => false,
91
                ),
92
                'logging_not_mentioned' => array(
93
                    'url' => 'http://localhost:9200',
94
                ),
95
                'logging_custom' => array(
96
                    'url' => 'http://localhost:9200',
97
                    'logger' => 'custom.service',
98
                ),
99
            ),
100
        ));
101
102
        $this->assertCount(4, $configuration['clients']);
103
104
        $this->assertEquals('fos_elastica.logger', $configuration['clients']['logging_enabled']['connections'][0]['logger']);
105
        $this->assertFalse($configuration['clients']['logging_disabled']['connections'][0]['logger']);
106
        $this->assertEquals('fos_elastica.logger', $configuration['clients']['logging_not_mentioned']['connections'][0]['logger']);
107
        $this->assertEquals('custom.service', $configuration['clients']['logging_custom']['connections'][0]['logger']);
108
    }
109
110 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...
111
    {
112
        $config = array(
113
            'clients' => array(
114
                'default' => array('url' => 'http://www.github.com'),
115
            ),
116
        );
117
        $configuration = $this->getConfigs($config);
118
119
        $this->assertEquals('http://www.github.com/', $configuration['clients']['default']['connections'][0]['url']);
120
    }
121
122
    public function testTypeConfig()
123
    {
124
        $this->getConfigs(array(
125
            'clients' => array(
126
                'default' => array('url' => 'http://localhost:9200'),
127
            ),
128
            'indexes' => array(
129
                'test' => array(
130
                    'type_prototype' => array(
131
                        'index_analyzer' => 'custom_analyzer',
132
                        'persistence' => array(
133
                            'identifier' => 'ID',
134
                        ),
135
                        'serializer' => array(
136
                            'groups' => array('Search'),
137
                            'version' => 1,
138
                            'serialize_null' => false,
139
                        ),
140
                    ),
141
                    'types' => array(
142
                        'test' => array(
143
                            'mappings' => array(
144
                                'title' => array(),
145
                                'published' => array('type' => 'datetime'),
146
                                'body' => null,
147
                            ),
148
                            'persistence' => array(
149
                                'listener' => array(
150
                                    'logger' => true,
151
                                ),
152
                            ),
153
                        ),
154
                        'test2' => array(
155
                            'mappings' => array(
156
                                'title' => null,
157
                                'children' => array(
158
                                    'type' => 'nested',
159
                                ),
160
                            ),
161
                        ),
162
                    ),
163
                ),
164
            ),
165
        ));
166
    }
167
168 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...
169
    {
170
        $configuration = $this->getConfigs(array(
171
            'clients' => array(
172
                'default' => array(
173
                    'host' => 'localhost',
174
                    'port' => 9200,
175
                ),
176
            ),
177
        ));
178
179
        $this->assertTrue(empty($configuration['clients']['default']['connections'][0]['url']));
180
    }
181
182
    public function testMappingsRenamedToProperties()
183
    {
184
        $configuration = $this->getConfigs(array(
185
                'clients' => array(
186
                    'default' => array('url' => 'http://localhost:9200'),
187
                ),
188
                'indexes' => array(
189
                    'test' => array(
190
                        'types' => array(
191
                            'test' => array(
192
                                'mappings' => array(
193
                                    'title' => array(),
194
                                    'published' => array('type' => 'datetime'),
195
                                    'body' => null,
196
                                ),
197
                            ),
198
                        ),
199
                    ),
200
                ),
201
            ));
202
203
        $this->assertCount(3, $configuration['indexes']['test']['types']['test']['properties']);
204
    }
205
206
    public function testUnconfiguredType()
207
    {
208
        $configuration = $this->getConfigs(array(
209
                'clients' => array(
210
                    'default' => array('url' => 'http://localhost:9200'),
211
                ),
212
                'indexes' => array(
213
                    'test' => array(
214
                        'types' => array(
215
                            'test' => null,
216
                        ),
217
                    ),
218
                ),
219
            ));
220
221
        $this->assertArrayHasKey('properties', $configuration['indexes']['test']['types']['test']);
222
    }
223
224
    public function testNestedProperties()
225
    {
226
        $this->getConfigs(array(
227
            'clients' => array(
228
                'default' => array('url' => 'http://localhost:9200'),
229
            ),
230
            'indexes' => array(
231
                'test' => array(
232
                    'types' => array(
233
                        'user' => array(
234
                            'properties' => array(
235
                                'field1' => array(),
236
                            ),
237
                            'persistence' => array(),
238
                        ),
239
                        'user_profile' => array(
240
                            '_parent' => array(
241
                                'type' => 'user',
242
                                'property' => 'owner',
243
                            ),
244
                            'properties' => array(
245
                                'field1' => array(),
246
                                'field2' => array(
247
                                    'type' => 'nested',
248
                                    'properties' => array(
249
                                        'nested_field1' => array(
250
                                            'type' => 'integer',
251
                                        ),
252
                                        'nested_field2' => array(
253
                                            'type' => 'object',
254
                                            'properties' => array(
255
                                                'id' => array(
256
                                                    'type' => 'integer',
257
                                                ),
258
                                            ),
259
                                        ),
260
                                    ),
261
                                ),
262
                            ),
263
                        ),
264
                    ),
265
                ),
266
            ),
267
        ));
268
    }
269
270
    public function testCompressionConfig()
271
    {
272
        $configuration = $this->getConfigs(array(
273
            'clients' => array(
274
                'compression_enabled' => array(
275
                    'compression' => true,
276
                ),
277
                'compression_disabled' => array(
278
                    'compression' => false,
279
                ),
280
            ),
281
        ));
282
283
        $this->assertTrue($configuration['clients']['compression_enabled']['connections'][0]['compression']);
284
        $this->assertFalse($configuration['clients']['compression_disabled']['connections'][0]['compression']);
285
    }
286
287
    public function testCompressionDefaultConfig()
288
    {
289
        $configuration = $this->getConfigs(array(
290
            'clients' => array(
291
                'default' => array(),
292
            ),
293
        ));
294
295
        $this->assertFalse($configuration['clients']['default']['connections'][0]['compression']);
296
    }
297
298
    public function testTimeoutConfig()
299
    {
300
        $configuration = $this->getConfigs(array(
301
            'clients' => array(
302
                'simple_timeout'       => array(
303
                    'url'    => 'http://localhost:9200',
304
                    'timeout' => 123,
305
                ),
306
                'connect_timeout'      => array(
307
                    'url'    => 'http://localhost:9200',
308
                    'connectTimeout' => 234,
309
                ),
310
            ),
311
        ));
312
313
        $this->assertEquals(123, $configuration['clients']['simple_timeout']['connections'][0]['timeout']);
314
        $this->assertEquals(234, $configuration['clients']['connect_timeout']['connections'][0]['connectTimeout']);
315
    }
316
317
    public function testIndexTemplates()
318
    {
319
        $this->getConfigs(array(
320
            'clients' => array(
321
                'default' => array('url' => 'http://localhost:9200'),
322
            ),
323
            'index_templates' => array(
324
                'test' => array(
325
                    'template' => 't*',
326
                    'settings' => array(
327
                        'number_of_shards' => 1
328
                    ),
329
                    'types' => array(
330
                        'user' => array(
331
                            'properties' => array(
332
                                'field1' => array(),
333
                            ),
334
                            'persistence' => array(),
335
                        ),
336
                        'user_profile' => array(
337
                            '_parent' => array(
338
                                'type' => 'user',
339
                                'property' => 'owner',
340
                            ),
341
                            'properties' => array(
342
                                'field1' => array(),
343
                                'field2' => array(
344
                                    'type' => 'nested',
345
                                    'properties' => array(
346
                                        'nested_field1' => array(
347
                                            'type' => 'integer',
348
                                        ),
349
                                        'nested_field2' => array(
350
                                            'type' => 'object',
351
                                            'properties' => array(
352
                                                'id' => array(
353
                                                    'type' => 'integer',
354
                                                ),
355
                                            ),
356
                                        ),
357
                                    ),
358
                                ),
359
                            ),
360
                        ),
361
                    ),
362
                ),
363
            ),
364
        ));
365
    }
366
}
367