From a0f583406cccee238e5f81a9a7bb0c4068cda1ee Mon Sep 17 00:00:00 2001 From: Ilyas Salikhov Date: Thu, 6 Nov 2014 13:48:07 +0300 Subject: [PATCH] Complete orders and customers methods --- lib/RetailCrm/ApiClient.php | 164 +++++++++- .../Tests/ApiClientCustomersTest.php | 271 +++++++++++++++++ tests/RetailCrm/Tests/ApiClientOrdersTest.php | 282 ++++++++++++++++++ tests/RetailCrm/Tests/ApiClientTest.php | 143 --------- 4 files changed, 713 insertions(+), 147 deletions(-) create mode 100644 tests/RetailCrm/Tests/ApiClientCustomersTest.php create mode 100644 tests/RetailCrm/Tests/ApiClientOrdersTest.php diff --git a/lib/RetailCrm/ApiClient.php b/lib/RetailCrm/ApiClient.php index beab987..3671e2f 100644 --- a/lib/RetailCrm/ApiClient.php +++ b/lib/RetailCrm/ApiClient.php @@ -40,6 +40,10 @@ class ApiClient */ public function ordersCreate(array $order) { + if (!sizeof($order)) { + throw new \InvalidArgumentException('Parameter `order` must contains a data'); + } + return $this->client->makeRequest("/orders/create", Client::METHOD_POST, array( 'order' => json_encode($order) )); @@ -53,6 +57,10 @@ class ApiClient */ public function ordersEdit(array $order, $by = 'externalId') { + if (!sizeof($order)) { + throw new \InvalidArgumentException('Parameter `order` must contains a data'); + } + $this->checkIdParameter($by); if (!isset($order[$by])) { @@ -65,6 +73,23 @@ class ApiClient )); } + /** + * Upload array of the orders + * + * @param array $orders + * @return ApiResponse + */ + public function ordersUpload(array $orders) + { + if (!sizeof($orders)) { + throw new \InvalidArgumentException('Parameter `orders` must contains array of the orders'); + } + + return $this->client->makeRequest("/orders/upload", Client::METHOD_POST, array( + 'orders' => json_encode($orders), + )); + } + /** * Get order by id or externalId * @@ -111,10 +136,10 @@ class ApiClient /** * Returns filtered orders list * - * @param array $filter (default: array()) - * @param int $page (default: null) - * @param int $limit (default: null) - * @return void + * @param array $filter (default: array()) + * @param int $page (default: null) + * @param int $limit (default: null) + * @return ApiResponse */ public function ordersList(array $filter = array(), $page = null, $limit = null) { @@ -133,6 +158,137 @@ class ApiClient return $this->client->makeRequest('/orders', Client::METHOD_GET, $parameters); } + /** + * Save order IDs' (id and externalId) association in the CRM + * + * @param array $ids + * @return ApiResponse + */ + public function ordersFixExternalIds(array $ids) + { + if (!sizeof($ids)) { + throw new \InvalidArgumentException('Method parameter must contains at least one IDs pair'); + } + + return $this->client->makeRequest("/orders/fix-external-ids", Client::METHOD_POST, array( + 'orders' => json_encode($ids), + )); + } + + /** + * Create a customer + * + * @param array $customer + * @return ApiResponse + */ + public function customersCreate(array $customer) + { + if (!sizeof($customer)) { + throw new \InvalidArgumentException('Parameter `customer` must contains a data'); + } + + return $this->client->makeRequest("/customers/create", Client::METHOD_POST, array( + 'customer' => json_encode($customer) + )); + } + + /** + * Edit a customer + * + * @param array $customer + * @return ApiResponse + */ + public function customersEdit(array $customer, $by = 'externalId') + { + if (!sizeof($customer)) { + throw new \InvalidArgumentException('Parameter `customer` must contains a data'); + } + + $this->checkIdParameter($by); + + if (!isset($customer[$by])) { + throw new \InvalidArgumentException(sprintf('Customer array must contain the "%s" parameter.', $by)); + } + + return $this->client->makeRequest("/customers/" . $customer[$by] . "/edit", Client::METHOD_POST, array( + 'customer' => json_encode($customer), + 'by' => $by, + )); + } + + /** + * Upload array of the customers + * + * @param array $customers + * @return ApiResponse + */ + public function customersUpload(array $customers) + { + if (!sizeof($customers)) { + throw new \InvalidArgumentException('Parameter `customers` must contains array of the customers'); + } + + return $this->client->makeRequest("/customers/upload", Client::METHOD_POST, array( + 'customers' => json_encode($customers), + )); + } + + /** + * Get customer by id or externalId + * + * @param string $id + * @param string $by (default: 'externalId') + * @return ApiResponse + */ + public function customersGet($id, $by = 'externalId') + { + $this->checkIdParameter($by); + + return $this->client->makeRequest("/customers/$id", Client::METHOD_GET, array('by' => $by)); + } + + /** + * Returns filtered customers list + * + * @param array $filter (default: array()) + * @param int $page (default: null) + * @param int $limit (default: null) + * @return ApiResponse + */ + public function customersList(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('/customers', Client::METHOD_GET, $parameters); + } + + /** + * Save customer IDs' (id and externalId) association in the CRM + * + * @param array $ids + * @return ApiResponse + */ + public function customersFixExternalIds(array $ids) + { + if (!sizeof($ids)) { + throw new \InvalidArgumentException('Method parameter must contains at least one IDs pair'); + } + + return $this->client->makeRequest("/customers/fix-external-ids", Client::METHOD_POST, array( + 'customers' => json_encode($ids), + )); + } + /** * Check ID parameter * diff --git a/tests/RetailCrm/Tests/ApiClientCustomersTest.php b/tests/RetailCrm/Tests/ApiClientCustomersTest.php new file mode 100644 index 0000000..cde18d6 --- /dev/null +++ b/tests/RetailCrm/Tests/ApiClientCustomersTest.php @@ -0,0 +1,271 @@ +customersCreate(array( + 'firstName' => self::FIRST_NAME, + 'externalId' => $externalId, + )); + $this->assertInstanceOf('RetailCrm\Response\ApiResponse', $response); + $this->assertEquals(201, $response->getStatusCode()); + $this->assertTrue(is_int($response->getId())); + + return array( + 'id' => $response->getId(), + 'externalId' => $externalId, + ); + } + + /** + * @group unit + * @expectedException \InvalidArgumentException + */ + public function testCustomersCreateExceptionEmpty() + { + $client = static::getApiClient(); + + $response = $client->customersCreate(array()); + } + + /** + * @group integration + * @depends testCustomersCreate + */ + public function testCustomersGet(array $ids) + { + $client = static::getApiClient(); + + $response = $client->customersGet(678678678); + $this->assertInstanceOf('RetailCrm\Response\ApiResponse', $response); + $this->assertEquals(404, $response->getStatusCode()); + $this->assertFalse($response->success); + + $response = $client->customersGet($ids['id'], 'id'); + $customerById = $response->customer; + $this->assertInstanceOf('RetailCrm\Response\ApiResponse', $response); + $this->assertEquals(200, $response->getStatusCode()); + $this->assertTrue($response->success); + $this->assertEquals(self::FIRST_NAME, $response->customer['firstName']); + + $response = $client->customersGet($ids['externalId'], 'externalId'); + $this->assertEquals($customerById['id'], $response->customer['id']); + + return $ids; + } + + /** + * @group unit + * @expectedException \InvalidArgumentException + */ + public function testCustomersGetException() + { + $client = static::getApiClient(); + + $response = $client->customersGet(678678678, 'asdf'); + } + + /** + * @group integration + * @depends testCustomersGet + */ + public function testCustomersEdit(array $ids) + { + $client = static::getApiClient(); + + $response = $client->customersEdit( + array( + 'id' => 22342134, + 'lastName' => '12345', + ), + 'id' + ); + $this->assertInstanceOf('RetailCrm\Response\ApiResponse', $response); + $this->assertEquals(404, $response->getStatusCode()); + + $response = $client->customersEdit(array( + 'externalId' => $ids['externalId'], + 'lastName' => '12345', + )); + $this->assertInstanceOf('RetailCrm\Response\ApiResponse', $response); + $this->assertEquals(200, $response->getStatusCode()); + $this->assertTrue($response->success); + + $response = $client->customersEdit(array( + 'externalId' => 'c-edit-' . time(), + 'lastName' => '12345', + )); + $this->assertInstanceOf('RetailCrm\Response\ApiResponse', $response); + $this->assertEquals(201, $response->getStatusCode()); + $this->assertTrue($response->success); + } + + /** + * @group unit + * @expectedException \InvalidArgumentException + */ + public function testCustomersEditExceptionEmpty() + { + $client = static::getApiClient(); + + $response = $client->customersEdit(array(), 'asdf'); + } + + /** + * @group unit + * @expectedException \InvalidArgumentException + */ + public function testCustomersEditException() + { + $client = static::getApiClient(); + + $response = $client->customersEdit(array('id' => 678678678), 'asdf'); + } + + /** + * @group integration + */ + public function testCustomersList() + { + $client = static::getApiClient(); + + $response = $client->customersList(); + $this->assertInstanceOf('RetailCrm\Response\ApiResponse', $response); + $this->assertTrue($response->isSuccessful()); + $this->assertTrue(isset($response['customers'])); + + $response = $client->customersList(array(), 1, 300); + $this->assertInstanceOf('RetailCrm\Response\ApiResponse', $response); + $this->assertFalse( + $response->isSuccessful(), + 'Pagination error' + ); + + $response = $client->customersList(array('maxOrdersCount' => 10), 1); + $this->assertInstanceOf('RetailCrm\Response\ApiResponse', $response); + $this->assertTrue( + $response->isSuccessful(), + 'API returns customers list' + ); + } + + /** + * @group unit + * @expectedException \InvalidArgumentException + */ + public function testCustomersFixExternalIdsException() + { + $client = static::getApiClient(); + + $response = $client->customersFixExternalIds(array()); + } + + /** + * @group integration + */ + public function testCustomersFixExternalIds() + { + $client = static::getApiClient(); + + $response = $client->ordersCreate(array( + 'firstName' => 'Aaa111', + )); + $this->assertTrue( + $response->isSuccessful(), + 'Order created' + ); + + $response = $client->ordersGet($response->id, 'id'); + $this->assertTrue( + $response->isSuccessful(), + 'Order fetched' + ); + + $id = $response->order['customer']['id']; + $externalId = 'asdf' . time(); + + $response = $client->customersFixExternalIds(array( + array('id' => $id, 'externalId' => $externalId) + )); + + $this->assertTrue( + $response->isSuccessful(), + 'Fixed customer ids' + ); + + $response = $client->customersGet($externalId); + $this->assertTrue( + $response->isSuccessful(), + 'Got customer' + ); + $this->assertEquals( + $id, + $response->customer['id'], + 'Fixing of customer ids were right' + ); + $this->assertEquals( + $externalId, + $response->customer['externalId'], + 'Fixing of customer ids were right' + ); + } + + /** + * @group unit + * @expectedException \InvalidArgumentException + */ + public function testCustomersUploadExceptionEmpty() + { + $client = static::getApiClient(); + + $response = $client->customersUpload(array()); + } + + /** + * @group integration + */ + public function testCustomersUpload() + { + $client = static::getApiClient(); + + $externalIdA = 'upload-a-' . time(); + $externalIdB = 'upload-b-' . time(); + + $response = $client->customersUpload(array( + array( + 'externalId' => $externalIdA, + 'firstName' => 'Aaa', + ), + array( + 'externalId' => $externalIdB, + 'lastName' => 'Bbb', + ), + )); + $this->assertTrue( + $response->isSuccessful(), + 'Got customer' + ); + $this->assertEquals( + $externalIdA, + $response->uploadedCustomers[0]['externalId'] + ); + $this->assertEquals( + $externalIdB, + $response->uploadedCustomers[1]['externalId'] + ); + } +} diff --git a/tests/RetailCrm/Tests/ApiClientOrdersTest.php b/tests/RetailCrm/Tests/ApiClientOrdersTest.php new file mode 100644 index 0000000..2b50977 --- /dev/null +++ b/tests/RetailCrm/Tests/ApiClientOrdersTest.php @@ -0,0 +1,282 @@ +ordersCreate(array( + 'firstName' => self::FIRST_NAME, + 'externalId' => $externalId, + )); + $this->assertInstanceOf('RetailCrm\Response\ApiResponse', $response); + $this->assertEquals(201, $response->getStatusCode()); + $this->assertTrue(is_int($response->getId())); + + return array( + 'id' => $response->getId(), + 'externalId' => $externalId, + ); + } + + /** + * @group unit + * @expectedException \InvalidArgumentException + */ + public function testOrdersCreateExceptionEmpty() + { + $client = static::getApiClient(); + + $response = $client->ordersCreate(array()); + } + + /** + * @group integration + * @depends testOrdersCreate + */ + public function testOrdersGet(array $ids) + { + $client = static::getApiClient(); + + $response = $client->ordersGet(678678678); + $this->assertInstanceOf('RetailCrm\Response\ApiResponse', $response); + $this->assertEquals(404, $response->getStatusCode()); + $this->assertFalse($response->success); + + $response = $client->ordersGet($ids['id'], 'id'); + $orderById = $response->order; + $this->assertInstanceOf('RetailCrm\Response\ApiResponse', $response); + $this->assertEquals(200, $response->getStatusCode()); + $this->assertTrue($response->success); + $this->assertEquals(self::FIRST_NAME, $response->order['firstName']); + + $response = $client->ordersGet($ids['externalId'], 'externalId'); + $this->assertEquals($orderById['id'], $response->order['id']); + + return $ids; + } + + /** + * @group unit + * @expectedException \InvalidArgumentException + */ + public function testOrdersGetException() + { + $client = static::getApiClient(); + + $response = $client->ordersGet(678678678, 'asdf'); + } + + /** + * @group integration + * @depends testOrdersGet + */ + public function testOrdersEdit(array $ids) + { + $client = static::getApiClient(); + + $response = $client->ordersEdit( + array( + 'id' => 22342134, + 'lastName' => '12345', + ), + 'id' + ); + $this->assertInstanceOf('RetailCrm\Response\ApiResponse', $response); + $this->assertEquals(404, $response->getStatusCode()); + + $response = $client->ordersEdit(array( + 'externalId' => $ids['externalId'], + 'lastName' => '12345', + )); + $this->assertInstanceOf('RetailCrm\Response\ApiResponse', $response); + $this->assertEquals(200, $response->getStatusCode()); + $this->assertTrue($response->success); + + $response = $client->ordersEdit(array( + 'externalId' => time(), + 'lastName' => '12345', + )); + $this->assertInstanceOf('RetailCrm\Response\ApiResponse', $response); + $this->assertEquals(201, $response->getStatusCode()); + $this->assertTrue($response->success); + } + + /** + * @group unit + * @expectedException \InvalidArgumentException + */ + public function testOrdersEditExceptionEmpty() + { + $client = static::getApiClient(); + + $response = $client->ordersEdit(array(), 'asdf'); + } + + /** + * @group unit + * @expectedException \InvalidArgumentException + */ + public function testOrdersEditException() + { + $client = static::getApiClient(); + + $response = $client->ordersEdit(array('id' => 678678678), 'asdf'); + } + + /** + * @group integration + */ + public function testOrdersHistory() + { + $client = static::getApiClient(); + + $response = $client->ordersHistory(); + $this->assertInstanceOf('RetailCrm\Response\ApiResponse', $response); + $this->assertEquals(200, $response->getStatusCode()); + $this->assertTrue($response->success); + $this->assertTrue( + isset($response['orders']), + 'API returns orders history' + ); + } + + /** + * @group integration + */ + public function testOrdersList() + { + $client = static::getApiClient(); + + $response = $client->ordersList(); + $this->assertInstanceOf('RetailCrm\Response\ApiResponse', $response); + $this->assertTrue($response->isSuccessful()); + $this->assertTrue(isset($response['orders'])); + + $response = $client->ordersList(array(), 1, 300); + $this->assertInstanceOf('RetailCrm\Response\ApiResponse', $response); + $this->assertFalse( + $response->isSuccessful(), + 'Pagination error' + ); + + $response = $client->ordersList(array('paymentStatus' => 'paid'), 1); + $this->assertInstanceOf('RetailCrm\Response\ApiResponse', $response); + $this->assertTrue( + $response->isSuccessful(), + 'API returns orders list' + ); + } + + /** + * @group unit + * @expectedException \InvalidArgumentException + */ + public function testOrdersFixExternalIdsException() + { + $client = static::getApiClient(); + + $response = $client->ordersFixExternalIds(array()); + } + + /** + * @group integration +] */ + public function testOrdersFixExternalIds() + { + $client = static::getApiClient(); + + $response = $client->ordersCreate(array( + 'firstName' => 'Aaa', + )); + $this->assertTrue( + $response->isSuccessful(), + 'Order created' + ); + + $id = $response->id; + $externalId = 'asdf' . time(); + + $response = $client->ordersFixExternalIds(array( + array('id' => $id, 'externalId' => $externalId) + )); + + $this->assertTrue( + $response->isSuccessful(), + 'Fixed order ids' + ); + + $response = $client->ordersGet($externalId); + $this->assertTrue( + $response->isSuccessful(), + 'Got order' + ); + $this->assertEquals( + $id, + $response->order['id'], + 'Fixing of order ids were right' + ); + $this->assertEquals( + $externalId, + $response->order['externalId'], + 'Fixing of order ids were right' + ); + } + + /** + * @group unit + * @expectedException \InvalidArgumentException + */ + public function testOrdersUploadExceptionEmpty() + { + $client = static::getApiClient(); + + $response = $client->ordersUpload(array()); + } + + /** + * @group integration + */ + public function testOrdersUpload() + { + $client = static::getApiClient(); + + $externalIdA = 'upload-a-' . time(); + $externalIdB = 'upload-b-' . time(); + + $response = $client->ordersUpload(array( + array( + 'externalId' => $externalIdA, + 'firstName' => 'Aaa', + ), + array( + 'externalId' => $externalIdB, + 'lastName' => 'Bbb', + ), + )); + $this->assertTrue( + $response->isSuccessful(), + 'Got order' + ); + $this->assertEquals( + $externalIdA, + $response->uploadedOrders[0]['externalId'] + ); + $this->assertEquals( + $externalIdB, + $response->uploadedOrders[1]['externalId'] + ); + } +} diff --git a/tests/RetailCrm/Tests/ApiClientTest.php b/tests/RetailCrm/Tests/ApiClientTest.php index cf5bc03..fe78cc1 100644 --- a/tests/RetailCrm/Tests/ApiClientTest.php +++ b/tests/RetailCrm/Tests/ApiClientTest.php @@ -6,8 +6,6 @@ use RetailCrm\Test\TestCase; class ApiClientTest extends TestCase { - const FIRST_NAME = 'Иннокентий'; - /** * @group unit */ @@ -17,145 +15,4 @@ class ApiClientTest extends TestCase $this->assertInstanceOf('RetailCrm\ApiClient', $client); } - - /** - * @group integration - */ - public function testOrdersCreate() - { - $client = static::getApiClient(); - - $externalId = time(); - - $response = $client->ordersCreate(array( - 'firstName' => self::FIRST_NAME, - 'externalId' => $externalId, - )); - $this->assertInstanceOf('RetailCrm\Response\ApiResponse', $response); - $this->assertEquals(201, $response->getStatusCode()); - $this->assertTrue(is_int($response->getId())); - - return array( - 'id' => $response->getId(), - 'externalId' => $externalId, - ); - } - - /** - * @group integration - * @depends testOrdersCreate - */ - public function testOrdersGet(array $ids) - { - $client = static::getApiClient(); - - $response = $client->ordersGet(678678678); - $this->assertInstanceOf('RetailCrm\Response\ApiResponse', $response); - $this->assertEquals(404, $response->getStatusCode()); - $this->assertFalse($response->success); - - $response = $client->ordersGet($ids['id'], 'id'); - $orderById = $response->order; - $this->assertInstanceOf('RetailCrm\Response\ApiResponse', $response); - $this->assertEquals(200, $response->getStatusCode()); - $this->assertTrue($response->success); - $this->assertEquals(self::FIRST_NAME, $response->order['firstName']); - - $response = $client->ordersGet($ids['externalId'], 'externalId'); - $this->assertEquals($orderById['id'], $response->order['id']); - - return $ids; - } - - /** - * @group unit - * @expectedException \InvalidArgumentException - */ - public function testOrdersGetException() - { - $client = static::getApiClient(); - - $response = $client->ordersGet(678678678, 'asdf'); - } - - /** - * @group integration - * @depends testOrdersGet - */ - public function testOrdersEdit(array $ids) - { - $client = static::getApiClient(); - - $response = $client->ordersEdit( - array( - 'id' => 22342134, - 'lastName' => '12345', - ), - 'id' - ); - $this->assertInstanceOf('RetailCrm\Response\ApiResponse', $response); - $this->assertEquals(404, $response->getStatusCode()); - - $response = $client->ordersEdit(array( - 'externalId' => $ids['externalId'], - 'lastName' => '12345', - )); - $this->assertInstanceOf('RetailCrm\Response\ApiResponse', $response); - $this->assertEquals(200, $response->getStatusCode()); - $this->assertTrue($response->success); - - $response = $client->ordersEdit(array( - 'externalId' => time(), - 'lastName' => '12345', - )); - $this->assertInstanceOf('RetailCrm\Response\ApiResponse', $response); - $this->assertEquals(201, $response->getStatusCode()); - $this->assertTrue($response->success); - } - - /** - * @group unit - * @expectedException \InvalidArgumentException - */ - public function testOrdersEditException() - { - $client = static::getApiClient(); - - $response = $client->ordersEdit(array('id' => 678678678), 'asdf'); - } - - /** - * @group integration - */ - public function testOrdersHistory() - { - $client = static::getApiClient(); - - $response = $client->ordersHistory(); - $this->assertInstanceOf('RetailCrm\Response\ApiResponse', $response); - $this->assertEquals(200, $response->getStatusCode()); - $this->assertTrue($response->success); - $this->assertTrue(isset($response['orders'])); - } - - /** - * @group integration - */ - public function testOrdersList() - { - $client = static::getApiClient(); - - $response = $client->ordersList(); - $this->assertInstanceOf('RetailCrm\Response\ApiResponse', $response); - $this->assertTrue($response->isSuccessful()); - $this->assertTrue(isset($response['orders'])); - - $response = $client->ordersList(array(), 1, 300); - $this->assertInstanceOf('RetailCrm\Response\ApiResponse', $response); - $this->assertFalse($response->isSuccessful()); - - $response = $client->ordersList(array('paymentStatus' => 'paid'), 1); - $this->assertInstanceOf('RetailCrm\Response\ApiResponse', $response); - $this->assertTrue($response->isSuccessful()); - } }