refactoring and tests

This commit is contained in:
Daniel Weiser 2020-07-15 23:06:12 +03:00
parent caf55f67ad
commit 434ec7b431
2 changed files with 65 additions and 16 deletions

View File

@ -37,9 +37,7 @@ class ModelExtensionRetailcrmOrder extends Model {
}
if (!isset($order['customer']['externalId']) && !isset($order['customer']['id'])) {
$new_customer = $this->createCustomer($data);
$res = $retailcrmApiClient->customersCreate($new_customer);
$res = $this->createCustomer($data, $retailcrmApiClient);
if ($res->isSuccessful() && isset($res['id'])) {
$order['customer']['id'] = $res['id'];
}
@ -49,8 +47,12 @@ class ModelExtensionRetailcrmOrder extends Model {
$order = self::filterRecursive($order);
$response = $retailcrmApiClient->ordersCreate($order);
if (isset($response['errors']['customer.externalId'])) {
$order['customer'] = $this->createCustomer($data);
$response = $retailcrmApiClient->ordersCreate($order);
$res = $this->createCustomer($data, $retailcrmApiClient);
if ($res->isSuccessful() && isset($res['id'])) {
$order['customer']['id'] = $res['id'];
}
$retailcrmApiClient->ordersCreate($order);
}
} else {
$order_payment = reset($order['payments']);
@ -58,7 +60,11 @@ class ModelExtensionRetailcrmOrder extends Model {
$order = self::filterRecursive($order);
$response = $retailcrmApiClient->ordersEdit($order);
if (isset($response['errors']['customer.externalId'])) {
$order['customer'] = $this->createCustomer($data);
$res = $this->createCustomer($data, $retailcrmApiClient);
if ($res->isSuccessful() && isset($res['id'])) {
$order['customer']['id'] = $res['id'];
}
$response = $retailcrmApiClient->ordersEdit($order);
}
@ -413,10 +419,11 @@ class ModelExtensionRetailcrmOrder extends Model {
/**
* @param array $data
* @param \RetailcrmProxy $retailcrmApiClient
*
* @return array $customer
* @return ApiResponse
*/
private function createCustomer($data)
private function createCustomer($data, $retailcrmApiClient)
{
$customer = array(
'firstName' => $data['firstname'],
@ -440,6 +447,6 @@ class ModelExtensionRetailcrmOrder extends Model {
);
}
return $customer;
return $retailcrmApiClient->customersCreate($customer);
}
}

View File

@ -71,6 +71,32 @@ class ModelRetailcrmOrderCatalogTest extends TestCase
}
$orderProcess = $this->orderModel->processOrder($order);
$successResponse = new \RetailcrmApiResponse(
200,
json_encode(
array(
'success' => true,
'id' => 1,
)
)
);
$orderCreateErrorResponse = new \RetailcrmApiResponse(
400,
json_encode(
array(
'errors' => array (
'customer.externalId' => "Customer with externalId=1 not found.",
)
)
)
);
$this->apiClientMock->expects($this->any())->method('customersCreate')->willReturn($successResponse);
$this->apiClientMock->expects($this->any())->method('ordersCreate')->will(
$this->onConsecutiveCalls($orderCreateErrorResponse, $successResponse)
);
$orderSend = $this->orderModel->sendToCrm($orderProcess, $this->apiClientMock, $order);
$this->assertArrayHasKey('status', $orderSend);
@ -103,6 +129,7 @@ class ModelRetailcrmOrderCatalogTest extends TestCase
$this->assertArrayHasKey('customer', $orderSend);
$this->assertArrayHasKey('externalId', $orderSend['customer']);
$this->assertEquals(self::CUSTOMER_ID, $orderSend['customer']['externalId']);
$this->assertEquals(1, $orderSend['customer']['id']);
$this->assertArrayHasKey('payments', $orderSend);
$this->assertEquals('cod', $orderSend['payments'][0]['type']);
$this->assertNotEmpty($orderSend['payments']);
@ -145,9 +172,23 @@ class ModelRetailcrmOrderCatalogTest extends TestCase
)
);
$this->apiClientMock->expects($this->any())->method('ordersEdit')->willReturn($orderEditResponse);
$orderEditErrorResponse = new \RetailcrmApiResponse(
400,
json_encode(
array(
'errors' => array (
'customer.externalId' => "Customer with externalId=1 not found.",
)
)
)
);
$this->apiClientMock->expects($this->any())->method('ordersGet')->willReturn($ordersGetResponse);
$this->apiClientMock->expects($this->any())->method('customersCreate')->willReturn($orderEditResponse);
$this->apiClientMock->expects($this->any())->method('ordersEdit')->will(
$this->onConsecutiveCalls($orderEditErrorResponse, $orderEditResponse)
);
$orderProcess = $this->orderModel->processOrder($order);
$orderSend = $this->orderModel->sendToCrm($orderProcess, $this->apiClientMock, $order, false);
@ -171,6 +212,7 @@ class ModelRetailcrmOrderCatalogTest extends TestCase
$this->assertEquals('Rostov-na-Donu', $orderSend['delivery']['address']['region']);
$this->assertEquals('111111', $orderSend['delivery']['address']['index']);
$this->assertArrayHasKey('items', $orderSend);
$this->assertEquals(1, $orderSend['customer']['id']);
foreach($orderSend['items'] as $item) {
$this->assertArrayHasKey('priceType', $item);