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:
Complex classes like QueueTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use QueueTest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | final class QueueTest extends TestCase |
||
| 14 | { |
||
| 15 | private $collection; |
||
| 16 | private $mongoUrl; |
||
| 17 | private $queue; |
||
| 18 | |||
| 19 | public function setUp() |
||
| 20 | { |
||
| 21 | $this->mongoUrl = getenv('TESTING_MONGO_URL') ?: 'mongodb://localhost:27017'; |
||
| 22 | $mongo = new \MongoDB\Client( |
||
| 23 | $this->mongoUrl, |
||
| 24 | [], |
||
| 25 | ['typeMap' => ['root' => 'array', 'document' => 'array', 'array' => 'array']] |
||
| 26 | ); |
||
| 27 | $this->collection = $mongo->selectDatabase('testing')->selectCollection('messages'); |
||
| 28 | $this->collection->drop(); |
||
| 29 | |||
| 30 | $this->queue = new Queue($this->mongoUrl, 'testing', 'messages'); |
||
| 31 | } |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @test |
||
| 35 | * @covers ::__construct |
||
| 36 | * @expectedException \InvalidArgumentException |
||
| 37 | */ |
||
| 38 | public function constructWithNonStringUrl() |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @test |
||
| 45 | * @covers ::ensureGetIndex |
||
| 46 | */ |
||
| 47 | public function ensureGetIndex() |
||
| 48 | { |
||
| 49 | $this->queue->ensureGetIndex(['type' => 1], ['boo' => -1]); |
||
| 50 | $this->queue->ensureGetIndex(['another.sub' => 1]); |
||
| 51 | |||
| 52 | $indexes = iterator_to_array($this->collection->listIndexes()); |
||
| 53 | $this->assertSame(3, count($indexes)); |
||
| 54 | |||
| 55 | $expectedOne = [ |
||
| 56 | 'earliestGet' => 1, |
||
| 57 | 'payload.type' => 1, |
||
| 58 | 'priority' => 1, |
||
| 59 | 'created' => 1, |
||
| 60 | 'payload.boo' => -1, |
||
| 61 | ]; |
||
| 62 | $this->assertSame($expectedOne, $indexes[1]['key']); |
||
| 63 | |||
| 64 | $expectedTwo = [ |
||
| 65 | 'earliestGet' => 1, |
||
| 66 | 'payload.another.sub' => 1, |
||
| 67 | 'priority' => 1, |
||
| 68 | 'created' => 1, |
||
| 69 | ]; |
||
| 70 | $this->assertSame($expectedTwo, $indexes[2]['key']); |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @test |
||
| 75 | * @covers ::ensureGetIndex |
||
| 76 | * @expectedException \Exception |
||
| 77 | */ |
||
| 78 | public function ensureGetIndexWithTooLongCollectionName() |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @test |
||
| 89 | * @covers ::ensureGetIndex |
||
| 90 | * @expectedException \InvalidArgumentException |
||
| 91 | */ |
||
| 92 | public function ensureGetIndexWithNonStringBeforeSortKey() |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @test |
||
| 99 | * @covers ::ensureGetIndex |
||
| 100 | * @expectedException \InvalidArgumentException |
||
| 101 | */ |
||
| 102 | public function ensureGetIndexWithNonStringAfterSortKey() |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @test |
||
| 109 | * @covers ::ensureGetIndex |
||
| 110 | * @expectedException \InvalidArgumentException |
||
| 111 | */ |
||
| 112 | public function ensureGetIndexWithBadBeforeSortValue() |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @test |
||
| 119 | * @covers ::ensureGetIndex |
||
| 120 | * @expectedException \InvalidArgumentException |
||
| 121 | */ |
||
| 122 | public function ensureGetIndexWithBadAfterSortValue() |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Verifies the behaviour of the Queue when it cannot create an index after 5 attempts. |
||
| 129 | * |
||
| 130 | * @test |
||
| 131 | * @covers ::ensureGetIndex |
||
| 132 | * @expectedException \Exception |
||
| 133 | * @expectedExceptionMessage couldnt create index after 5 attempts |
||
| 134 | */ |
||
| 135 | public function ensureIndexCannotBeCreatedAfterFiveAttempts() |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @test |
||
| 147 | * @covers ::ensureCountIndex |
||
| 148 | */ |
||
| 149 | public function ensureCountIndex() |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @test |
||
| 166 | * @covers ::ensureCountIndex |
||
| 167 | */ |
||
| 168 | public function ensureCountIndexWithPrefixOfPrevious() |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @test |
||
| 182 | * @covers ::ensureCountIndex |
||
| 183 | * @expectedException \InvalidArgumentException |
||
| 184 | */ |
||
| 185 | public function ensureCountIndexWithNonStringKey() |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @test |
||
| 192 | * @covers ::ensureCountIndex |
||
| 193 | * @expectedException \InvalidArgumentException |
||
| 194 | */ |
||
| 195 | public function ensureCountIndexWithBadValue() |
||
| 199 | |||
| 200 | /** |
||
| 201 | * @test |
||
| 202 | * @covers ::get |
||
| 203 | */ |
||
| 204 | public function getByBadQuery() |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @test |
||
| 216 | * @covers ::get |
||
| 217 | */ |
||
| 218 | public function getWithNegativePollDuration() |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @test |
||
| 226 | * @covers ::get |
||
| 227 | * @expectedException \InvalidArgumentException |
||
| 228 | */ |
||
| 229 | public function getWithNonStringKey() |
||
| 230 | { |
||
| 231 | $this->queue->get([0 => 'a value'], 0); |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @test |
||
| 236 | * @covers ::get |
||
| 237 | */ |
||
| 238 | public function getByFullQuery() |
||
| 239 | { |
||
| 240 | $messageOne = ['id' => 'SHOULD BE REMOVED', 'key1' => 0, 'key2' => true]; |
||
| 241 | |||
| 242 | $this->queue->send($messageOne); |
||
| 243 | $this->queue->send(['key' => 'value']); |
||
| 244 | |||
| 245 | $result = $this->queue->get($messageOne, PHP_INT_MAX, 0); |
||
| 246 | |||
| 247 | $this->assertNotSame($messageOne['id'], $result['id']); |
||
| 248 | |||
| 249 | $messageOne['id'] = $result['id']; |
||
| 250 | $this->assertSame($messageOne, $result); |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @test |
||
| 255 | * @covers ::get |
||
| 256 | */ |
||
| 257 | public function getBySubDocQuery() |
||
| 258 | { |
||
| 259 | $messageTwo = [ |
||
| 260 | 'one' => [ |
||
| 261 | 'two' => [ |
||
| 262 | 'three' => 5, |
||
| 263 | 'notused' => 'notused', |
||
| 264 | ], |
||
| 265 | 'notused' => 'notused', |
||
| 266 | ], |
||
| 267 | 'notused' => 'notused', |
||
| 268 | ]; |
||
| 269 | |||
| 270 | $this->queue->send(['key1' => 0, 'key2' => true]); |
||
| 271 | $this->queue->send($messageTwo); |
||
| 272 | |||
| 273 | $result = $this->queue->get(['one.two.three' => ['$gt' => 4]], PHP_INT_MAX, 0); |
||
| 274 | $this->assertSame(['id' => $result['id']] + $messageTwo, $result); |
||
| 275 | } |
||
| 276 | |||
| 277 | /** |
||
| 278 | * @test |
||
| 279 | * @covers ::get |
||
| 280 | */ |
||
| 281 | public function getBeforeAck() |
||
| 282 | { |
||
| 283 | $messageOne = ['key1' => 0, 'key2' => true]; |
||
| 284 | |||
| 285 | $this->queue->send($messageOne); |
||
| 286 | $this->queue->send(['key' => 'value']); |
||
| 287 | |||
| 288 | $this->queue->get($messageOne, PHP_INT_MAX, 0); |
||
| 289 | |||
| 290 | //try get message we already have before ack |
||
| 291 | $result = $this->queue->get($messageOne, PHP_INT_MAX, 0); |
||
| 292 | $this->assertNull($result); |
||
| 293 | } |
||
| 294 | |||
| 295 | /** |
||
| 296 | * @test |
||
| 297 | * @covers ::get |
||
| 298 | */ |
||
| 299 | public function getWithCustomPriority() |
||
| 317 | |||
| 318 | /** |
||
| 319 | * @test |
||
| 320 | * @covers ::get |
||
| 321 | */ |
||
| 322 | public function getWithTimeBasedPriority() |
||
| 340 | |||
| 341 | /** |
||
| 342 | * @test |
||
| 343 | * @covers ::get |
||
| 344 | */ |
||
| 345 | public function getWithTimeBasedPriorityWithOldTimestamp() |
||
| 367 | |||
| 368 | /** |
||
| 369 | * @test |
||
| 370 | * @covers ::get |
||
| 371 | */ |
||
| 372 | public function getWait() |
||
| 383 | |||
| 384 | /** |
||
| 385 | * @test |
||
| 386 | * @covers ::get |
||
| 387 | */ |
||
| 388 | public function earliestGet() |
||
| 400 | |||
| 401 | /** |
||
| 402 | * @test |
||
| 403 | * @covers ::get |
||
| 404 | */ |
||
| 405 | public function resetStuck() |
||
| 406 | { |
||
| 407 | $messageOne = ['key' => 0]; |
||
| 408 | $messageTwo = ['key' => 1]; |
||
| 409 | |||
| 410 | $this->queue->send($messageOne); |
||
| 411 | $this->queue->send($messageTwo); |
||
| 412 | |||
| 413 | //sets to running |
||
| 414 | $this->collection->updateOne( |
||
| 415 | ['payload.key' => 0], |
||
| 416 | ['$set' => ['earliestGet' => new \MongoDB\BSON\UTCDateTime(time() * 1000)]] |
||
| 417 | ); |
||
| 418 | $this->collection->updateOne( |
||
| 419 | ['payload.key' => 1], |
||
| 420 | ['$set' => ['earliestGet' => new \MongoDB\BSON\UTCDateTime(time() * 1000)]] |
||
| 421 | ); |
||
| 422 | |||
| 423 | $this->assertSame( |
||
| 424 | 2, |
||
| 425 | $this->collection->count( |
||
| 426 | ['earliestGet' => ['$lte' => new \MongoDB\BSON\UTCDateTime((int)(microtime(true) * 1000))]] |
||
| 427 | ) |
||
| 428 | ); |
||
| 429 | |||
| 430 | //resets and gets messageOne |
||
| 431 | $this->assertNotNull($this->queue->get($messageOne, PHP_INT_MAX, 0)); |
||
| 432 | |||
| 433 | $this->assertSame( |
||
| 434 | 1, |
||
| 435 | $this->collection->count( |
||
| 436 | ['earliestGet' => ['$lte' => new \MongoDB\BSON\UTCDateTime((int)(microtime(true) * 1000))]] |
||
| 437 | ) |
||
| 438 | ); |
||
| 439 | } |
||
| 440 | |||
| 441 | /** |
||
| 442 | * @test |
||
| 443 | * @covers ::count |
||
| 444 | * @expectedException \InvalidArgumentException |
||
| 445 | */ |
||
| 446 | public function countWithNonStringKey() |
||
| 450 | |||
| 451 | /** |
||
| 452 | * @test |
||
| 453 | * @covers ::count |
||
| 454 | */ |
||
| 455 | public function testCount() |
||
| 473 | |||
| 474 | /** |
||
| 475 | * @test |
||
| 476 | * @covers ::ack |
||
| 477 | */ |
||
| 478 | public function ack() |
||
| 491 | |||
| 492 | /** |
||
| 493 | * @test |
||
| 494 | * @covers ::ack |
||
| 495 | * @expectedException \InvalidArgumentException |
||
| 496 | */ |
||
| 497 | public function ackBadArg() |
||
| 501 | |||
| 502 | /** |
||
| 503 | * @test |
||
| 504 | * @covers ::ackSend |
||
| 505 | */ |
||
| 506 | public function ackSend() |
||
| 507 | { |
||
| 508 | $messageOne = ['key1' => 0, 'key2' => true]; |
||
| 509 | $messageThree = ['hi' => 'there', 'rawr' => 2]; |
||
| 510 | |||
| 511 | $this->queue->send($messageOne); |
||
| 512 | $this->queue->send(['key' => 'value']); |
||
| 513 | |||
| 514 | $resultOne = $this->queue->get($messageOne, PHP_INT_MAX, 0); |
||
| 515 | $this->assertSame(2, $this->collection->count()); |
||
| 516 | |||
| 517 | $this->queue->ackSend($resultOne, $messageThree); |
||
| 518 | $this->assertSame(2, $this->collection->count()); |
||
| 519 | |||
| 520 | $actual = $this->queue->get(['hi' => 'there'], PHP_INT_MAX, 0); |
||
| 521 | $expected = ['id' => $resultOne['id']] + $messageThree; |
||
| 522 | |||
| 523 | $actual['id'] = $actual['id']->__toString(); |
||
| 524 | $expected['id'] = $expected['id']->__toString(); |
||
| 525 | $this->assertSame($expected, $actual); |
||
| 526 | } |
||
| 527 | |||
| 528 | /** |
||
| 529 | * Verify earliestGet with ackSend. |
||
| 530 | * |
||
| 531 | * @test |
||
| 532 | * @covers ::ackSend |
||
| 533 | * |
||
| 534 | * @return void |
||
| 535 | */ |
||
| 536 | public function ackSendWithEarliestGet() |
||
| 547 | |||
| 548 | /** |
||
| 549 | * @test |
||
| 550 | * @covers ::ackSend |
||
| 551 | * @expectedException \InvalidArgumentException |
||
| 552 | */ |
||
| 553 | public function ackSendWithWrongIdType() |
||
| 557 | |||
| 558 | /** |
||
| 559 | * @test |
||
| 560 | * @covers ::ackSend |
||
| 561 | * @expectedException \InvalidArgumentException |
||
| 562 | */ |
||
| 563 | public function ackSendWithNanPriority() |
||
| 567 | |||
| 568 | /** |
||
| 569 | * @test |
||
| 570 | * @covers ::ackSend |
||
| 571 | */ |
||
| 572 | public function ackSendWithHighEarliestGet() |
||
| 587 | |||
| 588 | /** |
||
| 589 | * @covers ::ackSend |
||
| 590 | */ |
||
| 591 | public function ackSendWithLowEarliestGet() |
||
| 606 | |||
| 607 | /** |
||
| 608 | * @test |
||
| 609 | * @covers ::requeue |
||
| 610 | */ |
||
| 611 | public function requeue() |
||
| 612 | { |
||
| 613 | $messageOne = ['key1' => 0, 'key2' => true]; |
||
| 614 | |||
| 615 | $this->queue->send($messageOne); |
||
| 616 | $this->queue->send(['key' => 'value']); |
||
| 617 | |||
| 618 | $resultBeforeRequeue = $this->queue->get($messageOne, PHP_INT_MAX, 0); |
||
| 619 | |||
| 620 | $this->queue->requeue($resultBeforeRequeue); |
||
| 621 | $this->assertSame(2, $this->collection->count()); |
||
| 622 | |||
| 623 | $resultAfterRequeue = $this->queue->get($messageOne, 0); |
||
| 624 | $this->assertSame(['id' => $resultAfterRequeue['id']] + $messageOne, $resultAfterRequeue); |
||
| 625 | } |
||
| 626 | |||
| 627 | /** |
||
| 628 | * @test |
||
| 629 | * @covers ::requeue |
||
| 630 | * @expectedException \InvalidArgumentException |
||
| 631 | */ |
||
| 632 | public function requeueBadArg() |
||
| 633 | { |
||
| 634 | $this->queue->requeue(['id' => new \stdClass()]); |
||
| 635 | } |
||
| 636 | |||
| 637 | /** |
||
| 638 | * @test |
||
| 639 | * @covers ::send |
||
| 640 | */ |
||
| 641 | View Code Duplication | public function send() |
|
| 642 | { |
||
| 643 | $payload = ['key1' => 0, 'key2' => true]; |
||
| 644 | $this->queue->send($payload, 34, 0.8); |
||
| 645 | |||
| 646 | $this->assertSingleMessage( |
||
| 647 | [ |
||
| 648 | 'payload' => $payload, |
||
| 649 | 'earliestGet' => 34, |
||
| 650 | 'priority' => 0.8, |
||
| 651 | ] |
||
| 652 | ); |
||
| 653 | } |
||
| 654 | |||
| 655 | /** |
||
| 656 | * @test |
||
| 657 | * @covers ::send |
||
| 658 | * @expectedException \InvalidArgumentException |
||
| 659 | */ |
||
| 660 | public function sendWithNanPriority() |
||
| 664 | |||
| 665 | /** |
||
| 666 | * @test |
||
| 667 | * @covers ::send |
||
| 668 | */ |
||
| 669 | public function sendWithHighEarliestGet() |
||
| 681 | |||
| 682 | /** |
||
| 683 | * @test |
||
| 684 | * @covers ::send |
||
| 685 | */ |
||
| 686 | public function sendWithLowEarliestGet() |
||
| 687 | { |
||
| 688 | $this->queue->send([], -1); |
||
| 689 | |||
| 690 | $this->assertSingleMessage( |
||
| 691 | [ |
||
| 692 | 'payload' => [], |
||
| 693 | 'earliestGet' => 0, |
||
| 694 | 'priority' => 0.0, |
||
| 695 | ] |
||
| 696 | ); |
||
| 697 | } |
||
| 698 | |||
| 699 | /** |
||
| 700 | * Verify Queue can be constructed with \MongoDB\Collection |
||
| 701 | * |
||
| 702 | * @test |
||
| 703 | * @covers ::__construct |
||
| 704 | * |
||
| 705 | * @return void |
||
| 706 | */ |
||
| 707 | View Code Duplication | public function constructWithCollection() |
|
| 708 | { |
||
| 709 | $queue = new Queue($this->collection); |
||
| 710 | |||
| 711 | $payload = ['key1' => 0, 'key2' => true]; |
||
| 712 | $queue->send($payload, 34, 0.8); |
||
| 713 | |||
| 714 | $this->assertSingleMessage( |
||
| 715 | [ |
||
| 716 | 'payload' => $payload, |
||
| 717 | 'earliestGet' => 34, |
||
| 718 | 'priority' => 0.8, |
||
| 719 | ] |
||
| 720 | ); |
||
| 721 | |||
| 722 | } |
||
| 723 | |||
| 724 | private function assertSingleMessage(array $expected) |
||
| 738 | } |
||
| 739 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.