diff --git a/lib/RetailCrm/ApiClient.php b/lib/RetailCrm/ApiClient.php index e77740a..593c63f 100644 --- a/lib/RetailCrm/ApiClient.php +++ b/lib/RetailCrm/ApiClient.php @@ -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( - 'customer' => json_encode($customer), - 'by' => $by, + $this->fillSite( + $site, + array( + 'customer' => json_encode($customer), + '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 * diff --git a/lib/RetailCrm/Http/Client.php b/lib/RetailCrm/Http/Client.php index 5dbc826..a3374dc 100644 --- a/lib/RetailCrm/Http/Client.php +++ b/lib/RetailCrm/Http/Client.php @@ -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) { 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/ApiClientReferenceTest.php b/tests/RetailCrm/Tests/ApiClientReferenceTest.php index 1f10988..b31415e 100644 --- a/tests/RetailCrm/Tests/ApiClientReferenceTest.php +++ b/tests/RetailCrm/Tests/ApiClientReferenceTest.php @@ -9,6 +9,7 @@ class ApiClientReferenceTest extends TestCase /** * @group integration * @dataProvider getListDictionaries + * @param $name */ public function testList($name) { 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 6e46653..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://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); } }