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 SearchOrdersRequestTest extends TestCase |
||
12 | { |
||
13 | /** |
||
14 | * @test |
||
15 | */ |
||
16 | public function it_sets_order_id_and_returns_itself() |
||
17 | { |
||
18 | $request = new SearchOrdersRequest; |
||
19 | |||
20 | $this->assertSame($request, $request->setSellerId('SELLER_ID')->setOrderId('ORDER_ID')); |
||
21 | $simpleXml = simplexml_load_string($request->getParams()); |
||
22 | |||
23 | $this->assertEquals('ORDER_ID', $simpleXml->Search->Condition->OrderId->__toString()); |
||
24 | } |
||
25 | |||
26 | /** |
||
27 | * @test |
||
28 | * @expectedException \LogicException |
||
29 | */ |
||
30 | public function it_cannot_order_id_seen_more_than_once() |
||
31 | { |
||
32 | $request = new SearchOrdersRequest; |
||
33 | $this->assertSame($request, $request->setSellerId('SELLER_ID')->setOrderId('ORDER_ID')); |
||
34 | |||
35 | $request->setOrderId('ORDER_ID2'); |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * @test |
||
40 | */ |
||
41 | View Code Duplication | public function it_sets_seller_id_and_returns_itself() |
|
50 | |||
51 | /** |
||
52 | * @test |
||
53 | * @expectedException \LogicException |
||
54 | */ |
||
55 | public function it_cannot_set_seller_id_more_than_once() |
||
56 | { |
||
57 | $request = new SearchOrdersRequest; |
||
58 | $this->assertSame($request, $request->setOrderId('ORDER_ID')->setSellerId('SELLER_ID_1')); |
||
59 | |||
60 | $request->setSellerId('SELLER_ID_2'); |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * @test |
||
65 | */ |
||
66 | public function it_sets_is_seen_and_returns_itself() |
||
67 | { |
||
68 | $request = new SearchOrdersRequest; |
||
69 | |||
70 | $this->assertSame($request, $request->setOrderId('ORDER_ID')->setSellerId('SELLER_ID')->setIsSeen(true)); |
||
71 | $simpleXml = simplexml_load_string($request->getParams()); |
||
72 | |||
73 | $this->assertEquals('true', $simpleXml->Search->Condition->IsSeen->__toString()); |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * @test |
||
78 | * @expectedException \LogicException |
||
79 | */ |
||
80 | public function it_cannot_set_is_seen_more_than_once() |
||
81 | { |
||
82 | $request = new SearchOrdersRequest; |
||
83 | $this->assertSame($request, $request->setOrderId('ORDER_ID')->setSellerId('SELLER_ID')->setIsSeen(false)); |
||
84 | |||
85 | $request->setIsSeen(true); |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * @test |
||
90 | */ |
||
91 | public function it_sets_ordered_datetime_range_and_returns_itself() |
||
104 | |||
105 | /** |
||
106 | * @test |
||
107 | * @expectedException \LogicException |
||
108 | */ |
||
109 | View Code Duplication | public function it_cannot_set_ordered_datetime_range_when_to_is_earlier_than_from() |
|
118 | |||
119 | /** |
||
120 | * @test |
||
121 | */ |
||
122 | public function it_does_not_set_ordered_datetime_range_when_null() |
||
123 | { |
||
124 | $request = new SearchOrdersRequest; |
||
125 | $this->assertSame($request, $request->setOrderId('ORDER_ID')->setSellerId('SELLER_ID')); |
||
126 | |||
127 | $this->assertSame($request, $request->setOrderedDateTimeRange(null, new DateTimeImmutable())); |
||
128 | $simpleXml = simplexml_load_string($request->getParams()); |
||
129 | $this->assertFalse(isset($simpleXml->Search->Condition->OrderTimeFrom)); |
||
130 | |||
131 | |||
132 | $request = new SearchOrdersRequest; |
||
133 | $this->assertSame($request, $request->setOrderId('ORDER_ID')->setSellerId('SELLER_ID')); |
||
134 | |||
135 | $this->assertSame($request, $request->setOrderedDateTimeRange(new DateTimeImmutable(), null)); |
||
136 | $simpleXml = simplexml_load_string($request->getParams()); |
||
137 | |||
138 | $this->assertFalse(isset($simpleXml->Search->Condition->OrderTimeTo)); |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * @test |
||
143 | * @expectedException \LogicException |
||
144 | */ |
||
145 | View Code Duplication | public function it_cannot_set_from_of_ordered_datetime_range_more_than_once() |
|
146 | { |
||
147 | $request = new SearchOrdersRequest; |
||
148 | $this->assertSame($request, $request->setOrderedDateTimeRange(new DateTimeImmutable, null)); |
||
149 | |||
150 | $request->setOrderedDateTimeRange(new DateTimeImmutable, null); |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * @test |
||
155 | * @expectedException \LogicException |
||
156 | */ |
||
157 | View Code Duplication | public function it_cannot_set_to_of_ordered_datetime_range_more_than_once() |
|
164 | |||
165 | /** |
||
166 | * @test |
||
167 | * @expectedException \InvalidArgumentException |
||
168 | */ |
||
169 | public function it_cannot_set_ordered_datetime_range_to_both_null() |
||
170 | { |
||
171 | $request = new SearchOrdersRequest; |
||
172 | |||
173 | $request->setOrderedDateTimeRange(null, null); |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * @test |
||
178 | */ |
||
179 | public function it_sets_order_status_and_returns_itself() |
||
180 | { |
||
181 | $request = new SearchOrdersRequest; |
||
182 | |||
183 | $this->assertSame($request, |
||
184 | $request->setOrderId('ORDER_ID') |
||
185 | ->setSellerId('SELLER_ID') |
||
186 | ->setOrderStatus(OrderStatus::PREORDERED())); |
||
187 | $simpleXml = simplexml_load_string($request->getParams()); |
||
188 | |||
189 | $this->assertEquals(OrderStatus::PREORDERED()->getValue(), $simpleXml->Search->Condition->OrderStatus->__toString()); |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * @test |
||
194 | * @expectedException \LogicException |
||
195 | */ |
||
196 | public function it_cannot_set_order_status_more_than_once() |
||
197 | { |
||
198 | $request = new SearchOrdersRequest; |
||
199 | $this->assertSame($request, |
||
200 | $request->setOrderId('ORDER_ID') |
||
201 | ->setSellerId('SELLER_ID') |
||
202 | ->setOrderStatus(OrderStatus::PROCESSED())); |
||
203 | |||
204 | $request->setOrderStatus(OrderStatus::PROCESSING()); |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * @test |
||
209 | */ |
||
210 | public function it_sets_ship_status_and_returns_itself() |
||
211 | { |
||
212 | $request = new SearchOrdersRequest; |
||
213 | |||
214 | $this->assertSame($request, |
||
215 | $request->setOrderId('ORDER_ID') |
||
216 | ->setSellerId('SELLER_ID') |
||
217 | ->setShipStatus(ShipStatus::SHIPPABLE())); |
||
218 | $simpleXml = simplexml_load_string($request->getParams()); |
||
219 | |||
220 | $this->assertEquals(ShipStatus::SHIPPABLE()->getValue(), $simpleXml->Search->Condition->ShipStatus->__toString()); |
||
221 | } |
||
222 | |||
223 | /** |
||
224 | * @test |
||
225 | * @expectedException \LogicException |
||
226 | */ |
||
227 | public function it_cannot_set_ship_status_more_than_once() |
||
228 | { |
||
229 | $request = new SearchOrdersRequest; |
||
230 | $this->assertSame($request, |
||
231 | $request->setOrderId('ORDER_ID') |
||
232 | ->setSellerId('SELLER_ID') |
||
233 | ->setShipStatus(ShipStatus::SHIPPED())); |
||
234 | |||
235 | $request->setShipStatus(ShipStatus::UNSHIPPABLE()); |
||
236 | } |
||
237 | |||
238 | /** |
||
239 | * @test |
||
240 | */ |
||
241 | public function it_sets_store_status_and_returns_itself() |
||
242 | { |
||
243 | $request = new SearchOrdersRequest; |
||
244 | |||
245 | $this->assertSame($request, |
||
246 | $request->setOrderId('ORDER_ID') |
||
247 | ->setSellerId('SELLER_ID') |
||
248 | ->setStoreStatus(StoreStatus::STORE_STATUS1())); |
||
249 | $simpleXml = simplexml_load_string($request->getParams()); |
||
250 | |||
251 | $this->assertEquals(StoreStatus::STORE_STATUS1()->getValue(), $simpleXml->Search->Condition->StoreStatus->__toString()); |
||
252 | } |
||
253 | |||
254 | /** |
||
255 | * @test |
||
256 | * @expectedException \LogicException |
||
257 | */ |
||
258 | public function it_cannot_set_store_status_more_than_once() |
||
259 | { |
||
260 | $request = new SearchOrdersRequest; |
||
261 | $this->assertSame($request, $request->setSellerId('SELLER_ID')->setStoreStatus(StoreStatus::STORE_STATUS2())); |
||
262 | |||
263 | $request->setStoreStatus(StoreStatus::STORE_STATUS3()); |
||
264 | } |
||
265 | |||
266 | /** |
||
267 | * @test |
||
268 | */ |
||
269 | View Code Duplication | public function it_sets_offset() |
|
280 | |||
281 | /** |
||
282 | * @test |
||
283 | * @expectedException \LogicException |
||
284 | */ |
||
285 | View Code Duplication | public function it_cannot_set_offset_more_than_once() |
|
286 | { |
||
287 | $request = new SearchOrdersRequest; |
||
288 | $this->assertSame($request, $request->setOrderId('ORDER_ID')->setSellerId('SELLER_ID')); |
||
289 | $this->assertSame($request, $request->setOffset(5)); |
||
290 | |||
291 | $request->setOffset(5); |
||
292 | } |
||
293 | |||
294 | /** |
||
295 | * @test |
||
296 | * @expectedException \InvalidArgumentException |
||
297 | */ |
||
298 | public function it_cannot_set_offset_to_less_than_zero() |
||
304 | |||
305 | |||
306 | |||
307 | |||
308 | /** |
||
309 | * @test |
||
310 | */ |
||
311 | public function it_sets_limit() |
||
312 | { |
||
313 | $request = new SearchOrdersRequest; |
||
314 | $this->assertSame($request, $request->setOrderId('ORDER_ID')->setSellerId('SELLER_ID')); |
||
315 | |||
316 | $request->setLimit(5); |
||
317 | $simpleXml = simplexml_load_string($request->getParams()); |
||
318 | |||
319 | $this->assertEquals(5, $simpleXml->Search->Result->__toString()); |
||
320 | |||
321 | } |
||
322 | |||
323 | /** |
||
324 | * @test |
||
325 | * @expectedException \LogicException |
||
326 | */ |
||
327 | public function it_cannot_set_limit_more_than_once() |
||
335 | |||
336 | /** |
||
337 | * @test |
||
338 | * @expectedException \InvalidArgumentException |
||
339 | */ |
||
340 | public function it_cannot_set_limit_to_less_than_zero() |
||
346 | |||
347 | /** |
||
348 | * @test |
||
349 | * @expectedException \Shippinno\YahooShoppingJp\Exception\InvalidRequestException |
||
350 | */ |
||
351 | View Code Duplication | public function it_validates_that_seller_id_is_set() |
|
352 | { |
||
353 | $request = new SearchOrdersRequest; |
||
354 | $from = new DateTimeImmutable('2000-01-01 00:00:00'); |
||
355 | $to = new DateTimeImmutable('2000-01-02 00:00:00'); |
||
356 | |||
357 | $this->assertSame($request, $request->setOrderedDateTimeRange($from, $to)); |
||
358 | |||
359 | $request->getParams(); |
||
360 | } |
||
361 | |||
362 | /** |
||
363 | * @test |
||
364 | * @expectedException \Shippinno\YahooShoppingJp\Exception\InvalidRequestException |
||
365 | * @expectedExceptionMessage OrderId,OrderTime or OrderTimeFrom&OrderTimeTo is necessary. |
||
366 | */ |
||
367 | public function it_validates_that_order_id_or_datetime_is_set() |
||
375 | } |
||
376 |
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.