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 |
||
30 | class PaperHiveControllerTest extends TestCase { |
||
31 | |||
32 | /** @var PaperHiveController */ |
||
33 | protected $controller; |
||
34 | |||
35 | /** @var string */ |
||
36 | protected $appName; |
||
37 | |||
38 | /** @var \OCP\IRequest | \PHPUnit_Framework_MockObject_MockObject */ |
||
39 | protected $requestMock; |
||
40 | |||
41 | /** @var \OCP\Http\Client\IResponse | \PHPUnit_Framework_MockObject_MockObject */ |
||
42 | protected $responseMock; |
||
43 | |||
44 | /** @var \OCP\IL10N | \PHPUnit_Framework_MockObject_MockObject */ |
||
45 | private $l10nMock; |
||
46 | |||
47 | /** @var \OCP\ILogger | \PHPUnit_Framework_MockObject_MockObject */ |
||
48 | private $loggerMock; |
||
49 | |||
50 | /** @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject */ |
||
51 | private $viewMock; |
||
52 | |||
53 | /** @var \OCP\Http\Client\IClient | \PHPUnit_Framework_MockObject_MockObject */ |
||
54 | private $clientMock; |
||
55 | |||
56 | public function setUp() { |
||
92 | |||
93 | private function fakeAll($bookID, $title) { |
||
94 | $contentsDoc = '{' . '"id" : "'.$bookID .'", "title" : "'. $title .'" }'; |
||
95 | $contentsDis = '{' . '"discussions" : [ "blabla", "blabla" ]' .'}'; |
||
96 | $this->responseMock->expects($this->any()) |
||
97 | ->method('getBody') |
||
98 | ->willReturnOnConsecutiveCalls($contentsDis, $contentsDoc); |
||
99 | $this->clientMock->expects($this->any()) |
||
100 | ->method('get') |
||
101 | ->willReturn($this->responseMock); |
||
102 | |||
103 | return $contentsDoc; |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * Test if the basic parameters has changed |
||
108 | */ |
||
109 | public function testPaperHiveDetails() { |
||
125 | |||
126 | |||
127 | public function dataExceptionWithException() { |
||
134 | |||
135 | /** |
||
136 | * @dataProvider dataExceptionWithException |
||
137 | * @param \Exception $exception |
||
138 | * @param string $expectedMessage |
||
139 | */ |
||
140 | View Code Duplication | public function testLoadMetadataForRenamedDocumentExceptions(\Exception $exception, $expectedMessage) { |
|
|
|||
141 | $dir = ''; |
||
142 | $bookID = 'Ra5WnkxImoOE'; |
||
143 | $title = "Borderland City in New India"; |
||
144 | $path = $dir . '/'. $title. '.renamed.paperhive'; |
||
145 | $contents = $this->fakeAll($bookID, $title); |
||
146 | |||
147 | $this->viewMock->expects($this->once()) |
||
148 | ->method('file_exists') |
||
149 | ->willReturn(true); |
||
150 | |||
151 | $this->viewMock->expects($this->any()) |
||
152 | ->method('file_get_contents') |
||
153 | ->willReturnCallback(function() use ($exception) { |
||
154 | throw $exception; |
||
155 | }); |
||
156 | |||
157 | $this->viewMock->expects($this->any()) |
||
158 | ->method('rename') |
||
159 | ->willReturn(false); |
||
160 | |||
161 | $result = $this->controller->loadMetadata('/', $path, "true"); |
||
162 | $data = $result->getData(); |
||
163 | |||
164 | $this->assertSame(400, $result->getStatus()); |
||
165 | $this->assertArrayHasKey('message', $data); |
||
166 | $this->assertSame($expectedMessage, $data['message']); |
||
167 | } |
||
168 | |||
169 | public function testLoadMetadataForRenamedDocumentNoExtension() { |
||
170 | $dir = ''; |
||
171 | $bookID = 'Ra5WnkxImoOE'; |
||
172 | $title = "Borderland City in New India"; |
||
173 | $path = $dir . '/'. $title; |
||
174 | $contents = $this->fakeAll($bookID, $title); |
||
175 | |||
176 | $result = $this->controller->loadMetadata('/', $path, "true"); |
||
177 | $data = $result->getData(); |
||
178 | |||
179 | $this->assertSame(400, $result->getStatus()); |
||
180 | $this->assertArrayHasKey('message', $data); |
||
181 | $this->assertSame('Invalid file path supplied.', $data['message']); |
||
182 | } |
||
183 | |||
184 | public function testLoadMetadataForRenamedDocumentNotExisting() { |
||
185 | $dir = ''; |
||
186 | $bookID = 'Ra5WnkxImoOE'; |
||
187 | $title = "Borderland City in New India"; |
||
188 | $path = $dir . '/'. $title. '.renamed.paperhive'; |
||
189 | $contents = $this->fakeAll($bookID, $title); |
||
190 | |||
191 | $this->viewMock->expects($this->once()) |
||
192 | ->method('file_exists') |
||
193 | ->willReturn(false); |
||
194 | |||
195 | $result = $this->controller->loadMetadata('/', $path, "true"); |
||
196 | $data = $result->getData(); |
||
197 | |||
198 | $this->assertSame(400, $result->getStatus()); |
||
199 | $this->assertArrayHasKey('message', $data); |
||
200 | $this->assertSame('File is obsolete, incorrectly renamed or cannot be read.', $data['message']); |
||
201 | } |
||
202 | |||
203 | public function testLoadMetadataForRenamedDocumentWrongContent() { |
||
204 | $dir = ''; |
||
205 | $bookID = 'Ra5WnkxImoOE'; |
||
206 | $title = "Borderland City in New India"; |
||
207 | $path = $dir . '/'. $title. '.renamed.paperhive'; |
||
208 | $contents = $this->fakeAll($bookID, $title); |
||
209 | |||
210 | $this->viewMock->expects($this->once()) |
||
211 | ->method('file_exists') |
||
212 | ->willReturn(true); |
||
213 | |||
214 | $this->viewMock->expects($this->once()) |
||
215 | ->method('file_get_contents') |
||
216 | ->willReturn('wrong content'); |
||
217 | |||
218 | $result = $this->controller->loadMetadata('/', $path, "true"); |
||
219 | $data = $result->getData(); |
||
220 | |||
221 | $this->assertSame(400, $result->getStatus()); |
||
222 | $this->assertArrayHasKey('message', $data); |
||
223 | $this->assertSame('File is obsolete, incorrectly renamed or cannot be read.', $data['message']); |
||
224 | } |
||
225 | |||
226 | View Code Duplication | public function testLoadMetadataForRenamedDocumentFailRename() { |
|
227 | $dir = ''; |
||
228 | $bookID = 'Ra5WnkxImoOE'; |
||
229 | $title = "Borderland City in New India"; |
||
230 | $path = $dir . '/'. $title. '.renamed.paperhive'; |
||
231 | $contents = $this->fakeAll($bookID, $title); |
||
232 | |||
233 | $this->viewMock->expects($this->once()) |
||
234 | ->method('file_exists') |
||
235 | ->willReturn(true); |
||
236 | |||
237 | $this->viewMock->expects($this->once()) |
||
238 | ->method('file_get_contents') |
||
239 | ->willReturn($contents); |
||
240 | |||
241 | $this->viewMock->expects($this->once()) |
||
242 | ->method('rename') |
||
243 | ->willReturn(false); |
||
244 | |||
245 | $result = $this->controller->loadMetadata('/', $path, "true"); |
||
246 | $data = $result->getData(); |
||
247 | |||
248 | $this->assertSame(400, $result->getStatus()); |
||
249 | $this->assertArrayHasKey('message', $data); |
||
250 | $this->assertSame('File is obsolete, incorrectly renamed or cannot be read.', $data['message']); |
||
251 | } |
||
252 | |||
253 | public function testLoadMetadataForRenamedDocument() { |
||
254 | $dir = ''; |
||
255 | $bookID = 'Ra5WnkxImoOE'; |
||
256 | $title = "Borderland City in New India"; |
||
257 | $path = $dir . '/'. $title. '.renamed.paperhive'; |
||
258 | $contents = $this->fakeAll($bookID, $title); |
||
259 | |||
260 | $this->viewMock->expects($this->once()) |
||
261 | ->method('file_exists') |
||
262 | ->willReturn(true); |
||
263 | |||
264 | $this->viewMock->expects($this->once()) |
||
265 | ->method('file_get_contents') |
||
266 | ->willReturn($contents); |
||
267 | |||
268 | $this->viewMock->expects($this->once()) |
||
269 | ->method('rename') |
||
270 | ->willReturn(true); |
||
271 | |||
272 | $result = $this->controller->loadMetadata('/', $path, "true"); |
||
273 | $data = $result->getData(); |
||
274 | |||
275 | $this->assertSame(200, $result->getStatus()); |
||
276 | $this->assertArrayHasKey('paperhive_base_url', $data); |
||
277 | $this->assertArrayHasKey('paperhive_api_url', $data); |
||
278 | $this->assertArrayHasKey('paperhive_document_url', $data); |
||
279 | $this->assertArrayHasKey('paperhive_document_id', $data); |
||
280 | $this->assertArrayHasKey('paperhive_discussion_api_endpoint', $data); |
||
281 | $this->assertArrayHasKey('paperhive_extension', $data); |
||
282 | $this->assertArrayHasKey('paperhive_discussion_count', $data); |
||
283 | $this->assertSame($bookID, $data['paperhive_document_id']); |
||
284 | $this->assertSame(2, $data['paperhive_discussion_count']); |
||
285 | } |
||
286 | |||
287 | public function testLoadMetadataWithoutDiscussions() { |
||
307 | |||
308 | public function testLoadMetadata() { |
||
329 | |||
330 | public function dataTestSave() { |
||
337 | |||
338 | /** |
||
339 | * @dataProvider dataTestSave |
||
340 | * |
||
341 | * @param $dir |
||
342 | * @param $bookID |
||
343 | * @param $correctDiscussions |
||
344 | * @param $correctDocument |
||
345 | * @param $expectedStatus |
||
346 | * @param $expectedMessage |
||
347 | */ |
||
348 | public function testGetPaperHiveDocument($dir, $bookID, $correctDiscussions, $correctDocument, $expectedStatus, $expectedMessage) { |
||
380 | |||
381 | /** |
||
382 | * @dataProvider dataExceptionWithException |
||
383 | * @param \Exception $exception |
||
384 | * @param string $expectedMessage |
||
385 | */ |
||
386 | public function testGetDocumentExceptionWithException(\Exception $exception, $expectedMessage) { |
||
404 | } |
||
405 |
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.