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 |
||
| 15 | abstract class TestCase extends PHPUnit_Framework_TestCase |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * Variables representing the test modes. On MOCK mode no http request will be made. |
||
| 19 | * In SANDBOX mode HTTP requests will be made to the Moip::SANDBOX_ENDPOINT, the authentication information |
||
| 20 | * is retrieved from the MOIP_TOKEN and MOIP_KEY environment variables. |
||
| 21 | */ |
||
| 22 | const MOCK = 'mock'; |
||
| 23 | const SANDBOX = 'sandbox'; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Intance of \Moip\Moip. |
||
| 27 | * |
||
| 28 | * @var \Moip\Moip |
||
| 29 | * */ |
||
| 30 | protected $moip; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var string current format for dates. |
||
| 34 | */ |
||
| 35 | protected $date_format = 'Y-m-d'; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var string date used for testing. |
||
| 39 | */ |
||
| 40 | protected $date_string = '1989-06-01'; |
||
| 41 | //todo: add the ability to use the play(https://github.com/rodrigosaito/mockwebserver-player) files from the jada sdk |
||
| 42 | //the two responses below were based on the moip Java sdk's test files (https://github.com/moip/moip-sdk-java/) |
||
| 43 | /** |
||
| 44 | * @var string response from the client moip API. |
||
| 45 | */ |
||
| 46 | protected $body_client; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var string response from the order moip API. |
||
| 50 | */ |
||
| 51 | protected $body_order; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var string response from moip API. |
||
| 55 | */ |
||
| 56 | protected $body_cc_pay_pci; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var string response from moip API. |
||
| 60 | */ |
||
| 61 | protected $body_cc_pay_pci_store; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var string response from moip API. |
||
| 65 | */ |
||
| 66 | protected $body_cc_pay_pci_escrow; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var string response from moip API. |
||
| 70 | */ |
||
| 71 | protected $body_release_escrow; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var string response from moip API. |
||
| 75 | */ |
||
| 76 | protected $body_billet_pay; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var string response from moip API. |
||
| 80 | */ |
||
| 81 | protected $body_refund_full_bankaccount; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var string response from moip API. |
||
| 85 | */ |
||
| 86 | protected $body_refund_partial_bankaccount; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var string response from moip API. |
||
| 90 | */ |
||
| 91 | protected $body_notification_preference; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var string response from moip API. |
||
| 95 | */ |
||
| 96 | protected $body_moip_account_create; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @var string response from moip API. |
||
| 100 | */ |
||
| 101 | protected $body_moip_account_get; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @var string response from moip API. |
||
| 105 | */ |
||
| 106 | protected $body_order_list; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @var string response from moip API. |
||
| 110 | */ |
||
| 111 | protected $body_notification_list; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @var string response from moip API. |
||
| 115 | */ |
||
| 116 | protected $body_transfers_create; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @var string response from moip API. |
||
| 120 | */ |
||
| 121 | protected $body_transfers_list; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @var string response from moip API. |
||
| 125 | */ |
||
| 126 | protected $body_transfers_revert; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @var string response from moip API. |
||
| 130 | */ |
||
| 131 | protected $body_bank_account_create; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @var string response from moip API. |
||
| 135 | */ |
||
| 136 | protected $body_bank_account_list; |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @var string response from moip API. |
||
| 140 | */ |
||
| 141 | protected $body_bank_account_update; |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @var string holds the last generated customer ownId. In mock mode it'll be always the default, but it changes on sandbox mode. |
||
| 145 | */ |
||
| 146 | protected $last_cus_id = 'meu_id_customer'; |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @var string same as `$last_cus_id` but for orders. |
||
| 150 | * |
||
| 151 | * @see $last_cus_id |
||
| 152 | */ |
||
| 153 | protected $last_ord_id = 'meu_id_pedido'; |
||
| 154 | protected $sandbox_mock = self::MOCK; |
||
| 155 | |||
| 156 | public function __construct() |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Sets up the fixture, for example, open a network connection. |
||
| 249 | * This method is called before a test is executed. |
||
| 250 | */ |
||
| 251 | public function setUp() |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Method to read JSON from a file. |
||
| 268 | * |
||
| 269 | * @param string $filename location of file |
||
| 270 | */ |
||
| 271 | public function readJsonFile($filename) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * If in MOCK mode returns a mocked Requests_Sessesion if in SANDBOX mode, creates a new session. |
||
| 278 | * |
||
| 279 | * @param string $body what the request will return |
||
| 280 | * @param int $status_code what http code the request will return |
||
| 281 | */ |
||
| 282 | public function mockHttpSession($body, $status_code = 200) |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Creates a customer. |
||
| 299 | * |
||
| 300 | * @return Customer |
||
| 301 | */ |
||
| 302 | public function createCustomer() |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Creates a account. |
||
| 323 | * |
||
| 324 | * @return Account |
||
| 325 | */ |
||
| 326 | public function createAccount() |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Creates an order. |
||
| 348 | * |
||
| 349 | * @return Orders |
||
| 350 | */ |
||
| 351 | public function createOrder() |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Creates a multiorder. |
||
| 371 | * |
||
| 372 | * @return Multiorders |
||
| 373 | */ |
||
| 374 | public function createMultiorder() |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Tears down the fixture, for example, close a network connection. |
||
| 413 | * This method is called after a test is executed. |
||
| 414 | */ |
||
| 415 | public function tearDown() |
||
| 419 | } |
||
| 420 |
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: