1
0
mirror of synced 2025-01-30 14:51:42 +03:00

ref #88120 Adding API methods for handling cart (#296)

* 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:
Kocmonavtik 2023-02-28 18:08:00 +03:00 committed by Alex Lushpai
parent 6c08749e8b
commit be5aedef94
3 changed files with 258 additions and 0 deletions

View File

@ -70,6 +70,90 @@ class WC_Retailcrm_Client_V5
return $this->unversionedClient->makeRequest('/credentials', WC_Retailcrm_Request::METHOD_GET); 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 * Returns filtered corporate customers list
* *

View 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',
],
],
];
}
}

View 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'));
}
}