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 |
||
| 13 | class SubscriptionTest extends \PHPUnit_Framework_TestCase |
||
| 14 | { |
||
| 15 | protected function setUp() |
||
| 16 | { |
||
| 17 | $this->dDate = new DateTime('2011-11-23T13:40:00.000-02:00'); |
||
| 18 | $this->eventDate = new DateTime('2011-11-25T20:04:00.000-02:00'); |
||
| 19 | $this->detailsAddress = new Address('SP', 'SAO PAULO', '01421000', 'J Paulista', 'ALAMEDAITU', '78', 'ap.2601'); |
||
| 20 | $this->customer = new Customer('[email protected]', 'Comprador', new Phone('11', '30389678'), $this->detailsAddress); |
||
| 21 | } |
||
| 22 | |||
| 23 | public function testConstructShouldReturnOfGetters() |
||
| 24 | { |
||
| 25 | $details = new Details('C89', 'R12', '', $this->dDate, $this->eventDate, $this->customer); |
||
| 26 | $subscription = new Subscription('FooBar', $details, 'TRACKER-AAAA', ''); |
||
| 27 | |||
| 28 | $this->assertEquals('FooBar', $subscription->getName()); |
||
| 29 | $this->assertEquals($details, $subscription->getDetails()); |
||
| 30 | $this->assertEquals('TRACKER-AAAA', $subscription->getTracker()); |
||
| 31 | |||
| 32 | $this->assertFalse($subscription->isAutomatic()); |
||
| 33 | $this->assertFalse($subscription->isManual()); |
||
| 34 | $this->assertFalse($subscription->isInitiated()); |
||
| 35 | $this->assertFalse($subscription->isPending()); |
||
| 36 | $this->assertFalse($subscription->isActive()); |
||
| 37 | $this->assertFalse($subscription->isCancelledByAcquirer()); |
||
| 38 | $this->assertFalse($subscription->isCancelledByReceiver()); |
||
| 39 | $this->assertFalse($subscription->isCancelledByCustomer()); |
||
| 40 | $this->assertFalse($subscription->isExpired()); |
||
| 41 | } |
||
| 42 | |||
| 43 | public function testIsAutomaticShouldReturnTrue() |
||
| 44 | { |
||
| 45 | $details = new Details('C89', 'R12', '', $this->dDate, $this->eventDate, $this->customer); |
||
| 46 | $subscription = new Subscription('FooBar', $details, 'TRACKER-AAAA', 'auto'); |
||
| 47 | |||
| 48 | $this->assertTrue($subscription->isAutomatic()); |
||
| 49 | $this->assertFalse($subscription->isManual()); |
||
| 50 | } |
||
| 51 | |||
| 52 | public function testIsManualShouldReturnTrue() |
||
| 53 | { |
||
| 54 | $details = new Details('C89', 'R12', '', $this->dDate, $this->eventDate, $this->customer); |
||
| 55 | $subscription = new Subscription('FooBar', $details, 'TRACKER-AAAA', 'manual'); |
||
| 56 | |||
| 57 | $this->assertTrue($subscription->isManual()); |
||
| 58 | $this->assertFalse($subscription->isAutomatic()); |
||
| 59 | } |
||
| 60 | |||
| 61 | public function testIsInitiatedShouldReturnTrue() |
||
| 62 | { |
||
| 63 | $details = new Details('C89', 'R12', 'INITIATED', $this->dDate, $this->eventDate, $this->customer); |
||
| 64 | |||
| 65 | $subscription = new Subscription('FooBar', $details, 'TRACKER-AAAA', 'manual'); |
||
| 66 | |||
| 67 | $this->assertTrue($subscription->isInitiated()); |
||
| 68 | } |
||
| 69 | |||
| 70 | public function testIsPendingShouldReturnTrue() |
||
| 71 | { |
||
| 72 | $details = new Details('C89', 'R12', 'PENDING', $this->dDate, $this->eventDate, $this->customer); |
||
| 73 | |||
| 74 | $subscription = new Subscription('FooBar', $details, 'TRACKER-AAAA', 'manual'); |
||
| 75 | |||
| 76 | $this->assertTrue($subscription->isPending()); |
||
| 77 | } |
||
| 78 | |||
| 79 | public function testIsActiveShouldReturnTrue() |
||
| 80 | { |
||
| 81 | $details = new Details('C89', 'R12', 'ACTIVE', $this->dDate, $this->eventDate, $this->customer); |
||
| 82 | |||
| 83 | $subscription = new Subscription('FooBar', $details, 'TRACKER-AAAA', 'manual'); |
||
| 84 | |||
| 85 | $this->assertTrue($subscription->isActive()); |
||
| 86 | } |
||
| 87 | |||
| 88 | public function testIsCancelledByAcquirerShouldReturnTrue() |
||
| 89 | { |
||
| 90 | $details = new Details('C89', 'R12', 'CANCELLED', $this->dDate, $this->eventDate, $this->customer); |
||
| 91 | |||
| 92 | $subscription = new Subscription('FooBar', $details, 'TRACKER-AAAA', 'manual'); |
||
| 93 | |||
| 94 | $this->assertTrue($subscription->isCancelledByAcquirer()); |
||
| 95 | } |
||
| 96 | |||
| 97 | public function testIsCancelledByReceiverShouldReturnTrue() |
||
| 98 | { |
||
| 99 | $details = new Details('C89', 'R12', 'CANCELLED_BY_RECEIVER', $this->dDate, $this->eventDate, $this->customer); |
||
| 100 | |||
| 101 | $subscription = new Subscription('FooBar', $details, 'TRACKER-AAAA', 'manual'); |
||
| 102 | |||
| 103 | $this->assertTrue($subscription->isCancelledByReceiver()); |
||
| 104 | } |
||
| 105 | |||
| 106 | public function testIsCancelledByCustomerShouldReturnTrue() |
||
| 107 | { |
||
| 108 | $details = new Details('C89', 'R12', 'CANCELLED_BY_SENDER', $this->dDate, $this->eventDate, $this->customer); |
||
| 109 | |||
| 110 | $subscription = new Subscription('FooBar', $details, 'TRACKER-AAAA', 'manual'); |
||
| 111 | |||
| 112 | $this->assertTrue($subscription->isCancelledByCustomer()); |
||
| 113 | } |
||
| 114 | |||
| 115 | public function testIsExpiredShouldReturnTrue() |
||
| 116 | { |
||
| 117 | $details = new Details('C89', 'R12', 'EXPIRED', $this->dDate, $this->eventDate, $this->customer); |
||
| 118 | |||
| 119 | $subscription = new Subscription('FooBar', $details, 'TRACKER-AAAA', 'manual'); |
||
| 120 | |||
| 121 | $this->assertTrue($subscription->isExpired()); |
||
| 122 | } |
||
| 123 | } |
||
| 124 |