Completed
Push — master ( 4e4cd8...063296 )
by Maksim
16s
created

ConfigurationTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the FOSElasticaBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FOS\ElasticaBundle\Tests\Resetter\DependencyInjection;
13
14
use FOS\ElasticaBundle\DependencyInjection\Configuration;
15
use Symfony\Component\Config\Definition\Processor;
16
17
/**
18
 * ConfigurationTest.
19
 */
20
class ConfigurationTest extends \PHPUnit_Framework_TestCase
21
{
22
    /**
23
     * @var Processor
24
     */
25
    private $processor;
26
27
    public function setUp()
28
    {
29
        $this->processor = new Processor();
30
    }
31
32
    public function testUnconfiguredConfiguration()
33
    {
34
        $configuration = $this->getConfigs([]);
35
36
        $this->assertSame([
37
            'clients' => [],
38
            'indexes' => [],
39
            'default_manager' => 'orm',
40
        ], $configuration);
41
    }
42
43
    public function testClientConfiguration()
44
    {
45
        $configuration = $this->getConfigs([
46
            'clients' => [
47
                'default' => [
48
                    'url' => 'http://localhost:9200',
49
                    'retryOnConflict' => 5,
50
                ],
51
                'clustered' => [
52
                    'connections' => [
53
                        [
54
                            'url' => 'http://es1:9200',
55
                            'headers' => [
56
                                'Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==',
57
                            ],
58
                        ],
59
                        [
60
                            'url' => 'http://es2:9200',
61
                            'headers' => [
62
                                'Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==',
63
                            ],
64
                        ],
65
                    ],
66
                ],
67
            ],
68
        ]);
69
70
        $this->assertCount(2, $configuration['clients']);
71
        $this->assertCount(1, $configuration['clients']['default']['connections']);
72
        $this->assertCount(0, $configuration['clients']['default']['connections'][0]['headers']);
73
        $this->assertSame(5, $configuration['clients']['default']['connections'][0]['retryOnConflict']);
74
75
        $this->assertCount(2, $configuration['clients']['clustered']['connections']);
76
        $this->assertSame('http://es2:9200/', $configuration['clients']['clustered']['connections'][1]['url']);
77
        $this->assertCount(1, $configuration['clients']['clustered']['connections'][1]['headers']);
78
        $this->assertSame('Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==', $configuration['clients']['clustered']['connections'][0]['headers'][0]);
79
    }
80
81 View Code Duplication
    public function testShouldSetPagerProviderFlagToFalseByDefault()
82
    {
83
        $configuration = $this->getConfigs([
84
            'clients' => [
85
                'default' => ['host' => 'a_host', 'port' => 'a_port'],
86
            ],
87
            'indexes' => [
88
                'acme_index' => [
89
                    'types' => [
90
                        'acme_type' => [
91
                            'properties' => ['text' => null],
92
                            'persistence' => [
93
                                'driver' => 'orm',
94
                                'model' => 'AppBundle\Entity\Blog',
95
                                'provider' => [/*'pager_provider' => true*/],
96
                                'listener' => null,
97
                                'finder' => null,
98
                            ]
99
                        ]
100
                    ]
101
                ]
102
            ]
103
        ]);
104
105
        $this->assertSame(false, $configuration['indexes']['acme_index']['types']['acme_type']['persistence']['provider']['pager_provider']);
106
    }
107
108 View Code Duplication
    public function testShouldAllowToSetPagerProviderExplicitly()
109
    {
110
        $configuration = $this->getConfigs([
111
            'clients' => [
112
                'default' => ['host' => 'a_host', 'port' => 'a_port'],
113
            ],
114
            'indexes' => [
115
                'acme_index' => [
116
                    'types' => [
117
                        'acme_type' => [
118
                            'properties' => ['text' => null],
119
                            'persistence' => [
120
                                'driver' => 'orm',
121
                                'model' => 'AppBundle\Entity\Blog',
122
                                'provider' => ['pager_provider' => true],
123
                                'listener' => null,
124
                                'finder' => null,
125
                            ]
126
                        ]
127
                    ]
128
                ]
129
            ]
130
        ]);
131
132
        $this->assertSame(true, $configuration['indexes']['acme_index']['types']['acme_type']['persistence']['provider']['pager_provider']);
133
    }
134
135
    public function testLogging()
136
    {
137
        $configuration = $this->getConfigs([
138
            'clients' => [
139
                'logging_enabled' => [
140
                    'url' => 'http://localhost:9200',
141
                    'logger' => true,
142
                ],
143
                'logging_disabled' => [
144
                    'url' => 'http://localhost:9200',
145
                    'logger' => false,
146
                ],
147
                'logging_not_mentioned' => [
148
                    'url' => 'http://localhost:9200',
149
                ],
150
                'logging_custom' => [
151
                    'url' => 'http://localhost:9200',
152
                    'logger' => 'custom.service',
153
                ],
154
            ],
155
        ]);
156
157
        $this->assertCount(4, $configuration['clients']);
158
159
        $this->assertSame('fos_elastica.logger', $configuration['clients']['logging_enabled']['connections'][0]['logger']);
160
        $this->assertFalse($configuration['clients']['logging_disabled']['connections'][0]['logger']);
161
        $this->assertSame('fos_elastica.logger', $configuration['clients']['logging_not_mentioned']['connections'][0]['logger']);
162
        $this->assertSame('custom.service', $configuration['clients']['logging_custom']['connections'][0]['logger']);
163
    }
164
165
    public function testSlashIsAddedAtTheEndOfServerUrl()
166
    {
167
        $config = [
168
            'clients' => [
169
                'default' => ['url' => 'http://www.github.com'],
170
            ],
171
        ];
172
        $configuration = $this->getConfigs($config);
173
174
        $this->assertSame('http://www.github.com/', $configuration['clients']['default']['connections'][0]['url']);
175
    }
176
177
    public function testTypeConfig()
178
    {
179
        $this->getConfigs([
180
            'clients' => [
181
                'default' => ['url' => 'http://localhost:9200'],
182
            ],
183
            'indexes' => [
184
                'test' => [
185
                    'type_prototype' => [
186
                        'analyzer' => 'custom_analyzer',
187
                        'persistence' => [
188
                            'identifier' => 'ID',
189
                        ],
190
                        'serializer' => [
191
                            'groups' => ['Search'],
192
                            'version' => 1,
193
                            'serialize_null' => false,
194
                        ],
195
                    ],
196
                    'types' => [
197
                        'test' => [
198
                            'properties' => [
199
                                'title' => [],
200
                                'published' => ['type' => 'datetime'],
201
                                'body' => null,
202
                            ],
203
                            'persistence' => [
204
                                'listener' => [
205
                                    'logger' => true,
206
                                ],
207
                            ],
208
                        ],
209
                        'test2' => [
210
                            'properties' => [
211
                                'title' => null,
212
                                'children' => [
213
                                    'type' => 'nested',
214
                                ],
215
                            ],
216
                        ],
217
                    ],
218
                ],
219
            ],
220
        ]);
221
    }
222
223
    public function testClientConfigurationNoUrl()
224
    {
225
        $configuration = $this->getConfigs([
226
            'clients' => [
227
                'default' => [
228
                    'host' => 'localhost',
229
                    'port' => 9200,
230
                ],
231
            ],
232
        ]);
233
234
        $this->assertTrue(empty($configuration['clients']['default']['connections'][0]['url']));
235
    }
236
237
    public function testUnconfiguredType()
238
    {
239
        $configuration = $this->getConfigs([
240
                'clients' => [
241
                    'default' => ['url' => 'http://localhost:9200'],
242
                ],
243
                'indexes' => [
244
                    'test' => [
245
                        'types' => [
246
                            'test' => null,
247
                        ],
248
                    ],
249
                ],
250
            ]);
251
252
        $this->assertArrayHasKey('properties', $configuration['indexes']['test']['types']['test']);
253
    }
254
255
    public function testNestedProperties()
256
    {
257
        $this->getConfigs([
258
            'clients' => [
259
                'default' => ['url' => 'http://localhost:9200'],
260
            ],
261
            'indexes' => [
262
                'test' => [
263
                    'types' => [
264
                        'user' => [
265
                            'properties' => [
266
                                'field1' => [],
267
                            ],
268
                            'persistence' => [],
269
                        ],
270
                        'user_profile' => [
271
                            '_parent' => [
272
                                'type' => 'user',
273
                            ],
274
                            'properties' => [
275
                                'field1' => [],
276
                                'field2' => [
277
                                    'type' => 'nested',
278
                                    'properties' => [
279
                                        'nested_field1' => [
280
                                            'type' => 'integer',
281
                                        ],
282
                                        'nested_field2' => [
283
                                            'type' => 'object',
284
                                            'properties' => [
285
                                                'id' => [
286
                                                    'type' => 'integer',
287
                                                ],
288
                                            ],
289
                                        ],
290
                                    ],
291
                                ],
292
                            ],
293
                        ],
294
                    ],
295
                ],
296
            ],
297
        ]);
298
    }
299
300
    public function testCompressionConfig()
301
    {
302
        $configuration = $this->getConfigs([
303
            'clients' => [
304
                'compression_enabled' => [
305
                    'compression' => true,
306
                ],
307
                'compression_disabled' => [
308
                    'compression' => false,
309
                ],
310
            ],
311
        ]);
312
313
        $this->assertTrue($configuration['clients']['compression_enabled']['connections'][0]['compression']);
314
        $this->assertFalse($configuration['clients']['compression_disabled']['connections'][0]['compression']);
315
    }
316
317
    public function testCompressionDefaultConfig()
318
    {
319
        $configuration = $this->getConfigs([
320
            'clients' => [
321
                'default' => [],
322
            ],
323
        ]);
324
325
        $this->assertFalse($configuration['clients']['default']['connections'][0]['compression']);
326
    }
327
328
    public function testTimeoutConfig()
329
    {
330
        $configuration = $this->getConfigs([
331
            'clients' => [
332
                'simple_timeout' => [
333
                    'url' => 'http://localhost:9200',
334
                    'timeout' => 123,
335
                ],
336
                'connect_timeout' => [
337
                    'url' => 'http://localhost:9200',
338
                    'connectTimeout' => 234,
339
                ],
340
            ],
341
        ]);
342
343
        $this->assertSame(123, $configuration['clients']['simple_timeout']['connections'][0]['timeout']);
344
        $this->assertSame(234, $configuration['clients']['connect_timeout']['connections'][0]['connectTimeout']);
345
    }
346
347
    public function testAWSConfig()
348
    {
349
        $configuration = $this->getConfigs([
350
            'clients' => [
351
                'default' => [
352
                    'aws_access_key_id' => 'AWS_KEY',
353
                    'aws_secret_access_key' => 'AWS_SECRET',
354
                    'aws_region' => 'AWS_REGION',
355
                    'aws_session_token' => 'AWS_SESSION_TOKEN',
356
                    'ssl' => true,
357
                ],
358
            ],
359
        ]);
360
361
        $connection = $configuration['clients']['default']['connections'][0];
362
        $this->assertSame('AWS_KEY', $connection['aws_access_key_id']);
363
        $this->assertSame('AWS_SECRET', $connection['aws_secret_access_key']);
364
        $this->assertSame('AWS_REGION', $connection['aws_region']);
365
        $this->assertSame('AWS_SESSION_TOKEN', $connection['aws_session_token']);
366
        $this->assertTrue($connection['ssl']);
367
    }
368
369
    private function getConfigs(array $configArray)
370
    {
371
        $configuration = new Configuration(true);
372
373
        return $this->processor->processConfiguration($configuration, [$configArray]);
374
    }
375
}
376