Completed
Pull Request — master (#1325)
by Maksim
02:52
created

PagerProviderFlagToFalseByDefault()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 16

Duplication

Lines 26
Ratio 100 %

Importance

Changes 0
Metric Value
dl 26
loc 26
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 16
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
    public function testLogging()
82
    {
83
        $configuration = $this->getConfigs([
84
            'clients' => [
85
                'logging_enabled' => [
86
                    'url' => 'http://localhost:9200',
87
                    'logger' => true,
88
                ],
89
                'logging_disabled' => [
90
                    'url' => 'http://localhost:9200',
91
                    'logger' => false,
92
                ],
93
                'logging_not_mentioned' => [
94
                    'url' => 'http://localhost:9200',
95
                ],
96
                'logging_custom' => [
97
                    'url' => 'http://localhost:9200',
98
                    'logger' => 'custom.service',
99
                ],
100
            ],
101
        ]);
102
103
        $this->assertCount(4, $configuration['clients']);
104
105
        $this->assertSame('fos_elastica.logger', $configuration['clients']['logging_enabled']['connections'][0]['logger']);
106
        $this->assertFalse($configuration['clients']['logging_disabled']['connections'][0]['logger']);
107
        $this->assertSame('fos_elastica.logger', $configuration['clients']['logging_not_mentioned']['connections'][0]['logger']);
108
        $this->assertSame('custom.service', $configuration['clients']['logging_custom']['connections'][0]['logger']);
109
    }
110
111
    public function testSlashIsAddedAtTheEndOfServerUrl()
112
    {
113
        $config = [
114
            'clients' => [
115
                'default' => ['url' => 'http://www.github.com'],
116
            ],
117
        ];
118
        $configuration = $this->getConfigs($config);
119
120
        $this->assertSame('http://www.github.com/', $configuration['clients']['default']['connections'][0]['url']);
121
    }
122
123
    public function testTypeConfig()
124
    {
125
        $this->getConfigs([
126
            'clients' => [
127
                'default' => ['url' => 'http://localhost:9200'],
128
            ],
129
            'indexes' => [
130
                'test' => [
131
                    'type_prototype' => [
132
                        'analyzer' => 'custom_analyzer',
133
                        'persistence' => [
134
                            'identifier' => 'ID',
135
                        ],
136
                        'serializer' => [
137
                            'groups' => ['Search'],
138
                            'version' => 1,
139
                            'serialize_null' => false,
140
                        ],
141
                    ],
142
                    'types' => [
143
                        'test' => [
144
                            'properties' => [
145
                                'title' => [],
146
                                'published' => ['type' => 'datetime'],
147
                                'body' => null,
148
                            ],
149
                            'persistence' => [
150
                                'listener' => [
151
                                    'logger' => true,
152
                                ],
153
                            ],
154
                        ],
155
                        'test2' => [
156
                            'properties' => [
157
                                'title' => null,
158
                                'children' => [
159
                                    'type' => 'nested',
160
                                ],
161
                            ],
162
                        ],
163
                    ],
164
                ],
165
            ],
166
        ]);
167
    }
168
169
    public function testClientConfigurationNoUrl()
170
    {
171
        $configuration = $this->getConfigs([
172
            'clients' => [
173
                'default' => [
174
                    'host' => 'localhost',
175
                    'port' => 9200,
176
                ],
177
            ],
178
        ]);
179
180
        $this->assertTrue(empty($configuration['clients']['default']['connections'][0]['url']));
181
    }
182
183
    public function testUnconfiguredType()
184
    {
185
        $configuration = $this->getConfigs([
186
                'clients' => [
187
                    'default' => ['url' => 'http://localhost:9200'],
188
                ],
189
                'indexes' => [
190
                    'test' => [
191
                        'types' => [
192
                            'test' => null,
193
                        ],
194
                    ],
195
                ],
196
            ]);
197
198
        $this->assertArrayHasKey('properties', $configuration['indexes']['test']['types']['test']);
199
    }
200
201
    public function testNestedProperties()
202
    {
203
        $this->getConfigs([
204
            'clients' => [
205
                'default' => ['url' => 'http://localhost:9200'],
206
            ],
207
            'indexes' => [
208
                'test' => [
209
                    'types' => [
210
                        'user' => [
211
                            'properties' => [
212
                                'field1' => [],
213
                            ],
214
                            'persistence' => [],
215
                        ],
216
                        'user_profile' => [
217
                            '_parent' => [
218
                                'type' => 'user',
219
                            ],
220
                            'properties' => [
221
                                'field1' => [],
222
                                'field2' => [
223
                                    'type' => 'nested',
224
                                    'properties' => [
225
                                        'nested_field1' => [
226
                                            'type' => 'integer',
227
                                        ],
228
                                        'nested_field2' => [
229
                                            'type' => 'object',
230
                                            'properties' => [
231
                                                'id' => [
232
                                                    'type' => 'integer',
233
                                                ],
234
                                            ],
235
                                        ],
236
                                    ],
237
                                ],
238
                            ],
239
                        ],
240
                    ],
241
                ],
242
            ],
243
        ]);
244
    }
245
246
    public function testCompressionConfig()
247
    {
248
        $configuration = $this->getConfigs([
249
            'clients' => [
250
                'compression_enabled' => [
251
                    'compression' => true,
252
                ],
253
                'compression_disabled' => [
254
                    'compression' => false,
255
                ],
256
            ],
257
        ]);
258
259
        $this->assertTrue($configuration['clients']['compression_enabled']['connections'][0]['compression']);
260
        $this->assertFalse($configuration['clients']['compression_disabled']['connections'][0]['compression']);
261
    }
262
263
    public function testCompressionDefaultConfig()
264
    {
265
        $configuration = $this->getConfigs([
266
            'clients' => [
267
                'default' => [],
268
            ],
269
        ]);
270
271
        $this->assertFalse($configuration['clients']['default']['connections'][0]['compression']);
272
    }
273
274
    public function testTimeoutConfig()
275
    {
276
        $configuration = $this->getConfigs([
277
            'clients' => [
278
                'simple_timeout' => [
279
                    'url' => 'http://localhost:9200',
280
                    'timeout' => 123,
281
                ],
282
                'connect_timeout' => [
283
                    'url' => 'http://localhost:9200',
284
                    'connectTimeout' => 234,
285
                ],
286
            ],
287
        ]);
288
289
        $this->assertSame(123, $configuration['clients']['simple_timeout']['connections'][0]['timeout']);
290
        $this->assertSame(234, $configuration['clients']['connect_timeout']['connections'][0]['connectTimeout']);
291
    }
292
293
    public function testAWSConfig()
294
    {
295
        $configuration = $this->getConfigs([
296
            'clients' => [
297
                'default' => [
298
                    'aws_access_key_id' => 'AWS_KEY',
299
                    'aws_secret_access_key' => 'AWS_SECRET',
300
                    'aws_region' => 'AWS_REGION',
301
                    'aws_session_token' => 'AWS_SESSION_TOKEN',
302
                    'ssl' => true,
303
                ],
304
            ],
305
        ]);
306
307
        $connection = $configuration['clients']['default']['connections'][0];
308
        $this->assertSame('AWS_KEY', $connection['aws_access_key_id']);
309
        $this->assertSame('AWS_SECRET', $connection['aws_secret_access_key']);
310
        $this->assertSame('AWS_REGION', $connection['aws_region']);
311
        $this->assertSame('AWS_SESSION_TOKEN', $connection['aws_session_token']);
312
        $this->assertTrue($connection['ssl']);
313
    }
314
315
    private function getConfigs(array $configArray)
316
    {
317
        $configuration = new Configuration(true);
318
319
        return $this->processor->processConfiguration($configuration, [$configArray]);
320
    }
321
}
322