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 |
||
16 | abstract class TestCase extends BaseTestCase |
||
17 | { |
||
18 | /** |
||
19 | * Variables representing the test modes. On MOCK mode no http request will be made. |
||
20 | * In SANDBOX mode HTTP requests will be made to the Moip::SANDBOX_ENDPOINT, the authentication information |
||
21 | * is retrieved from the MOIP_TOKEN and MOIP_KEY environment variables. |
||
22 | */ |
||
23 | const MOCK = 'mock'; |
||
24 | const SANDBOX = 'sandbox'; |
||
25 | |||
26 | /** |
||
27 | * Intance of \Moip\Moip. |
||
28 | * |
||
29 | * @var \Moip\Moip |
||
30 | * */ |
||
31 | protected $moip; |
||
32 | |||
33 | /** |
||
34 | * @var string current format for dates. |
||
35 | */ |
||
36 | protected $date_format = 'Y-m-d'; |
||
37 | |||
38 | /** |
||
39 | * @var string date used for testing. |
||
40 | */ |
||
41 | protected $date_string = '1989-06-01'; |
||
42 | //todo: add the ability to use the play(https://github.com/rodrigosaito/mockwebserver-player) files from the jada sdk |
||
43 | //the two responses below were based on the moip Java sdk's test files (https://github.com/moip/moip-sdk-java/) |
||
44 | /** |
||
45 | * @var string response from the client moip API. |
||
46 | */ |
||
47 | protected $body_client; |
||
48 | |||
49 | /** |
||
50 | * @var string response from the order moip API. |
||
51 | */ |
||
52 | protected $body_order; |
||
53 | |||
54 | /** |
||
55 | * @var string response from moip API. |
||
56 | */ |
||
57 | protected $body_cc_pay_pci; |
||
58 | |||
59 | /** |
||
60 | * @var string response from moip API. |
||
61 | */ |
||
62 | protected $body_cc_pay_pci_store; |
||
63 | |||
64 | /** |
||
65 | * @var string response from moip API. |
||
66 | */ |
||
67 | protected $body_cc_pay_pci_escrow; |
||
68 | |||
69 | /** |
||
70 | * @var string response from moip API. |
||
71 | */ |
||
72 | protected $body_release_escrow; |
||
73 | |||
74 | /** |
||
75 | * @var string response from moip API. |
||
76 | */ |
||
77 | protected $body_billet_pay; |
||
78 | |||
79 | /** |
||
80 | * @var string response from moip API. |
||
81 | */ |
||
82 | protected $body_refund_full_bankaccount; |
||
83 | |||
84 | /** |
||
85 | * @var string response from moip API. |
||
86 | */ |
||
87 | protected $body_refund_partial_bankaccount; |
||
88 | |||
89 | /** |
||
90 | * @var string response from moip API. |
||
91 | */ |
||
92 | protected $body_notification_preference; |
||
93 | |||
94 | /** |
||
95 | * @var string response from moip API. |
||
96 | */ |
||
97 | protected $body_moip_account_create; |
||
98 | |||
99 | /** |
||
100 | * @var string response from moip API. |
||
101 | */ |
||
102 | protected $body_moip_account_get; |
||
103 | |||
104 | /** |
||
105 | * @var string response from moip API. |
||
106 | */ |
||
107 | protected $body_order_list; |
||
108 | |||
109 | /** |
||
110 | * @var string response from moip API. |
||
111 | */ |
||
112 | protected $body_notification_list; |
||
113 | |||
114 | /** |
||
115 | * @var string response from moip API. |
||
116 | */ |
||
117 | protected $body_transfers_create; |
||
118 | |||
119 | /** |
||
120 | * @var string response from moip API. |
||
121 | */ |
||
122 | protected $body_transfers_list; |
||
123 | |||
124 | /** |
||
125 | * @var string response from moip API. |
||
126 | */ |
||
127 | protected $body_transfers_revert; |
||
128 | |||
129 | /** |
||
130 | * @var string response from moip API. |
||
131 | */ |
||
132 | protected $body_bank_account_create; |
||
133 | |||
134 | /** |
||
135 | * @var string response from moip API. |
||
136 | */ |
||
137 | protected $body_bank_account_list; |
||
138 | |||
139 | /** |
||
140 | * @var string response from moip API. |
||
141 | */ |
||
142 | protected $body_bank_account_update; |
||
143 | |||
144 | /** |
||
145 | * @var string response from moip API. |
||
146 | */ |
||
147 | protected $body_balances; |
||
148 | |||
149 | /** |
||
150 | * @var string holds the last generated customer ownId. In mock mode it'll be always the default, but it changes on sandbox mode. |
||
151 | */ |
||
152 | protected $last_cus_id = 'meu_id_customer'; |
||
153 | |||
154 | /** |
||
155 | * @var string same as `$last_cus_id` but for orders. |
||
156 | * |
||
157 | * @see $last_cus_id |
||
158 | */ |
||
159 | protected $last_ord_id = 'meu_id_pedido'; |
||
160 | protected $sandbox_mock = self::MOCK; |
||
161 | |||
162 | public function __construct() |
||
254 | |||
255 | /** |
||
256 | * Sets up the fixture, for example, open a network connection. |
||
257 | * This method is called before a test is executed. |
||
258 | */ |
||
259 | public function setUp() |
||
273 | |||
274 | /** |
||
275 | * Method to read JSON from a file. |
||
276 | * |
||
277 | * @param string $filename location of file |
||
278 | */ |
||
279 | public function readJsonFile($filename) |
||
283 | |||
284 | /** |
||
285 | * If in MOCK mode returns a mocked Requests_Sessesion if in SANDBOX mode, creates a new session. |
||
286 | * |
||
287 | * @param string $body what the request will return |
||
288 | * @param int $status_code what http code the request will return |
||
289 | */ |
||
290 | public function mockHttpSession($body, $status_code = 200) |
||
304 | |||
305 | /** |
||
306 | * Creates a customer. |
||
307 | * |
||
308 | * @return Customer |
||
309 | */ |
||
310 | public function createCustomer() |
||
328 | |||
329 | /** |
||
330 | * Creates a holder. |
||
331 | * |
||
332 | * @return Holder |
||
333 | */ |
||
334 | public function createHolder() |
||
335 | { |
||
336 | $holder = $this->moip->holders()->setFullname('Jose Silva') |
||
337 | ->setBirthDate(\DateTime::createFromFormat($this->date_format, $this->date_string)) |
||
338 | ->setTaxDocument('22222222222', 'CPF') |
||
339 | ->setPhone(11, 66778899, 55) |
||
340 | ->setAddress(Holder::ADDRESS_BILLING, 'Avenida Faria Lima', '2927', 'Itaim', 'Sao Paulo', 'SP', '01234000', '8'); |
||
341 | |||
342 | return $holder; |
||
343 | } |
||
344 | |||
345 | /** |
||
346 | * Creates a account. |
||
347 | * |
||
348 | * @return Account |
||
349 | */ |
||
350 | public function createAccount() |
||
369 | |||
370 | /** |
||
371 | * Creates an order. |
||
372 | * |
||
373 | * @return Orders |
||
374 | */ |
||
375 | public function createOrder() |
||
392 | |||
393 | /** |
||
394 | * Creates a multiorder. |
||
395 | * |
||
396 | * @return Multiorders |
||
397 | */ |
||
398 | public function createMultiorder() |
||
399 | { |
||
400 | View Code Duplication | if ($this->sandbox_mock == self::SANDBOX) { |
|
401 | $this->last_ord_id = uniqid('MOR-'); |
||
402 | } else { |
||
403 | $this->last_ord_id = 'meu_id_pedido'; |
||
404 | } |
||
405 | |||
406 | $order = $this->moip->orders()->setOwnId(uniqid()) |
||
407 | ->addItem('bicicleta 1', 1, 'sku1', 10000) |
||
408 | ->addItem('bicicleta 2', 1, 'sku2', 11000) |
||
409 | ->addItem('bicicleta 3', 1, 'sku3', 12000) |
||
410 | ->addItem('bicicleta 4', 1, 'sku4', 13000) |
||
411 | ->setShippingAmount(3000) |
||
412 | ->setAddition(1000) |
||
413 | ->setDiscount(5000) |
||
414 | ->setCustomer($this->createCustomer()) |
||
415 | ->addReceiver('MPA-VB5OGTVPCI52', 'PRIMARY', null); |
||
416 | |||
417 | $order2 = $this->moip->orders()->setOwnId(uniqid()) |
||
418 | ->addItem('bicicleta 1', 1, 'sku1', 10000) |
||
419 | ->addItem('bicicleta 2', 1, 'sku2', 11000) |
||
420 | ->addItem('bicicleta 3', 1, 'sku3', 12000) |
||
421 | ->setShippingAmount(3000) |
||
422 | ->setAddition(1000) |
||
423 | ->setDiscount(5000) |
||
424 | ->setCustomer($this->createCustomer()) |
||
425 | ->addReceiver('MPA-IFYRB1HBL73Z', 'PRIMARY', null); |
||
426 | |||
427 | $multiorder = $this->moip->multiorders() |
||
428 | ->setOwnId(uniqid()) |
||
429 | ->addOrder($order) |
||
430 | ->addOrder($order2); |
||
431 | |||
432 | return $multiorder; |
||
433 | } |
||
434 | |||
435 | /** |
||
436 | * Tears down the fixture, for example, close a network connection. |
||
437 | * This method is called after a test is executed. |
||
438 | */ |
||
439 | public function tearDown() |
||
443 | } |
||
444 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: