1
0
mirror of synced 2024-11-24 22:36:06 +03:00

Merge pull request #6 from gwinn/master

Методы `/orders/packs/history`, `/stores/inventories`, `/stores/inventories/upload`.
This commit is contained in:
Ilyas Salikhov 2015-05-20 11:41:17 +03:00
commit 2098ad3bf2
7 changed files with 245 additions and 17 deletions

View File

@ -222,6 +222,31 @@ class ApiClient
));
}
/**
* 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
*
@ -263,11 +288,14 @@ class ApiClient
return $this->client->makeRequest(
"/customers/" . $customer[$by] . "/edit",
Client::METHOD_POST,
$this->fillSite($site, array(
$this->fillSite(
$site,
array(
'customer' => json_encode($customer),
'by' => $by,
'by' => $by
)
));
)
);
}
/**
@ -347,6 +375,52 @@ class ApiClient
));
}
/**
* Get purchace prices & stock balance
*
* @param array $filter (default: array())
* @param int $page (default: null)
* @param int $limit (default: null)
* @param string $site (default: null)
* @return ApiResponse
*/
public function storeInventories(array $filter = array(), $page = null, $limit = null, $site = 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, $this->fillSite($site, $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

@ -15,6 +15,7 @@ class Client
protected $url;
protected $defaultParameters;
protected $retry;
public function __construct($url, array $defaultParameters = array())
{
@ -24,6 +25,7 @@ class Client
$this->url = $url;
$this->defaultParameters = $defaultParameters;
$this->retry = 0;
}
/**
@ -33,10 +35,18 @@ class Client
* @param string $method (default: 'GET')
* @param array $parameters (default: array())
* @param int $timeout
* @param bool $verify
* @param bool $debug
* @return ApiResponse
*/
public function makeRequest($path, $method, array $parameters = array(), $timeout = 30)
{
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)) {
throw new \InvalidArgumentException(sprintf(
@ -49,17 +59,20 @@ class Client
$parameters = array_merge($this->defaultParameters, $parameters);
$path = $this->url . $path;
if (self::METHOD_GET === $method && sizeof($parameters)) {
$path .= '?' . http_build_query($parameters);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $path);
curl_setopt($ch, CURLOPT_TIMEOUT, (int) $timeout);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, (int) $timeout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_FAILONERROR, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // return into a variable
curl_setopt($ch, CURLOPT_TIMEOUT, (int) $timeout); // times out after 30s
// curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // allow redirects
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $verify);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $verify);
if (self::METHOD_POST === $method) {
curl_setopt($ch, CURLOPT_POST, true);
@ -68,9 +81,9 @@ class Client
$responseBody = curl_exec($ch);
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$errno = curl_errno($ch);
$error = curl_error($ch);
curl_close($ch);
if ($errno) {

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

@ -200,6 +200,27 @@ class ApiClientOrdersTest extends TestCase
);
}
/**
* @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

@ -9,6 +9,7 @@ class ApiClientReferenceTest extends TestCase
/**
* @group integration
* @dataProvider getListDictionaries
* @param $name
*/
public function testList($name)
{

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://a.intarocrm.ru', array('apiKey' => '123'));
$client = new Client('http://demo.retailcrm.ru/api/' . ApiClient::VERSION, array('apiKey' => '123'));
}
/**
@ -33,13 +33,13 @@ class ClientTest extends TestCase
*/
public function testMakeRequestWrongMethod()
{
$client = new Client('https://asdf.df', array());
$client = static::getClient();
$client->makeRequest('/a', 'adsf');
}
/**
* @group integration
* @expectedException RetailCrm\Exception\CurlException
* @expectedException \RetailCrm\Exception\CurlException
*/
public function testMakeRequestWrongUrl()
{
@ -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);
}
}