1
0
mirror of synced 2024-11-28 16:26:06 +03:00

Complete orders and customers methods

This commit is contained in:
Ilyas Salikhov 2014-11-06 13:48:07 +03:00
parent 5b1e5651e7
commit a0f583406c
4 changed files with 713 additions and 147 deletions

View File

@ -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
*

View File

@ -0,0 +1,271 @@
<?php
namespace RetailCrm\Tests;
use RetailCrm\Test\TestCase;
class ApiClientCustomersTest extends TestCase
{
const FIRST_NAME = 'Иннокентий';
/**
* @group integration
*/
public function testCustomersCreate()
{
$client = static::getApiClient();
$externalId = 'c-create-' . time();
$response = $client->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']
);
}
}

View File

@ -0,0 +1,282 @@
<?php
namespace RetailCrm\Tests;
use RetailCrm\Test\TestCase;
class ApiClientOrdersTest extends TestCase
{
const FIRST_NAME = 'Иннокентий';
/**
* @group integration
*/
public function testOrdersCreate()
{
$client = static::getApiClient();
$externalId = 'o-create-' . 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 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']
);
}
}

View File

@ -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());
}
}