1
0
Fork 0
mirror of synced 2025-04-06 07:13:33 +03:00

Adding api client validation

Added function existence check
Creating tests
This commit is contained in:
Ivan Chaplygin 2024-09-13 15:36:15 +03:00
parent 762d5ec78a
commit 7a146e0172
3 changed files with 139 additions and 2 deletions

View file

@ -58,7 +58,12 @@ if (!class_exists('WC_Retailcrm_Upload_Discount_Price')):
]
);
wp_cache_flush_runtime();
/** WP version >= 6 */
if (function_exists('wp_cache_flush_runtime')) {
wp_cache_flush_runtime();
} else {
wp_cache_flush();
}
if (empty($products)) {
writeBaseLogs('Can`t get products!');
@ -147,7 +152,9 @@ if (!class_exists('WC_Retailcrm_Upload_Discount_Price')):
private function uploadSettings()
{
if (!$this->apiClient instanceof WC_Retailcrm_Proxy) {
if (!$this->apiClient instanceof WC_Retailcrm_Proxy
&& !$this->apiClient instanceof WC_Retailcrm_Client_V5
) {
return 'API client has not been initialized';
}

View file

@ -0,0 +1,56 @@
<?php
namespace datasets;
/**
* PHP version 7.0
*
* Class DataUploadPriceRetailCrm - Data set for WC_Retailcrm_Upload_Discount_Price_Test.
*
* @category Integration
* @author RetailCRM <integration@retailcrm.ru>
* @license http://retailcrm.ru Proprietary
* @link http://retailcrm.ru
* @see http://help.retailcrm.ru
*/
class DataUploadPriceRetailCrm
{
public static function dataGetPriceTypes() {
return [
'success' => true,
'priceTypes' => [
[
'code' => 'test',
'name' => 'test',
'active' => true,
'description' => 'test',
'ordering' => 999,
'promo' => true,
'default' => false
],
[
'code' => 'default',
'name' => 'default',
'active' => true,
'description' => 'default',
'ordering' => 999,
'promo' => true,
'default' => true
],
]
];
}
public static function willSendPriceType() {
return [
'code' => 'woo-promotion-lp',
'name' => 'Woocommerce promotional price',
'active' => true,
'description' => 'Promotional price type for Woocommerce store, generated automatically.
Necessary for correct synchronization work when loyalty program is enabled
(Do not delete. Do not deactivate)',
'ordering' => 999,
'promo' => true,
];
}
}

View file

@ -0,0 +1,74 @@
<?php
use datasets\DataUploadPriceRetailCrm;
/**
* PHP version 7.0
*
* WC_Retailcrm_Upload_Discount_Price_Test
*
* @category Integration
* @author RetailCRM <integration@retailcrm.ru>
* @license http://retailcrm.ru Proprietary
* @link http://retailcrm.ru
* @see http://help.retailcrm.ru
*/
class WC_Retailcrm_Upload_Discount_Price_Test extends WC_Retailcrm_Test_Case_Helper
{
protected $apiMock;
protected $responseMock;
public function setUp()
{
WC_Helper_Product::create_simple_product();
WC_Helper_Product::create_variation_product();
$this->setOptions();
$this->responseMock = $this->getMockBuilder('\WC_Retailcrm_Response_Helper')
->disableOriginalConstructor()
->setMethods(['isSuccessful'])
->getMock()
;
$this->apiMock = $this->getMockBuilder('\WC_Retailcrm_Client_V5')
->disableOriginalConstructor()
->setMethods(['storePricesUpload', 'getSingleSiteForKey', 'getPriceTypes', 'editPriceType'])
->getMock()
;
$this->responseMock->setResponse(['success' => true]);
$this->setMockResponse($this->responseMock, 'isSuccessful', true);
$this->setMockResponse($this->apiMock, 'getSingleSiteForKey', 'woo');
$this->responseMock->setResponse(DataUploadPriceRetailCrm::dataGetPriceTypes());
$this->setMockResponse($this->apiMock, 'getPriceTypes', $this->responseMock);
}
public function testUpload()
{
$this->apiMock
->expects($this->exactly(1))
->method('storePricesUpload')
->with($this->callback(
function ($parameter) {
if (is_array($parameter)) {
return true;
}
return false;
}
), $this->equalTo('woo'))
;
$this->apiMock
->expects($this->exactly(1))
->method('editPriceType')
->with($this->identicalTo(DataUploadPriceRetailCrm::willSendPriceType()))
->willReturn($this->responseMock)
;
$uploadService = new WC_Retailcrm_Upload_Discount_Price($this->apiMock);
$uploadService->upload();
}
}