Completed
Pull Request — master (#1151)
by Richard
05:24
created

ConfigurationTest::testAWSConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 19
rs 9.4285
cc 1
eloc 13
nc 1
nop 0
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
                            'mappings' => 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
                            'mappings' => 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 testMappingsRenamedToProperties()
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' => array(
191
                                'mappings' => array(
192
                                    'title' => array(),
193
                                    'published' => array('type' => 'datetime'),
194
                                    'body' => null,
195
                                ),
196
                            ),
197
                        ),
198
                    ),
199
                ),
200
            ));
201
202
        $this->assertCount(3, $configuration['indexes']['test']['types']['test']['properties']);
203
    }
204
205
    public function testUnconfiguredType()
206
    {
207
        $configuration = $this->getConfigs(array(
208
                'clients' => array(
209
                    'default' => array('url' => 'http://localhost:9200'),
210
                ),
211
                'indexes' => array(
212
                    'test' => array(
213
                        'types' => array(
214
                            'test' => null,
215
                        ),
216
                    ),
217
                ),
218
            ));
219
220
        $this->assertArrayHasKey('properties', $configuration['indexes']['test']['types']['test']);
221
    }
222
223
    public function testNestedProperties()
224
    {
225
        $this->getConfigs(array(
226
            'clients' => array(
227
                'default' => array('url' => 'http://localhost:9200'),
228
            ),
229
            'indexes' => array(
230
                'test' => array(
231
                    'types' => array(
232
                        'user' => array(
233
                            'properties' => array(
234
                                'field1' => array(),
235
                            ),
236
                            'persistence' => array(),
237
                        ),
238
                        'user_profile' => array(
239
                            '_parent' => array(
240
                                'type' => 'user',
241
                            ),
242
                            'properties' => array(
243
                                'field1' => array(),
244
                                'field2' => array(
245
                                    'type' => 'nested',
246
                                    'properties' => array(
247
                                        'nested_field1' => array(
248
                                            'type' => 'integer',
249
                                        ),
250
                                        'nested_field2' => array(
251
                                            'type' => 'object',
252
                                            'properties' => array(
253
                                                'id' => array(
254
                                                    'type' => 'integer',
255
                                                ),
256
                                            ),
257
                                        ),
258
                                    ),
259
                                ),
260
                            ),
261
                        ),
262
                    ),
263
                ),
264
            ),
265
        ));
266
    }
267
268
    public function testCompressionConfig()
269
    {
270
        $configuration = $this->getConfigs(array(
271
            'clients' => array(
272
                'compression_enabled' => array(
273
                    'compression' => true,
274
                ),
275
                'compression_disabled' => array(
276
                    'compression' => false,
277
                ),
278
            ),
279
        ));
280
281
        $this->assertTrue($configuration['clients']['compression_enabled']['connections'][0]['compression']);
282
        $this->assertFalse($configuration['clients']['compression_disabled']['connections'][0]['compression']);
283
    }
284
285
    public function testCompressionDefaultConfig()
286
    {
287
        $configuration = $this->getConfigs(array(
288
            'clients' => array(
289
                'default' => array(),
290
            ),
291
        ));
292
293
        $this->assertFalse($configuration['clients']['default']['connections'][0]['compression']);
294
    }
295
296
    public function testTimeoutConfig()
297
    {
298
        $configuration = $this->getConfigs(array(
299
            'clients' => array(
300
                'simple_timeout'       => array(
301
                    'url'    => 'http://localhost:9200',
302
                    'timeout' => 123,
303
                ),
304
                'connect_timeout'      => array(
305
                    'url'    => 'http://localhost:9200',
306
                    'connectTimeout' => 234,
307
                ),
308
            ),
309
        ));
310
311
        $this->assertEquals(123, $configuration['clients']['simple_timeout']['connections'][0]['timeout']);
312
        $this->assertEquals(234, $configuration['clients']['connect_timeout']['connections'][0]['connectTimeout']);
313
    }
314
315
    public function testAWSConfig()
316
    {
317
        $configuration = $this->getConfigs(array(
318
            'clients' => array(
319
                'default' => array(
320
                    'aws_access_key_id'     => 'AWS_KEY',
321
                    'aws_secret_access_key' => 'AWS_SECRET',
322
                    'aws_region'            => 'AWS_REGION',
323
                    'aws_session_token'     => 'AWS_SESSION_TOKEN',
324
                ),
325
            ),
326
        ));
327
328
        $connection = $configuration['clients']['default']['connections'][0];
329
        $this->assertEquals('AWS_KEY', $connection['aws_access_key_id']);
330
        $this->assertEquals('AWS_SECRET', $connection['aws_secret_access_key']);
331
        $this->assertEquals('AWS_REGION', $connection['aws_region']);
332
        $this->assertEquals('AWS_SESSION_TOKEN', $connection['aws_session_token']);
333
    }
334
}
335