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 holds the last generated customer ownId. In mock mode it'll be always the default, but it changes on sandbox mode. |
||
146 | */ |
||
147 | protected $last_cus_id = 'meu_id_customer'; |
||
148 | |||
149 | /** |
||
150 | * @var string same as `$last_cus_id` but for orders. |
||
151 | * |
||
152 | * @see $last_cus_id |
||
153 | */ |
||
154 | protected $last_ord_id = 'meu_id_pedido'; |
||
155 | protected $sandbox_mock = self::MOCK; |
||
156 | |||
157 | public function __construct() |
||
247 | |||
248 | /** |
||
249 | * Sets up the fixture, for example, open a network connection. |
||
250 | * This method is called before a test is executed. |
||
251 | */ |
||
252 | public function setUp() |
||
266 | |||
267 | /** |
||
268 | * Method to read JSON from a file. |
||
269 | * |
||
270 | * @param string $filename location of file |
||
271 | */ |
||
272 | public function readJsonFile($filename) |
||
276 | |||
277 | /** |
||
278 | * If in MOCK mode returns a mocked Requests_Sessesion if in SANDBOX mode, creates a new session. |
||
279 | * |
||
280 | * @param string $body what the request will return |
||
281 | * @param int $status_code what http code the request will return |
||
282 | */ |
||
283 | public function mockHttpSession($body, $status_code = 200) |
||
297 | |||
298 | /** |
||
299 | * Creates a customer. |
||
300 | * |
||
301 | * @return Customer |
||
302 | */ |
||
303 | public function createCustomer() |
||
321 | |||
322 | /** |
||
323 | * Creates a holder. |
||
324 | * |
||
325 | * @return Holder |
||
326 | */ |
||
327 | public function createHolder() |
||
337 | |||
338 | /** |
||
339 | * Creates a account. |
||
340 | * |
||
341 | * @return Account |
||
342 | */ |
||
343 | public function createAccount() |
||
362 | |||
363 | /** |
||
364 | * Creates an order. |
||
365 | * |
||
366 | * @return Orders |
||
367 | */ |
||
368 | public function createOrder() |
||
385 | |||
386 | /** |
||
387 | * Creates a multiorder. |
||
388 | * |
||
389 | * @return Multiorders |
||
390 | */ |
||
391 | public function createMultiorder() |
||
427 | |||
428 | /** |
||
429 | * Tears down the fixture, for example, close a network connection. |
||
430 | * This method is called after a test is executed. |
||
431 | */ |
||
432 | public function tearDown() |
||
436 | } |
||
437 |
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: