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 |
||
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 | ), |
||
48 | 'clustered' => array( |
||
49 | 'connections' => array( |
||
50 | array( |
||
51 | 'url' => 'http://es1:9200', |
||
52 | 'headers' => array( |
||
53 | 'Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==', |
||
54 | ), |
||
55 | ), |
||
56 | array( |
||
57 | 'url' => 'http://es2:9200', |
||
58 | 'headers' => array( |
||
59 | 'Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==', |
||
60 | ), |
||
61 | ), |
||
62 | ), |
||
63 | ), |
||
64 | ), |
||
65 | )); |
||
66 | |||
67 | $this->assertCount(2, $configuration['clients']); |
||
68 | $this->assertCount(1, $configuration['clients']['default']['connections']); |
||
69 | $this->assertCount(0, $configuration['clients']['default']['connections'][0]['headers']); |
||
70 | |||
71 | $this->assertCount(2, $configuration['clients']['clustered']['connections']); |
||
72 | $this->assertEquals('http://es2:9200/', $configuration['clients']['clustered']['connections'][1]['url']); |
||
73 | $this->assertCount(1, $configuration['clients']['clustered']['connections'][1]['headers']); |
||
74 | $this->assertEquals('Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==', $configuration['clients']['clustered']['connections'][0]['headers'][0]); |
||
75 | } |
||
76 | |||
77 | public function testLogging() |
||
78 | { |
||
79 | $configuration = $this->getConfigs(array( |
||
80 | 'clients' => array( |
||
81 | 'logging_enabled' => array( |
||
82 | 'url' => 'http://localhost:9200', |
||
83 | 'logger' => true, |
||
84 | ), |
||
85 | 'logging_disabled' => array( |
||
86 | 'url' => 'http://localhost:9200', |
||
87 | 'logger' => false, |
||
88 | ), |
||
89 | 'logging_not_mentioned' => array( |
||
90 | 'url' => 'http://localhost:9200', |
||
91 | ), |
||
92 | 'logging_custom' => array( |
||
93 | 'url' => 'http://localhost:9200', |
||
94 | 'logger' => 'custom.service', |
||
95 | ), |
||
96 | ), |
||
97 | )); |
||
98 | |||
99 | $this->assertCount(4, $configuration['clients']); |
||
100 | |||
101 | $this->assertEquals('fos_elastica.logger', $configuration['clients']['logging_enabled']['connections'][0]['logger']); |
||
102 | $this->assertFalse($configuration['clients']['logging_disabled']['connections'][0]['logger']); |
||
103 | $this->assertEquals('fos_elastica.logger', $configuration['clients']['logging_not_mentioned']['connections'][0]['logger']); |
||
104 | $this->assertEquals('custom.service', $configuration['clients']['logging_custom']['connections'][0]['logger']); |
||
105 | } |
||
106 | |||
107 | public function testSlashIsAddedAtTheEndOfServerUrl() |
||
108 | { |
||
109 | $config = array( |
||
110 | 'clients' => array( |
||
111 | 'default' => array('url' => 'http://www.github.com'), |
||
112 | ), |
||
113 | ); |
||
114 | $configuration = $this->getConfigs($config); |
||
115 | |||
116 | $this->assertEquals('http://www.github.com/', $configuration['clients']['default']['connections'][0]['url']); |
||
117 | } |
||
118 | |||
119 | public function testTypeConfig() |
||
120 | { |
||
121 | $this->getConfigs(array( |
||
122 | 'clients' => array( |
||
123 | 'default' => array('url' => 'http://localhost:9200'), |
||
124 | ), |
||
125 | 'indexes' => array( |
||
126 | 'test' => array( |
||
127 | 'type_prototype' => array( |
||
128 | 'index_analyzer' => 'custom_analyzer', |
||
129 | 'persistence' => array( |
||
130 | 'identifier' => 'ID', |
||
131 | ), |
||
132 | 'serializer' => array( |
||
133 | 'groups' => array('Search'), |
||
134 | 'version' => 1, |
||
135 | ), |
||
136 | ), |
||
137 | 'types' => array( |
||
138 | 'test' => array( |
||
139 | 'mappings' => array( |
||
140 | 'title' => array(), |
||
141 | 'published' => array('type' => 'datetime'), |
||
142 | 'body' => null, |
||
143 | ), |
||
144 | 'persistence' => array( |
||
145 | 'listener' => array( |
||
146 | 'logger' => true, |
||
147 | ), |
||
148 | ), |
||
149 | ), |
||
150 | 'test2' => array( |
||
151 | 'mappings' => array( |
||
152 | 'title' => null, |
||
153 | 'children' => array( |
||
154 | 'type' => 'nested', |
||
155 | ), |
||
156 | ), |
||
157 | ), |
||
158 | ), |
||
159 | ), |
||
160 | ), |
||
161 | )); |
||
162 | } |
||
163 | |||
164 | public function testClientConfigurationNoUrl() |
||
165 | { |
||
166 | $configuration = $this->getConfigs(array( |
||
167 | 'clients' => array( |
||
168 | 'default' => array( |
||
169 | 'host' => 'localhost', |
||
170 | 'port' => 9200, |
||
171 | ), |
||
172 | ), |
||
173 | )); |
||
174 | |||
175 | $this->assertTrue(empty($configuration['clients']['default']['connections'][0]['url'])); |
||
176 | } |
||
177 | |||
178 | public function testMappingsRenamedToProperties() |
||
179 | { |
||
180 | $configuration = $this->getConfigs(array( |
||
181 | 'clients' => array( |
||
182 | 'default' => array('url' => 'http://localhost:9200'), |
||
183 | ), |
||
184 | 'indexes' => array( |
||
185 | 'test' => array( |
||
186 | 'types' => array( |
||
187 | 'test' => array( |
||
188 | 'mappings' => array( |
||
189 | 'title' => array(), |
||
190 | 'published' => array('type' => 'datetime'), |
||
191 | 'body' => null, |
||
192 | ), |
||
193 | ), |
||
194 | ), |
||
195 | ), |
||
196 | ), |
||
197 | )); |
||
198 | |||
199 | $this->assertCount(3, $configuration['indexes']['test']['types']['test']['properties']); |
||
200 | } |
||
201 | |||
202 | public function testUnconfiguredType() |
||
203 | { |
||
204 | $configuration = $this->getConfigs(array( |
||
205 | 'clients' => array( |
||
206 | 'default' => array('url' => 'http://localhost:9200'), |
||
207 | ), |
||
208 | 'indexes' => array( |
||
209 | 'test' => array( |
||
210 | 'types' => array( |
||
211 | 'test' => null, |
||
212 | ), |
||
213 | ), |
||
214 | ), |
||
215 | )); |
||
216 | |||
217 | $this->assertArrayHasKey('properties', $configuration['indexes']['test']['types']['test']); |
||
218 | } |
||
219 | |||
220 | public function testNestedProperties() |
||
221 | { |
||
222 | $this->getConfigs(array( |
||
223 | 'clients' => array( |
||
224 | 'default' => array('url' => 'http://localhost:9200'), |
||
225 | ), |
||
226 | 'indexes' => array( |
||
227 | 'test' => array( |
||
228 | 'types' => array( |
||
229 | 'user' => array( |
||
230 | 'properties' => array( |
||
231 | 'field1' => array(), |
||
232 | ), |
||
233 | 'persistence' => array(), |
||
234 | ), |
||
235 | 'user_profile' => array( |
||
236 | '_parent' => array( |
||
237 | 'type' => 'user', |
||
238 | 'property' => 'owner', |
||
239 | ), |
||
240 | 'properties' => array( |
||
241 | 'field1' => array(), |
||
242 | 'field2' => array( |
||
243 | 'type' => 'nested', |
||
244 | 'properties' => array( |
||
245 | 'nested_field1' => array( |
||
246 | 'type' => 'integer', |
||
247 | ), |
||
248 | 'nested_field2' => array( |
||
249 | 'type' => 'object', |
||
250 | 'properties' => array( |
||
251 | 'id' => array( |
||
252 | 'type' => 'integer', |
||
253 | ), |
||
254 | ), |
||
255 | ), |
||
256 | ), |
||
257 | ), |
||
258 | ), |
||
259 | ), |
||
260 | ), |
||
261 | ), |
||
262 | ), |
||
263 | )); |
||
264 | } |
||
265 | |||
266 | public function testCompressionConfig() |
||
290 |