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 MoipTest extends TestCase |
||
14 | { |
||
15 | /** |
||
16 | * MoipTest should return instance of \Moip\Resource\Customer. |
||
17 | */ |
||
18 | public function testShouldReceiveInstanceOfCustomer() |
||
19 | { |
||
20 | $customer = new \Moip\Resource\Customer($this->moip); |
||
21 | |||
22 | $this->assertEquals($customer, $this->moip->customers()); |
||
23 | } |
||
24 | |||
25 | /** |
||
26 | * MoipTest should return instance of \Moip\Resource\Account. |
||
27 | */ |
||
28 | public function testShouldReceiveInstanceOfAccount() |
||
34 | |||
35 | /** |
||
36 | * MoipTest should return instance of \Moip\Resource\Entry. |
||
37 | */ |
||
38 | public function testShouldReceiveInstanceOfEntry() |
||
44 | |||
45 | /** |
||
46 | * MoipTest should return instance of \Moip\Resource\Orders. |
||
47 | */ |
||
48 | public function testShouldReceiveInstanceOfOrders() |
||
54 | |||
55 | /** |
||
56 | * MoipTest should return instance of \Moip\Resource\Payment. |
||
57 | */ |
||
58 | public function testShouldReceiveInstanceOfPayment() |
||
64 | |||
65 | /** |
||
66 | * MoipTest should return instance of \Moip\Resource\Multiorders. |
||
67 | */ |
||
68 | public function testShouldReceiveInstanceOfMultiorders() |
||
74 | |||
75 | /** |
||
76 | * MoipTest if a \Moip\Exceptions\testShouldRaiseValidationException is thrown and is correctly constructed. |
||
77 | */ |
||
78 | public function testShouldRaiseValidationException() |
||
108 | |||
109 | /** |
||
110 | * MoipTest if \Moip\Exceptios\UnautorizedException is thrown. |
||
111 | * |
||
112 | * @expectedException Moip\Exceptions\UnautorizedException |
||
113 | */ |
||
114 | View Code Duplication | public function testShouldRaiseUnautorizedException() |
|
|
|||
115 | { |
||
116 | if ($this->sandbox_mock == self::SANDBOX) { |
||
117 | $this->markTestSkipped('Only testable in Mock mode'); |
||
118 | |||
119 | return; |
||
120 | } |
||
121 | $body = '{ "ERROR" : "Token or Key are invalids" }'; // the body is not processed in any way, i'm putting this for completeness |
||
122 | $this->mockHttpSession($body, 401); |
||
123 | $this->moip->orders()->get('ORD-1AWC30TWYZMX'); |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * MoipTest if UnexpectedException is thrown when 500 http status code is returned. |
||
128 | * |
||
129 | * @expectedException Moip\Exceptions\UnexpectedException |
||
130 | */ |
||
131 | View Code Duplication | public function testShouldRaiseUnexpectedException500() |
|
132 | { |
||
133 | if ($this->sandbox_mock == self::SANDBOX) { |
||
134 | $this->markTestSkipped('Only testable in Mock mode'); |
||
135 | |||
136 | return; |
||
137 | } |
||
138 | $this->mockHttpSession('error', 500); // the body isn't processed |
||
139 | $this->moip->orders()->get('ORD-1AWC30TWYZMX'); |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * MoipTest if UnexpectedException is thrown when a Requests_Exception is thrown. |
||
144 | */ |
||
145 | public function testShouldRaiseUnexpectedExceptionNetworkError() |
||
146 | { |
||
147 | if ($this->sandbox_mock == self::SANDBOX) { |
||
148 | $this->markTestSkipped('Only testable in Mock mode'); |
||
149 | |||
150 | return; |
||
151 | } |
||
152 | $sess = $this->getMockBuilder('\Requests_Session')->getMock(); |
||
153 | $sess->expects($this->once())->method('request')->willThrowException(new Requests_Exception('test error', |
||
154 | 'test')); |
||
155 | $this->moip->setSession($sess); |
||
156 | |||
157 | try { |
||
158 | $this->moip->orders()->get('ORD-1AWC30TWYZMX'); |
||
159 | } catch (Exceptions\UnexpectedException $e) { |
||
160 | // test exception chaining |
||
161 | $this->assertInstanceOf('Requests_Exception', $e->getPrevious()); |
||
162 | |||
163 | return; |
||
164 | } |
||
165 | $this->fail('Exception was not thrown'); |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * MoipTest if we can connect to the API endpoints. |
||
170 | * This is primarily to make user we are using HTTPS urls and the certification verification is ok. |
||
171 | */ |
||
172 | public function testConnectEndPoints() |
||
182 | |||
183 | /** |
||
184 | * MoipTest the convertion from money to cents using floats. |
||
185 | */ |
||
186 | public function testToCents() |
||
209 | |||
210 | public function testShouldGetEndpoint() |
||
216 | } |
||
217 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.