mirror of
https://github.com/retailcrm/prestashop-module.git
synced 2025-04-11 12:50:55 +00:00
Add tests for clear cart
This commit is contained in:
parent
026f8171bc
commit
ad99e04567
3 changed files with 89 additions and 44 deletions
|
@ -122,7 +122,7 @@ class RetailcrmCartUploader
|
|||
$cartExternalId = RetailcrmTools::getCartOrderExternalId($cart);
|
||||
$cartLastUpdateDate = null;
|
||||
|
||||
if (static::isGuestCart($cart) || static::isCartEmpty($cart)) {
|
||||
if (static::isGuestCart($cart)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
@ -138,38 +138,40 @@ class RetailcrmCartUploader
|
|||
}
|
||||
static::populateContextWithCart($cart);
|
||||
|
||||
// RetailcrmLogger::writeDebug(__METHOD__, 'Test');
|
||||
// RetailcrmLogger::writeDebug(__METHOD__, $cart->getProducts());
|
||||
|
||||
$response = static::$api->cartGet($cartExternalId, static::$site);
|
||||
$response = static::$api->cartGet($cart->id_customer, static::$site);
|
||||
|
||||
if ($response instanceof RetailcrmApiResponse) {
|
||||
if (empty($response['cart'])) {
|
||||
// TODO
|
||||
// Extract address from cart (if exists) and append to customer?
|
||||
// Or maybe this customer will not order anything, so we don't need it's address...
|
||||
static::$api->customersCreate(RetailcrmOrderBuilder::buildCrmCustomer(new Customer($cart->id_customer)));
|
||||
$isExistExternalId = !empty($response['cart']['externalId']);
|
||||
|
||||
$crmCart = static::buildCrmCart($cart, $cartExternalId);
|
||||
|
||||
if (empty($crmCart)) {
|
||||
continue;
|
||||
if (static::isCartEmpty($cart)) {
|
||||
if ($isExistExternalId) {
|
||||
static::$api->cartClear(
|
||||
[
|
||||
'clearedAt' => date('Y-m-d H:i:s'),
|
||||
'customer' => ['externalId' => $cart->id_customer],
|
||||
],
|
||||
static::$site
|
||||
);
|
||||
}
|
||||
|
||||
if (false !== static::$api->cartSet($crmCart, static::$site)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$crmCart = static::buildCrmCart($cart, $cartExternalId, $isExistExternalId);
|
||||
|
||||
if (empty($crmCart)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$response = static::$api->cartSet($crmCart, static::$site);
|
||||
|
||||
if ($response instanceof RetailcrmApiResponse && $response->isSuccessful()) {
|
||||
if ($isExistExternalId) {
|
||||
static::registerAbandonedCartSync($cart->id);
|
||||
} else {
|
||||
$cart->date_upd = date('Y-m-d H:i:s');
|
||||
$cart->save();
|
||||
}
|
||||
} elseif (!empty($response['cart']['externalId'])) {
|
||||
$crmCart = static::buildCrmCart($cart, $response['cart']['externalId']);
|
||||
|
||||
if (empty($crmCart)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (false !== static::$api->ordersEdit($crmCart)) {
|
||||
static::registerAbandonedCartSync($cart->id);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -241,22 +243,26 @@ class RetailcrmCartUploader
|
|||
/**
|
||||
* Build order for abandoned cart
|
||||
*
|
||||
* @param Cart|\CartCore $cart
|
||||
* @param Cart|CartCore $cart
|
||||
* @param string $cartExternalId
|
||||
* @param bool $isExistExternalId
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private static function buildCrmCart($cart, $cartExternalId)
|
||||
private static function buildCrmCart($cart, string $cartExternalId, bool $isExistExternalId)
|
||||
{
|
||||
try {
|
||||
$crmCart = [
|
||||
'externalId' => $cartExternalId,
|
||||
'customer' => ['externalId' => $cart->id_customer],
|
||||
'clearAt' => null,
|
||||
'createdAt' => $cart->date_add,
|
||||
'droppedAt' => date('Y-m-d H:i:s'),
|
||||
];
|
||||
|
||||
if (!$isExistExternalId) {
|
||||
$crmCart['externalId'] = $cartExternalId . uniqid('_', true);
|
||||
}
|
||||
|
||||
if (!empty($cart->date_upd)) {
|
||||
$crmCart['updatedAt'] = $cart->date_upd;
|
||||
}
|
||||
|
|
|
@ -73,4 +73,17 @@ class RetailcrmTestHelper
|
|||
'SELECT MAX(id_customer) FROM `' . _DB_PREFIX_ . 'customer`'
|
||||
);
|
||||
}
|
||||
|
||||
public static function getAbandonedCartLastSync($cartId)
|
||||
{
|
||||
$sql = 'SELECT `last_uploaded` FROM `' . _DB_PREFIX_ . 'retailcrm_abandonedcarts`
|
||||
WHERE `id_cart` = \'' . pSQL((int) $cartId) . '\'';
|
||||
$when = Db::getInstance()->getValue($sql);
|
||||
|
||||
if (empty($when)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return DateTimeImmutable::createFromFormat('Y-m-d H:i:s', $when);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -60,16 +60,17 @@ class RetailcrmCartUploaderTest extends RetailcrmTestCase
|
|||
|
||||
$this->cart = new Cart();
|
||||
$this->cart->id_lang = (int) Configuration::get('PS_LANG_DEFAULT');
|
||||
$this->cart->date_add = self::DEFAULT_UPD_CART_TIME;
|
||||
$this->cart->date_upd = date('Y-m-d H:i:s');
|
||||
$this->cart->id_customer = 1;
|
||||
$this->cart->id_currency = 1;
|
||||
|
||||
$this->cart->save();
|
||||
$this->cart->updateQty(1, $this->product['id']);
|
||||
}
|
||||
|
||||
public function testCreateCart()
|
||||
{
|
||||
$this->cart->updateQty(1, $this->product['id']);
|
||||
|
||||
$this->apiClientMock->expects($this->once())
|
||||
->method('cartGet')
|
||||
->willReturn(new RetailcrmApiResponse('200', json_encode(['cart' => []])))
|
||||
|
@ -84,13 +85,25 @@ class RetailcrmCartUploaderTest extends RetailcrmTestCase
|
|||
RetailcrmCartUploader::run();
|
||||
|
||||
$this->assertNotEquals(self::DEFAULT_UPD_CART_TIME, $this->cart->date_upd);
|
||||
$this->assertEquals(RetailcrmTestHelper::getAbandonedCartLastSync($this->cart->id), null);
|
||||
}
|
||||
|
||||
public function testUpdateCart()
|
||||
{
|
||||
$this->cart->updateQty(2, $this->product['id']);
|
||||
|
||||
$this->apiClientMock->expects($this->any())
|
||||
->method('cartGet')
|
||||
->willReturn(new RetailcrmApiResponse('200', json_encode(['cart' => ['externalId' => $this->cart->id]])))
|
||||
->willReturn(
|
||||
new RetailcrmApiResponse(
|
||||
'200',
|
||||
json_encode(
|
||||
[
|
||||
'cart' => ['externalId' => $this->cart->id_customer],
|
||||
]
|
||||
)
|
||||
)
|
||||
)
|
||||
;
|
||||
|
||||
$this->apiClientMock->expects($this->any())
|
||||
|
@ -98,27 +111,40 @@ class RetailcrmCartUploaderTest extends RetailcrmTestCase
|
|||
->willReturn(new RetailcrmApiResponse('200', json_encode(['success' => true])))
|
||||
;
|
||||
|
||||
$this->cart->updateQty(2, $this->product['id']);
|
||||
|
||||
RetailcrmCartUploader::$api = $this->apiMock;
|
||||
RetailcrmCartUploader::run();
|
||||
|
||||
$this->assertNotEquals(self::DEFAULT_UPD_CART_TIME, $this->cart->date_upd);
|
||||
$this->assertNotEquals($this->getAbandonedCartLastSync($this->cart->id), null);
|
||||
$this->assertNotEquals(RetailcrmTestHelper::getAbandonedCartLastSync($this->cart->id), null);
|
||||
}
|
||||
|
||||
private function getAbandonedCartLastSync($cartId)
|
||||
public function testClearCart()
|
||||
{
|
||||
$sql = 'SELECT `last_uploaded` FROM `' . _DB_PREFIX_ . 'retailcrm_abandonedcarts`
|
||||
WHERE `id_cart` = \'' . pSQL((int) $cartId) . '\'';
|
||||
$when = Db::getInstance()->getValue($sql);
|
||||
$this->apiClientMock->expects($this->once())
|
||||
->method('cartGet')
|
||||
->willReturn(
|
||||
new RetailcrmApiResponse(
|
||||
'200',
|
||||
json_encode(
|
||||
[
|
||||
'cart' => ['externalId' => $this->cart->id_customer],
|
||||
]
|
||||
)
|
||||
)
|
||||
)
|
||||
;
|
||||
|
||||
if (empty($when)) {
|
||||
return null;
|
||||
}
|
||||
$this->apiClientMock->expects($this->once())
|
||||
->method('cartClear')
|
||||
->willReturn(new RetailcrmApiResponse('200', json_encode(['success' => true])))
|
||||
;
|
||||
|
||||
return DateTimeImmutable::createFromFormat('Y-m-d H:i:s', $when);
|
||||
$cartLastUpdate = $this->cart->date_upd;
|
||||
|
||||
RetailcrmCartUploader::$api = $this->apiMock;
|
||||
RetailcrmCartUploader::run();
|
||||
|
||||
$this->assertEquals($cartLastUpdate, $this->cart->date_upd);
|
||||
$this->assertEquals(RetailcrmTestHelper::getAbandonedCartLastSync($this->cart->id), null);
|
||||
}
|
||||
|
||||
// TODO: add method for work cart.
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue