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 |
||
| 12 | class NewsletterTest extends TestCase |
||
| 13 | { |
||
| 14 | protected $driver; |
||
| 15 | |||
| 16 | /** @var Mockery\Mock */ |
||
| 17 | protected $client; |
||
| 18 | |||
| 19 | /** @var \DansMaCulotte\Newsletter\Newsletter */ |
||
| 20 | protected $newsletter; |
||
| 21 | |||
| 22 | public function setUp() : void |
||
| 23 | { |
||
| 24 | $this->client = Mockery::mock(MailChimp::class); |
||
| 25 | |||
| 26 | $this->driver = new MailchimpDriver([ |
||
| 27 | 'apiKey' => 'test-ApiKey', |
||
| 28 | 'ssl' => false, |
||
| 29 | ], [ |
||
| 30 | 'lists' => [ |
||
| 31 | 'list1' => ['id' => 123], |
||
| 32 | 'list2' => ['id' => 456], |
||
| 33 | ], |
||
| 34 | 'defaultList' => 'list1', |
||
| 35 | ]); |
||
| 36 | |||
| 37 | $this->driver->client = $this->client; |
||
| 38 | |||
| 39 | $this->newsletter = new Newsletter($this->driver); |
||
| 40 | } |
||
| 41 | |||
| 42 | public function tearDown() : void |
||
| 43 | { |
||
| 44 | parent::tearDown(); |
||
| 45 | |||
| 46 | if ($container = Mockery::getContainer()) { |
||
| 47 | $this->addToAssertionCount($container->mockery_getExpectationCount()); |
||
| 48 | } |
||
| 49 | |||
| 50 | Mockery::close(); |
||
| 51 | } |
||
| 52 | |||
| 53 | /** @test */ |
||
| 54 | public function it_can_subscribe_someone() |
||
| 55 | { |
||
| 56 | $this->client->shouldReceive('success')->andReturn(true); |
||
| 57 | |||
| 58 | $email = '[email protected]'; |
||
| 59 | |||
| 60 | $url = 'lists/123/members'; |
||
| 61 | |||
| 62 | $this->client->shouldReceive('post')->withArgs([ |
||
| 63 | $url, |
||
| 64 | [ |
||
| 65 | 'email_address' => $email, |
||
| 66 | 'status' => 'subscribed', |
||
| 67 | 'email_type' => 'html', |
||
| 68 | ], |
||
| 69 | ]); |
||
| 70 | |||
| 71 | $this->newsletter->subscribe($email); |
||
| 72 | } |
||
| 73 | |||
| 74 | /** @test */ |
||
| 75 | public function it_will_throw_an_error_when_subscribe_someone() |
||
| 76 | { |
||
| 77 | $this->client->shouldReceive('success')->andReturn(false); |
||
| 78 | $this->client->shouldReceive('getLastError')->andReturn('Error'); |
||
| 79 | |||
| 80 | $email = 'freekspatie.be'; |
||
| 81 | |||
| 82 | $url = 'lists/123/members'; |
||
| 83 | |||
| 84 | $this->client->shouldReceive('post')->withArgs([ |
||
| 85 | $url, |
||
| 86 | [ |
||
| 87 | 'email_address' => $email, |
||
| 88 | 'status' => 'subscribed', |
||
| 89 | 'email_type' => 'html', |
||
| 90 | ], |
||
| 91 | ]); |
||
| 92 | |||
| 93 | $this->expectExceptionObject(ApiError::responseError('Error', 'mailchimp')); |
||
| 94 | $this->newsletter->subscribe($email); |
||
| 95 | } |
||
| 96 | |||
| 97 | /** @test */ |
||
| 98 | public function it_can_subscribe_someone_as_pending() |
||
| 99 | { |
||
| 100 | $this->client->shouldReceive('success')->andReturn(true); |
||
| 101 | |||
| 102 | $email = '[email protected]'; |
||
| 103 | |||
| 104 | $url = 'lists/123/members'; |
||
| 105 | |||
| 106 | $options = [ |
||
| 107 | 'status' => 'pending' |
||
| 108 | ]; |
||
| 109 | |||
| 110 | $this->client->shouldReceive('post')->withArgs([ |
||
| 111 | $url, |
||
| 112 | [ |
||
| 113 | 'email_address' => $email, |
||
| 114 | 'status' => 'pending', |
||
| 115 | 'email_type' => 'html', |
||
| 116 | ], |
||
| 117 | ]); |
||
| 118 | |||
| 119 | $this->newsletter->subscribe($email, $options); |
||
| 120 | } |
||
| 121 | |||
| 122 | /** @test */ |
||
| 123 | public function it_can_subscribe_or_update_someone() |
||
| 124 | { |
||
| 125 | $this->client->shouldReceive('success')->andReturn(true); |
||
| 126 | |||
| 127 | $email = '[email protected]'; |
||
| 128 | |||
| 129 | $url = 'lists/123/members'; |
||
| 130 | |||
| 131 | $subscriberHash = 'abc123'; |
||
| 132 | |||
| 133 | $this->client->shouldReceive('subscriberHash') |
||
| 134 | ->once() |
||
| 135 | ->withArgs([$email]) |
||
| 136 | ->andReturn($subscriberHash); |
||
| 137 | |||
| 138 | $this->client->shouldReceive('put')->withArgs([ |
||
| 139 | "{$url}/{$subscriberHash}", |
||
| 140 | [ |
||
| 141 | 'email_address' => $email, |
||
| 142 | 'status' => 'subscribed', |
||
| 143 | 'email_type' => 'html', |
||
| 144 | ], |
||
| 145 | ]); |
||
| 146 | |||
| 147 | $this->newsletter->subscribeOrUpdate($email); |
||
| 148 | } |
||
| 149 | |||
| 150 | /** @test */ |
||
| 151 | public function it_can_subscribe_someone_with_merge_fields() |
||
| 152 | { |
||
| 153 | $this->client->shouldReceive('success')->andReturn(true); |
||
| 154 | |||
| 155 | $email = '[email protected]'; |
||
| 156 | |||
| 157 | $options = [ |
||
| 158 | 'merge_fields' => [ |
||
| 159 | 'FNAME' => 'Freek' |
||
| 160 | ] |
||
| 161 | ]; |
||
| 162 | |||
| 163 | $url = 'lists/123/members'; |
||
| 164 | |||
| 165 | $this->client->shouldReceive('post') |
||
| 166 | ->once() |
||
| 167 | ->withArgs([ |
||
| 168 | $url, |
||
| 169 | [ |
||
| 170 | 'email_address' => $email, |
||
| 171 | 'status' => 'subscribed', |
||
| 172 | 'merge_fields' => $options['merge_fields'], |
||
| 173 | 'email_type' => 'html', |
||
| 174 | ], |
||
| 175 | ]); |
||
| 176 | |||
| 177 | $this->newsletter->subscribe($email, $options); |
||
| 178 | } |
||
| 179 | |||
| 180 | /** @test */ |
||
| 181 | public function it_can_subscribe_or_update_someone_with_merge_fields() |
||
| 182 | { |
||
| 183 | $this->client->shouldReceive('success')->andReturn(true); |
||
| 184 | |||
| 185 | $email = '[email protected]'; |
||
| 186 | |||
| 187 | $options = [ |
||
| 188 | 'merge_fields' => [ |
||
| 189 | 'FNAME' => 'Freek' |
||
| 190 | ] |
||
| 191 | ]; |
||
| 192 | |||
| 193 | $url = 'lists/123/members'; |
||
| 194 | |||
| 195 | $subscriberHash = 'abc123'; |
||
| 196 | |||
| 197 | $this->client->shouldReceive('subscriberHash') |
||
| 198 | ->once() |
||
| 199 | ->withArgs([$email]) |
||
| 200 | ->andReturn($subscriberHash); |
||
| 201 | |||
| 202 | $this->client->shouldReceive('put') |
||
| 203 | ->once() |
||
| 204 | ->withArgs([ |
||
| 205 | "{$url}/{$subscriberHash}", |
||
| 206 | [ |
||
| 207 | 'email_address' => $email, |
||
| 208 | 'status' => 'subscribed', |
||
| 209 | 'merge_fields' => $options['merge_fields'], |
||
| 210 | 'email_type' => 'html', |
||
| 211 | ], |
||
| 212 | ]); |
||
| 213 | |||
| 214 | $this->newsletter->subscribeOrUpdate($email, $options); |
||
| 215 | } |
||
| 216 | |||
| 217 | /** @test */ |
||
| 218 | public function it_will_throw_an_error_when_subscribe_or_update_someone_with_merge_fields() |
||
| 219 | { |
||
| 220 | $this->client->shouldReceive('success')->andReturn(false); |
||
| 221 | $this->client->shouldReceive('getLastError')->andReturn('Error'); |
||
| 222 | |||
| 223 | $email = 'freekspatie.be'; |
||
| 224 | |||
| 225 | $options = [ |
||
| 226 | 'merge_fields' => [ |
||
| 227 | 'FNAME' => 'Freek' |
||
| 228 | ] |
||
| 229 | ]; |
||
| 230 | |||
| 231 | $url = 'lists/123/members'; |
||
| 232 | |||
| 233 | $subscriberHash = 'abc123'; |
||
| 234 | |||
| 235 | $this->client->shouldReceive('subscriberHash') |
||
| 236 | ->once() |
||
| 237 | ->withArgs([$email]) |
||
| 238 | ->andReturn($subscriberHash); |
||
| 239 | |||
| 240 | $this->client->shouldReceive('put') |
||
| 241 | ->once() |
||
| 242 | ->withArgs([ |
||
| 243 | "{$url}/{$subscriberHash}", |
||
| 244 | [ |
||
| 245 | 'email_address' => $email, |
||
| 246 | 'status' => 'subscribed', |
||
| 247 | 'merge_fields' => $options['merge_fields'], |
||
| 248 | 'email_type' => 'html', |
||
| 249 | ], |
||
| 250 | ]); |
||
| 251 | |||
| 252 | $this->expectExceptionObject(ApiError::responseError('Error', 'mailchimp')); |
||
| 253 | $this->newsletter->subscribeOrUpdate($email, $options); |
||
| 254 | } |
||
| 255 | |||
| 256 | /** @test */ |
||
| 257 | public function it_can_subscribe_someone_to_an_alternative_list() |
||
| 258 | { |
||
| 259 | $this->client->shouldReceive('success')->andReturn(true); |
||
| 260 | |||
| 261 | $email = '[email protected]'; |
||
| 262 | |||
| 263 | $url = 'lists/456/members'; |
||
| 264 | |||
| 265 | $this->client->shouldReceive('post') |
||
| 266 | ->once() |
||
| 267 | ->withArgs([ |
||
| 268 | $url, |
||
| 269 | [ |
||
| 270 | 'email_address' => $email, |
||
| 271 | 'status' => 'subscribed', |
||
| 272 | 'email_type' => 'html', |
||
| 273 | ], |
||
| 274 | ]); |
||
| 275 | |||
| 276 | $this->newsletter->subscribe($email, [], 'list2'); |
||
| 277 | } |
||
| 278 | |||
| 279 | /** @test */ |
||
| 280 | public function it_can_subscribe_or_update_someone_to_an_alternative_list() |
||
| 281 | { |
||
| 282 | $this->client->shouldReceive('success')->andReturn(true); |
||
| 283 | |||
| 284 | $email = '[email protected]'; |
||
| 285 | |||
| 286 | $url = 'lists/456/members'; |
||
| 287 | |||
| 288 | $subscriberHash = 'abc123'; |
||
| 289 | |||
| 290 | $this->client->shouldReceive('subscriberHash') |
||
| 291 | ->once() |
||
| 292 | ->withArgs([$email]) |
||
| 293 | ->andReturn($subscriberHash); |
||
| 294 | |||
| 295 | $this->client->shouldReceive('put') |
||
| 296 | ->once() |
||
| 297 | ->withArgs([ |
||
| 298 | "{$url}/{$subscriberHash}", |
||
| 299 | [ |
||
| 300 | 'email_address' => $email, |
||
| 301 | 'status' => 'subscribed', |
||
| 302 | 'email_type' => 'html', |
||
| 303 | ], |
||
| 304 | ]); |
||
| 305 | |||
| 306 | $this->newsletter->subscribeOrUpdate($email, [], 'list2'); |
||
| 307 | } |
||
| 308 | |||
| 309 | /** @test */ |
||
| 310 | public function it_can_override_the_defaults_when_subscribing_someone() |
||
| 311 | { |
||
| 312 | $this->client->shouldReceive('success')->andReturn(true); |
||
| 313 | |||
| 314 | $email = '[email protected]'; |
||
| 315 | |||
| 316 | $url = 'lists/123/members'; |
||
| 317 | |||
| 318 | $this->client->shouldReceive('post') |
||
| 319 | ->once() |
||
| 320 | ->withArgs([ |
||
| 321 | $url, |
||
| 322 | [ |
||
| 323 | 'email_address' => $email, |
||
| 324 | 'status' => 'pending', |
||
| 325 | 'email_type' => 'text', |
||
| 326 | ], |
||
| 327 | ]); |
||
| 328 | |||
| 329 | $this->newsletter->subscribe($email, ['email_type' => 'text', 'status' => 'pending']); |
||
| 330 | } |
||
| 331 | |||
| 332 | /** @test */ |
||
| 333 | public function it_can_override_the_defaults_when_subscribing_or_updating_someone() |
||
| 334 | { |
||
| 335 | $this->client->shouldReceive('success')->andReturn(true); |
||
| 336 | |||
| 337 | $email = '[email protected]'; |
||
| 338 | |||
| 339 | $url = 'lists/123/members'; |
||
| 340 | |||
| 341 | $subscriberHash = 'abc123'; |
||
| 342 | |||
| 343 | $this->client->shouldReceive('subscriberHash') |
||
| 344 | ->once() |
||
| 345 | ->withArgs([$email]) |
||
| 346 | ->andReturn($subscriberHash); |
||
| 347 | |||
| 348 | $this->client->shouldReceive('put') |
||
| 349 | ->once() |
||
| 350 | ->withArgs([ |
||
| 351 | "{$url}/{$subscriberHash}", |
||
| 352 | [ |
||
| 353 | 'email_address' => $email, |
||
| 354 | 'status' => 'pending', |
||
| 355 | 'email_type' => 'text', |
||
| 356 | ], |
||
| 357 | ]); |
||
| 358 | |||
| 359 | $this->newsletter->subscribeOrUpdate($email, ['email_type' => 'text', 'status' => 'pending']); |
||
| 360 | } |
||
| 361 | |||
| 362 | |||
| 363 | /** @test */ |
||
| 364 | public function it_can_unsubscribe_someone() |
||
| 365 | { |
||
| 366 | $this->client->shouldReceive('success')->andReturn(true); |
||
| 367 | |||
| 368 | $email = '[email protected]'; |
||
| 369 | |||
| 370 | $subscriberHash = 'abc123'; |
||
| 371 | |||
| 372 | $this->client->shouldReceive('subscriberHash') |
||
| 373 | ->once() |
||
| 374 | ->withArgs([$email]) |
||
| 375 | ->andReturn($subscriberHash); |
||
| 376 | |||
| 377 | $this->client |
||
| 378 | ->shouldReceive('patch') |
||
| 379 | ->once() |
||
| 380 | ->withArgs([ |
||
| 381 | "lists/123/members/{$subscriberHash}", |
||
| 382 | [ |
||
| 383 | 'status' => 'unsubscribed', |
||
| 384 | ], |
||
| 385 | ]); |
||
| 386 | |||
| 387 | $this->newsletter->unsubscribe('[email protected]'); |
||
| 388 | } |
||
| 389 | |||
| 390 | /** @test */ |
||
| 391 | public function it_will_throw_an_error_when_unsubscribe_someone() |
||
| 392 | { |
||
| 393 | $this->client->shouldReceive('success')->andReturn(false); |
||
| 394 | $this->client->shouldReceive('getLastError')->andReturn('Error'); |
||
| 395 | |||
| 396 | $email = '[email protected]'; |
||
| 397 | |||
| 398 | $subscriberHash = 'abc123'; |
||
| 399 | |||
| 400 | $this->client->shouldReceive('subscriberHash') |
||
| 401 | ->once() |
||
| 402 | ->withArgs([$email]) |
||
| 403 | ->andReturn($subscriberHash); |
||
| 404 | |||
| 405 | $this->client |
||
| 406 | ->shouldReceive('patch') |
||
| 407 | ->once() |
||
| 408 | ->withArgs([ |
||
| 409 | "lists/123/members/{$subscriberHash}", |
||
| 410 | [ |
||
| 411 | 'status' => 'unsubscribed', |
||
| 412 | ], |
||
| 413 | ]); |
||
| 414 | |||
| 415 | $this->expectExceptionObject(ApiError::responseError('Error', 'mailchimp')); |
||
| 416 | $this->newsletter->unsubscribe('[email protected]'); |
||
| 417 | } |
||
| 418 | |||
| 419 | /** @test */ |
||
| 420 | public function it_can_unsubscribe_someone_from_a_specific_list() |
||
| 421 | { |
||
| 422 | $this->client->shouldReceive('success')->andReturn(true); |
||
| 423 | |||
| 424 | $email = '[email protected]'; |
||
| 425 | |||
| 426 | $subscriberHash = 'abc123'; |
||
| 427 | |||
| 428 | $this->client->shouldReceive('subscriberHash') |
||
| 429 | ->once() |
||
| 430 | ->withArgs([$email]) |
||
| 431 | ->andReturn($subscriberHash); |
||
| 432 | |||
| 433 | $this->client |
||
| 434 | ->shouldReceive('patch') |
||
| 435 | ->once() |
||
| 436 | ->withArgs([ |
||
| 437 | "lists/456/members/{$subscriberHash}", |
||
| 438 | [ |
||
| 439 | 'status' => 'unsubscribed', |
||
| 440 | ], |
||
| 441 | ]); |
||
| 442 | |||
| 443 | $this->newsletter->unsubscribe('[email protected]', 'list2'); |
||
| 444 | } |
||
| 445 | |||
| 446 | /** @test */ |
||
| 447 | public function it_can_delete_someone() |
||
| 448 | { |
||
| 449 | $this->client->shouldReceive('success')->andReturn(true); |
||
| 450 | |||
| 451 | $email = '[email protected]'; |
||
| 452 | |||
| 453 | $subscriberHash = 'abc123'; |
||
| 454 | |||
| 455 | $this->client->shouldReceive('subscriberHash') |
||
| 456 | ->once() |
||
| 457 | ->withArgs([$email]) |
||
| 458 | ->andReturn($subscriberHash); |
||
| 459 | |||
| 460 | $this->client |
||
| 461 | ->shouldReceive('delete') |
||
| 462 | ->once() |
||
| 463 | ->withArgs(["lists/123/members/{$subscriberHash}"]); |
||
| 464 | |||
| 465 | $this->newsletter->delete('[email protected]'); |
||
| 466 | } |
||
| 467 | |||
| 468 | /** @test */ |
||
| 469 | public function it_can_delete_someone_from_a_specific_list() |
||
| 470 | { |
||
| 471 | $this->client->shouldReceive('success')->andReturn(true); |
||
| 472 | |||
| 473 | $email = '[email protected]'; |
||
| 474 | |||
| 475 | $subscriberHash = 'abc123'; |
||
| 476 | |||
| 477 | $this->client->shouldReceive('subscriberHash') |
||
| 478 | ->once() |
||
| 479 | ->withArgs([$email]) |
||
| 480 | ->andReturn($subscriberHash); |
||
| 481 | |||
| 482 | $this->client |
||
| 483 | ->shouldReceive('delete') |
||
| 484 | ->once() |
||
| 485 | ->withArgs(["lists/456/members/{$subscriberHash}"]); |
||
| 486 | |||
| 487 | $this->newsletter->delete('[email protected]', 'list2'); |
||
| 488 | } |
||
| 489 | |||
| 490 | /** @test */ |
||
| 491 | public function it_exposes_the_api() |
||
| 492 | { |
||
| 493 | $api = $this->newsletter->getApi(); |
||
| 494 | |||
| 495 | $this->assertSame($this->client, $api); |
||
| 496 | } |
||
| 497 | |||
| 498 | /** @test */ |
||
| 499 | public function it_can_get_the_list_members() |
||
| 500 | { |
||
| 501 | $this->client->shouldReceive('success')->andReturn(true); |
||
| 502 | |||
| 503 | $this->client |
||
| 504 | ->shouldReceive('get') |
||
| 505 | ->once() |
||
| 506 | ->withArgs(['lists/123/members', []]); |
||
| 507 | |||
| 508 | $this->newsletter->getMembers(); |
||
| 509 | } |
||
| 510 | |||
| 511 | /** @test */ |
||
| 512 | public function it_can_get_the_member() |
||
| 513 | { |
||
| 514 | $this->client->shouldReceive('success')->andReturn(true); |
||
| 515 | |||
| 516 | $email = '[email protected]'; |
||
| 517 | |||
| 518 | $subscriberHash = 'abc123'; |
||
| 519 | |||
| 520 | $this->client->shouldReceive('subscriberHash') |
||
| 521 | ->once() |
||
| 522 | ->withArgs([$email]) |
||
| 523 | ->andReturn($subscriberHash); |
||
| 524 | |||
| 525 | $this->client |
||
| 526 | ->shouldReceive('get') |
||
| 527 | ->once() |
||
| 528 | ->withArgs(["lists/123/members/{$subscriberHash}"]); |
||
| 529 | |||
| 530 | $this->newsletter->getMember($email); |
||
| 531 | } |
||
| 532 | |||
| 533 | |||
| 534 | /** @test */ |
||
| 535 | public function it_can_get_the_member_from_a_specific_list() |
||
| 536 | { |
||
| 537 | $this->client->shouldReceive('success')->andReturn(true); |
||
| 538 | |||
| 539 | $email = '[email protected]'; |
||
| 540 | |||
| 541 | $subscriberHash = 'abc123'; |
||
| 542 | |||
| 543 | $this->client->shouldReceive('subscriberHash') |
||
| 544 | ->once() |
||
| 545 | ->withArgs([$email]) |
||
| 546 | ->andReturn($subscriberHash); |
||
| 547 | |||
| 548 | $this->client |
||
| 549 | ->shouldReceive('get') |
||
| 550 | ->once() |
||
| 551 | ->withArgs(["lists/456/members/{$subscriberHash}"]); |
||
| 552 | |||
| 553 | $this->newsletter->getMember($email, 'list2'); |
||
| 554 | } |
||
| 555 | } |
||
| 556 |