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 |
||
| 28 | class AccountsControllerTest extends \Test\TestCase { |
||
| 29 | |||
| 30 | private $appName; |
||
| 31 | private $request; |
||
| 32 | private $accountService; |
||
| 33 | private $userId; |
||
| 34 | private $userFolder; |
||
| 35 | private $autoConfig; |
||
| 36 | private $logger; |
||
| 37 | private $l10n; |
||
| 38 | private $crypto; |
||
| 39 | private $collector; |
||
| 40 | private $controller; |
||
| 41 | private $accountId; |
||
| 42 | private $account; |
||
| 43 | private $unifiedAccount; |
||
| 44 | private $aliasesService; |
||
| 45 | |||
| 46 | protected function setUp() { |
||
| 47 | parent::setUp(); |
||
| 48 | |||
| 49 | $this->appName = 'mail'; |
||
| 50 | $this->request = $this->getMockBuilder('\OCP\IRequest') |
||
| 51 | ->disableOriginalConstructor() |
||
| 52 | ->getMock(); |
||
| 53 | $this->accountService = $this->getMockBuilder('\OCA\Mail\Service\AccountService') |
||
| 54 | ->disableOriginalConstructor() |
||
| 55 | ->getMock(); |
||
| 56 | $this->userId = 'manfred'; |
||
| 57 | $this->userFolder = $this->getMockBuilder('\OCP\Files\Folder') |
||
| 58 | ->disableOriginalConstructor() |
||
| 59 | ->getMock(); |
||
| 60 | $this->autoConfig = $this->getMockBuilder('\OCA\Mail\Service\AutoConfig\AutoConfig') |
||
| 61 | ->disableOriginalConstructor() |
||
| 62 | ->getMock(); |
||
| 63 | $this->logger = $this->getMockBuilder('\OCA\Mail\Service\Logger') |
||
| 64 | ->disableOriginalConstructor() |
||
| 65 | ->getMock(); |
||
| 66 | $this->l10n = $this->getMockBuilder('\OCP\IL10N') |
||
| 67 | ->disableOriginalConstructor() |
||
| 68 | ->getMock(); |
||
| 69 | $this->crypto = $this->getMockBuilder('\OCP\Security\ICrypto') |
||
| 70 | ->disableOriginalConstructor() |
||
| 71 | ->getMock(); |
||
| 72 | $this->collector = $this->getMockBuilder('\OCA\Mail\Service\AutoCompletion\AddressCollector') |
||
| 73 | ->disableOriginalConstructor() |
||
| 74 | ->getMock(); |
||
| 75 | $this->aliasesService = $this->getMockBuilder('\OCA\Mail\Service\AliasesService') |
||
| 76 | ->disableOriginalConstructor() |
||
| 77 | ->getMock(); |
||
| 78 | |||
| 79 | $this->controller = new AccountsController($this->appName, $this->request, |
||
| 80 | $this->accountService, $this->userId, $this->userFolder, $this->autoConfig, |
||
| 81 | $this->logger, $this->l10n, $this->crypto, $this->collector, $this->aliasesService); |
||
| 82 | |||
| 83 | $this->account = $this->getMockBuilder('\OCA\Mail\Account') |
||
| 84 | ->disableOriginalConstructor() |
||
| 85 | ->getMock(); |
||
| 86 | $this->unifiedAccount = $this->getMockBuilder('\OCA\Mail\Service\UnifiedAccount') |
||
| 87 | ->disableOriginalConstructor() |
||
| 88 | ->getMock(); |
||
| 89 | $this->accountId = 123; |
||
| 90 | } |
||
| 91 | |||
| 92 | public function testIndex() { |
||
| 93 | $this->account->expects($this->once()) |
||
| 94 | ->method('getConfiguration') |
||
| 95 | ->will($this->returnValue([ |
||
| 96 | 'accountId' => 123, |
||
| 97 | ])); |
||
| 98 | $this->accountService->expects($this->once()) |
||
| 99 | ->method('findByUserId') |
||
| 100 | ->with($this->equalTo($this->userId)) |
||
| 101 | ->will($this->returnValue([$this->account])); |
||
| 102 | $this->aliasesService->expects($this->any()) |
||
| 103 | ->method('findAll') |
||
| 104 | ->with($this->equalTo($this->accountId), $this->equalTo($this->userId)) |
||
| 105 | ->will($this->returnValue('aliases')); |
||
| 106 | |||
| 107 | $response = $this->controller->index(); |
||
| 108 | |||
| 109 | $expectedResponse = new JSONResponse([ |
||
| 110 | [ |
||
| 111 | 'accountId' => 123, |
||
| 112 | 'aliases' => 'aliases' |
||
| 113 | ] |
||
| 114 | ]); |
||
| 115 | $this->assertEquals($expectedResponse, $response); |
||
| 116 | } |
||
| 117 | |||
| 118 | public function testShow() { |
||
| 119 | $this->accountService->expects($this->once()) |
||
| 120 | ->method('find') |
||
| 121 | ->with($this->equalTo($this->userId), $this->equalTo($this->accountId)) |
||
| 122 | ->will($this->returnValue($this->account)); |
||
| 123 | $this->account->expects($this->once()) |
||
| 124 | ->method('getConfiguration') |
||
| 125 | ->will($this->returnValue('conf')); |
||
| 126 | |||
| 127 | $response = $this->controller->show($this->accountId); |
||
| 128 | |||
| 129 | $expectedResponse = new JSONResponse('conf'); |
||
| 130 | $this->assertEquals($expectedResponse, $response); |
||
| 131 | } |
||
| 132 | |||
| 133 | public function testShowDoesNotExist() { |
||
| 134 | $this->accountService->expects($this->once()) |
||
| 135 | ->method('find') |
||
| 136 | ->with($this->equalTo($this->userId), $this->equalTo($this->accountId)) |
||
| 137 | ->will($this->returnValue($this->account)); |
||
| 138 | $this->account->expects($this->once()) |
||
| 139 | ->method('getConfiguration') |
||
| 140 | ->will($this->throwException(new OCP\AppFramework\Db\DoesNotExistException('test123'))); |
||
| 141 | |||
| 142 | $response = $this->controller->show($this->accountId); |
||
| 143 | |||
| 144 | $expectedResponse = new JSONResponse([]); |
||
| 145 | $expectedResponse->setStatus(404); |
||
| 146 | $this->assertEquals($expectedResponse, $response); |
||
| 147 | } |
||
| 148 | |||
| 149 | public function testDestroy() { |
||
| 150 | $this->accountService->expects($this->once()) |
||
| 151 | ->method('delete') |
||
| 152 | ->with($this->equalTo($this->userId), $this->equalTo($this->accountId)); |
||
| 153 | |||
| 154 | $response = $this->controller->destroy($this->accountId); |
||
| 155 | |||
| 156 | $expectedResponse = new JSONResponse(); |
||
| 157 | $this->assertEquals($expectedResponse, $response); |
||
| 158 | } |
||
| 159 | |||
| 160 | public function testDestroyDoesNotExist() { |
||
| 161 | $this->accountService->expects($this->once()) |
||
| 162 | ->method('delete') |
||
| 163 | ->with($this->equalTo($this->userId), $this->equalTo($this->accountId)) |
||
| 164 | ->will($this->throwException(new \OCP\AppFramework\Db\DoesNotExistException('test'))); |
||
| 165 | |||
| 166 | $response = $this->controller->destroy($this->accountId); |
||
| 167 | |||
| 168 | $expectedResponse = new JSONResponse(); |
||
| 169 | $this->assertEquals($expectedResponse, $response); |
||
| 170 | } |
||
| 171 | |||
| 172 | public function testCreateAutoDetectSuccess() { |
||
| 173 | $email = '[email protected]'; |
||
| 174 | $password = '123456'; |
||
| 175 | $accountName = 'John Doe'; |
||
| 176 | |||
| 177 | $this->account->expects($this->exactly(2)) |
||
| 178 | ->method('getId') |
||
| 179 | ->will($this->returnValue(135)); |
||
| 180 | $this->autoConfig->expects($this->once()) |
||
| 181 | ->method('createAutoDetected') |
||
| 182 | ->with($this->equalTo($email), $this->equalTo($password), |
||
| 183 | $this->equalTo($accountName)) |
||
| 184 | ->will($this->returnValue($this->account)); |
||
| 185 | $this->accountService->expects($this->once()) |
||
| 186 | ->method('save') |
||
| 187 | ->with($this->equalTo($this->account)); |
||
| 188 | |||
| 189 | $response = $this->controller->create($accountName, $email, $password, null, |
||
| 190 | null, null, null, null, null, null, null, null, null, true); |
||
| 191 | |||
| 192 | $expectedResponse = new JSONResponse([ |
||
| 193 | 'data' => [ |
||
| 194 | 'id' => 135, |
||
| 195 | ], |
||
| 196 | ], Http::STATUS_CREATED); |
||
| 197 | $this->assertEquals($expectedResponse, $response); |
||
| 198 | } |
||
| 199 | |||
| 200 | public function testCreateAutoDetectFailure() { |
||
| 201 | $email = '[email protected]'; |
||
| 202 | $password = '123456'; |
||
| 203 | $accountName = 'John Doe'; |
||
| 204 | |||
| 205 | $this->autoConfig->expects($this->once()) |
||
| 206 | ->method('createAutoDetected') |
||
| 207 | ->with($this->equalTo($email), $this->equalTo($password), |
||
| 208 | $this->equalTo($accountName)) |
||
| 209 | ->will($this->returnValue(null)); |
||
| 210 | $this->l10n->expects($this->once()) |
||
| 211 | ->method('t') |
||
| 212 | ->will($this->returnValue('fail')); |
||
| 213 | |||
| 214 | $response = $this->controller->create($accountName, $email, $password, null, |
||
| 215 | null, null, null, null, null, null, null, null, null, true); |
||
| 216 | |||
| 217 | $expectedResponse = new JSONResponse([ |
||
| 218 | 'message' => 'fail', |
||
| 219 | ], Http::STATUS_BAD_REQUEST); |
||
| 220 | $this->assertEquals($expectedResponse, $response); |
||
| 221 | } |
||
| 222 | |||
| 223 | public function newMessageDataProvider() { |
||
| 224 | return [ |
||
| 225 | [false, false, false], |
||
| 226 | [true, false, false], |
||
| 227 | [false, true, false], |
||
| 228 | [true, true, true], |
||
| 229 | ]; |
||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @dataProvider newMessageDataProvider |
||
| 234 | */ |
||
| 235 | public function testSend($isUnifiedInbox, $isReply, $addressCollectorError) { |
||
| 236 | $account = $isUnifiedInbox ? $this->unifiedAccount : $this->account; |
||
| 237 | $folderId = base64_encode('My folder'); |
||
| 238 | $subject = 'Hello'; |
||
| 239 | $body = 'Hi!'; |
||
| 240 | $from = '[email protected]'; |
||
| 241 | $to = '[email protected]'; |
||
| 242 | $cc = '"user2" <[email protected]>, [email protected]'; |
||
| 243 | $bcc = '[email protected]'; |
||
| 244 | $draftUID = 45; |
||
| 245 | $messageId = $isReply ? 123 : null; |
||
| 246 | $attachmentName = 'kitten.png'; |
||
| 247 | $attachments = [ |
||
| 248 | [ |
||
| 249 | 'fileName' => $attachmentName |
||
| 250 | ], |
||
| 251 | ]; |
||
| 252 | $aliasId = null; |
||
| 253 | |||
| 254 | $this->accountService->expects($this->once()) |
||
| 255 | ->method('find') |
||
| 256 | ->with($this->userId, $this->accountId) |
||
| 257 | ->will($this->returnValue($account)); |
||
| 258 | if ($isUnifiedInbox) { |
||
| 259 | $this->unifiedAccount->expects($this->once()) |
||
| 260 | ->method('resolve') |
||
| 261 | ->with($messageId) |
||
| 262 | ->will($this->returnValue([$this->account, $folderId, $messageId])); |
||
| 263 | } |
||
| 264 | |||
| 265 | if ($isReply) { |
||
| 266 | $message = $this->getMockBuilder('OCA\Mail\Model\ReplyMessage') |
||
| 267 | ->disableOriginalConstructor() |
||
| 268 | ->getMock(); |
||
| 269 | $this->account->expects($this->once()) |
||
| 270 | ->method('newReplyMessage') |
||
| 271 | ->will($this->returnValue($message)); |
||
| 272 | $mailbox = $this->getMockBuilder('OCA\Mail\Service\IMailBox') |
||
| 273 | ->disableOriginalConstructor() |
||
| 274 | ->getMock(); |
||
| 275 | $this->account->expects($this->once()) |
||
| 276 | ->method('getMailbox') |
||
| 277 | ->with(base64_decode($folderId)) |
||
| 278 | ->will($this->returnValue($mailbox)); |
||
| 279 | $reply = new Message(); |
||
| 280 | $mailbox->expects($this->once()) |
||
| 281 | ->method('getMessage') |
||
| 282 | ->with($messageId) |
||
| 283 | ->will($this->returnValue($reply)); |
||
| 284 | $message->expects($this->once()) |
||
| 285 | ->method('setRepliedMessage') |
||
| 286 | ->with($reply); |
||
| 287 | } else { |
||
| 288 | $message = $this->getMockBuilder('OCA\Mail\Model\Message') |
||
| 289 | ->disableOriginalConstructor() |
||
| 290 | ->getMock(); |
||
| 291 | $this->account->expects($this->once()) |
||
| 292 | ->method('newMessage') |
||
| 293 | ->will($this->returnValue($message)); |
||
| 294 | } |
||
| 295 | |||
| 296 | $message->expects($this->once()) |
||
| 297 | ->method('setTo') |
||
| 298 | ->with(Message::parseAddressList($to)); |
||
| 299 | $message->expects($this->once()) |
||
| 300 | ->method('setSubject') |
||
| 301 | ->with($subject); |
||
| 302 | $message->expects($this->once()) |
||
| 303 | ->method('setFrom') |
||
| 304 | ->with($from); |
||
| 305 | $message->expects($this->once()) |
||
| 306 | ->method('setCC') |
||
| 307 | ->with(Message::parseAddressList($cc)); |
||
| 308 | $message->expects($this->once()) |
||
| 309 | ->method('setBcc') |
||
| 310 | ->with(Message::parseAddressList($bcc)); |
||
| 311 | $message->expects($this->once()) |
||
| 312 | ->method('setContent') |
||
| 313 | ->with($body); |
||
| 314 | $message->expects($this->once()) |
||
| 315 | ->method('getToList') |
||
| 316 | ->willReturn([]); |
||
| 317 | $message->expects($this->once()) |
||
| 318 | ->method('getCCList') |
||
| 319 | ->willReturn([]); |
||
| 320 | $message->expects($this->once()) |
||
| 321 | ->method('getBCCList') |
||
| 322 | ->willReturn([]); |
||
| 323 | |||
| 324 | $this->account->expects($this->once()) |
||
| 325 | ->method('getEMailAddress') |
||
| 326 | ->will($this->returnValue($from)); |
||
| 327 | $this->account->expects($this->once()) |
||
| 328 | ->method('sendMessage') |
||
| 329 | ->with($message, $draftUID); |
||
| 330 | $this->userFolder->expects($this->at(0)) |
||
| 331 | ->method('nodeExists') |
||
| 332 | ->with($attachmentName) |
||
| 333 | ->will($this->returnValue(true)); |
||
| 334 | $file = $this->getMockBuilder('OCP\Files\File') |
||
| 335 | ->disableOriginalConstructor() |
||
| 336 | ->getMock(); |
||
| 337 | $this->userFolder->expects($this->once()) |
||
| 338 | ->method('get') |
||
| 339 | ->with($attachmentName) |
||
| 340 | ->will($this->returnValue($file)); |
||
| 341 | if ($addressCollectorError) { |
||
| 342 | $this->collector->expects($this->once()) |
||
| 343 | ->method('addAddresses') |
||
| 344 | ->will($this->throwException(new \Exception('some error'))); |
||
| 345 | } else { |
||
| 346 | $this->collector->expects($this->once()) |
||
| 347 | ->method('addAddresses'); |
||
| 348 | } |
||
| 349 | |||
| 350 | $expected = new JSONResponse(); |
||
| 351 | $actual = $this->controller->send($this->accountId, $folderId, $subject, |
||
| 352 | $body, $to, $cc, $bcc, $draftUID, $messageId, $attachments, $aliasId); |
||
| 353 | |||
| 354 | $this->assertEquals($expected, $actual); |
||
| 355 | } |
||
| 356 | |||
| 357 | public function draftDataProvider() { |
||
| 358 | return [ |
||
| 359 | [false, false], |
||
| 360 | [true, true], |
||
| 361 | [true, false], |
||
| 362 | [true, true], |
||
| 363 | ]; |
||
| 364 | } |
||
| 365 | |||
| 366 | /** |
||
| 367 | * @dataProvider newMessageDataProvider |
||
| 368 | */ |
||
| 369 | public function testDraft($isUnifiedInbox, $withPreviousDraft) { |
||
| 370 | $account = $isUnifiedInbox ? $this->unifiedAccount : $this->account; |
||
| 371 | $subject = 'Hello'; |
||
| 372 | $body = 'Hi!'; |
||
| 373 | $from = '[email protected]'; |
||
| 374 | $to = '[email protected]'; |
||
| 375 | $cc = '"user2" <[email protected]>, [email protected]'; |
||
| 376 | $bcc = '[email protected]'; |
||
| 377 | $messageId = 123; |
||
| 378 | $uid = $withPreviousDraft ? 123 : null; |
||
| 379 | $newUID = 124; |
||
| 380 | |||
| 381 | $this->accountService->expects($this->once()) |
||
| 382 | ->method('find') |
||
| 383 | ->with($this->userId, $this->accountId) |
||
| 384 | ->will($this->returnValue($account)); |
||
| 385 | if ($isUnifiedInbox) { |
||
| 386 | $this->unifiedAccount->expects($this->once()) |
||
| 387 | ->method('resolve') |
||
| 388 | ->with($messageId) |
||
| 389 | ->will($this->returnValue([$this->account])); |
||
| 390 | } |
||
| 391 | |||
| 392 | $message = $this->getMockBuilder('OCA\Mail\Model\Message') |
||
| 393 | ->disableOriginalConstructor() |
||
| 394 | ->getMock(); |
||
| 395 | $this->account->expects($this->once()) |
||
| 396 | ->method('newMessage') |
||
| 397 | ->will($this->returnValue($message)); |
||
| 398 | $message->expects($this->once()) |
||
| 399 | ->method('setTo') |
||
| 400 | ->with(Message::parseAddressList($to)); |
||
| 401 | $message->expects($this->once()) |
||
| 402 | ->method('setSubject') |
||
| 403 | ->with($subject); |
||
| 404 | $message->expects($this->once()) |
||
| 405 | ->method('setFrom') |
||
| 406 | ->with($from); |
||
| 407 | $message->expects($this->once()) |
||
| 408 | ->method('setCC') |
||
| 409 | ->with(Message::parseAddressList($cc)); |
||
| 410 | $message->expects($this->once()) |
||
| 411 | ->method('setBcc') |
||
| 412 | ->with(Message::parseAddressList($bcc)); |
||
| 413 | $message->expects($this->once()) |
||
| 414 | ->method('setContent') |
||
| 415 | ->with($body); |
||
| 416 | $this->account->expects($this->once()) |
||
| 417 | ->method('getEMailAddress') |
||
| 418 | ->will($this->returnValue($from)); |
||
| 419 | $this->account->expects($this->once()) |
||
| 420 | ->method('saveDraft') |
||
| 421 | ->with($message, $uid) |
||
| 422 | ->will($this->returnValue($newUID)); |
||
| 423 | |||
| 424 | $expected = new JSONResponse([ |
||
| 425 | 'uid' => $newUID, |
||
| 426 | ]); |
||
| 427 | $actual = $this->controller->draft($this->accountId, $subject, $body, $to, |
||
| 428 | $cc, $bcc, $uid, $messageId); |
||
| 429 | |||
| 430 | $this->assertEquals($expected, $actual); |
||
| 431 | } |
||
| 432 | |||
| 433 | } |
||
| 434 |