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() |
|
|
|
|
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
|
|
|
'index_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() |
|
|
|
|
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
|
|
|
'property' => 'owner', |
242
|
|
|
), |
243
|
|
|
'properties' => array( |
244
|
|
|
'field1' => array(), |
245
|
|
|
'field2' => array( |
246
|
|
|
'type' => 'nested', |
247
|
|
|
'properties' => array( |
248
|
|
|
'nested_field1' => array( |
249
|
|
|
'type' => 'integer', |
250
|
|
|
), |
251
|
|
|
'nested_field2' => array( |
252
|
|
|
'type' => 'object', |
253
|
|
|
'properties' => array( |
254
|
|
|
'id' => array( |
255
|
|
|
'type' => 'integer', |
256
|
|
|
), |
257
|
|
|
), |
258
|
|
|
), |
259
|
|
|
), |
260
|
|
|
), |
261
|
|
|
), |
262
|
|
|
), |
263
|
|
|
), |
264
|
|
|
), |
265
|
|
|
), |
266
|
|
|
)); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
public function testCompressionConfig() |
270
|
|
|
{ |
271
|
|
|
$configuration = $this->getConfigs(array( |
272
|
|
|
'clients' => array( |
273
|
|
|
'compression_enabled' => array( |
274
|
|
|
'compression' => true, |
275
|
|
|
), |
276
|
|
|
'compression_disabled' => array( |
277
|
|
|
'compression' => false, |
278
|
|
|
), |
279
|
|
|
), |
280
|
|
|
)); |
281
|
|
|
|
282
|
|
|
$this->assertTrue($configuration['clients']['compression_enabled']['connections'][0]['compression']); |
283
|
|
|
$this->assertFalse($configuration['clients']['compression_disabled']['connections'][0]['compression']); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
public function testCompressionDefaultConfig() |
287
|
|
|
{ |
288
|
|
|
$configuration = $this->getConfigs(array( |
289
|
|
|
'clients' => array( |
290
|
|
|
'default' => array(), |
291
|
|
|
), |
292
|
|
|
)); |
293
|
|
|
|
294
|
|
|
$this->assertFalse($configuration['clients']['default']['connections'][0]['compression']); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
public function testTimeoutConfig() |
298
|
|
|
{ |
299
|
|
|
$configuration = $this->getConfigs(array( |
300
|
|
|
'clients' => array( |
301
|
|
|
'simple_timeout' => array( |
302
|
|
|
'url' => 'http://localhost:9200', |
303
|
|
|
'timeout' => 123, |
304
|
|
|
), |
305
|
|
|
'connect_timeout' => array( |
306
|
|
|
'url' => 'http://localhost:9200', |
307
|
|
|
'connectTimeout' => 234, |
308
|
|
|
), |
309
|
|
|
), |
310
|
|
|
)); |
311
|
|
|
|
312
|
|
|
$this->assertEquals(123, $configuration['clients']['simple_timeout']['connections'][0]['timeout']); |
313
|
|
|
$this->assertEquals(234, $configuration['clients']['connect_timeout']['connections'][0]['connectTimeout']); |
314
|
|
|
} |
315
|
|
|
} |
316
|
|
|
|
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.