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 |
||
55 | class ViewControllerTest extends \PHPUnit_Framework_TestCase { |
||
56 | |||
57 | private $appName; |
||
58 | private $request; |
||
59 | private $config; |
||
60 | private $userSession; |
||
61 | private $mailer; |
||
62 | private $l10n; |
||
63 | private $defaults; |
||
64 | |||
65 | private $dummyUser; |
||
66 | |||
67 | private $controller; |
||
68 | |||
69 | public function setUp() { |
||
100 | |||
101 | /** |
||
102 | * @dataProvider indexDataProvider |
||
103 | */ |
||
104 | public function testIndex($isAssetPipelineEnabled, $showAssetPipelineError, $serverVersion, $expectsSupportsClass) { |
||
105 | $this->config->expects($this->at(0)) |
||
106 | ->method('getSystemValue') |
||
107 | ->with('version') |
||
108 | ->will($this->returnValue($serverVersion)); |
||
109 | |||
110 | $this->config->expects($this->at(1)) |
||
111 | ->method('getSystemValue') |
||
112 | ->with('asset-pipeline.enabled', false) |
||
113 | ->will($this->returnValue($isAssetPipelineEnabled)); |
||
114 | |||
115 | if ($showAssetPipelineError) { |
||
116 | $actual = $this->controller->index(); |
||
117 | |||
118 | $this->assertInstanceOf('OCP\AppFramework\Http\TemplateResponse', $actual); |
||
119 | $this->assertEquals([], $actual->getParams()); |
||
120 | $this->assertEquals('main-asset-pipeline-unsupported', $actual->getTemplateName()); |
||
121 | } else { |
||
122 | $this->userSession->expects($this->once()) |
||
123 | ->method('getUser') |
||
124 | ->will($this->returnValue($this->dummyUser)); |
||
125 | |||
126 | $this->dummyUser->expects($this->once()) |
||
127 | ->method('getUID') |
||
128 | ->will($this->returnValue('user123')); |
||
129 | |||
130 | $this->dummyUser->expects($this->once()) |
||
131 | ->method('getEMailAddress') |
||
132 | ->will($this->returnValue('[email protected]')); |
||
133 | |||
134 | $this->config->expects($this->at(2)) |
||
135 | ->method('getAppValue') |
||
136 | ->with($this->appName, 'installed_version') |
||
137 | ->will($this->returnValue('42.13.37')); |
||
138 | |||
139 | $this->config->expects($this->at(3)) |
||
140 | ->method('getUserValue') |
||
141 | ->with('user123', $this->appName, 'currentView', 'month') |
||
142 | ->will($this->returnValue('someView')); |
||
143 | |||
144 | $this->config->expects($this->at(4)) |
||
145 | ->method('getUserValue') |
||
146 | ->with('user123', $this->appName, 'skipPopover', 'no') |
||
147 | ->will($this->returnValue('someSkipPopoverValue')); |
||
148 | |||
149 | $this->config->expects($this->at(5)) |
||
150 | ->method('getUserValue') |
||
151 | ->with('user123', $this->appName, 'showWeekNr', 'no') |
||
152 | ->will($this->returnValue('someShowWeekNrValue')); |
||
153 | |||
154 | $this->config->expects($this->at(6)) |
||
155 | ->method('getAppValue') |
||
156 | ->with('theming', 'color', '#0082C9') |
||
157 | ->will($this->returnValue('#ff00ff')); |
||
158 | |||
159 | $actual = $this->controller->index(); |
||
160 | |||
161 | $this->assertInstanceOf('OCP\AppFramework\Http\TemplateResponse', $actual); |
||
162 | $this->assertEquals([ |
||
163 | 'appVersion' => '42.13.37', |
||
164 | 'defaultView' => 'someView', |
||
165 | 'emailAddress' => '[email protected]', |
||
166 | 'skipPopover' => 'someSkipPopoverValue', |
||
167 | 'weekNumbers' => 'someShowWeekNrValue', |
||
168 | 'supportsClass' => $expectsSupportsClass, |
||
169 | 'defaultColor' => '#ff00ff', |
||
170 | 'isPublic' => false |
||
171 | ], $actual->getParams()); |
||
172 | $this->assertEquals('main', $actual->getTemplateName()); |
||
173 | } |
||
174 | |||
175 | } |
||
176 | |||
177 | View Code Duplication | public function indexDataProvider() { |
|
185 | |||
186 | /** |
||
187 | * @dataProvider indexPublicDataProvider |
||
188 | */ |
||
189 | public function testPublicIndex($isAssetPipelineEnabled, $showAssetPipelineError, $serverVersion, $expectsSupportsClass) { |
||
190 | $this->config->expects($this->at(0)) |
||
191 | ->method('getSystemValue') |
||
192 | ->with('version') |
||
193 | ->will($this->returnValue($serverVersion)); |
||
194 | |||
195 | $this->config->expects($this->at(1)) |
||
196 | ->method('getSystemValue') |
||
197 | ->with('asset-pipeline.enabled', false) |
||
198 | ->will($this->returnValue($isAssetPipelineEnabled)); |
||
199 | |||
200 | if ($showAssetPipelineError) { |
||
201 | $actual = $this->controller->index(); |
||
202 | |||
203 | $this->assertInstanceOf('OCP\AppFramework\Http\TemplateResponse', $actual); |
||
204 | $this->assertEquals([], $actual->getParams()); |
||
205 | $this->assertEquals('main-asset-pipeline-unsupported', $actual->getTemplateName()); |
||
206 | } else { |
||
207 | $this->config->expects($this->once()) |
||
208 | ->method('getAppValue') |
||
209 | ->with($this->appName, 'installed_version') |
||
210 | ->will($this->returnValue('42.13.37')); |
||
211 | |||
212 | $actual = $this->controller->publicIndex(); |
||
213 | |||
214 | $this->assertInstanceOf('OCP\AppFramework\Http\TemplateResponse', $actual); |
||
215 | $this->assertEquals([ |
||
216 | 'appVersion' => '42.13.37', |
||
217 | 'defaultView' => 'month', |
||
218 | 'emailAddress' => '', |
||
219 | 'supportsClass' => $expectsSupportsClass, |
||
220 | 'isPublic' => true |
||
221 | ], $actual->getParams()); |
||
222 | $this->assertEquals('main', $actual->getTemplateName()); |
||
223 | } |
||
224 | |||
225 | } |
||
226 | |||
227 | View Code Duplication | public function indexPublicDataProvider() { |
|
228 | return [ |
||
229 | [true, true, '9.0.5.2', false], |
||
230 | [true, false, '9.1.0.0', true], |
||
231 | [false, false, '9.0.5.2', false], |
||
232 | [false, false, '9.1.0.0', true] |
||
233 | ]; |
||
234 | } |
||
235 | |||
236 | public function testGetTimezone() { |
||
242 | |||
243 | public function testGetTimezoneWithFakeTz() { |
||
248 | |||
249 | public function testGetTimezoneWithRegion() { |
||
255 | |||
256 | public function testGetTimezoneWithRegionWithFakeTz() { |
||
261 | |||
262 | /** |
||
263 | * @dataProvider indexEmailPublicLink |
||
264 | */ |
||
265 | public function testEmailPublicLink($to, $url, $name) { |
||
276 | |||
277 | public function indexEmailPublicLink() { |
||
283 | } |
||
284 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.