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 |
||
| 18 | class ConfigurationTest extends \Phpunit_Framework_TestCase |
||
| 19 | { |
||
| 20 | public function testImplementsConfigurationInterface() |
||
| 21 | { |
||
| 22 | $rc = new \ReflectionClass('Liip\ImagineBundle\DependencyInjection\Configuration'); |
||
| 23 | |||
| 24 | $this->assertTrue($rc->implementsInterface('Symfony\Component\Config\Definition\ConfigurationInterface')); |
||
| 25 | } |
||
| 26 | |||
| 27 | public function testCouldBeConstructedWithResolversAndLoadersFactoriesAsArguments() |
||
| 28 | { |
||
| 29 | new Configuration(array(), array()); |
||
| 30 | } |
||
| 31 | |||
| 32 | public function testInjectLoaderFactoryConfig() |
||
| 33 | { |
||
| 34 | $config = $this->processConfiguration( |
||
| 35 | new Configuration( |
||
| 36 | array( |
||
| 37 | new WebPathResolverFactory(), |
||
| 38 | ), |
||
| 39 | array( |
||
| 40 | new FooLoaderFactory(), |
||
| 41 | new FileSystemLoaderFactory(), |
||
| 42 | ) |
||
| 43 | ), |
||
| 44 | array(array( |
||
| 45 | 'loaders' => array( |
||
| 46 | 'aLoader' => array( |
||
| 47 | 'foo' => array( |
||
| 48 | 'foo_option' => 'theValue', |
||
| 49 | ), |
||
| 50 | ), |
||
| 51 | ), |
||
| 52 | |||
| 53 | )) |
||
| 54 | ); |
||
| 55 | |||
| 56 | $this->assertArrayHasKey('loaders', $config); |
||
| 57 | $this->assertArrayHasKey('aLoader', $config['loaders']); |
||
| 58 | $this->assertArrayHasKey('foo', $config['loaders']['aLoader']); |
||
| 59 | $this->assertArrayHasKey('foo_option', $config['loaders']['aLoader']['foo']); |
||
| 60 | $this->assertEquals('theValue', $config['loaders']['aLoader']['foo']['foo_option']); |
||
| 61 | } |
||
| 62 | |||
| 63 | public function testAllowToUseLoaderFactorySeveralTimes() |
||
| 64 | { |
||
| 65 | $config = $this->processConfiguration( |
||
| 66 | new Configuration( |
||
| 67 | array( |
||
| 68 | new WebPathResolverFactory(), |
||
| 69 | ), |
||
| 70 | array( |
||
| 71 | new FooLoaderFactory(), |
||
| 72 | new FileSystemLoaderFactory(), |
||
| 73 | ) |
||
| 74 | ), |
||
| 75 | array(array( |
||
| 76 | 'loaders' => array( |
||
| 77 | 'aLoader' => array( |
||
| 78 | 'foo' => array( |
||
| 79 | 'foo_option' => 'theValue', |
||
| 80 | ), |
||
| 81 | ), |
||
| 82 | 'anotherLoader' => array( |
||
| 83 | 'foo' => array( |
||
| 84 | 'foo_option' => 'theValue', |
||
| 85 | ), |
||
| 86 | ), |
||
| 87 | ), |
||
| 88 | |||
| 89 | )) |
||
| 90 | ); |
||
| 91 | |||
| 92 | $this->assertArrayHasKey('loaders', $config); |
||
| 93 | $this->assertArrayHasKey('aLoader', $config['loaders']); |
||
| 94 | $this->assertArrayHasKey('anotherLoader', $config['loaders']); |
||
| 95 | } |
||
| 96 | |||
| 97 | public function testSetFilesystemLoaderAsDefaultLoaderIfNotDefined() |
||
| 98 | { |
||
| 99 | $config = $this->processConfiguration( |
||
| 100 | new Configuration( |
||
| 101 | array( |
||
| 102 | new WebPathResolverFactory(), |
||
| 103 | ), |
||
| 104 | array( |
||
| 105 | new FileSystemLoaderFactory(), |
||
| 106 | ) |
||
| 107 | ), |
||
| 108 | array(array( |
||
| 109 | 'loaders' => array( |
||
| 110 | ), |
||
| 111 | )) |
||
| 112 | ); |
||
| 113 | |||
| 114 | $this->assertArrayHasKey('loaders', $config); |
||
| 115 | $this->assertArrayHasKey('default', $config['loaders']); |
||
| 116 | $this->assertArrayHasKey('filesystem', $config['loaders']['default']); |
||
| 117 | } |
||
| 118 | |||
| 119 | public function testSetFilesystemLoaderAsDefaultLoaderIfNull() |
||
| 120 | { |
||
| 121 | $config = $this->processConfiguration( |
||
| 122 | new Configuration( |
||
| 123 | array( |
||
| 124 | new WebPathResolverFactory(), |
||
| 125 | ), |
||
| 126 | array( |
||
| 127 | new FileSystemLoaderFactory(), |
||
| 128 | ) |
||
| 129 | ), |
||
| 130 | array(array( |
||
| 131 | 'loaders' => null, |
||
| 132 | )) |
||
| 133 | ); |
||
| 134 | |||
| 135 | $this->assertArrayHasKey('loaders', $config); |
||
| 136 | $this->assertArrayHasKey('default', $config['loaders']); |
||
| 137 | $this->assertArrayHasKey('filesystem', $config['loaders']['default']); |
||
| 138 | } |
||
| 139 | |||
| 140 | public function testThrowIfLoadersNotArray() |
||
| 141 | { |
||
| 142 | $this->setExpectedException('LogicException', 'Loaders has to be array'); |
||
| 143 | $this->processConfiguration( |
||
| 144 | new Configuration( |
||
| 145 | array( |
||
| 146 | new WebPathResolverFactory(), |
||
| 147 | ), |
||
| 148 | array( |
||
| 149 | new FileSystemLoaderFactory(), |
||
| 150 | ) |
||
| 151 | ), |
||
| 152 | array(array( |
||
| 153 | 'loaders' => 'not_array', |
||
| 154 | )) |
||
| 155 | ); |
||
| 156 | } |
||
| 157 | |||
| 158 | public function testSetFilesystemLoaderAsDefaultIfLoadersSectionNotDefined() |
||
| 159 | { |
||
| 160 | $config = $this->processConfiguration( |
||
| 161 | new Configuration( |
||
| 162 | array( |
||
| 163 | new WebPathResolverFactory(), |
||
| 164 | ), |
||
| 165 | array( |
||
| 166 | new FileSystemLoaderFactory(), |
||
| 167 | ) |
||
| 168 | ), |
||
| 169 | array(array()) |
||
| 170 | ); |
||
| 171 | |||
| 172 | $this->assertArrayHasKey('loaders', $config); |
||
| 173 | $this->assertArrayHasKey('default', $config['loaders']); |
||
| 174 | $this->assertArrayHasKey('filesystem', $config['loaders']['default']); |
||
| 175 | } |
||
| 176 | |||
| 177 | public function testSetWebPathResolversAsDefaultIfResolversSectionNotDefined() |
||
| 178 | { |
||
| 179 | $config = $this->processConfiguration( |
||
| 180 | new Configuration( |
||
| 181 | array( |
||
| 182 | new WebPathResolverFactory(), |
||
| 183 | ), |
||
| 184 | array( |
||
| 185 | new FileSystemLoaderFactory(), |
||
| 186 | ) |
||
| 187 | ), |
||
| 188 | array(array()) |
||
| 189 | ); |
||
| 190 | |||
| 191 | $this->assertArrayHasKey('resolvers', $config); |
||
| 192 | $this->assertArrayHasKey('default', $config['resolvers']); |
||
| 193 | $this->assertArrayHasKey('web_path', $config['resolvers']['default']); |
||
| 194 | } |
||
| 195 | |||
| 196 | public function testShouldNotOverwriteDefaultLoaderIfDefined() |
||
| 197 | { |
||
| 198 | $config = $this->processConfiguration( |
||
| 199 | new Configuration( |
||
| 200 | array( |
||
| 201 | new WebPathResolverFactory(), |
||
| 202 | ), |
||
| 203 | array( |
||
| 204 | new FooLoaderFactory(), |
||
| 205 | new FileSystemLoaderFactory(), |
||
| 206 | ) |
||
| 207 | ), |
||
| 208 | array(array( |
||
| 209 | 'loaders' => array( |
||
| 210 | 'default' => array( |
||
| 211 | 'foo' => array( |
||
| 212 | 'foo_option' => 'theValue', |
||
| 213 | ), |
||
| 214 | ), |
||
| 215 | ), |
||
| 216 | |||
| 217 | )) |
||
| 218 | ); |
||
| 219 | |||
| 220 | $this->assertArrayHasKey('loaders', $config); |
||
| 221 | $this->assertArrayHasKey('default', $config['loaders']); |
||
| 222 | $this->assertArrayHasKey('foo', $config['loaders']['default']); |
||
| 223 | } |
||
| 224 | |||
| 225 | public function testInjectResolverFactoryConfig() |
||
| 226 | { |
||
| 227 | $config = $this->processConfiguration( |
||
| 228 | new Configuration( |
||
| 229 | array( |
||
| 230 | new BarResolverFactory(), |
||
| 231 | new WebPathResolverFactory(), |
||
| 232 | ), array( |
||
| 233 | new FileSystemLoaderFactory(), |
||
| 234 | ) |
||
| 235 | ), |
||
| 236 | array(array( |
||
| 237 | 'resolvers' => array( |
||
| 238 | 'aResolver' => array( |
||
| 239 | 'bar' => array( |
||
| 240 | 'bar_option' => 'theValue', |
||
| 241 | ), |
||
| 242 | ), |
||
| 243 | ), |
||
| 244 | |||
| 245 | )) |
||
| 246 | ); |
||
| 247 | |||
| 248 | $this->assertArrayHasKey('resolvers', $config); |
||
| 249 | $this->assertArrayHasKey('aResolver', $config['resolvers']); |
||
| 250 | $this->assertArrayHasKey('bar', $config['resolvers']['aResolver']); |
||
| 251 | $this->assertArrayHasKey('bar_option', $config['resolvers']['aResolver']['bar']); |
||
| 252 | $this->assertEquals('theValue', $config['resolvers']['aResolver']['bar']['bar_option']); |
||
| 253 | } |
||
| 254 | |||
| 255 | public function testAllowToUseResolverFactorySeveralTimes() |
||
| 256 | { |
||
| 257 | $config = $this->processConfiguration( |
||
| 258 | new Configuration( |
||
| 259 | array( |
||
| 260 | new BarResolverFactory(), |
||
| 261 | new WebPathResolverFactory(), |
||
| 262 | ), |
||
| 263 | array( |
||
| 264 | new FileSystemLoaderFactory(), |
||
| 265 | ) |
||
| 266 | ), |
||
| 267 | array(array( |
||
| 268 | 'resolvers' => array( |
||
| 269 | 'aResolver' => array( |
||
| 270 | 'bar' => array( |
||
| 271 | 'bar_option' => 'theValue', |
||
| 272 | ), |
||
| 273 | ), |
||
| 274 | 'anotherResolver' => array( |
||
| 275 | 'bar' => array( |
||
| 276 | 'bar_option' => 'theValue', |
||
| 277 | ), |
||
| 278 | ), |
||
| 279 | ), |
||
| 280 | |||
| 281 | )) |
||
| 282 | ); |
||
| 283 | |||
| 284 | $this->assertArrayHasKey('resolvers', $config); |
||
| 285 | $this->assertArrayHasKey('aResolver', $config['resolvers']); |
||
| 286 | $this->assertArrayHasKey('anotherResolver', $config['resolvers']); |
||
| 287 | } |
||
| 288 | |||
| 289 | public function testSetWebPathAsDefaultResolverIfNotDefined() |
||
| 290 | { |
||
| 291 | $config = $this->processConfiguration( |
||
| 292 | new Configuration( |
||
| 293 | array( |
||
| 294 | new WebPathResolverFactory(), |
||
| 295 | ), array( |
||
| 296 | new FileSystemLoaderFactory(), |
||
| 297 | ) |
||
| 298 | ), |
||
| 299 | array(array( |
||
| 300 | 'resolvers' => array( |
||
| 301 | ), |
||
| 302 | )) |
||
| 303 | ); |
||
| 304 | |||
| 305 | $this->assertArrayHasKey('resolvers', $config); |
||
| 306 | $this->assertArrayHasKey('default', $config['resolvers']); |
||
| 307 | $this->assertArrayHasKey('web_path', $config['resolvers']['default']); |
||
| 308 | } |
||
| 309 | |||
| 310 | public function testSetWebPathAsDefaultResolverIfNull() |
||
| 311 | { |
||
| 312 | $config = $this->processConfiguration( |
||
| 313 | new Configuration( |
||
| 314 | array( |
||
| 315 | new WebPathResolverFactory(), |
||
| 316 | ), array( |
||
| 317 | new FileSystemLoaderFactory(), |
||
| 318 | ) |
||
| 319 | ), |
||
| 320 | array(array( |
||
| 321 | 'resolvers' => null, |
||
| 322 | )) |
||
| 323 | ); |
||
| 324 | |||
| 325 | $this->assertArrayHasKey('resolvers', $config); |
||
| 326 | $this->assertArrayHasKey('default', $config['resolvers']); |
||
| 327 | $this->assertArrayHasKey('web_path', $config['resolvers']['default']); |
||
| 328 | } |
||
| 329 | |||
| 330 | public function testThrowsIfResolversNotArray() |
||
| 331 | { |
||
| 332 | $this->setExpectedException('LogicException', 'Resolvers has to be array'); |
||
| 333 | $config = $this->processConfiguration( |
||
| 334 | new Configuration( |
||
| 335 | array( |
||
| 336 | new WebPathResolverFactory(), |
||
| 337 | ), array( |
||
| 338 | new FileSystemLoaderFactory(), |
||
| 339 | ) |
||
| 340 | ), |
||
| 341 | array(array( |
||
| 342 | 'resolvers' => 'not_array', |
||
| 343 | )) |
||
| 344 | ); |
||
| 345 | |||
| 346 | $this->assertArrayHasKey('resolvers', $config); |
||
| 347 | $this->assertArrayHasKey('default', $config['resolvers']); |
||
| 348 | $this->assertArrayHasKey('web_path', $config['resolvers']['default']); |
||
| 349 | } |
||
| 350 | |||
| 351 | public function testShouldNotOverwriteDefaultResolverIfDefined() |
||
| 352 | { |
||
| 353 | $config = $this->processConfiguration( |
||
| 354 | new Configuration( |
||
| 355 | array( |
||
| 356 | new BarResolverFactory(), |
||
| 357 | new WebPathResolverFactory(), |
||
| 358 | ), |
||
| 359 | array( |
||
| 360 | new FileSystemLoaderFactory(), |
||
| 361 | ) |
||
| 362 | ), |
||
| 363 | array(array( |
||
| 364 | 'resolvers' => array( |
||
| 365 | 'default' => array( |
||
| 366 | 'bar' => array( |
||
| 367 | 'bar_option' => 'theValue', |
||
| 368 | ), |
||
| 369 | ), |
||
| 370 | ), |
||
| 371 | |||
| 372 | )) |
||
| 373 | ); |
||
| 374 | |||
| 375 | $this->assertArrayHasKey('resolvers', $config); |
||
| 376 | $this->assertArrayHasKey('default', $config['resolvers']); |
||
| 377 | $this->assertArrayHasKey('bar', $config['resolvers']['default']); |
||
| 378 | } |
||
| 379 | |||
| 380 | public function testNewFilterQualitySettings() |
||
| 381 | { |
||
| 382 | $config = $this->processConfiguration( |
||
| 383 | new Configuration( |
||
| 384 | array( |
||
| 385 | new BarResolverFactory(), |
||
| 386 | new WebPathResolverFactory(), |
||
| 387 | ), |
||
| 388 | array( |
||
| 389 | new FileSystemLoaderFactory(), |
||
| 390 | ) |
||
| 391 | ), |
||
| 392 | array(array( |
||
| 393 | 'filter_sets' => array( |
||
| 394 | 'test' => array( |
||
| 395 | 'jpeg_quality' => 70, |
||
| 396 | 'png_compression_level' => 9, |
||
| 397 | 'png_compression_filter' => PNG_ALL_FILTERS, |
||
| 398 | ), |
||
| 399 | ), |
||
| 400 | )) |
||
| 401 | ); |
||
| 402 | |||
| 403 | $this->assertArrayHasKey('filter_sets', $config); |
||
| 404 | $this->assertArrayHasKey('test', $config['filter_sets']); |
||
| 405 | $this->assertArrayHasKey('jpeg_quality', $config['filter_sets']['test']); |
||
| 406 | $this->assertEquals(70, $config['filter_sets']['test']['jpeg_quality']); |
||
| 407 | $this->assertArrayHasKey('png_compression_level', $config['filter_sets']['test']); |
||
| 408 | $this->assertEquals(9, $config['filter_sets']['test']['png_compression_level']); |
||
| 409 | $this->assertArrayHasKey('png_compression_filter', $config['filter_sets']['test']); |
||
| 410 | $this->assertEquals(PNG_ALL_FILTERS, $config['filter_sets']['test']['png_compression_filter']); |
||
| 411 | } |
||
| 412 | |||
| 413 | /** |
||
| 414 | * @param ConfigurationInterface $configuration |
||
| 415 | * @param array $configs |
||
| 416 | * |
||
| 417 | * @return array |
||
| 418 | */ |
||
| 419 | protected function processConfiguration(ConfigurationInterface $configuration, array $configs) |
||
| 420 | { |
||
| 421 | $processor = new Processor(); |
||
| 422 | |||
| 423 | return $processor->processConfiguration($configuration, $configs); |
||
| 424 | } |
||
| 425 | } |
||
| 426 | |||
| 468 |