diff --git a/tests/helpers/class-wc-retailcrm-response-helper.php b/tests/helpers/class-wc-retailcrm-response-helper.php new file mode 100644 index 0000000..0da46de --- /dev/null +++ b/tests/helpers/class-wc-retailcrm-response-helper.php @@ -0,0 +1,13 @@ +response = $response; + } +} diff --git a/tests/phpunit/bootstrap.php b/tests/phpunit/bootstrap.php index 61766c8..3e4a910 100644 --- a/tests/phpunit/bootstrap.php +++ b/tests/phpunit/bootstrap.php @@ -10,6 +10,9 @@ require_once $_tests_dir . '/includes/functions.php'; function _manually_load_plugin() { $plugin_dir = dirname( dirname( dirname( dirname( __FILE__ ) ) ) ) . '/'; + // helpers + require $plugin_dir . 'woocommerce-retailcrm/tests/helpers/class-wc-retailcrm-response-helper.php'; + require $plugin_dir . 'woocommerce-retailcrm/woo-retailcrm/include/class-wc-retailcrm-orders.php'; require $plugin_dir . 'woocommerce-retailcrm/woo-retailcrm/include/class-wc-retailcrm-customers.php'; require $plugin_dir . 'woocommerce-retailcrm/woo-retailcrm/include/class-wc-retailcrm-inventories.php'; diff --git a/tests/phpunit/test-wc-retailcrm-inventories.php b/tests/phpunit/test-wc-retailcrm-inventories.php index 5e9c5a4..8e48fea 100644 --- a/tests/phpunit/test-wc-retailcrm-inventories.php +++ b/tests/phpunit/test-wc-retailcrm-inventories.php @@ -1,6 +1,6 @@ offer = new WC_Product_Simple(); $this->offer->save(); - $this->responseMock = $this->getMockBuilder('\WC_Retailcrm_Response') + $this->responseMock = $this->getMockBuilder('\WC_Retailcrm_Response_Helper') ->disableOriginalConstructor() ->setMethods(array( 'isSuccessful' @@ -25,40 +25,72 @@ class WC_Retailcrm_Inventories_Test extends WC_Unit_Test_Case )) ->getMock(); - $this->apiMock->expects($this->any()) - ->method('storeInventories') - ->willReturn($this->getTestData()); - parent::setUp(); } /** * @param $retailcrm + * @param $response + * * @dataProvider dataProviderLoadStocks */ - public function test_load_stocks($retailcrm) + public function test_load_stocks($retailcrm, $response) { + if ($response['success'] == true) { + $this->responseMock->expects($this->any()) + ->method('isSuccessful') + ->willReturn(true); + } elseif ($response['success'] == false) { + $this->responseMock->expects($this->any()) + ->method('isSuccessful') + ->willReturn(false); + } + + $this->responseMock->setResponse($response); + + if ($retailcrm) { + $retailcrm->expects($this->any()) + ->method('storeInventories') + ->willReturn($this->responseMock); + } + $retailcrm_inventories = new WC_Retailcrm_Inventories($retailcrm); - $retailcrm_inventories->load_stocks(); + $result = $retailcrm_inventories->load_stocks(); + + if ($retailcrm && $response['success'] == true) { + $product = new WC_Product_Simple($result[0]); + $this->assertInstanceOf('WC_Product', $product); + $this->assertEquals(10, $product->get_stock_quantity()); + $this->assertContains($product->get_id(), $result); + $this->assertInternalType('array', $result); + } else { + $this->assertEquals(null, $result); + } } - private function getTestData() + private function getResponseData() { return array( - 'success' => true, - 'pagination' => array( - 'limit' => 250, - 'totalCount' => 1, - 'currentPage' => 1, - 'totalPageCount' => 1 - ), - 'offers' => array( - array( - 'id' => 1, - 'externalId' => $this->offer->get_id(), - 'xmlId' => 'xmlId', - 'quantity' => 1 + 'true' => array( + 'success' => true, + 'pagination' => array( + 'limit' => 250, + 'totalCount' => 1, + 'currentPage' => 1, + 'totalPageCount' => 1 + ), + 'offers' => array( + array( + 'id' => 1, + 'externalId' => $this->offer->get_id(), + 'xmlId' => 'xmlId', + 'quantity' => 10 + ) ) + ), + 'false' => array( + 'success' => false, + 'errorMsg' => 'Forbidden' ) ); } @@ -67,13 +99,25 @@ class WC_Retailcrm_Inventories_Test extends WC_Unit_Test_Case { $this->setUp(); + $response = $this->getResponseData(); + return array( array( - 'retailcrm' => $this->apiMock + 'retailcrm' => $this->apiMock, + 'response' => $response['true'] ), array( - 'retailcrm' => false + 'retailcrm' => false, + 'response' => $response['true'] + ), + array( + 'retailcrm' => $this->apiMock, + 'response' => $response['false'] + ), + array( + 'retailcrm' => false, + 'response' => $response['false'] ) ); } -} \ No newline at end of file +} diff --git a/woo-retailcrm/include/class-wc-retailcrm-base.php b/woo-retailcrm/include/class-wc-retailcrm-base.php index c01da5b..964fdff 100644 --- a/woo-retailcrm/include/class-wc-retailcrm-base.php +++ b/woo-retailcrm/include/class-wc-retailcrm-base.php @@ -675,7 +675,7 @@ if (!class_exists('WC_Retailcrm_Base')) { * * @return string */ - public function validate_api_version_field( $key, $value ) { + public function validate_api_version_field($key, $value) { $post = $this->get_post_data(); $versionMap = array( @@ -708,7 +708,7 @@ if (!class_exists('WC_Retailcrm_Base')) { * * @return string */ - public function validate_api_url_field( $key, $value ) { + public function validate_api_url_field($key, $value) { $post = $this->get_post_data(); $api = new WC_Retailcrm_Proxy( $value, @@ -717,8 +717,8 @@ if (!class_exists('WC_Retailcrm_Base')) { $response = $api->apiVersions(); - if ($response == NULL) { - WC_Admin_Settings::add_error( esc_html__( 'Enter the correct CRM address', 'retailcrm' ) ); + if ($response == null) { + WC_Admin_Settings::add_error(esc_html__( 'Enter the correct CRM address', 'retailcrm')); $value = ''; } @@ -780,6 +780,7 @@ if (!class_exists('WC_Retailcrm_Base')) { /** * Get retailcrm api client + * * @return bool|WC_Retailcrm_Proxy */ public function getApiClient() { diff --git a/woo-retailcrm/include/class-wc-retailcrm-inventories.php b/woo-retailcrm/include/class-wc-retailcrm-inventories.php index aa192db..bdc09e9 100644 --- a/woo-retailcrm/include/class-wc-retailcrm-inventories.php +++ b/woo-retailcrm/include/class-wc-retailcrm-inventories.php @@ -7,7 +7,7 @@ * @author RetailCRM */ -if ( ! class_exists( 'WC_Retailcrm_Inventories' ) ) : +if (!class_exists('WC_Retailcrm_Inventories')) : /** * Class WC_Retailcrm_Inventories @@ -27,8 +27,15 @@ if ( ! class_exists( 'WC_Retailcrm_Inventories' ) ) : $this->retailcrm = $retailcrm; } + /** + * Load stock from retailCRM + * + * @return mixed + */ public function load_stocks() { + $success = array(); + if (!$this->retailcrm) { return; } @@ -37,35 +44,46 @@ if ( ! class_exists( 'WC_Retailcrm_Inventories' ) ) : do { $result = $this->retailcrm->storeInventories(array(), $page, 250); + + if (!$result->isSuccessful()) { + return; + } + $totalPageCount = $result['pagination']['totalPageCount']; $page++; foreach ($result['offers'] as $offer) { if (isset($offer['externalId'])) { $product = wc_get_product($offer['externalId']); - - if ($product != false) { + + if ($product instanceof WC_Product) { if ($product->get_type() == 'variable') { continue; } - update_post_meta($offer['externalId'], '_manage_stock', 'yes'); + + $product->set_manage_stock(true); $product->set_stock_quantity($offer['quantity']); - $product->save(); + $success[] = $product->save(); } } } } while ($page <= $totalPageCount); + + return $success; } + /** + * Update stock quantity in WooCommerce + * + * @return mixed + */ public function updateQuantity() { - $options = array_filter(get_option(WC_Retailcrm_Base::$option_key)); - - if ($options['sync'] == 'yes') { - $this->load_stocks(); - } else { - return false; + if ($this->retailcrm_settings['sync'] == 'yes') { + return $this->load_stocks(); } + + return false; } } endif;