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 |
||
| 19 | class ConfigurationTest extends DependencyInjectionTestCase |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * Some basic tests to make sure the configuration is correctly processed in the standard case. |
||
| 23 | * |
||
| 24 | * @param array $sourceConfig |
||
| 25 | * @param array $expectedConfig |
||
| 26 | * @param $expectedExceptionClass |
||
| 27 | * |
||
| 28 | * @dataProvider configurationProvider |
||
| 29 | */ |
||
| 30 | public function testProcessConfig(array $sourceConfig, array $expectedConfig, $expectedExceptionClass) |
||
| 31 | { |
||
| 32 | $processor = new Processor(); |
||
| 33 | |||
| 34 | $exceptionOccurred = false; |
||
| 35 | $exceptionClass = ''; |
||
| 36 | $exceptionMessage = ''; |
||
| 37 | $calculatedConfig = []; |
||
| 38 | try { |
||
| 39 | $calculatedConfig = $processor->processConfiguration(new Configuration(), [$sourceConfig]); |
||
| 40 | } catch (\Exception $e) { |
||
| 41 | $exceptionOccurred = true; |
||
| 42 | $exceptionClass = get_class($e); |
||
| 43 | $exceptionMessage = $e->getMessage(); |
||
| 44 | } |
||
| 45 | |||
| 46 | if ($expectedExceptionClass !== false) { |
||
| 47 | self::assertTrue( |
||
| 48 | $exceptionOccurred, |
||
| 49 | sprintf( |
||
| 50 | '"%s" exception should occurred for "%s".', |
||
| 51 | $expectedExceptionClass, |
||
| 52 | json_encode($sourceConfig) |
||
| 53 | ) |
||
| 54 | ); |
||
| 55 | } |
||
| 56 | |||
| 57 | if ($expectedExceptionClass === false) { |
||
| 58 | self::assertFalse( |
||
| 59 | $exceptionOccurred, |
||
| 60 | sprintf( |
||
| 61 | '"%s" exception occurred but should not for "%s". Message "%s"', |
||
| 62 | $exceptionClass, |
||
| 63 | json_encode($sourceConfig), |
||
| 64 | $exceptionMessage |
||
| 65 | ) |
||
| 66 | ); |
||
| 67 | $this->assertEquals($expectedConfig, $calculatedConfig); |
||
| 68 | } |
||
| 69 | } |
||
| 70 | } |
||
| 71 |