inventories tests
This commit is contained in:
parent
fa2994fc3a
commit
fd8e98ac63
5 changed files with 119 additions and 40 deletions
13
tests/helpers/class-wc-retailcrm-response-helper.php
Normal file
13
tests/helpers/class-wc-retailcrm-response-helper.php
Normal file
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
|
||||
if (!class_exists('WC_Retailcrm_Response')) {
|
||||
require_once dirname(__FILE__) . '/../../woo-retailcrm/include/api/class-wc-retailcrm-response.php';
|
||||
}
|
||||
|
||||
class WC_Retailcrm_Response_Helper extends WC_Retailcrm_Response
|
||||
{
|
||||
public function setResponse($response)
|
||||
{
|
||||
$this->response = $response;
|
||||
}
|
||||
}
|
|
@ -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';
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
class WC_Retailcrm_Inventories_Test extends WC_Unit_Test_Case
|
||||
class WC_Retailcrm_Inventories_Test extends WC_Unit_Test_Case
|
||||
{
|
||||
protected $apiMock;
|
||||
protected $responseMock;
|
||||
|
@ -11,7 +11,7 @@ class WC_Retailcrm_Inventories_Test extends WC_Unit_Test_Case
|
|||
$this->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']
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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() {
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Add table
Reference in a new issue