From d61ab837e8a441d940206be73a072f72c015da1f Mon Sep 17 00:00:00 2001 From: Alex Lushpai Date: Wed, 10 Jan 2018 11:35:57 +0300 Subject: [PATCH] delivery shipments --- lib/RetailCrm/Client/AbstractLoader.php | 2 +- lib/RetailCrm/Http/Client.php | 2 +- lib/RetailCrm/Methods/V4/Orders.php | 11 +- lib/RetailCrm/Methods/V5/Delivery.php | 137 +++++++++++++++++- lib/RetailCrm/Methods/V5/Orders.php | 24 +-- tests/RetailCrm/Test/TestCase.php | 26 ++-- .../Tests/Methods/CommonMethodsTest.php | 16 +- .../Version5/ApiClientDeliveryTest.php | 109 ++++++++++++++ .../Version5/ApiClientMarketplaceTest.php | 15 +- tests/bootstrap.php | 8 +- 10 files changed, 315 insertions(+), 35 deletions(-) create mode 100644 tests/RetailCrm/Tests/Methods/Version5/ApiClientDeliveryTest.php diff --git a/lib/RetailCrm/Client/AbstractLoader.php b/lib/RetailCrm/Client/AbstractLoader.php index 98592b8..560fb20 100755 --- a/lib/RetailCrm/Client/AbstractLoader.php +++ b/lib/RetailCrm/Client/AbstractLoader.php @@ -50,7 +50,7 @@ abstract class AbstractLoader if (empty($version) || !in_array($version, ['v3', 'v4', 'v5'])) { throw new \InvalidArgumentException( - 'Version parameter must be not empty and must be equal one of v3|v4|v5' + 'Version must be not empty and must be equal one of v3|v4|v5' ); } diff --git a/lib/RetailCrm/Http/Client.php b/lib/RetailCrm/Http/Client.php index 5939c51..419dd00 100755 --- a/lib/RetailCrm/Http/Client.php +++ b/lib/RetailCrm/Http/Client.php @@ -63,7 +63,7 @@ class Client * @param string $path request url * @param string $method (default: 'GET') * @param array $parameters (default: array()) - * @param bool $fullPath (default: false) + * @param bool $fullPath (default: false) * * @SuppressWarnings(PHPMD.ExcessiveParameterList) * diff --git a/lib/RetailCrm/Methods/V4/Orders.php b/lib/RetailCrm/Methods/V4/Orders.php index d34e752..899bb62 100644 --- a/lib/RetailCrm/Methods/V4/Orders.php +++ b/lib/RetailCrm/Methods/V4/Orders.php @@ -242,9 +242,14 @@ trait Orders /** * Get orders history - * @param array $filter - * @param null $page - * @param null $limit + * + * @param array $filter (default: array()) + * @param int $page (default: null) + * @param int $limit (default: null) + * + * @throws \InvalidArgumentException + * @throws \RetailCrm\Exception\CurlException + * @throws \RetailCrm\Exception\InvalidJsonException * * @return \RetailCrm\Response\ApiResponse */ diff --git a/lib/RetailCrm/Methods/V5/Delivery.php b/lib/RetailCrm/Methods/V5/Delivery.php index de43c1c..6ee47c2 100644 --- a/lib/RetailCrm/Methods/V5/Delivery.php +++ b/lib/RetailCrm/Methods/V5/Delivery.php @@ -34,7 +34,25 @@ trait Delivery /** * Get delivery settings * - * @param string $code + * @param string $code delivery code + * + * @throws \InvalidArgumentException + * @throws \RetailCrm\Exception\CurlException + * @throws \RetailCrm\Exception\InvalidJsonException + * + * @return void + */ + public function deliverySettingsGet($code) + { + throw new \InvalidArgumentException('This method is not available'); + } + + /** + * Get delivery list + * + * @param array $filter (default: array()) + * @param int $page (default: null) + * @param int $limit (default: null) * * @throws \InvalidArgumentException * @throws \RetailCrm\Exception\CurlException @@ -42,8 +60,121 @@ trait Delivery * * @return \RetailCrm\Response\ApiResponse */ - public function deliverySettingsGet($code) + public function deliveryShipmentsList( + array $filter = [], + $page = null, + $limit = null + ) { + $parameters = []; + + if (count($filter)) { + $parameters['filter'] = $filter; + } + if (null !== $page) { + $parameters['page'] = (int) $page; + } + if (null !== $limit) { + $parameters['limit'] = (int) $limit; + } + + return $this->client->makeRequest( + '/delivery/shipments', + "GET", + $parameters + ); + } + + /** + * Create delivery shipment + * + * @param array $shipment (default: array()) + * @param string $deliveryType (default: string) + * @param null $site (default: null) + * + * @throws \InvalidArgumentException + * @throws \RetailCrm\Exception\CurlException + * @throws \RetailCrm\Exception\InvalidJsonException + * + * @return \RetailCrm\Response\ApiResponse + */ + public function deliveryShipmentsCreate( + array $shipment, + $deliveryType, + $site = null + ) { + if (!count($shipment)) { + throw new \InvalidArgumentException( + 'Parameter `shipment` must contains a data' + ); + } + + return $this->client->makeRequest( + '/delivery/shipments/create', + "POST", + $this->fillSite( + $site, + [ + 'deliveryShipment' => json_encode($shipment), + 'deliveryType' => $deliveryType + ] + ) + ); + } + + /** + * Get shipment + * + * @param string $id shipment id + * + * @throws \InvalidArgumentException + * @throws \RetailCrm\Exception\CurlException + * @throws \RetailCrm\Exception\InvalidJsonException + * + * @return \RetailCrm\Response\ApiResponse + */ + public function deliveryShipmentGet($id) { - throw new \InvalidArgumentException('This method is not available'); + return $this->client->makeRequest( + sprintf("/delivery/shipments/%s", $id), + "GET" + ); + } + + /** + * Edit delivery shipment + * + * @param array $shipment (default: array()) + * @param null $site (default: null) + * + * @throws \InvalidArgumentException + * @throws \RetailCrm\Exception\CurlException + * @throws \RetailCrm\Exception\InvalidJsonException + * + * @return \RetailCrm\Response\ApiResponse + */ + public function deliveryShipmentsEdit(array $shipment, $site = null) + { + if (!count($shipment)) { + throw new \InvalidArgumentException( + 'Parameter `shipment` must contains a data' + ); + } + + if (empty($shipment['id'])) { + throw new \InvalidArgumentException( + 'Parameter `shipment` must contains an `id` field' + ); + } + + return $this->client->makeRequest( + sprintf("/delivery/shipments/%s/edit", $shipment['id']), + "POST", + $this->fillSite( + $site, + [ + 'deliveryShipment' => json_encode($shipment) + ] + ) + ); } } diff --git a/lib/RetailCrm/Methods/V5/Orders.php b/lib/RetailCrm/Methods/V5/Orders.php index 8099925..0c3aead 100644 --- a/lib/RetailCrm/Methods/V5/Orders.php +++ b/lib/RetailCrm/Methods/V5/Orders.php @@ -34,9 +34,9 @@ trait Orders /** * Combine orders * - * @param string $technique - * @param array $order - * @param array $resultOrder + * @param array $order orgin order + * @param array $resultOrder result order + * @param string $technique combining technique * * @return \RetailCrm\Response\ApiResponse */ @@ -71,7 +71,7 @@ trait Orders * Create an order payment * * @param array $payment order data - * @param null $site site code + * @param null $site site code * * @throws \InvalidArgumentException * @throws \RetailCrm\Exception\CurlException @@ -98,14 +98,14 @@ trait Orders } /** - * Edit an order payment - * - * @param array $payment order data - * @param string $by by key - * @param null $site site code - * - * @return \RetailCrm\Response\ApiResponse - */ + * Edit an order payment + * + * @param array $payment order data + * @param string $by by key + * @param null $site site code + * + * @return \RetailCrm\Response\ApiResponse + */ public function ordersPaymentEdit(array $payment, $by = 'id', $site = null) { if (!count($payment)) { diff --git a/tests/RetailCrm/Test/TestCase.php b/tests/RetailCrm/Test/TestCase.php index a0d4e2b..b5e705f 100644 --- a/tests/RetailCrm/Test/TestCase.php +++ b/tests/RetailCrm/Test/TestCase.php @@ -20,22 +20,30 @@ use RetailCrm\Http\Client; /** * Class TestCase * - * @package RetailCrm\Test + * @category RetailCrm + * @package RetailCrm + * @author RetailCrm + * @license https://opensource.org/licenses/MIT MIT License + * @link http://www.retailcrm.ru/docs/Developers/ApiVersion5 */ class TestCase extends \PHPUnit_Framework_TestCase { /** * Return ApiClient object * - * @param string $url (default: null) - * @param string $apiKey (default: null) - * @param string $version (default: null) - * @param string $site (default: null) + * @param string $url (default: null) + * @param string $apiKey (default: null) + * @param string $version (default: null) + * @param string $site (default: null) * * @return ApiClient */ - public static function getApiClient($url = null, $apiKey = null, $version = null, $site = null) - { + public static function getApiClient( + $url = null, + $apiKey = null, + $version = null, + $site = null + ) { $configUrl = getenv('CRM_API_URL') ?: $_SERVER['CRM_API_URL']; $configKey = getenv('CRM_API_KEY') ?: $_SERVER['CRM_API_KEY']; $configVersion = getenv('CRM_API_VERSION') ?: $_SERVER['CRM_API_VERSION']; @@ -51,8 +59,8 @@ class TestCase extends \PHPUnit_Framework_TestCase /** * Return Client object * - * @param string $url (default: null) - * @param array $defaultParameters (default: array()) + * @param string $url (default: null) + * @param array $defaultParameters (default: array()) * * @return Client */ diff --git a/tests/RetailCrm/Tests/Methods/CommonMethodsTest.php b/tests/RetailCrm/Tests/Methods/CommonMethodsTest.php index 4248cbc..3b74cae 100755 --- a/tests/RetailCrm/Tests/Methods/CommonMethodsTest.php +++ b/tests/RetailCrm/Tests/Methods/CommonMethodsTest.php @@ -28,7 +28,11 @@ use RetailCrm\Test\TestCase; class CommonMethodsTest extends TestCase { /** + * Available versions + * * @group api_methods + * + * @return void */ public function testAvailableVersions() { @@ -37,12 +41,16 @@ class CommonMethodsTest extends TestCase $response = $client->request->availableVersions(); static::assertEquals(200, $response->getStatusCode()); - static::assertTrue($response->getSuccess()); - static::assertGreaterThan(0, count($response->getVersions())); + static::assertTrue($response->isSuccessful()); + static::assertGreaterThan(0, count($response['versions'])); } /** + * Available methods + * * @group api_methods + * + * @return void */ public function testCredentials() { @@ -51,7 +59,7 @@ class CommonMethodsTest extends TestCase $response = $client->request->credentials(); static::assertEquals(200, $response->getStatusCode()); - static::assertTrue($response->getSuccess()); - static::assertGreaterThan(0, count($response->getCredentials())); + static::assertTrue($response->isSuccessful()); + static::assertGreaterThan(0, count($response['credentials'])); } } diff --git a/tests/RetailCrm/Tests/Methods/Version5/ApiClientDeliveryTest.php b/tests/RetailCrm/Tests/Methods/Version5/ApiClientDeliveryTest.php new file mode 100644 index 0000000..cb6d806 --- /dev/null +++ b/tests/RetailCrm/Tests/Methods/Version5/ApiClientDeliveryTest.php @@ -0,0 +1,109 @@ + + * @license https://opensource.org/licenses/MIT MIT License + * @link http://www.retailcrm.ru/docs/Developers/ApiVersion5 + */ + +namespace RetailCrm\Tests\Methods\Version5; + +use RetailCrm\Test\TestCase; + +/** + * Class ApiClientMarketplaceTest + * + * @category RetailCrm + * @package RetailCrm + * @author RetailCrm + * @license https://opensource.org/licenses/MIT MIT License + * @link http://www.retailcrm.ru/docs/Developers/ApiVersion5 + */ +class ApiClientDeliveryTest extends TestCase +{ + /** + * Test delivery list + * + * @group marketplace_v5 + * + * @return void + */ + public function testDeliveryShipmentsList() + { + $client = static::getApiClient(); + + $response = $client->request->deliveryShipmentsList(); + + static::assertInstanceOf('RetailCrm\Response\ApiResponse', $response); + static::assertEquals($response->getStatusCode(), 200); + static::assertTrue($response->isSuccessful()); + } + + + /** + * Test delivery methods + * + * @group marketplace_v5 + * + * @return void + */ + public function testDeliveryShipments() + { + $client = static::getApiClient(); + + $deliveryType = 'courier'; + + $order = [ + 'number' => uniqid(), + 'firstName' => 'Test', + 'lastName' => 'Customer', + 'email' => 'test@example.com', + 'delivery' => ['code' => $deliveryType] + ]; + + $responseOrder = $client->request->ordersCreate($order); + + static::assertInstanceOf('RetailCrm\Response\ApiResponse', $responseOrder); + static::assertEquals($responseOrder->getStatusCode(), 201); + static::assertTrue($responseOrder->isSuccessful()); + + $orderid = $responseOrder['id']; + + $shipment = [ + 'date' => date('Y-m-d'), + 'orders' => [ + [ + 'id' => $orderid + ] + ], + 'comment' => 'test shipment' + ]; + + $responseCreate = $client + ->request + ->deliveryShipmentsCreate($shipment, $deliveryType); + + static::assertInstanceOf('RetailCrm\Response\ApiResponse', $responseCreate); + static::assertTrue($responseCreate->isSuccessful()); + + $responseGet = $client->request->deliveryShipmentGet($responseCreate['id']); + + static::assertInstanceOf('RetailCrm\Response\ApiResponse', $responseGet); + static::assertTrue($responseGet->isSuccessful()); + + $updateShipment = array_merge($shipment, ['status' => 'cancelled']); + + $responseUpdate = $client + ->request + ->deliveryShipmentsUpdate($updateShipment); + + static::assertInstanceOf('RetailCrm\Response\ApiResponse', $responseUpdate); + static::assertTrue($responseUpdate->isSuccessful()); + } +} diff --git a/tests/RetailCrm/Tests/Methods/Version5/ApiClientMarketplaceTest.php b/tests/RetailCrm/Tests/Methods/Version5/ApiClientMarketplaceTest.php index 95a14e0..81575b8 100644 --- a/tests/RetailCrm/Tests/Methods/Version5/ApiClientMarketplaceTest.php +++ b/tests/RetailCrm/Tests/Methods/Version5/ApiClientMarketplaceTest.php @@ -19,7 +19,11 @@ use RetailCrm\Test\TestCase; /** * Class ApiClientMarketplaceTest * - * @package RetailCrm\Tests + * @category RetailCrm + * @package RetailCrm + * @author RetailCrm + * @license https://opensource.org/licenses/MIT MIT License + * @link http://www.retailcrm.ru/docs/Developers/ApiVersion5 */ class ApiClientMarketplaceTest extends TestCase { @@ -27,12 +31,21 @@ class ApiClientMarketplaceTest extends TestCase const SERVICE_CODE = 'integration_v5'; /** + * Test configuration + * * @group marketplace_v5 + * + * @return void */ public function testConfigurationEdit() { $client = static::getApiClient(); + /** + * Response + * + * @var \RetailCrm\Response\ApiResponse $response + */ $response = $client->request->integrationModulesEdit( [ 'name' => self::SERVICE_NAME, diff --git a/tests/bootstrap.php b/tests/bootstrap.php index f1cd65b..f8117eb 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -1,4 +1,10 @@ add('RetailCrm\\Test', __DIR__);