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 |
||
14 | class ConfigurationTest extends \PHPUnit_Framework_TestCase |
||
15 | { |
||
16 | private $configuration; |
||
17 | |||
18 | public function setUp() |
||
19 | { |
||
20 | $this->configuration = new Configuration(); |
||
21 | } |
||
22 | |||
23 | public function testEmptyConfiguration() |
||
24 | { |
||
25 | $processed = $this->process([]); |
||
26 | |||
27 | $this->assertEquals([ |
||
28 | 'clients' => [] |
||
29 | ], $processed); |
||
30 | } |
||
31 | |||
32 | protected function process(array $config) |
||
33 | { |
||
34 | $processor = new Processor(); |
||
35 | |||
36 | return $processor->processConfiguration( |
||
37 | $this->configuration, |
||
38 | $config |
||
39 | ); |
||
40 | } |
||
41 | |||
42 | public function testProcessOneClient() |
||
43 | { |
||
44 | $processed = $this->process([ |
||
45 | [ |
||
46 | 'clients' => [ |
||
47 | 'default' => [ |
||
48 | 'host' => '127.0.0.1', |
||
49 | 'port' => '9200', |
||
50 | ] |
||
51 | ] |
||
52 | ] |
||
53 | ]); |
||
54 | |||
55 | $this->assertArrayHasKey('connections', $processed['clients']['default']); |
||
56 | $this->assertEquals( |
||
57 | '127.0.0.1', |
||
58 | $processed['clients']['default']['connections'][0]['host'] |
||
59 | ); |
||
60 | $this->assertEquals( |
||
61 | '9200', |
||
62 | $processed['clients']['default']['connections'][0]['port'] |
||
63 | ); |
||
64 | } |
||
65 | |||
66 | public function testProcessTwoClients() |
||
67 | { |
||
68 | $processed = $this->process([ |
||
69 | [ |
||
70 | 'clients' => [ |
||
71 | 'default' => [ |
||
72 | 'host' => '127.0.0.1', |
||
73 | 'port' => '9200', |
||
74 | ], |
||
75 | 'my_client' => [ |
||
76 | 'host' => '192.168.0.100', |
||
77 | 'port' => '9201', |
||
78 | ] |
||
79 | ] |
||
80 | ] |
||
81 | ]); |
||
82 | |||
83 | $this->assertArrayHasKey('connections', $processed['clients']['default']); |
||
84 | $this->assertArrayHasKey('connections', $processed['clients']['my_client']); |
||
85 | } |
||
86 | |||
87 | public function testProcessManyConnections() |
||
88 | { |
||
89 | $processed = $this->process([ |
||
90 | [ |
||
91 | 'clients' => [ |
||
92 | 'default' => [ |
||
93 | 'connections' => [ |
||
94 | [ |
||
95 | 'host' => '127.0.0.1', |
||
96 | 'port' => '9200', |
||
97 | ], |
||
98 | [ |
||
99 | 'host' => '192.168.0.100', |
||
100 | 'port' => '9201', |
||
101 | ], |
||
102 | ] |
||
103 | ], |
||
104 | ] |
||
105 | ] |
||
106 | ]); |
||
107 | |||
108 | |||
109 | $this->assertArrayHasKey('connections', $processed['clients']['default']); |
||
110 | $connections = $processed['clients']['default']['connections']; |
||
111 | $this->assertEquals('127.0.0.1', $connections[0]['host']); |
||
112 | $this->assertEquals('9200', $connections[0]['port']); |
||
113 | $this->assertEquals('192.168.0.100', $connections[1]['host']); |
||
114 | $this->assertEquals('9201', $connections[1]['port']); |
||
115 | } |
||
116 | |||
117 | |||
118 | public function testProcessAddDefaultValuesForConnections() |
||
119 | { |
||
120 | $processed = $this->process([ |
||
121 | [ |
||
122 | 'clients' => [ |
||
123 | 'default' => [ |
||
124 | 'connections' => [ |
||
125 | [ |
||
126 | 'host' => '127.0.0.1', |
||
127 | 'port' => '9200', |
||
128 | ] |
||
129 | ] |
||
130 | ], |
||
131 | ] |
||
132 | ] |
||
133 | ]); |
||
134 | |||
135 | $this->assertArrayHasKey('connections', $processed['clients']['default']); |
||
136 | $this->assertNull($processed['clients']['default']['connections'][0]['path']); |
||
137 | $this->assertNull($processed['clients']['default']['connections'][0]['url']); |
||
138 | $this->assertNull($processed['clients']['default']['connections'][0]['proxy']); |
||
139 | $this->assertNull($processed['clients']['default']['connections'][0]['transport']); |
||
140 | $this->assertNull($processed['clients']['default']['connections'][0]['timeout']); |
||
141 | $this->assertTrue($processed['clients']['default']['connections'][0]['persistent']); |
||
142 | } |
||
143 | } |