From 026f8171bccad4955d535b29d5cb74121b7cda67 Mon Sep 17 00:00:00 2001 From: uryvskiy-dima Date: Thu, 12 Jan 2023 02:12:03 +0300 Subject: [PATCH] Add tests for cart --- retailcrm/lib/RetailcrmCartUploader.php | 13 ++- retailcrm/lib/api/RetailcrmApiClientV5.php | 6 +- tests/lib/RetailcrmCartUploaderTest.php | 124 +++++++++++++++++++++ 3 files changed, 134 insertions(+), 9 deletions(-) create mode 100644 tests/lib/RetailcrmCartUploaderTest.php diff --git a/retailcrm/lib/RetailcrmCartUploader.php b/retailcrm/lib/RetailcrmCartUploader.php index f1ba4ce..941739b 100644 --- a/retailcrm/lib/RetailcrmCartUploader.php +++ b/retailcrm/lib/RetailcrmCartUploader.php @@ -136,13 +136,12 @@ class RetailcrmCartUploader )) { continue; } - static::populateContextWithCart($cart); - RetailcrmLogger::writeDebug(__METHOD__, 'Test'); - RetailcrmLogger::writeDebug(__METHOD__, $cart->getProducts()); +// RetailcrmLogger::writeDebug(__METHOD__, 'Test'); +// RetailcrmLogger::writeDebug(__METHOD__, $cart->getProducts()); - $response = static::$api->cartsGet($cartExternalId, static::$site); + $response = static::$api->cartGet($cartExternalId, static::$site); if ($response instanceof RetailcrmApiResponse) { if (empty($response['cart'])) { @@ -157,7 +156,7 @@ class RetailcrmCartUploader continue; } - if (false !== static::$api->cartsSet($crmCart, static::$site)) { + if (false !== static::$api->cartSet($crmCart, static::$site)) { $cart->date_upd = date('Y-m-d H:i:s'); $cart->save(); } @@ -275,8 +274,10 @@ class RetailcrmCartUploader : $product['id_product'], ], 'quantity' => $product['cart_quantity'], - 'price' => $product['price'], 'createdAt' => $product['date_add'], + 'price' => !empty($product['rate']) + ? round($product['price'], 2) + (round($product['price'], 2) * $product['rate'] / 100) + : round($product['price'], 2), ]; } } catch (Exception $exception) { diff --git a/retailcrm/lib/api/RetailcrmApiClientV5.php b/retailcrm/lib/api/RetailcrmApiClientV5.php index cc90abf..39f74f3 100644 --- a/retailcrm/lib/api/RetailcrmApiClientV5.php +++ b/retailcrm/lib/api/RetailcrmApiClientV5.php @@ -1220,7 +1220,7 @@ class RetailcrmApiClientV5 * @throws \RetailCrm\Exception\CurlException * @throws \RetailCrm\Exception\InvalidJsonException */ - public function cartsGet($customerId, $site, $by = 'externalId') + public function cartGet($customerId, $site, $by = 'externalId') { $this->checkIdParameter($by); @@ -1249,7 +1249,7 @@ class RetailcrmApiClientV5 * @throws \RetailCrm\Exception\CurlException * @throws \RetailCrm\Exception\InvalidJsonException */ - public function cartsSet(array $cart, string $site) + public function cartSet(array $cart, string $site) { if (empty($site)) { throw new \InvalidArgumentException( @@ -1276,7 +1276,7 @@ class RetailcrmApiClientV5 * @throws \RetailCrm\Exception\CurlException * @throws \RetailCrm\Exception\InvalidJsonException */ - public function cartsClear(array $cart, string $site) + public function cartClear(array $cart, string $site) { if (empty($site)) { throw new \InvalidArgumentException( diff --git a/tests/lib/RetailcrmCartUploaderTest.php b/tests/lib/RetailcrmCartUploaderTest.php new file mode 100644 index 0000000..28563a9 --- /dev/null +++ b/tests/lib/RetailcrmCartUploaderTest.php @@ -0,0 +1,124 @@ + + * @copyright 2021 DIGITAL RETAIL TECHNOLOGIES SL + * @license https://opensource.org/licenses/MIT The MIT License + * + * Don't forget to prefix your containers with your own identifier + * to avoid any conflicts with others containers. + */ + +class RetailcrmCartUploaderTest extends RetailcrmTestCase +{ + const DEFAULT_UPD_CART_TIME = '2023-01-01 12:00:00'; + + private $cart; + private $apiMock; + private $product; + + protected function setUp() + { + parent::setUp(); + + $this->apiMock = $this->getApiMock(['cartGet', 'cartSet', 'cartClear']); + + $catalog = new RetailcrmCatalog(); + $data = $catalog->getData(); + $this->product = $data[1]->current(); + + RetailcrmCartUploader::init(); + RetailcrmCartUploader::$site = 'test'; + RetailcrmCartUploader::setSyncDelay(Configuration::get(RetailCRM::SYNC_CARTS_DELAY)); + + $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->id_customer = 1; + $this->cart->id_currency = 1; + + $this->cart->save(); + $this->cart->updateQty(1, $this->product['id']); + } + + public function testCreateCart() + { + $this->apiClientMock->expects($this->once()) + ->method('cartGet') + ->willReturn(new RetailcrmApiResponse('200', json_encode(['cart' => []]))) + ; + + $this->apiClientMock->expects($this->once()) + ->method('cartSet') + ->willReturn(new RetailcrmApiResponse('200', json_encode(['success' => true]))) + ; + + RetailcrmCartUploader::$api = $this->apiMock; + RetailcrmCartUploader::run(); + + $this->assertNotEquals(self::DEFAULT_UPD_CART_TIME, $this->cart->date_upd); + } + + public function testUpdateCart() + { + $this->apiClientMock->expects($this->any()) + ->method('cartGet') + ->willReturn(new RetailcrmApiResponse('200', json_encode(['cart' => ['externalId' => $this->cart->id]]))) + ; + + $this->apiClientMock->expects($this->any()) + ->method('cartSet') + ->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); + } + + private 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); + } + + // TODO: add method for work cart. +}