* Adding and testing new cart API methods * Fix the causes of errors in old tests --------- Co-authored-by: Ivan Chaplygin <chaplygin@retailcrm.ru>
This commit is contained in:
parent
6c08749e8b
commit
be5aedef94
@ -70,6 +70,90 @@ class WC_Retailcrm_Client_V5
|
||||
return $this->unversionedClient->makeRequest('/credentials', WC_Retailcrm_Request::METHOD_GET);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get cart by id or externalId
|
||||
*
|
||||
* @param $customerId
|
||||
* @param string $site
|
||||
* @param $by (default: 'externalId')
|
||||
*
|
||||
* @return WC_Retailcrm_Response
|
||||
*
|
||||
* @throws InvalidArgumentException
|
||||
* @throws WC_Retailcrm_Exception_Curl
|
||||
* @throws WC_Retailcrm_Exception_Json
|
||||
*/
|
||||
public function cartGet($customerId, string $site, $by = 'externalId')
|
||||
{
|
||||
$this->checkIdParameter($by);
|
||||
|
||||
if (empty($site)) {
|
||||
throw new InvalidArgumentException(
|
||||
'Site must be set'
|
||||
);
|
||||
}
|
||||
|
||||
return $this->client->makeRequest(
|
||||
sprintf('/customer-interaction/%s/cart/%s', $site, $customerId),
|
||||
WC_Retailcrm_Request::METHOD_GET,
|
||||
['by' => $by]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create or update cart
|
||||
*
|
||||
* @param array $cart
|
||||
* @param string $site
|
||||
*
|
||||
* @return WC_Retailcrm_Response
|
||||
*
|
||||
* @throws InvalidArgumentException
|
||||
* @throws WC_Retailcrm_Exception_Curl
|
||||
* @throws WC_Retailcrm_Exception_Json
|
||||
*/
|
||||
public function cartSet(array $cart, string $site)
|
||||
{
|
||||
if (empty($site)) {
|
||||
throw new InvalidArgumentException(
|
||||
'Site must be set'
|
||||
);
|
||||
}
|
||||
|
||||
return $this->client->makeRequest(
|
||||
sprintf('/customer-interaction/%s/cart/set', $site),
|
||||
WC_Retailcrm_Request::METHOD_POST,
|
||||
['cart' => json_encode($cart)]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear customer cart
|
||||
*
|
||||
* @param array $cart
|
||||
* @param string $site
|
||||
*
|
||||
* @return WC_Retailcrm_Response
|
||||
*
|
||||
* @throws InvalidArgumentException
|
||||
* @throws WC_Retailcrm_Exception_Curl
|
||||
* @throws WC_Retailcrm_Exception_Json
|
||||
*/
|
||||
public function cartClear(array $cart, string $site)
|
||||
{
|
||||
if (empty($site)) {
|
||||
throw new InvalidArgumentException(
|
||||
'Site must be set'
|
||||
);
|
||||
}
|
||||
|
||||
return $this->client->makeRequest(
|
||||
sprintf('/customer-interaction/%s/cart/clear', $site),
|
||||
WC_Retailcrm_Request::METHOD_POST,
|
||||
['cart' => json_encode($cart)]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns filtered corporate customers list
|
||||
*
|
||||
|
102
tests/datasets/data-cart-retailcrm.php
Normal file
102
tests/datasets/data-cart-retailcrm.php
Normal file
@ -0,0 +1,102 @@
|
||||
<?php
|
||||
|
||||
namespace datasets;
|
||||
|
||||
/**
|
||||
* PHP version 7.0
|
||||
*
|
||||
* Class DataCartRetailCrm - Data set for WC_Cart_Customers_Test.
|
||||
*
|
||||
* @category Integration
|
||||
* @author RetailCRM <integration@retailcrm.ru>
|
||||
* @license http://retailcrm.ru Proprietary
|
||||
* @link http://retailcrm.ru
|
||||
* @see http://help.retailcrm.ru
|
||||
*/
|
||||
|
||||
class DataCartRetailCrm
|
||||
{
|
||||
public static function dataGetCart() {
|
||||
return [
|
||||
'success' => true,
|
||||
'cart' => [
|
||||
'clearedAt' => new \DateTime('now'),
|
||||
'externalId' => '1',
|
||||
'updateAt' => new \DateTime('now'),
|
||||
'droppedAt' => new \DateTime('now'),
|
||||
'link' => 'https:://link/cart/152',
|
||||
'items' => [
|
||||
0 => [
|
||||
'quantity' => 3,
|
||||
'price' => 1500,
|
||||
'createdAt' => new \DateTime('now'),
|
||||
'updatedAt' => new \DateTime('now'),
|
||||
'offer' => [
|
||||
'id' => 1,
|
||||
'externalId' => '1',
|
||||
'name' => 'test product',
|
||||
'properties' => [
|
||||
'prop1' => 'prop',
|
||||
],
|
||||
'unit' => [
|
||||
'code' => 'test code',
|
||||
'name' => 'test unit name',
|
||||
'sym' => 'sym',
|
||||
],
|
||||
'barcode' => '123456789',
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public static function dataSetCart() {
|
||||
return [
|
||||
'cart' => [
|
||||
'clearedAt' => new \DateTime('now'),
|
||||
'externalId' => '1',
|
||||
'updateAt' => new \DateTime('now'),
|
||||
'droppedAt' => new \DateTime('now'),
|
||||
'link' => 'https:://link/cart/152',
|
||||
'customer' => [
|
||||
'id' => 1,
|
||||
'externalId' => '1',
|
||||
'browserId' => '145874',
|
||||
'site' => 'test-site',
|
||||
],
|
||||
'items' => [
|
||||
0 => [
|
||||
'quantity' => 3,
|
||||
'price' => 1500,
|
||||
'createdAt' => new \DateTime('now'),
|
||||
'updatedAt' => new \DateTime('now'),
|
||||
'offer' => [
|
||||
'id' => 1,
|
||||
'externalId' => '1',
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public static function dataClearCart() {
|
||||
return [
|
||||
'cart' => [
|
||||
'clearedAt' => new \DateTime('now'),
|
||||
'customer' => [
|
||||
'id' => 1,
|
||||
'externalId' => '1',
|
||||
'browserId' => '145874',
|
||||
],
|
||||
'order' => [
|
||||
'id' => '1',
|
||||
'externalId' => '1',
|
||||
'number' => '152C',
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
}
|
72
tests/test-wc-retailcrm-cart.php
Normal file
72
tests/test-wc-retailcrm-cart.php
Normal file
@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
use datasets\DataCartRetailCrm;
|
||||
|
||||
/**
|
||||
* PHP version 7.0
|
||||
*
|
||||
* Class WC_Retailcrm_Cart_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_Cart_Test extends WC_Retailcrm_Test_Case_Helper
|
||||
{
|
||||
protected $apiClientMock;
|
||||
protected $responseMock;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->responseMock = $this->getMockBuilder('\WC_Retailcrm_Response_Helper')
|
||||
->disableOriginalConstructor()
|
||||
->setMethods(
|
||||
[
|
||||
'isSuccessful',
|
||||
'offsetExists',
|
||||
]
|
||||
)
|
||||
->getMock();
|
||||
|
||||
$this->responseMock->setResponse(['id' => 1]);
|
||||
|
||||
$this->apiClientMock = $this->getMockBuilder('\WC_Retailcrm_Client_V5')
|
||||
->disableOriginalConstructor()
|
||||
->setMethods(
|
||||
[
|
||||
'cartGet',
|
||||
'cartSet',
|
||||
'cartClear',
|
||||
]
|
||||
)
|
||||
->getMock();
|
||||
|
||||
$this->setMockResponse($this->responseMock, 'isSuccessful', true);
|
||||
$this->setMockResponse($this->responseMock, 'offsetExists', true);
|
||||
$this->setMockResponse($this->apiClientMock, 'cartSet', $this->responseMock);
|
||||
$this->setMockResponse($this->apiClientMock, 'cartClear', $this->responseMock);
|
||||
$this->setMockResponse($this->apiClientMock, 'cartGet',$this->responseMock);
|
||||
}
|
||||
|
||||
public function testGetCart()
|
||||
{
|
||||
$this->responseMock->setResponse(DataCartRetailCrm::dataGetCart());
|
||||
$response = $this->apiClientMock->cartGet(1, 'test-site');
|
||||
$this->assertNotEmpty($response->__get('cart'));
|
||||
$this->assertTrue($response->__get('success'));
|
||||
}
|
||||
|
||||
public function testSetCart()
|
||||
{
|
||||
$response = $this->apiClientMock->cartSet(DataCartRetailCrm::dataSetCart(), 'test-site');
|
||||
$this->assertEquals(1, $response->__get('id'));
|
||||
}
|
||||
|
||||
public function testClearCart()
|
||||
{
|
||||
$response = $this->apiClientMock->cartClear(DataCartRetailCrm::dataClearCart(), 'test-site');
|
||||
$this->assertEquals(1, $response->__get('id'));
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user