From fa2994fc3afdc290514a908bef9bc4c8c5bb89a8 Mon Sep 17 00:00:00 2001 From: Akolzin Dmitry Date: Wed, 18 Apr 2018 09:46:08 +0300 Subject: [PATCH] customer tests --- tests/phpunit/test-wc-retailcrm-base.php | 32 ++--- tests/phpunit/test-wc-retailcrm-customers.php | 92 ++++++++++--- .../phpunit/test-wc-retailcrm-inventories.php | 14 +- tests/phpunit/test-wc-retailcrm-orders.php | 10 +- .../include/class-wc-retailcrm-base.php | 2 + .../include/class-wc-retailcrm-customers.php | 121 +++++++++--------- .../include/class-wc-retailcrm-ga.php | 28 ++-- .../include/class-wc-retailcrm-plugin.php | 78 +++++------ woo-retailcrm/retailcrm.php | 12 +- 9 files changed, 223 insertions(+), 166 deletions(-) diff --git a/tests/phpunit/test-wc-retailcrm-base.php b/tests/phpunit/test-wc-retailcrm-base.php index c6056be..f2ccf2b 100644 --- a/tests/phpunit/test-wc-retailcrm-base.php +++ b/tests/phpunit/test-wc-retailcrm-base.php @@ -2,30 +2,30 @@ class WC_Retailcrm_Base_Test extends WC_Unit_Test_Case { - protected $apiMock; - private $unit; + protected $apiMock; + private $unit; - public function setUp() - { - $this->apiMock = $this->getMockBuilder('\WC_Retailcrm_Proxy') - ->disableOriginalConstructor() - ->getMock(); + public function setUp() + { + $this->apiMock = $this->getMockBuilder('\WC_Retailcrm_Proxy') + ->disableOriginalConstructor() + ->getMock(); - $this->unit = new \WC_Retailcrm_Base(); - $this->unit->apiClient = $this->apiMock; - } + $this->unit = new \WC_Retailcrm_Base(); + $this->unit->apiClient = $this->apiMock; + } public function test_retailcrm_check_custom_file() { - $file = \WC_Retailcrm_Base::checkCustomFile('ga'); - $this->assertInternalType('string', $file); + $file = \WC_Retailcrm_Base::checkCustomFile('ga'); + $this->assertInternalType('string', $file); } public function test_retailcrm_form_fields() { - $this->assertInternalType('array', $this->unit->form_fields); - $this->assertArrayHasKey('api_url', $this->unit->form_fields); - $this->assertArrayHasKey('api_key', $this->unit->form_fields); - $this->assertArrayHasKey('api_version', $this->unit->form_fields); + $this->assertInternalType('array', $this->unit->form_fields); + $this->assertArrayHasKey('api_url', $this->unit->form_fields); + $this->assertArrayHasKey('api_key', $this->unit->form_fields); + $this->assertArrayHasKey('api_version', $this->unit->form_fields); } } diff --git a/tests/phpunit/test-wc-retailcrm-customers.php b/tests/phpunit/test-wc-retailcrm-customers.php index b951751..dc18e0a 100644 --- a/tests/phpunit/test-wc-retailcrm-customers.php +++ b/tests/phpunit/test-wc-retailcrm-customers.php @@ -32,10 +32,22 @@ class WC_Retailcrm_Customers_Test extends WC_Unit_Test_Case $this->customer = new WC_Customer(); $this->customer->set_email(uniqid(md5(date('Y-m-d H:i:s'))) . '@mail.com'); $this->customer->set_password('password'); - $this->customer->set_role('customer'); + $this->customer->set_role(WC_Retailcrm_Customers::CUSTOMER_ROLE); + $this->customer->set_billing_phone('89000000000'); $this->customer->save(); } + /** + * @param retailcrm + * @dataProvider dataProviderApiClient + */ + public function test_wc_customer_get($retailcrm) + { + $wc_customer = new WC_Customer($this->customer->get_id()); + $retailcrm_customer = new WC_Retailcrm_Customers($retailcrm); + $this->assertEquals($wc_customer, $retailcrm_customer->wcCustomerGet($this->customer->get_id())); + } + /** * @param retailcrm * @dataProvider dataProviderApiClient @@ -43,40 +55,78 @@ class WC_Retailcrm_Customers_Test extends WC_Unit_Test_Case public function test_customers_upload($retailcrm) { $retailcrm_customer = new WC_Retailcrm_Customers($retailcrm); - $retailcrm_customer->customersUpload(); + $data = $retailcrm_customer->customersUpload(); + + if ($retailcrm) { + $this->assertInternalType('array', $data); + $this->assertInternalType('array', $data[0]); + $this->assertArrayHasKey('externalId', $data[0][0]); + } else { + $this->assertEquals(null, $data); + } } - /** - * @param $retailcrm - * @dataProvider dataProviderApiClient - */ + /** + * @param $retailcrm + * @dataProvider dataProviderApiClient + */ public function test_create_customer($retailcrm) { $retailcrm_customer = new WC_Retailcrm_Customers($retailcrm); - $retailcrm_customer->createCustomer($this->customer->get_id()); + $customer = $retailcrm_customer->createCustomer($this->customer->get_id()); + $customer_send = $retailcrm_customer->getCustomer(); + + if ($retailcrm) { + $this->assertArrayHasKey('externalId', $customer_send); + $this->assertArrayHasKey('firstName', $customer_send); + $this->assertArrayHasKey('createdAt', $customer_send); + $this->assertArrayHasKey('email', $customer_send); + $this->assertNotEmpty($customer_send['externalId']); + $this->assertNotEmpty($customer_send['firstName']); + $this->assertNotEmpty($customer_send['email']); + $this->assertInstanceOf('WC_Customer', $customer); + } else { + $this->assertEquals(null, $customer); + $this->assertEquals(array(), $customer_send); + } } - /** - * @param $retailcrm - * @dataProvider dataProviderApiClient - */ + /** + * @param $retailcrm + * @dataProvider dataProviderApiClient + */ public function test_update_customer($retailcrm) { $retailcrm_customer = new WC_Retailcrm_Customers($retailcrm); - $retailcrm_customer->updateCustomer($this->customer->get_id()); + $customer = $retailcrm_customer->updateCustomer($this->customer->get_id()); + $customer_send = $retailcrm_customer->getCustomer(); + + if ($retailcrm) { + $this->assertArrayHasKey('externalId', $customer_send); + $this->assertArrayHasKey('firstName', $customer_send); + $this->assertArrayHasKey('createdAt', $customer_send); + $this->assertArrayHasKey('email', $customer_send); + $this->assertNotEmpty($customer_send['externalId']); + $this->assertNotEmpty($customer_send['firstName']); + $this->assertNotEmpty($customer_send['email']); + $this->assertInstanceOf('WC_Customer', $customer); + } else { + $this->assertEquals(null, $customer); + $this->assertEquals(array(), $customer_send); + } } public function dataProviderApiClient() { - $this->setUp(); + $this->setUp(); - return array( - array( - 'retailcrm' => $this->apiMock - ), - array( - 'retailcrm' => false - ) - ); + return array( + array( + 'retailcrm' => $this->apiMock + ), + array( + 'retailcrm' => false + ) + ); } } \ No newline at end of file diff --git a/tests/phpunit/test-wc-retailcrm-inventories.php b/tests/phpunit/test-wc-retailcrm-inventories.php index 8d62588..5e9c5a4 100644 --- a/tests/phpunit/test-wc-retailcrm-inventories.php +++ b/tests/phpunit/test-wc-retailcrm-inventories.php @@ -8,8 +8,8 @@ class WC_Retailcrm_Inventories_Test extends WC_Unit_Test_Case public function setUp() { - $this->offer = new WC_Product_Simple(); - $this->offer->save(); + $this->offer = new WC_Product_Simple(); + $this->offer->save(); $this->responseMock = $this->getMockBuilder('\WC_Retailcrm_Response') ->disableOriginalConstructor() @@ -25,11 +25,11 @@ class WC_Retailcrm_Inventories_Test extends WC_Unit_Test_Case )) ->getMock(); - $this->apiMock->expects($this->any()) - ->method('storeInventories') - ->willReturn($this->getTestData()); + $this->apiMock->expects($this->any()) + ->method('storeInventories') + ->willReturn($this->getTestData()); - parent::setUp(); + parent::setUp(); } /** @@ -65,7 +65,7 @@ class WC_Retailcrm_Inventories_Test extends WC_Unit_Test_Case public function dataProviderLoadStocks() { - $this->setUp(); + $this->setUp(); return array( array( diff --git a/tests/phpunit/test-wc-retailcrm-orders.php b/tests/phpunit/test-wc-retailcrm-orders.php index 8f91f6d..fdb0373 100644 --- a/tests/phpunit/test-wc-retailcrm-orders.php +++ b/tests/phpunit/test-wc-retailcrm-orders.php @@ -27,9 +27,9 @@ class WC_Retailcrm_Orders_Test extends WC_Unit_Test_Case )) ->getMock(); - $this->apiMock->expects($this->any()) - ->method('ordersEdit') - ->willReturn($this->responseMock); + $this->apiMock->expects($this->any()) + ->method('ordersEdit') + ->willReturn($this->responseMock); $this->order = new WC_Order(); $this->order->save(); @@ -93,7 +93,7 @@ class WC_Retailcrm_Orders_Test extends WC_Unit_Test_Case public function dataProviderUpdateOrder() { - $this->setUp(); + $this->setUp(); return array( array( @@ -117,7 +117,7 @@ class WC_Retailcrm_Orders_Test extends WC_Unit_Test_Case public function dataProviderRetailcrm() { - $this->setUp(); + $this->setUp(); return array( array( diff --git a/woo-retailcrm/include/class-wc-retailcrm-base.php b/woo-retailcrm/include/class-wc-retailcrm-base.php index 11ee216..c01da5b 100644 --- a/woo-retailcrm/include/class-wc-retailcrm-base.php +++ b/woo-retailcrm/include/class-wc-retailcrm-base.php @@ -64,7 +64,9 @@ if (!class_exists('WC_Retailcrm_Base')) { /** * Check custom file + * * @param string $file + * * @return string */ public static function checkCustomFile($file) { diff --git a/woo-retailcrm/include/class-wc-retailcrm-customers.php b/woo-retailcrm/include/class-wc-retailcrm-customers.php index 1526254..b2f1622 100644 --- a/woo-retailcrm/include/class-wc-retailcrm-customers.php +++ b/woo-retailcrm/include/class-wc-retailcrm-customers.php @@ -7,20 +7,23 @@ * @author RetailCRM */ -if ( ! class_exists( 'WC_Retailcrm_Customers' ) ) : +if (!class_exists('WC_Retailcrm_Customers')) : /** * Class WC_Retailcrm_Customers */ - class WC_Retailcrm_Customers - { - const CUSTOMER_ROLE = 'customer'; + class WC_Retailcrm_Customers { + + const CUSTOMER_ROLE = 'customer'; protected $retailcrm; protected $retailcrm_settings; + private $customer = array(); + /** * WC_Retailcrm_Customers constructor. + * * @param $retailcrm */ public function __construct($retailcrm = false) @@ -32,105 +35,89 @@ if ( ! class_exists( 'WC_Retailcrm_Customers' ) ) : /** * Upload customers to CRM * - * @return void + * @return array $data */ public function customersUpload() { - if (!$this->retailcrm) { - return; - } + if (!$this->retailcrm) { + return; + } $users = get_users(); $data_customers = array(); foreach ($users as $user) { - if (!in_array(self::CUSTOMER_ROLE, $user->roles)) { + if (!\in_array(self::CUSTOMER_ROLE, $user->roles)) { continue; } - $customer = new WC_Customer($user->ID); - $firstName = $customer->get_first_name(); - $data_customer = array( - 'createdAt' => $user->data->user_registered, - 'externalId' => $user->ID, - 'firstName' => $firstName ? $firstName : $customer->get_username(), - 'lastName' => $customer->get_last_name(), - 'email' => $user->data->user_email, - 'address' => array( - 'index' => $customer->get_billing_postcode(), - 'countryIso' => $customer->get_billing_country(), - 'region' => $customer->get_billing_state(), - 'city' => $customer->get_billing_city(), - 'text' => $customer->get_billing_address_1() . ',' . $customer->get_billing_address_2() - ) - ); - - if ($customer->get_billing_phone()) { - $data_customer['phones'][] = array( - 'number' => $customer->get_billing_phone() - ); - } - - $data_customers[] = $data_customer; + $customer = $this->wcCustomerGet($user->ID); + $this->processCustomer($customer); + $data_customers[] = $this->customer; } - $data = array_chunk($data_customers, 50); + $data = \array_chunk($data_customers, 50); foreach ($data as $array_customers) { $this->retailcrm->customersUpload($array_customers); + time_nanosleep(0, 250000000); } + + return $data; } /** * Create customer in CRM - * + * * @param int $customer_id - * - * @return void + * + * @return WC_Customer $customer */ public function createCustomer($customer_id) { - if (!$this->retailcrm) { - return; - } + if (!$this->retailcrm) { + return; + } - $customer = new WC_Customer($customer_id); + $customer = $this->wcCustomerGet($customer_id); if ($customer->get_role() == self::CUSTOMER_ROLE) { - $data_customer = $this->processCustomer($customer); - - $this->retailcrm->customersCreate($data_customer); + $this->processCustomer($customer); + $this->retailcrm->customersCreate($this->customer); } + + return $customer; } /** * Edit customer in CRM - * + * * @param int $customer_id - * - * @return void + * + * @return WC_Customer $customer */ public function updateCustomer($customer_id) { - if (!$this->retailcrm) { - return; - } + if (!$this->retailcrm) { + return; + } - $customer = new WC_Customer($customer_id); + $customer = $this->wcCustomerGet($customer_id); if ($customer->get_role() == self::CUSTOMER_ROLE){ - $data_customer = $this->processCustomer($customer); - - $this->retailcrm->customersEdit($data_customer); + $this->processCustomer($customer); + $this->retailcrm->customersEdit($this->customer); } + + return $customer; } /** * Process customer - * + * * @param WC_Customer $customer - * - * @return array $data_customer + * + * @return void */ protected function processCustomer($customer) { @@ -157,7 +144,25 @@ if ( ! class_exists( 'WC_Retailcrm_Customers' ) ) : ); } - return apply_filters('retailcrm_process_customer', $data_customer); + $this->customer = apply_filters('retailcrm_process_customer', $data_customer); + } + + /** + * @param int $customer_id + * + * @return WC_Customer + */ + public function wcCustomerGet($customer_id) + { + return new WC_Customer($customer_id); + } + + /** + * @return array + */ + public function getCustomer() + { + return $this->customer; } } endif; diff --git a/woo-retailcrm/include/class-wc-retailcrm-ga.php b/woo-retailcrm/include/class-wc-retailcrm-ga.php index 80fcb15..3404b1b 100644 --- a/woo-retailcrm/include/class-wc-retailcrm-ga.php +++ b/woo-retailcrm/include/class-wc-retailcrm-ga.php @@ -6,9 +6,9 @@ if (!class_exists('WC_Retailcrm_Google_Analytics')) { private static $instance; private $options; - /** - * @return WC_Retailcrm_Google_Analytics - */ + /** + * @return WC_Retailcrm_Google_Analytics + */ public static function getInstance($options = array()) { if (self::$instance === null) { @@ -18,19 +18,19 @@ if (!class_exists('WC_Retailcrm_Google_Analytics')) { return self::$instance; } - /** - * WC_Retailcrm_Google_Analytics constructor. - * - * @param array $options - */ + /** + * WC_Retailcrm_Google_Analytics constructor. + * + * @param array $options + */ private function __construct($options = array()) { $this->options = $options; } - /** - * @return string - */ + /** + * @return string + */ public function initialize_analytics() { return "