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 |
||
7 | class ConfigReaderTest extends \PHPUnit_Framework_TestCase { |
||
8 | |||
9 | protected $config = [ |
||
10 | "system" => [ |
||
11 | "instanceid" => "oc8v9kkjo6bh", |
||
12 | "ldapIgnoreNamingRules" => false, |
||
13 | ], |
||
14 | "apps" => [ |
||
15 | "backgroundjob" => [ |
||
16 | "lastjob" => "3" |
||
17 | ], |
||
18 | "core" => [ |
||
19 | "installedat" => "1423763974.698", |
||
20 | "lastupdatedat" => "1450277990", |
||
21 | "lastcron" => "1444753126", |
||
22 | "OC_Channel" => "beta", |
||
23 | ], |
||
24 | "dav" => [ |
||
25 | "installed_version" => "0.1.3", |
||
26 | "types" => "filesystem", |
||
27 | "enabled" => "yes" |
||
28 | ] |
||
29 | ] |
||
30 | ]; |
||
31 | |||
32 | public function testGetByPathProvider(){ |
||
33 | return [ |
||
34 | [ 'apps.core.OC_Channel', 'beta'] |
||
35 | ]; |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * @dataProvider testGetByPathProvider |
||
40 | */ |
||
41 | public function testGetByPath($key, $expected){ |
||
42 | $occRunnerMock = $this->getOccRunnerMock(json_encode($this->config)); |
||
43 | $configReader = new ConfigReader($occRunnerMock); |
||
44 | $configReader->init(); |
||
45 | $value = $configReader->getByPath($key); |
||
46 | $this->assertEquals($expected, $value); |
||
47 | } |
||
48 | |||
49 | protected function getOccRunnerMock($result){ |
||
50 | $runnerMock = $this->getMockBuilder('Owncloud\Updater\Utils\OccRunner') |
||
51 | ->setMethods(['run']) |
||
52 | ->disableOriginalConstructor() |
||
53 | ->getMock() |
||
54 | ; |
||
55 | $runnerMock |
||
56 | ->expects($this->any()) |
||
57 | ->method('run') |
||
58 | ->willReturn($result) |
||
59 | ; |
||
60 | return $runnerMock; |
||
61 | } |
||
62 | |||
63 | } |
||
64 |