diff --git a/lib/RetailCrm/ApiClient.php b/lib/RetailCrm/ApiClient.php index e77740a..8f63fc9 100644 --- a/lib/RetailCrm/ApiClient.php +++ b/lib/RetailCrm/ApiClient.php @@ -221,6 +221,31 @@ class ApiClient 'orders' => json_encode($ids), )); } + + /** + * Get orders assembly history + * + * @param array $filter (default: array()) + * @param int $page (default: null) + * @param int $limit (default: null) + * @return ApiResponse + */ + public function ordersPacksHistory(array $filter = array(), $page = null, $limit = null) + { + $parameters = array(); + + if (sizeof($filter)) { + $parameters['filter'] = $filter; + } + if (null !== $page) { + $parameters['page'] = (int) $page; + } + if (null !== $limit) { + $parameters['limit'] = (int) $limit; + } + + return $this->client->makeRequest('/orders/packs/history', Client::METHOD_GET, $parameters); + } /** * Create a customer @@ -347,6 +372,49 @@ class ApiClient )); } + /** + * Get purchace prices & stock balance + * + * @param array $filter (default: array()) + * @param int $page (default: null) + * @param int $limit (default: null) + * @return ApiResponse + */ + public function storeInventories(array $filter = array(), $page = null, $limit = null) + { + $parameters = array(); + + if (sizeof($filter)) { + $parameters['filter'] = $filter; + } + if (null !== $page) { + $parameters['page'] = (int) $page; + } + if (null !== $limit) { + $parameters['limit'] = (int) $limit; + } + + return $this->client->makeRequest('/store/inventories', Client::METHOD_GET, $parameters); + } + + /** + * Upload store inventories + * + * @param array $offers + * @param string $site (default: null) + * @return ApiResponse + */ + public function storeInventoriesUpload(array $offers, $site = null) + { + if (!sizeof($offers)) { + throw new \InvalidArgumentException('Parameter `offers` must contains array of the customers'); + } + + return $this->client->makeRequest("/store/inventories/upload", Client::METHOD_POST, $this->fillSite($site, array( + 'offers' => json_encode($offers), + ))); + } + /** * Returns deliveryServices list * diff --git a/lib/RetailCrm/Http/Client.php b/lib/RetailCrm/Http/Client.php index da0c074..b57e170 100644 --- a/lib/RetailCrm/Http/Client.php +++ b/lib/RetailCrm/Http/Client.php @@ -38,7 +38,7 @@ class Client * @param bool $verify * @return ApiResponse */ - public function makeRequest($path, $method, array $parameters = array(), $timeout = 30, $verify=false) + public function makeRequest($path, $method, array $parameters = array(), $timeout = 30, $verify = false, $debug = false) { $allowedMethods = array(self::METHOD_GET, self::METHOD_POST); if (!in_array($method, $allowedMethods)) { @@ -71,7 +71,7 @@ class Client curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters); } - $responseBody = $this->curlExec($ch); + $responseBody = $this->curlExec($ch, $debug); $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $errno = curl_errno($ch); $error = curl_error($ch); @@ -87,14 +87,20 @@ class Client /** * @param resource $ch + * @param boolean $debug * @return mixed */ - private function curlExec($ch) { + private function curlExec($ch, $debug) { $exec = curl_exec($ch); if (curl_errno($ch) && in_array(curl_errno($ch), array(6, 7, 28, 34, 35)) && $this->retry < 3) { $this->retry += 1; - $this->curlExec($ch); + + if ($debug) { + error_log('CURL RETRY #' . $this->retry . PHP_EOL, 4); + } + + $this->curlExec($ch, $debug); } return $exec; diff --git a/tests/RetailCrm/Test/TestCase.php b/tests/RetailCrm/Test/TestCase.php index cb58cce..5eafabb 100644 --- a/tests/RetailCrm/Test/TestCase.php +++ b/tests/RetailCrm/Test/TestCase.php @@ -3,6 +3,7 @@ namespace RetailCrm\Test; use RetailCrm\ApiClient; +use RetailCrm\Http\Client; class TestCase extends \PHPUnit_Framework_TestCase { @@ -22,4 +23,20 @@ class TestCase extends \PHPUnit_Framework_TestCase $site ?: (isset($_SERVER['CRM_SITE']) ? $_SERVER['CRM_SITE'] : null) ); } -} \ No newline at end of file + + /** + * Return Client object + * + * @param string $url (default: null) + * @param string $apiKey (default: null) + * @return Client + */ + public static function getClient($url = null, $defaultParameters = array()) + { + return new Client( + $url ?: $_SERVER['CRM_URL'] . '/api/' . ApiClient::VERSION, + array('apiKey' => isset($defaultParameters['apiKey']) ? $defaultParameters['apiKey'] : $_SERVER['CRM_API_KEY']) + ); + } +} + diff --git a/tests/RetailCrm/Tests/ApiClientOrdersTest.php b/tests/RetailCrm/Tests/ApiClientOrdersTest.php index 8d1254d..1c1fc32 100644 --- a/tests/RetailCrm/Tests/ApiClientOrdersTest.php +++ b/tests/RetailCrm/Tests/ApiClientOrdersTest.php @@ -199,6 +199,27 @@ class ApiClientOrdersTest extends TestCase 'API returns generatedAt in orders history' ); } + + /** + * @group integration + */ + public function testOrdersPacksHistory() + { + $client = static::getApiClient(); + + $response = $client->ordersPacksHistory(); + $this->assertInstanceOf('RetailCrm\Response\ApiResponse', $response); + $this->assertEquals(200, $response->getStatusCode()); + $this->assertTrue($response->success); + $this->assertTrue( + isset($response['history']), + 'API returns orders assembly history' + ); + $this->assertTrue( + isset($response['generatedAt']), + 'API returns generatedAt in orders assembly history' + ); + } /** * @group integration diff --git a/tests/RetailCrm/Tests/ApiClientStoreTest.php b/tests/RetailCrm/Tests/ApiClientStoreTest.php new file mode 100644 index 0000000..de354b0 --- /dev/null +++ b/tests/RetailCrm/Tests/ApiClientStoreTest.php @@ -0,0 +1,92 @@ +storeInventories(); + $this->assertInstanceOf('RetailCrm\Response\ApiResponse', $response); + $this->assertEquals(200, $response->getStatusCode()); + $this->assertTrue($response->success); + $this->assertTrue( + isset($response['offers']), + 'API returns orders assembly history' + ); + } + + /** + * @group unit + * @expectedException \InvalidArgumentException + */ + public function testStoreInventoriesUploadExceptionEmpty() + { + $client = static::getApiClient(); + + $response = $client->storeInventoriesUpload(array()); + } + + /** + * @group integration + */ + public function testStoreInventoriesUpload() + { + $client = static::getApiClient(); + + $externalIdA = 'upload-a-' . time(); + $externalIdB = 'upload-b-' . time(); + + $response = $client->storeInventoriesUpload(array( + array( + 'externalId' => $externalIdA, + 'available' => 10, + 'purchasePrice' => 1700 + ), + array( + 'externalId' => $externalIdB, + 'available' => 20, + 'purchasePrice' => 1500 + ), + )); + $this->assertInstanceOf('RetailCrm\Response\ApiResponse', $response); + $this->assertTrue( + $response->isSuccessful(), + 'Got offer' + ); + } + + /** + * @group integration + */ + public function testStoreInventoriesUploadFailed() + { + $client = static::getApiClient(); + + $externalIdA = 'upload-a-' . time(); + $externalIdB = 'upload-b-' . time(); + + $response = $client->storeInventoriesUpload(array( + array( + 'externalId' => $externalIdA, + 'available' => 10, + 'purchasePrice' => 1700 + ), + array( + 'externalId' => $externalIdB, + 'available' => 20, + 'purchasePrice' => 1500 + ), + )); + $this->assertInstanceOf('RetailCrm\Response\ApiResponse', $response); + $this->assertEquals(400, $response->getStatusCode()); + $this->assertTrue(isset($response['errorMsg']), $response['errorMsg']); + } +} diff --git a/tests/RetailCrm/Tests/Http/ClientTest.php b/tests/RetailCrm/Tests/Http/ClientTest.php index 62a1150..84eecf2 100644 --- a/tests/RetailCrm/Tests/Http/ClientTest.php +++ b/tests/RetailCrm/Tests/Http/ClientTest.php @@ -24,7 +24,7 @@ class ClientTest extends TestCase */ public function testHttpRequiring() { - $client = new Client('http://demo.intarocrm.ru/api/' . ApiClient::VERSION, array('apiKey' => '123')); + $client = new Client('http://demo.retailcrm.ru/api/' . ApiClient::VERSION, array('apiKey' => '123')); } /** @@ -33,7 +33,7 @@ class ClientTest extends TestCase */ public function testMakeRequestWrongMethod() { - $client = new Client('https://demo.intarocrm.ru/api/' . ApiClient::VERSION, array('apiKey' => '123')); + $client = static::getClient(); $client->makeRequest('/a', 'adsf'); } @@ -52,10 +52,20 @@ class ClientTest extends TestCase */ public function testMakeRequestSuccess() { - $client = new Client('https://demo.intarocrm.ru/api/' . ApiClient::VERSION, array()); + $client = static::getClient(); $response = $client->makeRequest('/orders', Client::METHOD_GET); $this->assertInstanceOf('RetailCrm\Response\ApiResponse', $response); - $this->assertEquals(403, $response->getStatusCode()); + $this->assertEquals(200, $response->getStatusCode()); + } + + /** + * @group integration + * @expectedException \RetailCrm\Exception\CurlException + */ + public function testMakeRequestTimeout() + { + $client = static::getClient(); + $client->makeRequest('/orders', Client::METHOD_GET, array(), 1, false, true); } }