diff --git a/src/include/api/class-wc-retailcrm-client-v5.php b/src/include/api/class-wc-retailcrm-client-v5.php index 35ec6a4..fb4e897 100644 --- a/src/include/api/class-wc-retailcrm-client-v5.php +++ b/src/include/api/class-wc-retailcrm-client-v5.php @@ -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 * diff --git a/tests/datasets/data-cart-retailcrm.php b/tests/datasets/data-cart-retailcrm.php new file mode 100644 index 0000000..21470e9 --- /dev/null +++ b/tests/datasets/data-cart-retailcrm.php @@ -0,0 +1,102 @@ + + * @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', + ], + ], + ]; + } + +} diff --git a/tests/test-wc-retailcrm-cart.php b/tests/test-wc-retailcrm-cart.php new file mode 100644 index 0000000..3648436 --- /dev/null +++ b/tests/test-wc-retailcrm-cart.php @@ -0,0 +1,72 @@ + + * @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')); + } +}