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 |
||
| 11 | class SendSmsTest extends \Codeception\Test\Unit |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * @var \UnitTester |
||
| 15 | */ |
||
| 16 | protected $tester; |
||
| 17 | protected $phoneNumber = '639989764990'; |
||
| 18 | protected $message = 'Unit Testing for SendSmsTest'; |
||
| 19 | |||
| 20 | protected function _before() |
||
| 21 | { |
||
| 22 | } |
||
| 23 | |||
| 24 | protected function _after() |
||
| 25 | { |
||
| 26 | } |
||
| 27 | |||
| 28 | protected function sendSuccess(SmsProviderServicesInterface $provider) |
||
| 29 | { |
||
| 30 | $sms = new Sms($provider); |
||
| 31 | // if ($sms->getSmsProviderName() != 'Chikka') return; |
||
| 32 | |||
| 33 | $response = $sms->send($this->phoneNumber, $this->message); |
||
| 34 | |||
| 35 | $this->assertJson($response); |
||
| 36 | $response = json_decode($response, true); |
||
| 37 | |||
| 38 | $this->assertArrayHasKey('data', $response); |
||
| 39 | $this->assertArrayHasKey('code', $response['data']); |
||
| 40 | $this->assertArrayHasKey('message', $response['data']); |
||
| 41 | $this->assertArrayHasKey('provider', $response['data']); |
||
| 42 | $this->assertArrayHasKey('metadata', $response['data']); |
||
| 43 | |||
| 44 | $this->assertTrue(is_int($response['data']['code'])); |
||
| 45 | $this->assertEquals($sms->getSmsProviderName(), $response['data']['provider']); |
||
| 46 | $this->assertLessThanOrEqual(299, $response['data']['code']); |
||
| 47 | $this->assertGreaterThanOrEqual(200, $response['data']['code']); |
||
| 48 | $this->assertNotEmpty($response['data']['metadata']); |
||
| 49 | } |
||
| 50 | |||
| 51 | protected function sendFail(SmsProviderServicesInterface $provider) |
||
| 52 | { |
||
| 53 | $sms = new Sms($provider); |
||
| 54 | // if ($sms->getSmsProviderName() != 'Chikka') return; |
||
| 55 | |||
| 56 | $response = $sms->send($this->phoneNumber, $this->message); |
||
| 57 | |||
| 58 | $this->assertJson($response); |
||
| 59 | $response = json_decode($response, true); |
||
| 60 | |||
| 61 | $this->assertArrayHasKey('error', $response); |
||
| 62 | $this->assertArrayHasKey('code', $response['error']); |
||
| 63 | $this->assertArrayHasKey('message', $response['error']); |
||
| 64 | $this->assertArrayHasKey('provider', $response['error']); |
||
| 65 | $this->assertArrayHasKey('metadata', $response['error']); |
||
| 66 | |||
| 67 | $this->assertTrue(is_int($response['error']['code'])); |
||
| 68 | $this->assertEquals($sms->getSmsProviderName(), $response['error']['provider']); |
||
| 69 | $this->assertLessThanOrEqual(599, $response['error']['code']); |
||
| 70 | $this->assertGreaterThanOrEqual(400, $response['error']['code']); |
||
| 71 | $this->assertNotEmpty($response['error']['metadata']); |
||
| 72 | } |
||
| 73 | |||
| 74 | // Send via PromoTexter Success |
||
| 75 | public function testSendViaPromoTexterSuccess() |
||
| 76 | { |
||
| 77 | $provider = new PromoTexter(); |
||
| 78 | $this->sendSuccess($provider); |
||
| 79 | |||
| 80 | } |
||
| 81 | |||
| 82 | // Send via PromoTexter Fail (Incorrect Phone Number Format) |
||
| 83 | public function testSendViaPromoTexterFail() |
||
| 84 | { |
||
| 85 | $this->phoneNumber = 'AAA'; |
||
| 86 | $provider = new PromoTexter(); |
||
| 87 | $this->sendFail($provider); |
||
| 88 | } |
||
| 89 | |||
| 90 | // Send via RisingTide Success |
||
| 91 | public function testSendViaRisingTideSuccess() |
||
| 92 | { |
||
| 93 | $provider = new RisingTide(); |
||
| 94 | $this->sendSuccess($provider); |
||
| 95 | } |
||
| 96 | |||
| 97 | // Send via RisingTide Fail |
||
| 98 | public function testSendViaRisingTideFail() |
||
| 99 | { |
||
| 100 | $this->phoneNumber = 'AAA'; |
||
| 101 | $provider = new RisingTide(); |
||
| 102 | $this->sendFail($provider); |
||
| 103 | } |
||
| 104 | |||
| 105 | // Send via Semaphore Success |
||
| 106 | public function testSendViaSemaphoreSuccess() |
||
| 107 | { |
||
| 108 | $provider = new Semaphore(); |
||
| 109 | $this->sendSuccess($provider); |
||
| 110 | } |
||
| 111 | |||
| 112 | // Send via Semaphore Fail |
||
| 113 | public function testSendViaSemaphoreFail() |
||
| 114 | { |
||
| 115 | $this->phoneNumber = 'AAA'; |
||
| 116 | $provider = new Semaphore(); |
||
| 117 | $this->sendFail($provider); |
||
| 118 | } |
||
| 119 | |||
| 120 | // Send via Chikka Success |
||
| 121 | public function testSendViaChikkaSuccess() |
||
| 122 | { |
||
| 123 | $provider = new Chikka(); |
||
| 124 | $this->sendSuccess($provider); |
||
| 125 | } |
||
| 126 | |||
| 127 | // Send via Chikka Fail |
||
| 128 | public function testSendViaChikkaFail() |
||
| 129 | { |
||
| 130 | $this->phoneNumber = 'AAA'; |
||
| 131 | $provider = new Chikka(); |
||
| 132 | $this->sendFail($provider); |
||
| 133 | } |
||
| 134 | |||
| 135 | // Send via Chikka Success |
||
| 136 | public function testSendViaNexmoSuccess() |
||
| 137 | { |
||
| 138 | $provider = new Nexmo(); |
||
| 139 | $this->sendSuccess($provider); |
||
| 140 | } |
||
| 141 | |||
| 142 | // Send via Nexmo Fail |
||
| 143 | public function testSendViaNexmoFail() |
||
| 144 | { |
||
| 145 | $this->phoneNumber = 'AAA'; |
||
| 146 | $provider = new Nexmo(); |
||
| 147 | $this->sendFail($provider); |
||
| 148 | } |
||
| 149 | |||
| 150 | // Send via Twilio Success |
||
| 151 | public function testSendViaTwilioSuccess() |
||
| 152 | { |
||
| 153 | $provider = new Twilio(); |
||
| 154 | $this->sendSuccess($provider); |
||
| 155 | } |
||
| 156 | |||
| 157 | // Send via Nexmo Fail |
||
| 158 | public function testSendViaTwilioFail() |
||
| 159 | { |
||
| 160 | $this->phoneNumber = 'AAA'; |
||
| 161 | $provider = new Twilio(); |
||
| 162 | $this->sendFail($provider); |
||
| 163 | } |
||
| 164 | } |