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 |
||
24 | class SettingsControllerTest extends \PHPUnit_Framework_TestCase { |
||
25 | |||
26 | private $appName; |
||
27 | private $request; |
||
28 | private $userSession; |
||
29 | private $config; |
||
30 | |||
31 | private $dummyUser; |
||
32 | |||
33 | private $controller; |
||
34 | |||
35 | public function setUp() { |
||
36 | $this->appName = 'calendar'; |
||
37 | $this->request = $this->getMockBuilder('\OCP\IRequest') |
||
38 | ->disableOriginalConstructor() |
||
39 | ->getMock(); |
||
40 | $this->userSession = $this->getMockBuilder('OCP\IUserSession') |
||
41 | ->disableOriginalConstructor() |
||
42 | ->getMock(); |
||
43 | $this->config = $this->getMockBuilder('OCP\IConfig') |
||
44 | ->disableOriginalConstructor() |
||
45 | ->getMock(); |
||
46 | |||
47 | $this->dummyUser = $this->getMockBuilder('OCP\IUser') |
||
48 | ->disableOriginalConstructor() |
||
49 | ->getMock(); |
||
50 | |||
51 | $this->userSession->expects($this->once()) |
||
52 | ->method('getUser') |
||
53 | ->will($this->returnValue($this->dummyUser)); |
||
54 | |||
55 | $this->dummyUser->expects($this->once()) |
||
56 | ->method('getUID') |
||
57 | ->will($this->returnValue('user123')); |
||
58 | |||
59 | $this->controller = new SettingsController($this->appName, |
||
60 | $this->request, $this->userSession, $this->config); |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * @dataProvider setViewWithAllowedViewDataProvider |
||
65 | */ |
||
66 | View Code Duplication | public function testSetViewWithAllowedView($view) { |
|
|
|||
67 | $this->config->expects($this->once()) |
||
68 | ->method('setUserValue') |
||
69 | ->with('user123', $this->appName, 'currentView', $view); |
||
70 | |||
71 | $actual = $this->controller->setConfig('view', $view); |
||
72 | |||
73 | $this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual); |
||
74 | $this->assertEquals([], $actual->getData()); |
||
75 | $this->assertEquals(200, $actual->getStatus()); |
||
76 | } |
||
77 | |||
78 | public function setViewWithAllowedViewDataProvider() { |
||
79 | return [ |
||
80 | ['agendaDay'], |
||
81 | ['agendaWeek'], |
||
82 | ['month'], |
||
83 | ]; |
||
84 | } |
||
85 | |||
86 | View Code Duplication | public function testSetViewWithForbiddenView() { |
|
87 | $actual = $this->controller->setConfig('view','someForbiddenView'); |
||
88 | |||
89 | $this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual); |
||
90 | $this->assertEquals([], $actual->getData()); |
||
91 | $this->assertEquals(422, $actual->getStatus()); |
||
92 | } |
||
93 | |||
94 | View Code Duplication | public function testSetViewWithConfigException() { |
|
95 | $this->config->expects($this->once()) |
||
96 | ->method('setUserValue') |
||
97 | ->with('user123', $this->appName, 'currentView', 'month') |
||
98 | ->will($this->throwException(new \Exception)); |
||
99 | |||
100 | $actual = $this->controller->setConfig('view', 'month'); |
||
101 | |||
102 | $this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual); |
||
103 | $this->assertEquals([], $actual->getData()); |
||
104 | $this->assertEquals(500, $actual->getStatus()); |
||
105 | } |
||
106 | |||
107 | public function testGetView() { |
||
108 | $this->config->expects($this->once()) |
||
109 | ->method('getUserValue') |
||
110 | ->with('user123', $this->appName, 'currentView', 'month') |
||
111 | ->will($this->returnValue('agendaWeek')); |
||
112 | |||
113 | $actual = $this->controller->getConfig('view'); |
||
114 | |||
115 | $this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual); |
||
116 | $this->assertEquals(['value' => 'agendaWeek'], $actual->getData()); |
||
117 | $this->assertEquals(200, $actual->getStatus()); |
||
118 | } |
||
119 | |||
120 | View Code Duplication | public function testGetViewWithConfigException() { |
|
121 | $this->config->expects($this->once()) |
||
122 | ->method('getUserValue') |
||
123 | ->with('user123', $this->appName, 'currentView', 'month') |
||
124 | ->will($this->throwException(new \Exception)); |
||
125 | |||
126 | $actual = $this->controller->getConfig('view'); |
||
127 | |||
128 | $this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual); |
||
129 | $this->assertEquals([], $actual->getData()); |
||
130 | $this->assertEquals(500, $actual->getStatus()); |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * @dataProvider setPopoverWithAllowedValueDataProvider |
||
135 | */ |
||
136 | View Code Duplication | public function testSetPopoverWithAllowedValue($value) { |
|
137 | $this->config->expects($this->once()) |
||
138 | ->method('setUserValue') |
||
139 | ->with('user123', $this->appName, 'skipPopover', $value); |
||
140 | |||
141 | $actual = $this->controller->setConfig('skipPopover', $value); |
||
142 | |||
143 | $this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual); |
||
144 | $this->assertEquals([], $actual->getData()); |
||
145 | $this->assertEquals(200, $actual->getStatus()); |
||
146 | } |
||
147 | |||
148 | public function setPopoverWithAllowedValueDataProvider() { |
||
149 | return [ |
||
150 | ['yes'], |
||
151 | ['no'] |
||
152 | ]; |
||
153 | } |
||
154 | |||
155 | View Code Duplication | public function testSetPopoverWithForbiddenValue() { |
|
156 | $actual = $this->controller->setConfig('skipPopover','someForbiddenValue'); |
||
157 | |||
158 | $this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual); |
||
159 | $this->assertEquals([], $actual->getData()); |
||
160 | $this->assertEquals(422, $actual->getStatus()); |
||
161 | } |
||
162 | |||
163 | public function testSetPopoverWithConfigException() { |
||
164 | $this->config->expects($this->once()) |
||
165 | ->method('setUserValue') |
||
166 | ->with('user123', $this->appName, 'skipPopover', 'no') |
||
167 | ->will($this->throwException(new \Exception)); |
||
168 | |||
169 | $actual = $this->controller->setConfig('skipPopover', 'no'); |
||
170 | |||
171 | $this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual); |
||
172 | $this->assertEquals([], $actual->getData()); |
||
173 | $this->assertEquals(500, $actual->getStatus()); |
||
174 | } |
||
175 | |||
176 | public function testGetPopover() { |
||
177 | $this->config->expects($this->once()) |
||
178 | ->method('getUserValue') |
||
179 | ->with('user123', $this->appName, 'skipPopover', 'no') |
||
180 | ->will($this->returnValue('agendaWeek')); |
||
181 | |||
182 | $actual = $this->controller->getConfig('skipPopover'); |
||
183 | |||
184 | $this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual); |
||
185 | $this->assertEquals(['value' => 'agendaWeek'], $actual->getData()); |
||
186 | $this->assertEquals(200, $actual->getStatus()); |
||
187 | } |
||
188 | |||
189 | View Code Duplication | public function testGetPopoverWithConfigException() { |
|
190 | $this->config->expects($this->once()) |
||
191 | ->method('getUserValue') |
||
192 | ->with('user123', $this->appName, 'skipPopover', 'no') |
||
193 | ->will($this->throwException(new \Exception)); |
||
194 | |||
195 | $actual = $this->controller->getConfig('skipPopover'); |
||
196 | |||
197 | $this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual); |
||
198 | $this->assertEquals([], $actual->getData()); |
||
199 | $this->assertEquals(500, $actual->getStatus()); |
||
200 | } |
||
201 | |||
202 | View Code Duplication | public function testSetFirstRun() { |
|
213 | |||
214 | public function testSetFirstRunWithException() { |
||
226 | |||
227 | public function testGetFirstRun() { |
||
239 | |||
240 | View Code Duplication | public function testGetFirstRunWithException() { |
|
241 | $this->config->expects($this->once()) |
||
242 | ->method('getUserValue') |
||
243 | ->with('user123', $this->appName, 'firstRun', 'yes') |
||
244 | ->will($this->throwException(new \Exception)); |
||
245 | |||
246 | $actual = $this->controller->getConfig('firstRun'); |
||
247 | |||
248 | $this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual); |
||
249 | $this->assertEquals([], $actual->getData()); |
||
250 | $this->assertEquals(500, $actual->getStatus()); |
||
251 | } |
||
252 | |||
253 | public function testSetTimezone() { |
||
254 | $this->config->expects($this->once()) |
||
255 | ->method('setUserValue') |
||
256 | ->with('user123', $this->appName, 'timezone', 'Europe/Berlin'); |
||
257 | |||
258 | $actual = $this->controller->setConfig('timezone', 'Europe/Berlin'); |
||
259 | |||
260 | $this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual); |
||
261 | $this->assertEquals([], $actual->getData()); |
||
262 | $this->assertEquals(200, $actual->getStatus()); |
||
263 | } |
||
264 | |||
265 | public function testSetTimezoneWithException() { |
||
266 | $this->config->expects($this->once()) |
||
267 | ->method('setUserValue') |
||
268 | ->with('user123', $this->appName, 'timezone', 'Europe/Berlin') |
||
269 | ->will($this->throwException(new \Exception)); |
||
270 | |||
271 | $actual = $this->controller->setConfig('timezone', 'Europe/Berlin'); |
||
272 | |||
273 | $this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual); |
||
274 | $this->assertEquals([], $actual->getData()); |
||
275 | $this->assertEquals(500, $actual->getStatus()); |
||
276 | } |
||
277 | |||
278 | public function testGetTimezone() { |
||
279 | $this->config->expects($this->once()) |
||
280 | ->method('getUserValue') |
||
281 | ->with('user123', $this->appName, 'timezone', 'automatic') |
||
282 | ->will($this->returnValue('Europe/Berlin')); |
||
283 | |||
284 | $actual = $this->controller->getConfig('timezone'); |
||
285 | |||
286 | $this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual); |
||
287 | $this->assertEquals(['value' => 'Europe/Berlin'], $actual->getData()); |
||
288 | $this->assertEquals(200, $actual->getStatus()); |
||
289 | } |
||
290 | |||
291 | View Code Duplication | public function testGetTimezoneWithException() { |
|
292 | $this->config->expects($this->once()) |
||
293 | ->method('getUserValue') |
||
294 | ->with('user123', $this->appName, 'timezone', 'automatic') |
||
295 | ->will($this->throwException(new \Exception)); |
||
296 | |||
297 | $actual = $this->controller->getConfig('timezone'); |
||
298 | |||
299 | $this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual); |
||
300 | $this->assertEquals([], $actual->getData()); |
||
301 | $this->assertEquals(500, $actual->getStatus()); |
||
302 | } |
||
303 | |||
304 | View Code Duplication | public function testGetNotExistingConfig() { |
|
311 | |||
312 | View Code Duplication | public function testSetNotExistingConfig() { |
|
319 | } |
||
320 |
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.