From 086dea028501f631a42d045c305a8bd233ff4dfa Mon Sep 17 00:00:00 2001 From: Ivan Chaplygin Date: Tue, 28 Feb 2023 10:28:26 +0300 Subject: [PATCH] Corrections by review --- .../api/class-wc-retailcrm-client-v5.php | 32 +++--- tests/datasets/data-cart-retailcrm.php | 102 ++++++++++++++++++ ...ient-v5.php => test-wc-retailcrm-cart.php} | 51 +++++---- 3 files changed, 146 insertions(+), 39 deletions(-) create mode 100644 tests/datasets/data-cart-retailcrm.php rename tests/{api/test-wc-retailcrm-client-v5.php => test-wc-retailcrm-cart.php} (60%) diff --git a/src/include/api/class-wc-retailcrm-client-v5.php b/src/include/api/class-wc-retailcrm-client-v5.php index 62ce54b..fb4e897 100644 --- a/src/include/api/class-wc-retailcrm-client-v5.php +++ b/src/include/api/class-wc-retailcrm-client-v5.php @@ -71,9 +71,11 @@ class WC_Retailcrm_Client_V5 } /** - * Clearing current customer bucket - * @param array $cart + * Get cart by id or externalId + * + * @param $customerId * @param string $site + * @param $by (default: 'externalId') * * @return WC_Retailcrm_Response * @@ -81,8 +83,10 @@ class WC_Retailcrm_Client_V5 * @throws WC_Retailcrm_Exception_Curl * @throws WC_Retailcrm_Exception_Json */ - public function cartClear(array $cart, string $site) + public function cartGet($customerId, string $site, $by = 'externalId') { + $this->checkIdParameter($by); + if (empty($site)) { throw new InvalidArgumentException( 'Site must be set' @@ -90,9 +94,9 @@ class WC_Retailcrm_Client_V5 } return $this->client->makeRequest( - sprintf('/customer-interaction/%s/cart/clear', $site), - WC_Retailcrm_Request::METHOD_POST, - ['cart' => json_encode($cart)] + sprintf('/customer-interaction/%s/cart/%s', $site, $customerId), + WC_Retailcrm_Request::METHOD_GET, + ['by' => $by] ); } @@ -124,11 +128,10 @@ class WC_Retailcrm_Client_V5 } /** - * Get cart by id or externalId + * Clear customer cart * - * @param $customerId + * @param array $cart * @param string $site - * @param $by (default: 'externalId') * * @return WC_Retailcrm_Response * @@ -136,10 +139,8 @@ class WC_Retailcrm_Client_V5 * @throws WC_Retailcrm_Exception_Curl * @throws WC_Retailcrm_Exception_Json */ - public function cartGet($customerId, string $site, $by = 'externalId') + public function cartClear(array $cart, string $site) { - $this->checkIdParameter($by); - if (empty($site)) { throw new InvalidArgumentException( 'Site must be set' @@ -147,13 +148,12 @@ class WC_Retailcrm_Client_V5 } return $this->client->makeRequest( - sprintf('/customer-interaction/%s/cart/%s', $site, $customerId), - WC_Retailcrm_Request::METHOD_GET, - ['by' => $by] + 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..c972c99 --- /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', + ], + ], + ]; + } + +} \ No newline at end of file diff --git a/tests/api/test-wc-retailcrm-client-v5.php b/tests/test-wc-retailcrm-cart.php similarity index 60% rename from tests/api/test-wc-retailcrm-client-v5.php rename to tests/test-wc-retailcrm-cart.php index 8cb854e..74b4d2a 100644 --- a/tests/api/test-wc-retailcrm-client-v5.php +++ b/tests/test-wc-retailcrm-cart.php @@ -1,19 +1,19 @@ + * @author RetailCRM * @license http://retailcrm.ru Proprietary * @link http://retailcrm.ru * @see http://help.retailcrm.ru */ -class Test_WC_Retailcrm_client_V5 extends WC_Retailcrm_Test_Case_Helper +class WC_Retailcrm_Cart_Test extends WC_Retailcrm_Test_Case_Helper { protected $apiClientMock; protected $responseMock; @@ -22,46 +22,51 @@ class Test_WC_Retailcrm_client_V5 extends WC_Retailcrm_Test_Case_Helper { $this->responseMock = $this->getMockBuilder('\WC_Retailcrm_Response_Helper') ->disableOriginalConstructor() - ->setMethods([ - 'isSuccessful', - 'offsetExists' - ]) + ->setMethods( + [ + 'isSuccessful', + 'offsetExists', + ] + ) ->getMock(); $this->responseMock->setResponse(['id' => 1]); $this->apiClientMock = $this->getMockBuilder('\WC_Retailcrm_Client_V5') ->disableOriginalConstructor() - ->setMethods([ - 'cartGet', - 'cartSet', - 'cartClear' - ]) + ->setMethods( + [ + 'cartGet', + 'cartSet', + 'cartClear', + ] + ) ->getMock(); $this->setMockResponse($this->responseMock, 'isSuccessful', true); $this->setMockResponse($this->responseMock, 'offsetExists', true); - $this->setMockResponse($this->apiClientMock, 'cartGet',$this->responseMock); $this->setMockResponse($this->apiClientMock, 'cartSet', $this->responseMock); $this->setMockResponse($this->apiClientMock, 'cartClear', $this->responseMock); - } - - public function testSetCart() - { - $response = $this->apiClientMock->cartSet(array(), 'test-site'); - $this->assertEquals(1,$response->__get('id')); + $this->setMockResponse($this->apiClientMock, 'cartGet',$this->responseMock); } public function testGetCart() { + $this->responseMock->setResponse(DataCartRetailCrm::dataGetCart()); $response = $this->apiClientMock->cartGet(1, 'test-site'); - $this->assertEquals(1, $response->__get('id')); + $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(array(), 'test-site'); + $response = $this->apiClientMock->cartClear(DataCartRetailCrm::dataClearCart(), 'test-site'); $this->assertEquals(1, $response->__get('id')); } -} \ No newline at end of file +}