1
0
mirror of synced 2024-11-21 21:06:07 +03:00

packs & inventories, improve curl connection timeout handler

This commit is contained in:
Alex Lushpai 2015-05-04 18:47:14 +03:00
parent 03466cf0ad
commit c7e7ed92cf
6 changed files with 223 additions and 9 deletions

View File

@ -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
*

View File

@ -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;

View File

@ -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)
);
}
}
/**
* 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'])
);
}
}

View File

@ -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

View File

@ -0,0 +1,92 @@
<?php
namespace RetailCrm\Tests;
use RetailCrm\Test\TestCase;
class ApiClientStoreTest extends TestCase
{
/**
* @group integration
*/
public function testStoreInventories()
{
$client = static::getApiClient();
$response = $client->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']);
}
}

View File

@ -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);
}
}