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 | '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 | 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 | 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() |
||
| 293 |