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 MoipTestCase |
||
14 | { |
||
15 | /** |
||
16 | * Test 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 | * Test should return instance of \Moip\Resource\Entry. |
||
27 | */ |
||
28 | public function testShouldReceiveInstanceOfEntry() |
||
29 | { |
||
30 | $entry = new \Moip\Resource\Entry($this->moip); |
||
31 | |||
32 | $this->assertEquals($entry, $this->moip->entries()); |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * Test should return instance of \Moip\Resource\Orders. |
||
37 | */ |
||
38 | public function testShouldReceiveInstanceOfOrders() |
||
39 | { |
||
40 | $orders = new \Moip\Resource\Orders($this->moip); |
||
41 | |||
42 | $this->assertEquals($orders, $this->moip->orders()); |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * Test should return instance of \Moip\Resource\Payment. |
||
47 | */ |
||
48 | public function testShouldReceiveInstanceOfPayment() |
||
49 | { |
||
50 | $payment = new \Moip\Resource\Payment($this->moip); |
||
51 | |||
52 | $this->assertEquals($payment, $this->moip->payments()); |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * Test should return instance of \Moip\Resource\Multiorders. |
||
57 | */ |
||
58 | public function testShouldReceiveInstanceOfMultiorders() |
||
59 | { |
||
60 | $multiorders = new \Moip\Resource\Multiorders($this->moip); |
||
61 | |||
62 | $this->assertEquals($multiorders, $this->moip->multiorders()); |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * Test if a \Moip\Exceptions\testShouldRaiseValidationException is thrown and is correctly constructed. |
||
67 | */ |
||
68 | public function testShouldRaiseValidationException() |
||
69 | { |
||
70 | /* |
||
71 | * WARNING FIXME: |
||
72 | * The api has a bug right now it's return 'birthdateMatchesPattern' but thats wrong, it's supposed to return |
||
73 | * customer.birthDate. I talked to the moip support team, they're aware of the bug and WILL be fixing this bug |
||
74 | * wich means this test will eventually fail. |
||
75 | */ |
||
76 | $body = '{"errors":[{"code":"CUS-007","path":"birthdateMatchesPattern","description":"O valor deve ser uma string"}]}'; |
||
77 | $model = json_decode($body); |
||
78 | $error_model = $model->errors[0]; |
||
79 | $this->mockHttpSession($body, 400); |
||
80 | try { |
||
81 | $this->moip->customers()->setOwnId(uniqid()) |
||
82 | ->setFullname('Fulano teste') |
||
83 | ->setEmail('[email protected]') |
||
84 | ->setBirthDate('1111111')//invalid |
||
85 | ->create(); |
||
86 | } catch (Exceptions\ValidationException $e) { |
||
87 | $errors = $e->getErrors(); |
||
88 | $this->assertCount(1, $errors); |
||
89 | $error = $errors[0]; |
||
90 | $this->assertEquals($error_model->code, $error->getCode(), 'getCode didn\'t returned the expected value'); |
||
91 | $this->assertEquals($error_model->path, $error->getPath(), 'getPath didn\'t returned the expected value'); |
||
92 | |||
93 | return; |
||
94 | } |
||
95 | $this->fail('Exception testShouldRaiseValidationException not thrown'); |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * Test if \Moip\Exceptios\UnautorizedException is thrown. |
||
100 | */ |
||
101 | View Code Duplication | public function testShouldRaiseUnautorizedException() |
|
113 | |||
114 | /** |
||
115 | * Test if UnexpectedException is thrown when 500 http status code is returned. |
||
116 | */ |
||
117 | View Code Duplication | public function testShouldRaiseUnexpectedException500() |
|
128 | |||
129 | /** |
||
130 | * Test if UnexpectedException is thrown when a Requests_Exception is thrown. |
||
131 | */ |
||
132 | public function testShouldRaiseUnexpectedExceptionNetworkError() |
||
153 | |||
154 | /** |
||
155 | * Test if we can connect to the API endpoints. |
||
156 | * This is primarily to make user we are using HTTPS urls and the certification verification is ok. |
||
157 | */ |
||
158 | public function testConnectEndPoints() |
||
168 | |||
169 | /** |
||
170 | * Test if parsing of HATEOAS structure works. |
||
171 | */ |
||
172 | public function testLinks() |
||
173 | { |
||
174 | $order = $this->createOrder(); |
||
185 | |||
186 | /** |
||
187 | * Test the convertion from money to cents using floats. |
||
188 | */ |
||
189 | public function testToCents() |
||
212 | |||
213 | public function testShouldGetEndpoint() |
||
219 | } |
||
220 |
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.