diff --git a/retailcrm/job/inventories.php b/retailcrm/job/inventories.php new file mode 100644 index 0000000..05c8b74 --- /dev/null +++ b/retailcrm/job/inventories.php @@ -0,0 +1,29 @@ +storeInventories(array(), $page, 250); + + if (!$result->isSuccessful()) { + return null; + } + + $totalPageCount = $result['pagination']['totalPageCount']; + $page++; + foreach ($result['offers'] as $offer) { + if (isset($offer['externalId'])) { + $invOffer = explode('#', $offer['externalId']); + + if (isset($invOffer[1])) { + StockAvailable::setQuantity($invOffer[0], $invOffer[1], $offer['quantity']); + } else { + StockAvailable::setQuantity($offer['externalId'], 0, $offer['quantity']); + } + } + } + } while ($page <= $totalPageCount); + + return $success; + } + + /** + * Update stock quantity in WooCommerce + * + * @return mixed + */ + public function updateQuantity() { + + return $this->load_stocks(); + + } + +} diff --git a/tests/phpunit/RetailcrmInventoriesTest.php b/tests/phpunit/RetailcrmInventoriesTest.php new file mode 100644 index 0000000..0d7b194 --- /dev/null +++ b/tests/phpunit/RetailcrmInventoriesTest.php @@ -0,0 +1,112 @@ +apiMock = $this->getMockBuilder('RetailcrmProxy') + ->disableOriginalConstructor() + ->setMethods( + array( + 'storeInventories' + ) + ) + ->getMock(); + $catalog = new RetailcrmCatalog(); + $data = $catalog->getData(); + $this->product = $data[1][0]; + + $this->setConfig(); + } + + private function getResponseData() + { + return array( + 'true' => array( + 'success' => true, + 'pagination' => array( + 'limit' => 250, + 'totalCount' => 1, + 'currentPage' => 1, + 'totalPageCount' => 1 + ), + 'offers' => array( + array( + 'id' => 1, + 'xmlId' => 'xmlId', + 'quantity' => 10 + ) + ) + ), + 'false' => array( + 'success' => false, + 'errorMsg' => 'Forbidden' + ) + ); + } + /** + * @param $retailcrm + * @param $response + * + * @dataProvider dataProviderLoadStocks + */ + public function test_load_stocks($apiVersion, $response) + { + if ($response['success'] == true) { + $response['offers'][0]['externalId'] = $this->product['id']; + $this->responseMock->expects($this->any()) + ->method('isSuccessful') + ->willReturn(true); + } elseif ($response['success'] == false) { + $this->responseMock->expects($this->any()) + ->method('isSuccessful') + ->willReturn(false); + } + + $this->responseMock->setResponse($response); + + if ($retailcrm) { + $retailcrm->expects($this->any()) + ->method('storeInventories') + ->willReturn($this->responseMock); + } + + RetailcrmInventories::$apiVersion = $apiVersion; + RetailcrmInventories::$api = $this->apiMock; + + RetailcrmInventories::load_stocks(); + + } + + public function dataProviderLoadStocks() + { + $this->setUp(); + + $response = $this->getResponseData(); + + return array( + array( + + 'response' => $response['true'], + 'api_version' => 4 + ), + array( + 'api_version' => 5, + 'response' => $response['true'] + ), + array( + 'api_version' => 4, + 'response' => $response['false'] + ), + array( + 'api_version' => 5, + 'response' => $response['false'] + ) + ); + } +} \ No newline at end of file