From 0168ce754a5d1a471456eb8c70f5b28c1eaad1ff Mon Sep 17 00:00:00 2001 From: Alex Lushpai Date: Wed, 21 Sep 2016 00:09:24 +0300 Subject: [PATCH] prices methods: get, edit, upload --- lib/RetailCrm/ApiClient.php | 106 ++++++++++++++ tests/RetailCrm/Tests/ApiClientPricesTest.php | 134 ++++++++++++++++++ 2 files changed, 240 insertions(+) create mode 100644 tests/RetailCrm/Tests/ApiClientPricesTest.php diff --git a/lib/RetailCrm/ApiClient.php b/lib/RetailCrm/ApiClient.php index 1b51ee3..f8cfbe3 100644 --- a/lib/RetailCrm/ApiClient.php +++ b/lib/RetailCrm/ApiClient.php @@ -61,6 +61,8 @@ class ApiClient $this->client = new Client($url, array('apiKey' => $apiKey)); $this->siteCode = $site; + + return $this->client; } /** @@ -97,6 +99,35 @@ class ApiClient ); } + /** + * Get user groups + * + * @param null $page + * @param null $limit + * + * @throws \RetailCrm\Exception\InvalidJsonException + * @throws \RetailCrm\Exception\CurlException + * + * @return ApiResponse + */ + public function usersGroups($page = null, $limit = null) + { + $parameters = array(); + + if (null !== $page) { + $parameters['page'] = (int) $page; + } + if (null !== $limit) { + $parameters['limit'] = (int) $limit; + } + + return $this->client->makeRequest( + '/user-groups', + Client::METHOD_GET, + $parameters + ); + } + /** * Returns user data * @@ -835,6 +866,33 @@ class ApiClient ); } + /** + * Upload store prices + * + * @param array $prices prices data + * @param string $site default: null) + * + * @throws \InvalidArgumentException + * @throws \RetailCrm\Exception\CurlException + * @throws \RetailCrm\Exception\InvalidJsonException + * + * @return ApiResponse + */ + public function storePricesUpload(array $prices, $site = null) + { + if (!count($prices)) { + throw new \InvalidArgumentException( + 'Parameter `prices` must contains array of the prices' + ); + } + + return $this->client->makeRequest( + '/store/prices/upload', + Client::METHOD_POST, + $this->fillSite($site, array('prices' => json_encode($prices))) + ); + } + /** * Get products * @@ -1419,6 +1477,54 @@ class ApiClient ); } + /** + * Get prices types + * + * @throws \RetailCrm\Exception\CurlException + * @throws \RetailCrm\Exception\InvalidJsonException + * + * @return ApiResponse + */ + public function pricesTypes() + { + return $this->client->makeRequest( + '/reference/price-types', + Client::METHOD_GET + ); + } + + /** + * Edit price type + * + * @param array $data + * + * @throws \InvalidArgumentException + * @throws \RetailCrm\Exception\CurlException + * @throws \RetailCrm\Exception\InvalidJsonException + * + * @return ApiResponse + */ + public function pricesEdit(array $data) + { + if (!array_key_exists('code', $data)) { + throw new \InvalidArgumentException( + 'Data must contain "code" parameter.' + ); + } + + if (!array_key_exists('name', $data)) { + throw new \InvalidArgumentException( + 'Data must contain "name" parameter.' + ); + } + + return $this->client->makeRequest( + sprintf('/reference/price-types/%s/edit', $data['code']), + Client::METHOD_POST, + array('priceType' => json_encode($data)) + ); + } + /** * Get telephony settings * diff --git a/tests/RetailCrm/Tests/ApiClientPricesTest.php b/tests/RetailCrm/Tests/ApiClientPricesTest.php new file mode 100644 index 0000000..468f6ff --- /dev/null +++ b/tests/RetailCrm/Tests/ApiClientPricesTest.php @@ -0,0 +1,134 @@ + + * @license https://opensource.org/licenses/MIT MIT License + * @link http://www.retailcrm.ru/docs/Developers/ApiVersion4 + */ + +namespace RetailCrm\Tests; + +use RetailCrm\Test\TestCase; + +/** + * Class ApiClientPricesTest + * + * @category RetailCrm + * @package RetailCrm + * @author RetailCrm + * @license https://opensource.org/licenses/MIT MIT License + * @link http://www.retailcrm.ru/docs/Developers/ApiVersion4 + */ +class ApiClientPricesTest extends TestCase +{ + + protected $code; + + /** + * ApiClientPricesTest constructor. + */ + public function __construct() + { + parent::__construct(); + $this->code = 'price-code-a-' . time(); + } + + /** + * @group prices + */ + public function testUsersGroups() + { + $client = static::getApiClient(); + + $response = $client->usersGroups(); + $this->assertInstanceOf('RetailCrm\Response\ApiResponse', $response); + $this->assertEquals(200, $response->getStatusCode()); + $this->assertTrue($response->isSuccessful()); + } + + /** + * @group prices + */ + public function testPricesGet() + { + $client = static::getApiClient(); + + $response = $client->pricesTypes(); + $this->assertInstanceOf('RetailCrm\Response\ApiResponse', $response); + $this->assertEquals(200, $response->getStatusCode()); + $this->assertTrue($response->isSuccessful()); + } + + /** + * @group prices + */ + public function testPricesEdit() + { + + $client = static::getApiClient(); + + $response = $client->pricesEdit( + array( + 'code' => $this->code, + 'name' => $this->code, + 'ordering' => 500, + 'active' => true + ) + ); + + $this->assertInstanceOf('RetailCrm\Response\ApiResponse', $response); + $this->assertEquals(201, $response->getStatusCode()); + $this->assertTrue($response->isSuccessful()); + } + + /** + * @group prices + * @expectedException \InvalidArgumentException + */ + public function testPricesUploadExceptionEmpty() + { + $client = static::getApiClient(); + $client->storePricesUpload(array()); + } + + /** + * @group prices + */ + public function testPricesUpload() + { + $client = static::getApiClient(); + + $xmlIdA = 'upload-a-' . time(); + $xmlIdB = 'upload-b-' . time(); + + $response = $client->storePricesUpload(array( + array( + 'xmlId' => $xmlIdA, + 'prices' => array( + array( + 'code' => $this->code, + 'price' => 1700 + ) + ) + ), + array( + 'xmlId' => $xmlIdB, + 'prices' => array( + array( + 'code' => $this->code, + 'price' => 1500 + ) + ) + ), + )); + + $this->assertInstanceOf('RetailCrm\Response\ApiResponse', $response); + $this->assertEquals(200, $response->getStatusCode()); + } +}