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; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @var string response from moip API. |
||
| 100 | */ |
||
| 101 | protected $body_order_list; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @var string response from moip API. |
||
| 105 | */ |
||
| 106 | protected $body_notification_list; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @var string holds the last generated customer ownId. In mock mode it'll be always the default, but it changes on sandbox mode. |
||
| 110 | */ |
||
| 111 | protected $last_cus_id = 'meu_id_customer'; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @var string same as `$last_cus_id` but for orders. |
||
| 115 | * |
||
| 116 | * @see $last_cus_id |
||
| 117 | */ |
||
| 118 | protected $last_ord_id = 'meu_id_pedido'; |
||
| 119 | protected $sandbox_mock = self::MOCK; |
||
| 120 | |||
| 121 | public function __construct() |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Sets up the fixture, for example, open a network connection. |
||
| 184 | * This method is called before a test is executed. |
||
| 185 | */ |
||
| 186 | public function setUp() |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Method to read JSON from a file. |
||
| 203 | * |
||
| 204 | * @param string $filename location of file |
||
| 205 | */ |
||
| 206 | public function readJsonFile($filename) |
||
| 210 | |||
| 211 | /** |
||
| 212 | * If in MOCK mode returns a mocked Requests_Sessesion if in SANDBOX mode, creates a new session. |
||
| 213 | * |
||
| 214 | * @param string $body what the request will return |
||
| 215 | * @param int $status_code what http code the request will return |
||
| 216 | */ |
||
| 217 | public function mockHttpSession($body, $status_code = 200) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Creates a customer. |
||
| 234 | * |
||
| 235 | * @return Customer |
||
| 236 | */ |
||
| 237 | public function createCustomer() |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Creates a account. |
||
| 258 | * |
||
| 259 | * @return Account |
||
| 260 | */ |
||
| 261 | public function createAccount() |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Creates an order. |
||
| 283 | * |
||
| 284 | * @return Orders |
||
| 285 | */ |
||
| 286 | public function createOrder() |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Creates a multiorder. |
||
| 306 | * |
||
| 307 | * @return Multiorders |
||
| 308 | */ |
||
| 309 | public function createMultiorder() |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Tears down the fixture, for example, close a network connection. |
||
| 348 | * This method is called after a test is executed. |
||
| 349 | */ |
||
| 350 | public function tearDown() |
||
| 354 | } |
||
| 355 |
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: