diff --git a/src/include/abstracts/class-wc-retailcrm-abstract-builder.php b/src/include/abstracts/class-wc-retailcrm-abstract-builder.php index 648f920..1c6ad5f 100644 --- a/src/include/abstracts/class-wc-retailcrm-abstract-builder.php +++ b/src/include/abstracts/class-wc-retailcrm-abstract-builder.php @@ -41,7 +41,8 @@ abstract class WC_Retailcrm_Abstract_Builder implements WC_Retailcrm_Builder_Int */ public function reset() { - $this->data = array(); + $this->data = []; + return $this; } diff --git a/src/include/abstracts/class-wc-retailcrm-abstracts-address.php b/src/include/abstracts/class-wc-retailcrm-abstracts-address.php index 9f1c308..14099e4 100644 --- a/src/include/abstracts/class-wc-retailcrm-abstracts-address.php +++ b/src/include/abstracts/class-wc-retailcrm-abstracts-address.php @@ -12,28 +12,10 @@ */ abstract class WC_Retailcrm_Abstracts_Address extends WC_Retailcrm_Abstracts_Data { - /** @var array $data */ - protected $data = array( - 'index' => '', - 'city' => '', - 'region' => '', - 'text' => '', - ); - /** - * Resets inner state + * Divider for order delivery address_1 and address_2 */ - public function reset_data() - { - $this->data = array( - 'index' => '', - 'city' => '', - 'region' => '', - 'text' => '', - ); - - return $this; - } + const ADDRESS_LINE_DIVIDER = ' || '; /** * Returns shipping address from order. @@ -44,26 +26,16 @@ abstract class WC_Retailcrm_Abstracts_Address extends WC_Retailcrm_Abstracts_Dat */ protected function getOrderAddress($order) { - if ($order === null) { - return array(); + if (!$order instanceof WC_Order) { + return []; } - $orderShippingAddress = array( - 'postcode' => $order->get_shipping_postcode(), - 'state' => $order->get_shipping_state(), - 'city' => $order->get_shipping_city(), - 'address_1' => $order->get_shipping_address_1(), - 'address_2' => $order->get_shipping_address_2() - ); - - if (!empty($orderShippingAddress)) { - return array( - 'index' => $orderShippingAddress['postcode'], - 'city' => $orderShippingAddress['city'], - 'region' => $this->get_state_name($order->get_shipping_country(), $orderShippingAddress['state']), - 'text' => implode(' ', $orderShippingAddress) - ); - } + return [ + 'index' => $order->get_shipping_postcode(), + 'city' => $order->get_shipping_city(), + 'region' => $this->getRegion($order->get_shipping_country(), $order->get_shipping_state()), + 'text' => $this->getText($order, 'order'), + ]; } /** @@ -76,45 +48,31 @@ abstract class WC_Retailcrm_Abstracts_Address extends WC_Retailcrm_Abstracts_Dat */ protected function getCustomerAddress($customer, $order) { - if ($customer === null) { - return array(); + if (!$customer instanceof WC_Customer) { + return []; } $customerBillingAddress = $customer->get_billing_address(); if ($order instanceof WC_Order && empty($customerBillingAddress)) { - return array( + return [ 'index' => $order->get_billing_postcode(), - 'countryIso' => $this->validateCountryCode($order->get_billing_country()), - 'region' => $this->get_state_name($order->get_billing_country(), $order->get_billing_state()), + 'countryIso' => $this->getCountryCode($order->get_billing_country()), + 'region' => $this->getRegion($order->get_billing_country(), $order->get_billing_state()), 'city' => $order->get_billing_city(), - 'text' => $this->joinAddresses($order->get_billing_address_1(), $order->get_billing_address_2()) - ); + 'text' => $this->getText($order), + ]; } else { - return array( + return [ 'index' => $customer->get_billing_postcode(), - 'countryIso' => $this->validateCountryCode($customer->get_billing_country()), - 'region' => $this->get_state_name($customer->get_billing_country(), $customer->get_billing_state()), + 'countryIso' => $this->getCountryCode($customer->get_billing_country()), + 'region' => $this->getRegion($customer->get_billing_country(), $customer->get_billing_state()), 'city' => $customer->get_billing_city(), - 'text' => $this->joinAddresses($customer->get_billing_address_1(), $customer->get_billing_address_2()) - ); + 'text' => $this->getText($customer), + ]; } } - /** - * Validate countryIso. Check if a given code represents a valid ISO 3166-1 alpha-2 code. - * - * @param $countryCode - * - * @return string - */ - private function validateCountryCode($countryCode) - { - $countries = new WC_Countries(); - - return $countries->country_exists($countryCode) ? $countryCode : ''; - } - /** * Glue two addresses * @@ -123,9 +81,23 @@ abstract class WC_Retailcrm_Abstracts_Address extends WC_Retailcrm_Abstracts_Dat * * @return string */ - protected function joinAddresses($address1 = '', $address2 = '') + protected function joinAddresses(string $address1 = '', string $address2 = '') { - return implode(', ', array_filter(array($address1, $address2))); + return implode(self::ADDRESS_LINE_DIVIDER, array_filter([$address1, $address2])); + } + + /** + * Validate countryIso. Check if a given code represents a valid ISO 3166-1 alpha-2 code. + * + * @param $countryCode + * + * @return string + */ + private function getCountryCode($countryCode) + { + $countries = new WC_Countries(); + + return $countries->country_exists($countryCode) ? $countryCode : ''; } /** @@ -136,7 +108,7 @@ abstract class WC_Retailcrm_Abstracts_Address extends WC_Retailcrm_Abstracts_Dat * * @return string */ - protected function get_state_name($countryCode, $stateCode) + protected function getRegion(string $countryCode, string $stateCode) { if (preg_match('/^[A-Z\-0-9]{0,5}$/', $stateCode) && !is_null($countryCode)) { $countriesProvider = new WC_Countries(); @@ -149,4 +121,24 @@ abstract class WC_Retailcrm_Abstracts_Address extends WC_Retailcrm_Abstracts_Dat return $stateCode; } + + /** + * Returns data for CRM field 'text'. + * If type entity equals 'order', get address for order and use shipping address, + * else get address for customer and use billing address. + * + * @return string + */ + protected function getText($wcEntity, $typeEntity = 'customer') + { + if ($typeEntity === 'order') { + return empty($wcEntity->get_shipping_address_2()) + ? $wcEntity->get_shipping_address_1() + : $this->joinAddresses($wcEntity->get_shipping_address_1(), $wcEntity->get_shipping_address_2()); + } else { + return empty($wcEntity->get_billing_address_2()) + ? $wcEntity->get_billing_address_1() + : $this->joinAddresses($wcEntity->get_billing_address_1(), $wcEntity->get_billing_address_2()); + } + } } diff --git a/src/include/abstracts/class-wc-retailcrm-abstracts-data.php b/src/include/abstracts/class-wc-retailcrm-abstracts-data.php index 5e70612..67908a2 100644 --- a/src/include/abstracts/class-wc-retailcrm-abstracts-data.php +++ b/src/include/abstracts/class-wc-retailcrm-abstracts-data.php @@ -13,12 +13,7 @@ abstract class WC_Retailcrm_Abstracts_Data { /** @var array */ - protected $data = array(); - - /** - * @return void - */ - abstract public function reset_data(); + protected $data = []; /** * @param $data @@ -30,7 +25,7 @@ abstract class WC_Retailcrm_Abstracts_Data /** * @codeCoverageIgnore */ - protected function set_data_field($field, $value) + protected function setField($field, $value) { if (isset($this->data[$field]) && \gettype($value) !== \gettype($this->data[$field])) { return false; @@ -44,11 +39,11 @@ abstract class WC_Retailcrm_Abstracts_Data /** * @param $fields */ - protected function set_data_fields($fields) + protected function setDataFields($fields) { if (!empty($fields)) { foreach ($fields as $field => $value) { - $this->set_data_field($field, $value); + $this->setField($field, $value); } } } @@ -56,7 +51,7 @@ abstract class WC_Retailcrm_Abstracts_Data /** * @return array */ - public function get_data() + public function getData() { return $this->data; } diff --git a/src/include/class-wc-retailcrm-customers.php b/src/include/class-wc-retailcrm-customers.php index 05793ef..f0a4211 100644 --- a/src/include/class-wc-retailcrm-customers.php +++ b/src/include/class-wc-retailcrm-customers.php @@ -274,7 +274,7 @@ if (!class_exists('WC_Retailcrm_Customers')) : $newAddress = $builder ->setIsMain(false) ->build($customer, $order) - ->get_data(); + ->getData(); $addresses = $this->retailcrm->customersCorporateAddresses( $corporateId, @@ -335,9 +335,9 @@ if (!class_exists('WC_Retailcrm_Customers')) : ); if ($response->isSuccessful() && $response->offsetExists('id')) { - $this->customerCorporateCompany['address'] = array( + $this->customerCorporateCompany['address'] = [ 'id' => $response['id'], - ); + ]; $this->retailcrm->customersCorporateCompaniesCreate( $customerId, @@ -401,22 +401,22 @@ if (!class_exists('WC_Retailcrm_Customers')) : // then we take $order->get_date_created() order $createdAt = empty($createdAt) ? $order->get_date_created() : $createdAt; - $data_customer = array( + $customerData = [ 'createdAt' => $createdAt->date('Y-m-d H:i:s'), 'firstName' => $firstName ? $firstName : $customer->get_username(), 'lastName' => $lastName, 'email' => $email, - 'address' => $this->customer_address->build($customer, $order)->get_data() - ); + 'address' => $this->customer_address->build($customer, $order)->getData() + ]; if ($customer->get_id() > 0) { - $data_customer['externalId'] = $customer->get_id(); + $customerData['externalId'] = $customer->get_id(); } if (!empty($billingPhone)) { - $data_customer['phones'][] = array( + $customerData['phones'][] = [ 'number' => $customer->get_billing_phone() - ); + ]; } // If the client is corporate, set the value isContact. @@ -430,7 +430,7 @@ if (!class_exists('WC_Retailcrm_Customers')) : } if (!empty($company)) { - $data_customer['isContact'] = true; + $customerData['isContact'] = true; } } @@ -439,14 +439,14 @@ if (!class_exists('WC_Retailcrm_Customers')) : $metaValue = $customer->get_meta($metaKey); if (!empty($metaValue)) { - $data_customer['customFields'][$customKey] = $metaValue; + $customerData['customFields'][$customKey] = $metaValue; } } } $this->customer = apply_filters( 'retailcrm_process_customer', - WC_Retailcrm_Plugin::clearArray($data_customer), + WC_Retailcrm_Plugin::clearArray($customerData), $customer ); } @@ -462,39 +462,39 @@ if (!class_exists('WC_Retailcrm_Customers')) : */ protected function processCorporateCustomer($crmCustomerId, $customer, $order) { - $data_company = array( + $data_company = [ 'isMain' => true, 'name' => $order->get_billing_company() - ); + ]; - $data_customer = array( + $data_customer = [ 'nickName' => $order->get_billing_company(), - 'customerContacts' => array( - array( + 'customerContacts' => [ + [ 'isMain' => true, - 'customer' => array( + 'customer' => [ 'id' => $crmCustomerId - ) - ) - ) - ); + ] + ] + ] + ]; $corpAddress = new WC_Retailcrm_Customer_Corporate_Address(); $billingAddress = $corpAddress ->setIsMain(true) ->build($customer, $order) - ->get_data(); + ->getData(); if (!empty($billingAddress)) { $data_company['contragent']['legalAddress'] = implode( ', ', - array( + [ $billingAddress['index'], $billingAddress['city'], $billingAddress['region'], $billingAddress['text'] - ) + ] ); } @@ -577,11 +577,11 @@ if (!class_exists('WC_Retailcrm_Customers')) : $customer = false; if (!empty($customerExternalId)) { - $customer = $this->searchCustomer(array('externalId' => $customerExternalId)); + $customer = $this->searchCustomer(['externalId' => $customerExternalId]); } if (!$customer && !empty($customerEmailOrPhone)) { - $customer = $this->searchCustomer(array('email' => $customerEmailOrPhone, 'isContact' => $isContact)); + $customer = $this->searchCustomer(['email' => $customerEmailOrPhone, 'isContact' => $isContact]); } return $customer; diff --git a/src/include/class-wc-retailcrm-history.php b/src/include/class-wc-retailcrm-history.php index 674500c..0157a86 100644 --- a/src/include/class-wc-retailcrm-history.php +++ b/src/include/class-wc-retailcrm-history.php @@ -438,11 +438,11 @@ if (!class_exists('WC_Retailcrm_History')) : } if (isset($billingAddress['street'])) { - $wcOrder->set_shipping_address_1($billingAddress['street']); + $wcOrder->set_billing_address_1($billingAddress['street']); } if (isset($billingAddress['building'])) { - $wcOrder->set_shipping_address_2($billingAddress['building']); + $wcOrder->set_billing_address_2($billingAddress['building']); } // @codeCoverageIgnoreEnd } @@ -744,32 +744,40 @@ if (!class_exists('WC_Retailcrm_History')) : $companyName = $customer['mainCompany']['name']; } + $shippingAddress = $order['delivery']['address']; + $shippingAddressLines = $this->getAddressLines($shippingAddress['text']); + $addressShipping = [ 'first_name' => $order['firstName'] ?? '', 'last_name' => $order['lastName'] ?? '', 'company' => '', - 'address_1' => $order['delivery']['address']['text'] ?? '', - 'address_2' => '', - 'city' => $order['delivery']['address']['city'] ?? '', - 'state' => $order['delivery']['address']['region'] ?? '', - 'postcode' => $order['delivery']['address']['index'] ?? '', - 'country' => $order['delivery']['address']['countryIso'] ?? '' + 'address_1' => $shippingAddressLines['address_1'] ?? $shippingAddressLines, + 'address_2' => $shippingAddressLines['address_2'] ?? '', + 'city' => $shippingAddress['city'] ?? '', + 'state' => $shippingAddress['region'] ?? '', + 'postcode' => $shippingAddress['index'] ?? '', + 'country' => $shippingAddress['countryIso'] ?? '' ]; + $billingAddressLines = $this->getAddressLines($billingAddress['text']); + $addressBilling = [ 'first_name' => $contactOrCustomer['firstName'] ?? '', 'last_name' => $contactOrCustomer['lastName'] ?? '', 'company' => $companyName, 'email' => $contactOrCustomer['email'] ?? '', 'phone' => $contactOrCustomer['phones'][0]['number'] ?? '', - 'address_1' => $billingAddress['text'] ?? '', - 'address_2' => '', + 'address_1' => $billingAddressLines['address_1'] ?? $billingAddressLines, + 'address_2' => $billingAddressLines['address_2'] ?? '', 'city' => $billingAddress['city'] ?? '', 'state' => $billingAddress['region'] ?? '', 'postcode' => $billingAddress['index'] ?? '', 'country' => $billingAddress['countryIso'] ?? '' ]; + $wcOrder->set_address($addressShipping, 'shipping'); + $wcOrder->set_address($addressBilling, 'billing'); + if (isset($order['payments']) && $order['payments']) { $payment = WC_Payment_Gateways::instance(); @@ -784,9 +792,6 @@ if (!class_exists('WC_Retailcrm_History')) : } } - $wcOrder->set_address($addressBilling, 'billing'); - $wcOrder->set_address($addressShipping, 'shipping'); - $crmOrderItems = $order['items'] ?? []; if ($crmOrderItems) { @@ -989,6 +994,24 @@ if (!class_exists('WC_Retailcrm_History')) : } } + /** + * Returns data for address_1 and address_2(if exist data for this field) for WC order. + * + * @param string $addressLine + * + * @return mixed + */ + private function getAddressLines(string $addressLine) + { + if (strpos($addressLine, WC_Retailcrm_Abstracts_Address::ADDRESS_LINE_DIVIDER) !== false) { + $addressLines = explode(WC_Retailcrm_Abstracts_Address::ADDRESS_LINE_DIVIDER, $addressLine); + + return ['address_1' => $addressLines[0], 'address_2' => $addressLines[1]]; + } + + return $addressLine; + } + /** * Add product in WC order. * diff --git a/src/include/class-wc-retailcrm-orders.php b/src/include/class-wc-retailcrm-orders.php index 4fb96c7..6ee45b8 100644 --- a/src/include/class-wc-retailcrm-orders.php +++ b/src/include/class-wc-retailcrm-orders.php @@ -36,7 +36,7 @@ if (!class_exists('WC_Retailcrm_Orders')) : protected $orders; /** @var array */ - private $ordersGetRequestCache = array(); + private $ordersGetRequestCache = []; /** @var array */ private $order = []; @@ -83,7 +83,7 @@ if (!class_exists('WC_Retailcrm_Orders')) : return null; } - $this->order_payment->reset_data(); + $this->order_payment->resetData(); $wcOrder = wc_get_order($order_id); $this->processOrder($wcOrder); @@ -345,7 +345,7 @@ if (!class_exists('WC_Retailcrm_Orders')) : $this->orders->is_new = false; } - $orderData = $this->orders->build($order)->get_data(); + $orderData = $this->orders->build($order)->getData(); if ($order->get_items('shipping')) { $shippings = $order->get_items('shipping'); @@ -393,14 +393,14 @@ if (!class_exists('WC_Retailcrm_Orders')) : } } - $orderData['delivery']['address'] = $this->order_address->build($order)->get_data(); + $orderData['delivery']['address'] = $this->order_address->build($order)->getData(); $orderItems = []; /** @var WC_Order_Item_Product $item */ foreach ($order->get_items() as $item) { - $orderItems[] = $this->order_item->build($item)->get_data(); + $orderItems[] = $this->order_item->build($item)->getData(); - $this->order_item->reset_data(); + $this->order_item->resetData(); } $orderData['items'] = $orderItems; @@ -409,7 +409,7 @@ if (!class_exists('WC_Retailcrm_Orders')) : if (!$update && $order->get_total() > 0) { $this->order_payment->isNew = true; - $orderData['payments'][] = $this->order_payment->build($order)->get_data(); + $orderData['payments'][] = $this->order_payment->build($order)->getData(); } if (!empty($this->customFields)) { @@ -444,8 +444,8 @@ if (!class_exists('WC_Retailcrm_Orders')) : protected function sendPayment($order, $update = false, $externalId = false) { $this->order_payment->isNew = !$update; - $payment = $this->order_payment->build($order, $externalId)->get_data(); + $payment = $this->order_payment->build($order, $externalId)->getData(); $integrationPayments = get_option('retailcrm_integration_payments'); if (is_array($integrationPayments)) { diff --git a/src/include/customer/class-wc-retailcrm-customer-address.php b/src/include/customer/class-wc-retailcrm-customer-address.php index 22ad63d..0e85f96 100644 --- a/src/include/customer/class-wc-retailcrm-customer-address.php +++ b/src/include/customer/class-wc-retailcrm-customer-address.php @@ -30,7 +30,7 @@ class WC_Retailcrm_Customer_Address extends WC_Retailcrm_Abstracts_Address $order ); - $this->set_data_fields($customerAddress); + $this->setDataFields($customerAddress); } else { WC_Retailcrm_Logger::add('Error Customer address is empty'); } diff --git a/src/include/customer/class-wc-retailcrm-customer-corporate-address.php b/src/include/customer/class-wc-retailcrm-customer-corporate-address.php index 8c4abfe..b22aab6 100644 --- a/src/include/customer/class-wc-retailcrm-customer-corporate-address.php +++ b/src/include/customer/class-wc-retailcrm-customer-corporate-address.php @@ -43,12 +43,12 @@ class WC_Retailcrm_Customer_Corporate_Address extends WC_Retailcrm_Abstracts_Add 'retailcrm_process_customer_corporate_address', WC_Retailcrm_Plugin::clearArray(array_merge( $address, - array('isMain' => $this->isMain) + ['isMain' => $this->isMain] )), $customer ); - $this->set_data_fields($corporateCustomerAddress); + $this->setDataFields($corporateCustomerAddress); } else { WC_Retailcrm_Logger::add('Error Corporate Customer address is empty'); } diff --git a/src/include/order/class-wc-retailcrm-order-address.php b/src/include/order/class-wc-retailcrm-order-address.php index 65d94bc..7b6300e 100644 --- a/src/include/order/class-wc-retailcrm-order-address.php +++ b/src/include/order/class-wc-retailcrm-order-address.php @@ -28,7 +28,7 @@ class WC_Retailcrm_Order_Address extends WC_Retailcrm_Abstracts_Address $order ); - $this->set_data_fields($orderAddress); + $this->setDataFields($orderAddress); } else { WC_Retailcrm_Logger::add('Error: Order address is empty'); } diff --git a/src/include/order/class-wc-retailcrm-order-item.php b/src/include/order/class-wc-retailcrm-order-item.php index c4f31b4..7b984d1 100644 --- a/src/include/order/class-wc-retailcrm-order-item.php +++ b/src/include/order/class-wc-retailcrm-order-item.php @@ -63,9 +63,9 @@ class WC_Retailcrm_Order_Item extends WC_Retailcrm_Abstracts_Data ] ]; - $this->set_data_fields($data); - $this->set_offer($item); - $this->set_data_field('discountManualAmount', $discountPrice); + $this->setDataFields($data); + $this->setOffer($item); + $this->setField('discountManualAmount', $discountPrice); return $this; } @@ -75,7 +75,7 @@ class WC_Retailcrm_Order_Item extends WC_Retailcrm_Abstracts_Data * * @return void */ - private function set_offer(WC_Order_Item_Product $item) + private function setOffer(WC_Order_Item_Product $item) { $uid = $item['variation_id'] > 0 ? $item['variation_id'] : $item['product_id'] ; $offer = ['externalId' => $uid]; @@ -92,7 +92,7 @@ class WC_Retailcrm_Order_Item extends WC_Retailcrm_Abstracts_Data unset($offer['externalId']); } - $this->set_data_field('offer', $offer); + $this->setField('offer', $offer); } /** @@ -125,9 +125,9 @@ class WC_Retailcrm_Order_Item extends WC_Retailcrm_Abstracts_Data } /** - * Reset data for object + * Reset item data. */ - public function reset_data() + public function resetData() { $this->data = [ 'offer' => [], diff --git a/src/include/order/class-wc-retailcrm-order-payment.php b/src/include/order/class-wc-retailcrm-order-payment.php index cc46dcf..a221371 100644 --- a/src/include/order/class-wc-retailcrm-order-payment.php +++ b/src/include/order/class-wc-retailcrm-order-payment.php @@ -46,7 +46,8 @@ class WC_Retailcrm_Order_Payment extends WC_Retailcrm_Abstracts_Data */ public function build($order, $externalId = false) { - $this->reset_data(); + $this->resetData(); + $paymentData = []; if (!$this->isNew) { @@ -100,7 +101,7 @@ class WC_Retailcrm_Order_Payment extends WC_Retailcrm_Abstracts_Data $order ); - $this->set_data_fields($paymentData); + $this->setDataFields($paymentData); return $this; } @@ -110,9 +111,9 @@ class WC_Retailcrm_Order_Payment extends WC_Retailcrm_Abstracts_Data * * @return array */ - public function get_data() + public function getData() { - $data = parent::get_data(); + $data = parent::getData(); if (empty($data['type'])) { return []; @@ -122,7 +123,7 @@ class WC_Retailcrm_Order_Payment extends WC_Retailcrm_Abstracts_Data return array_filter($data); } - public function reset_data() + public function resetData() { $this->data = [ 'externalId' => '', diff --git a/src/include/order/class-wc-retailcrm-order.php b/src/include/order/class-wc-retailcrm-order.php index 6924b5d..b9b71cf 100644 --- a/src/include/order/class-wc-retailcrm-order.php +++ b/src/include/order/class-wc-retailcrm-order.php @@ -78,44 +78,25 @@ class WC_Retailcrm_Order extends WC_Retailcrm_Abstracts_Data $data['countryIso'] = $countries->get_base_country(); } - $this->set_data_fields($data); - $this->set_number($order); + $this->setDataFields($data); + $this->setNumber($order); if (isset($this->settings[$order->get_status()]) && 'not-upload' !== $this->settings[$order->get_status()]) { - $this->set_data_field('status', $this->settings[$order->get_status()]); + $this->setField('status', $this->settings[$order->get_status()]); } return $this; } - /** * @param WC_Order $order */ - protected function set_number($order) + protected function setNumber($order) { - if (isset($this->settings['update_number']) && $this->settings['update_number'] == WC_Retailcrm_Base::YES ) { - $this->set_data_field('number', $order->get_order_number()); + if (isset($this->settings['update_number']) && $this->settings['update_number'] == WC_Retailcrm_Base::YES) { + $this->setField('number', $order->get_order_number()); } else { unset($this->data['number']); } } - - public function reset_data() - { - $this->data = [ - 'externalId' => '', - 'status' => '', - 'number' => '', - 'createdAt' => '', - 'firstName' => '', - 'lastName' => '', - 'email' => '', - 'paymentType' => '', - 'customerComment' => '', - 'paymentStatus' => '', - 'phone' => '', - 'countryIso' => '' - ]; - } } diff --git a/tests/customer/test-wc-retailcrm-customer-address.php b/tests/customer/test-wc-retailcrm-customer-address.php index 2eed483..8b0faee 100644 --- a/tests/customer/test-wc-retailcrm-customer-address.php +++ b/tests/customer/test-wc-retailcrm-customer-address.php @@ -28,10 +28,10 @@ class WC_Retailcrm_Customer_Address_Test extends WC_Retailcrm_Test_Case_Helper $this->customer->set_billing_address_2('TestAddress2'); } - public function test_build_and_reset_address() + public function test_build_address() { $customer_address = new WC_Retailcrm_Customer_Address(); - $data = $customer_address->build($this->customer)->get_data(); + $data = $customer_address->build($this->customer)->getData(); $this->assertArrayHasKey('index', $data); $this->assertArrayHasKey('city', $data); @@ -41,37 +41,20 @@ class WC_Retailcrm_Customer_Address_Test extends WC_Retailcrm_Test_Case_Helper $this->assertEquals('000000', $data['index']); $this->assertEquals('TestCity', $data['city']); $this->assertEquals('TestState', $data['region']); - $this->assertEquals('TestAddress1, TestAddress2', $data['text']); + $this->assertEquals('TestAddress1 || TestAddress2', $data['text']); $this->assertEquals('CO', $data['countryIso']); - - // Check reset customer address data - $customer_address->reset_data(); - - $data = $customer_address->get_data(); - - $this->assertArrayHasKey('index', $data); - $this->assertArrayHasKey('city', $data); - $this->assertArrayHasKey('region', $data); - $this->assertArrayHasKey('text', $data); - $this->assertEquals('', $data['index']); - $this->assertEquals('', $data['city']); - $this->assertEquals('', $data['region']); - $this->assertEquals('', $data['text']); } public function test_empty_address() { - $customer_address = new WC_Retailcrm_Customer_Address(); - $data = $customer_address->build(null)->get_data(); + $customerAddress = new WC_Retailcrm_Customer_Address(); - $this->assertArrayHasKey('index', $data); - $this->assertArrayHasKey('city', $data); - $this->assertArrayHasKey('region', $data); - $this->assertArrayHasKey('text', $data); - $this->assertEquals('', $data['index']); - $this->assertEquals('', $data['city']); - $this->assertEquals('', $data['region']); - $this->assertEquals('', $data['text']); + $addressData = $customerAddress + ->build(null) + ->getData(); + + $this->assertInternalType('array', $addressData); + $this->assertEquals([], $addressData); } } diff --git a/tests/customer/test-wc-retailcrm-customer-corporate-address.php b/tests/customer/test-wc-retailcrm-customer-corporate-address.php index b434213..7dafc26 100644 --- a/tests/customer/test-wc-retailcrm-customer-corporate-address.php +++ b/tests/customer/test-wc-retailcrm-customer-corporate-address.php @@ -28,13 +28,13 @@ class WC_Retailcrm_Customer_Corporate_Address_Test extends WC_Retailcrm_Test_Cas $this->customer->set_billing_address_2('TestAddress2'); } - public function test_build_and_reset_address() + public function test_build_address() { $customer_address = new WC_Retailcrm_Customer_Corporate_Address(); $data = $customer_address ->setIsMain(true) ->build($this->customer) - ->get_data(); + ->getData(); $this->assertArrayHasKey('index', $data); $this->assertArrayHasKey('city', $data); @@ -45,23 +45,9 @@ class WC_Retailcrm_Customer_Corporate_Address_Test extends WC_Retailcrm_Test_Cas $this->assertEquals('000000', $data['index']); $this->assertEquals('TestCity', $data['city']); $this->assertEquals('TestState', $data['region']); - $this->assertEquals('TestAddress1, TestAddress2', $data['text']); + $this->assertEquals('TestAddress1 || TestAddress2', $data['text']); $this->assertEquals('CO', $data['countryIso']); $this->assertEquals(true, $data['isMain']); - - // Check reset customer corporate address data - $customer_address->reset_data(); - - $data = $customer_address->get_data(); - - $this->assertArrayHasKey('index', $data); - $this->assertArrayHasKey('city', $data); - $this->assertArrayHasKey('region', $data); - $this->assertArrayHasKey('text', $data); - $this->assertEquals('', $data['index']); - $this->assertEquals('', $data['city']); - $this->assertEquals('', $data['region']); - $this->assertEquals('', $data['text']); } public function test_build_not_main_company() @@ -70,7 +56,7 @@ class WC_Retailcrm_Customer_Corporate_Address_Test extends WC_Retailcrm_Test_Cas $data = $customer_address ->setIsMain(false) ->build($this->customer) - ->get_data(); + ->getData(); $this->assertArrayHasKey('index', $data); $this->assertArrayHasKey('city', $data); @@ -81,7 +67,7 @@ class WC_Retailcrm_Customer_Corporate_Address_Test extends WC_Retailcrm_Test_Cas $this->assertEquals('000000', $data['index']); $this->assertEquals('TestCity', $data['city']); $this->assertEquals('TestState', $data['region']); - $this->assertEquals('TestAddress1, TestAddress2', $data['text']); + $this->assertEquals('TestAddress1 || TestAddress2', $data['text']); $this->assertEquals('CO', $data['countryIso']); $this->assertEquals(false, $data['isMain']); } @@ -89,20 +75,15 @@ class WC_Retailcrm_Customer_Corporate_Address_Test extends WC_Retailcrm_Test_Cas public function test_empty_address() { - $customer_address = new WC_Retailcrm_Customer_Corporate_Address(); - $data = $customer_address + $customerCorporateAddress = new WC_Retailcrm_Customer_Corporate_Address(); + + $addressData = $customerCorporateAddress ->setIsMain(false) ->build(null) - ->get_data(); + ->getData(); - $this->assertArrayHasKey('index', $data); - $this->assertArrayHasKey('city', $data); - $this->assertArrayHasKey('region', $data); - $this->assertArrayHasKey('text', $data); - $this->assertEquals('', $data['index']); - $this->assertEquals('', $data['city']); - $this->assertEquals('', $data['region']); - $this->assertEquals('', $data['text']); + $this->assertInternalType('array', $addressData); + $this->assertEquals([], $addressData); } } diff --git a/tests/datasets/data-history-retailcrm.php b/tests/datasets/data-history-retailcrm.php index caba0f3..7a03a64 100644 --- a/tests/datasets/data-history-retailcrm.php +++ b/tests/datasets/data-history-retailcrm.php @@ -169,7 +169,7 @@ class DataHistoryRetailCrm 'countryIso' => 'ES', 'region' => 'Region', 'city' => 'City', - 'text' => 'Street' + 'text' => 'Street1 || Street2' ] ], 'site' => 'test-com', diff --git a/tests/order/test-wc-retailcrm-order-address.php b/tests/order/test-wc-retailcrm-order-address.php index 33dbf6c..5c1c478 100644 --- a/tests/order/test-wc-retailcrm-order-address.php +++ b/tests/order/test-wc-retailcrm-order-address.php @@ -28,10 +28,10 @@ class WC_Retailcrm_Order_Address_Test extends WC_Retailcrm_Test_Case_Helper $this->order->set_shipping_address_2('TestAddress2'); } - public function test_build_and_reset_address() + public function test_build_address() { $order_address = new WC_Retailcrm_Order_Address(); - $data = $order_address->build($this->order)->get_data(); + $data = $order_address->build($this->order)->getData(); $this->assertArrayHasKey('index', $data); $this->assertArrayHasKey('city', $data); @@ -40,36 +40,19 @@ class WC_Retailcrm_Order_Address_Test extends WC_Retailcrm_Test_Case_Helper $this->assertEquals('000000', $data['index']); $this->assertEquals('TestCity', $data['city']); $this->assertEquals('TestState', $data['region']); - $this->assertEquals('000000 TestState TestCity TestAddress1 TestAddress2', $data['text']); - - // Check reset order address data - $order_address->reset_data(); - - $data = $order_address->get_data(); - - $this->assertArrayHasKey('index', $data); - $this->assertArrayHasKey('city', $data); - $this->assertArrayHasKey('region', $data); - $this->assertArrayHasKey('text', $data); - $this->assertEquals('', $data['index']); - $this->assertEquals('', $data['city']); - $this->assertEquals('', $data['region']); - $this->assertEquals('', $data['text']); + $this->assertEquals('TestAddress1 || TestAddress2', $data['text']); } public function test_empty_address() { - $order_address = new WC_Retailcrm_Order_Address(); - $data = $order_address->build(null)->get_data(); + $orderAddress = new WC_Retailcrm_Order_Address(); - $this->assertArrayHasKey('index', $data); - $this->assertArrayHasKey('city', $data); - $this->assertArrayHasKey('region', $data); - $this->assertArrayHasKey('text', $data); - $this->assertEquals('', $data['index']); - $this->assertEquals('', $data['city']); - $this->assertEquals('', $data['region']); - $this->assertEquals('', $data['text']); + $addressData = $orderAddress + ->build(null) + ->getData(); + + $this->assertInternalType('array', $addressData); + $this->assertEquals([], $addressData); } } diff --git a/tests/order/test-wc-retailcrm-order-item.php b/tests/order/test-wc-retailcrm-order-item.php index b02a991..4c5184a 100644 --- a/tests/order/test-wc-retailcrm-order-item.php +++ b/tests/order/test-wc-retailcrm-order-item.php @@ -28,7 +28,7 @@ class WC_Retailcrm_Order_Item_Test extends WC_Retailcrm_Test_Case_Helper /** @var WC_Order_Item_Product $item */ foreach ($this->order->get_items() as $item) { - $data = $order_item->build($item)->get_data(); + $data = $order_item->build($item)->getData(); $this->assertArrayHasKey('productName', $data); $this->assertArrayHasKey('initialPrice', $data); @@ -42,7 +42,7 @@ class WC_Retailcrm_Order_Item_Test extends WC_Retailcrm_Test_Case_Helper $order_item = new WC_Retailcrm_Order_Item(['bind_by_sku' => 'yes']); foreach ($this->order->get_items() as $item) { - $data = $order_item->build($item)->get_data(); + $data = $order_item->build($item)->getData(); $this->assertArrayHasKey('offer', $data); $this->assertArrayHasKey('xmlId', $data['offer']); diff --git a/tests/order/test-wc-retailcrm-order-payment.php b/tests/order/test-wc-retailcrm-order-payment.php index fc5c8e0..21b23f9 100644 --- a/tests/order/test-wc-retailcrm-order-payment.php +++ b/tests/order/test-wc-retailcrm-order-payment.php @@ -33,7 +33,7 @@ class WC_Retailcrm_Order_Payment_Test extends WC_Retailcrm_Test_Case_Helper $settings = $this->getOptions(); $order_payment = new WC_Retailcrm_Order_Payment($settings); - $data = $order_payment->build($this->order, $externalId)->get_data(); + $data = $order_payment->build($this->order, $externalId)->getData(); $this->assertNotEmpty($data); @@ -53,7 +53,7 @@ class WC_Retailcrm_Order_Payment_Test extends WC_Retailcrm_Test_Case_Helper public function test_build_payment_type_not_exist($externalId) { $order_payment = new WC_Retailcrm_Order_Payment('test'); - $data = $order_payment->build($this->order, $externalId)->get_data(); + $data = $order_payment->build($this->order, $externalId)->getData(); $this->assertEmpty($data); } @@ -70,7 +70,7 @@ class WC_Retailcrm_Order_Payment_Test extends WC_Retailcrm_Test_Case_Helper $order_payment = new WC_Retailcrm_Order_Payment($settings); $order_payment->isNew = false; - $data = $order_payment->build($this->order, $externalId)->get_data(); + $data = $order_payment->build($this->order, $externalId)->getData(); $this->assertEmpty($data); } @@ -88,7 +88,7 @@ class WC_Retailcrm_Order_Payment_Test extends WC_Retailcrm_Test_Case_Helper $this->order->update_status('completed'); - $data = $order_payment->build($this->order, $externalId)->get_data(); + $data = $order_payment->build($this->order, $externalId)->getData(); $this->assertNotEmpty($data); @@ -111,7 +111,7 @@ class WC_Retailcrm_Order_Payment_Test extends WC_Retailcrm_Test_Case_Helper $settings = $this->getOptions(); $order_payment = new WC_Retailcrm_Order_Payment($settings); - $data = $order_payment->build($this->order, $externalId)->get_data(); + $data = $order_payment->build($this->order, $externalId)->getData(); $this->assertNotEmpty($data); diff --git a/tests/order/test-wc-retailcrm-order.php b/tests/order/test-wc-retailcrm-order.php index f268e08..a0906f5 100644 --- a/tests/order/test-wc-retailcrm-order.php +++ b/tests/order/test-wc-retailcrm-order.php @@ -21,22 +21,10 @@ class WC_Retailcrm_Order_Test extends WC_Retailcrm_Test_Case_Helper { $this->order = WC_Helper_Order::create_order(); } - public function test_reset_data() - { - $buildOrder = new WC_Retailcrm_Order($this->getOptions()); - $data = $buildOrder->build($this->order)->get_data(); - - $this->assertNotEmpty($data); - - $buildOrder->reset_data(); - - $this->assertEmpty(array_filter($buildOrder->get_data())); - } - public function test_empty_shipping_data() { $buildOrder = new WC_Retailcrm_Order($this->getOptions()); - $data = $buildOrder->build($this->order)->get_data(); + $data = $buildOrder->build($this->order)->getData(); $this->assertNotEmpty($data); $this->assertArrayHasKey('firstName', $data); @@ -51,7 +39,7 @@ class WC_Retailcrm_Order_Test extends WC_Retailcrm_Test_Case_Helper { $this->order->set_shipping_country(''); - $data = $buildOrder->build($this->order)->get_data(); + $data = $buildOrder->build($this->order)->getData(); $this->assertNotEmpty($data); $this->assertArrayHasKey('countryIso', $data); diff --git a/tests/test-wc-retailcrm-history.php b/tests/test-wc-retailcrm-history.php index 72c0556..d403294 100644 --- a/tests/test-wc-retailcrm-history.php +++ b/tests/test-wc-retailcrm-history.php @@ -46,57 +46,59 @@ class WC_Retailcrm_History_Test extends WC_Retailcrm_Test_Case_Helper if (!$wcOrder) { $this->fail('$order_added is null - no orders were added after receiving history'); } + $options = get_option(\WC_Retailcrm_Base::$option_key); + $orderItems = $wcOrder->get_items(); + $orderItem = reset($orderItems); + $shippingAddress = $wcOrder->get_address('shipping'); + $billingAddress = $wcOrder->get_address('billing'); - $order_added_items = $wcOrder->get_items(); - $order_added_item = reset($order_added_items); - $shipping_address = $wcOrder->get_address('shipping'); - $billing_address = $wcOrder->get_address('billing'); - $options = get_option(\WC_Retailcrm_Base::$option_key); $this->assertEquals('status1', $options[$wcOrder->get_status()]); - if (is_object($order_added_item)) { - $this->assertEquals($product->get_id(), $order_added_item->get_product()->get_id()); + if (is_object($orderItem)) { + $this->assertEquals($product->get_id(), $orderItem->get_product()->get_id()); } + $this->assertNotEmpty($wcOrder->get_date_created()); - $this->assertNotEmpty($shipping_address['first_name']); - $this->assertNotEmpty($shipping_address['last_name']); - $this->assertNotEmpty($shipping_address['postcode']); - $this->assertNotEmpty($shipping_address['city']); - $this->assertNotEmpty($shipping_address['country']); - $this->assertNotEmpty($shipping_address['state']); + $this->assertNotEmpty($shippingAddress['first_name']); + $this->assertNotEmpty($shippingAddress['last_name']); + $this->assertNotEmpty($shippingAddress['postcode']); + $this->assertNotEmpty($shippingAddress['city']); + $this->assertNotEmpty($shippingAddress['country']); + $this->assertNotEmpty($shippingAddress['state']); - $this->assertEquals('Test_Name', $shipping_address['first_name']); - $this->assertEquals('Test_LastName', $shipping_address['last_name']); - $this->assertEquals('City', $shipping_address['city']); - $this->assertEquals('Region', $shipping_address['state']); - $this->assertEquals('ES', $shipping_address['country']); - $this->assertEquals(123456, $shipping_address['postcode']); - $this->assertEquals('Street', $shipping_address['address_1']); + $this->assertEquals('Test_Name', $shippingAddress['first_name']); + $this->assertEquals('Test_LastName', $shippingAddress['last_name']); + $this->assertEquals('City', $shippingAddress['city']); + $this->assertEquals('Region', $shippingAddress['state']); + $this->assertEquals('ES', $shippingAddress['country']); + $this->assertEquals(123456, $shippingAddress['postcode']); + $this->assertEquals('Street1', $shippingAddress['address_1']); + $this->assertEquals('Street2', $shippingAddress['address_2']); - if (isset($billing_address['phone'])) { - $this->assertNotEmpty($billing_address['phone']); + if (isset($billingAddress['phone'])) { + $this->assertNotEmpty($billingAddress['phone']); } - if (isset($billing_address['email'])) { - $this->assertNotEmpty($billing_address['email']); + if (isset($billingAddress['email'])) { + $this->assertNotEmpty($billingAddress['email']); } - $this->assertNotEmpty($billing_address['first_name']); - $this->assertNotEmpty($billing_address['last_name']); - $this->assertNotEmpty($billing_address['postcode']); - $this->assertNotEmpty($billing_address['city']); - $this->assertNotEmpty($billing_address['country']); - $this->assertNotEmpty($billing_address['state']); + $this->assertNotEmpty($billingAddress['first_name']); + $this->assertNotEmpty($billingAddress['last_name']); + $this->assertNotEmpty($billingAddress['postcode']); + $this->assertNotEmpty($billingAddress['city']); + $this->assertNotEmpty($billingAddress['country']); + $this->assertNotEmpty($billingAddress['state']); - $this->assertEquals('Test_Name', $billing_address['first_name']); - $this->assertEquals('Test_LastName', $billing_address['last_name']); - $this->assertEquals('City', $billing_address['city']); - $this->assertEquals('Region', $billing_address['state']); - $this->assertEquals('ES', $billing_address['country']); - $this->assertEquals(123456, $billing_address['postcode']); - $this->assertEquals('Street', $billing_address['address_1']); + $this->assertEquals('Test_Name', $billingAddress['first_name']); + $this->assertEquals('Test_LastName', $billingAddress['last_name']); + $this->assertEquals('City', $billingAddress['city']); + $this->assertEquals('Region', $billingAddress['state']); + $this->assertEquals('ES', $billingAddress['country']); + $this->assertEquals(123456, $billingAddress['postcode']); + $this->assertEquals('Street', $billingAddress['address_1']); if ($wcOrder->get_payment_method()) { $this->assertEquals('payment4', $options[$wcOrder->get_payment_method()]);