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 |
||
12 | class InigoRendererTest extends PHPUnit_Framework_TestCase |
||
13 | { |
||
14 | /** |
||
15 | * |
||
16 | */ |
||
17 | public function testIntegrity() |
||
18 | { |
||
19 | // Environment |
||
20 | $contentToRender = file_get_contents(__DIR__ . '/examples/inigo/klangbilder.txt'); |
||
21 | $resourceBasePath = __DIR__ . '/examples/inigo/res'; |
||
22 | $filter = 'inigo'; |
||
23 | |||
24 | $config = new Config($resourceBasePath); |
||
25 | |||
26 | $renderer = Factory::createRenderer($filter, $config); |
||
27 | |||
28 | $output = $renderer->render($contentToRender); |
||
29 | |||
30 | $expected = file_get_contents(__DIR__ . '/examples/inigo/klangbilder.expected'); |
||
31 | |||
32 | $expected = str_replace('__RESOURCE_BASE_PATH__', $config->getResourceBasePath(), $expected); |
||
33 | |||
34 | $this->assertEquals($expected, $output); |
||
35 | } |
||
36 | |||
37 | public function runSuiteProvider() |
||
38 | { |
||
39 | $sets = array(); |
||
40 | |||
41 | foreach (glob(__DIR__ . '/examples/inigo/*.txt') as $file) { |
||
42 | $sets[] = array( |
||
43 | realpath($file), |
||
44 | realpath(substr($file, 0, -4) . '.expected') |
||
45 | ); |
||
46 | } |
||
47 | |||
48 | return $sets; |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * @dataProvider runSuiteProvider |
||
53 | */ |
||
54 | public function testRunSuite($fileInput, $fileExpected) |
||
55 | { |
||
56 | $resourceBasePath = __DIR__ . '/examples/inigo/res'; |
||
57 | |||
58 | $config = new Config($resourceBasePath); |
||
59 | |||
60 | $renderer = Factory::createRenderer('inigo', $config); |
||
61 | |||
62 | $output = $renderer->render(file_get_contents($fileInput)); |
||
63 | $expected = file_get_contents($fileExpected); |
||
64 | |||
65 | $expected = str_replace('__RESOURCE_BASE_PATH__', $config->getResourceBasePath(), $expected); |
||
66 | |||
67 | $this->assertEquals($expected, $output); |
||
68 | } |
||
69 | } |
||
70 |