opencart-module/tests/catalog/ModelRetailcrmOrderCatalogTest.php

266 lines
11 KiB
PHP
Raw Normal View History

2018-06-13 16:13:08 +03:00
<?php
2020-02-19 12:37:03 +03:00
require_once __DIR__ . '/../' . getenv('OPENCART') . '/TestCase.php';
class ModelRetailcrmOrderCatalogTest extends TestCase
2018-06-13 16:13:08 +03:00
{
private $orderModel;
private $apiClientMock;
private $retailcrm;
const CUSTOMER_ID = 1;
const ORDER_WITH_CUST_ID = 1;
const ORDER_ID = 2;
public function setUp()
{
parent::setUp();
$this->orderModel = $this->loadModel('extension/retailcrm/order');
$this->apiClientMock = $this->getMockBuilder(\RetailcrmProxy::class)
->disableOriginalConstructor()
->setMethods(array(
'ordersCreate',
'ordersEdit',
'ordersGet',
'ordersPaymentEdit',
2019-01-18 09:52:08 +03:00
'customersList',
'customersCreate'
2018-06-13 16:13:08 +03:00
))
->getMock();
$this->retailcrm = new \retailcrm\Retailcrm(self::$registry);
$this->setSetting(
$this->retailcrm->getModuleTitle(),
array(
$this->retailcrm->getModuleTitle() . '_apiversion' => 'v5',
2018-07-31 17:22:29 +03:00
$this->retailcrm->getModuleTitle() . '_order_number' => 1,
2018-06-13 16:13:08 +03:00
$this->retailcrm->getModuleTitle() . '_status' => array(
1 => 'new'
),
$this->retailcrm->getModuleTitle() . '_delivery' => array(
'flat.flat' => 'flat'
),
$this->retailcrm->getModuleTitle() . '_payment' => array(
'cod' => 'cod'
2019-03-21 12:27:19 +03:00
),
$this->retailcrm->getModuleTitle() . '_special_1' => 'special1',
$this->retailcrm->getModuleTitle() . '_special_2' => 'special2',
$this->retailcrm->getModuleTitle() . '_special_3' => 'special3'
2018-06-13 16:13:08 +03:00
)
);
}
public function testCreateOrderWithCustomer()
{
$orderCheckoutModel = $this->loadModel('checkout/order');
$orderAccountModel = $this->loadModel('account/order');
$order_id = self::ORDER_WITH_CUST_ID;
$order = $orderCheckoutModel->getOrder($order_id);
$order['products'] = $orderAccountModel->getOrderProducts($order_id);
$order['totals'] = $orderAccountModel->getOrderTotals($order_id);
foreach ($order['products'] as $key => $product) {
$productOptions = $orderAccountModel->getOrderOptions($order_id, $product['order_product_id']);
if (!empty($productOptions)) {
$order['products'][$key]['option'] = $productOptions;
}
}
$orderProcess = $this->orderModel->processOrder($order);
2019-01-18 09:52:08 +03:00
$orderSend = $this->orderModel->sendToCrm($orderProcess, $this->apiClientMock, $order);
2018-06-13 16:13:08 +03:00
$this->assertArrayHasKey('status', $orderSend);
$this->assertEquals('new', $orderSend['status']);
$this->assertArrayHasKey('externalId', $orderSend);
$this->assertArrayHasKey('number', $orderSend);
$this->assertArrayHasKey('firstName', $orderSend);
$this->assertEquals('Test', $orderSend['firstName']);
$this->assertArrayHasKey('lastName', $orderSend);
$this->assertEquals('Test', $orderSend['lastName']);
$this->assertArrayHasKey('email', $orderSend);
$this->assertEquals('test@mail.ru', $orderSend['email']);
$this->assertArrayHasKey('phone', $orderSend);
$this->assertEquals('+7 (000) 000-00-00', $orderSend['phone']);
$this->assertArrayHasKey('createdAt', $orderSend);
$this->assertArrayHasKey('delivery', $orderSend);
$this->assertInternalType('array', $orderSend['delivery']);
$this->assertEquals('flat', $orderSend['delivery']['code']);
$this->assertEquals('Test', $orderSend['delivery']['address']['city']);
$this->assertEquals('Rostov-na-Donu', $orderSend['delivery']['address']['region']);
$this->assertEquals('111111', $orderSend['delivery']['address']['index']);
$this->assertArrayHasKey('items', $orderSend);
2019-03-21 12:27:19 +03:00
foreach($orderSend['items'] as $item) {
$this->assertArrayHasKey('priceType', $item);
$this->assertEquals('special1', $item['priceType']['code']);
}
2018-06-13 16:13:08 +03:00
$this->assertArrayHasKey('customerComment', $orderSend);
$this->assertArrayHasKey('customer', $orderSend);
$this->assertArrayHasKey('externalId', $orderSend['customer']);
$this->assertEquals(self::CUSTOMER_ID, $orderSend['customer']['externalId']);
$this->assertArrayHasKey('payments', $orderSend);
$this->assertEquals('cod', $orderSend['payments'][0]['type']);
$this->assertNotEmpty($orderSend['payments']);
}
public function testEditOrderWithCustomer()
{
$orderCheckoutModel = $this->loadModel('checkout/order');
$orderAccountModel = $this->loadModel('account/order');
$order_id = self::ORDER_WITH_CUST_ID;
$order = $orderCheckoutModel->getOrder($order_id);
$order['products'] = $orderAccountModel->getOrderProducts($order_id);
$order['totals'] = $orderAccountModel->getOrderTotals($order_id);
foreach ($order['products'] as $key => $product) {
$productOptions = $orderAccountModel->getOrderOptions($order_id, $product['order_product_id']);
if (!empty($productOptions)) {
$order['products'][$key]['option'] = $productOptions;
}
}
$orderEditResponse = new \RetailcrmApiResponse(
200,
json_encode(
array(
'success' => true,
'id' => 1
)
)
);
$ordersGetResponse = new \RetailcrmApiResponse(
200,
json_encode(
array(
'success' => true,
'order' => $this->getOrder($order_id)
)
)
);
$this->apiClientMock->expects($this->any())->method('ordersEdit')->willReturn($orderEditResponse);
$this->apiClientMock->expects($this->any())->method('ordersGet')->willReturn($ordersGetResponse);
2019-01-18 09:52:08 +03:00
$this->apiClientMock->expects($this->any())->method('customersCreate')->willReturn($orderEditResponse);
2018-06-13 16:13:08 +03:00
$orderProcess = $this->orderModel->processOrder($order);
2019-01-18 09:52:08 +03:00
$orderSend = $this->orderModel->sendToCrm($orderProcess, $this->apiClientMock, $order, false);
2018-06-13 16:13:08 +03:00
$this->assertArrayHasKey('status', $orderSend);
$this->assertEquals('new', $orderSend['status']);
$this->assertArrayHasKey('externalId', $orderSend);
$this->assertArrayHasKey('number', $orderSend);
$this->assertArrayHasKey('firstName', $orderSend);
$this->assertEquals('Test', $orderSend['firstName']);
$this->assertArrayHasKey('lastName', $orderSend);
$this->assertEquals('Test', $orderSend['lastName']);
$this->assertArrayHasKey('email', $orderSend);
$this->assertEquals('test@mail.ru', $orderSend['email']);
$this->assertArrayHasKey('phone', $orderSend);
$this->assertEquals('+7 (000) 000-00-00', $orderSend['phone']);
$this->assertArrayHasKey('createdAt', $orderSend);
$this->assertArrayHasKey('delivery', $orderSend);
$this->assertInternalType('array', $orderSend['delivery']);
$this->assertEquals('flat', $orderSend['delivery']['code']);
$this->assertEquals('Test', $orderSend['delivery']['address']['city']);
$this->assertEquals('Rostov-na-Donu', $orderSend['delivery']['address']['region']);
$this->assertEquals('111111', $orderSend['delivery']['address']['index']);
$this->assertArrayHasKey('items', $orderSend);
2019-03-21 12:27:19 +03:00
foreach($orderSend['items'] as $item) {
$this->assertArrayHasKey('priceType', $item);
$this->assertEquals('special1', $item['priceType']['code']);
}
2018-06-13 16:13:08 +03:00
$this->assertArrayHasKey('customerComment', $orderSend);
}
2019-02-13 17:34:30 +03:00
public function testOrderCreateWithoutCustomerTest()
{
$order_id = self::ORDER_ID;
$orderCheckoutModel = $this->loadModel('checkout/order');
$orderAccountModel = $this->loadModel('account/order');
$order = $orderCheckoutModel->getOrder($order_id);
$order['products'] = $orderAccountModel->getOrderProducts($order_id);
$order['totals'] = $orderAccountModel->getOrderTotals($order_id);
foreach($order['products'] as $key => $product) {
$order['products'][$key]['option'] = $orderAccountModel->getOrderOptions($product['order_id'], $product['order_product_id']);
}
$responseCustomerCreate = new \RetailcrmApiResponse(
201,
json_encode(
array(
'success' => true,
'id' => 1
)
)
);
$responseCustomerList = new \RetailcrmApiResponse(
201,
json_encode(
array(
'success' => true,
"pagination"=> [
"limit"=>20,
"totalCount"=> 0,
"currentPage"=> 1,
"totalPageCount"=> 0
],
"customers"=> []
)
)
);
$this->apiClientMock->expects($this->any())->method('customersList')->willReturn($responseCustomerList);
$this->apiClientMock->expects($this->any())->method('customersCreate')->willReturn($responseCustomerCreate);
$orderProcess = $this->orderModel->processOrder($order);
$orderSend = $this->orderModel->sendToCrm($orderProcess, $this->apiClientMock, $order);
$this->assertArrayHasKey('customer', $orderSend);
$this->assertArrayNotHasKey('externalId', $orderSend['customer']);
$this->assertArrayHasKey('id', $orderSend['customer']);
$this->assertEquals(1, $orderSend['customer']['id']);
2019-03-21 12:27:19 +03:00
foreach($orderSend['items'] as $item) {
$this->assertArrayNotHasKey('priceType', $item);
}
2019-02-13 17:34:30 +03:00
}
2018-06-13 16:13:08 +03:00
protected function setSetting($code, $data, $store_id = 0) {
$this->db->query("DELETE FROM `" . DB_PREFIX . "setting` WHERE store_id = '" . (int)$store_id . "' AND `code` = '" . $this->db->escape($code) . "'");
foreach ($data as $key => $value) {
if (substr($key, 0, strlen($code)) == $code) {
if (!is_array($value)) {
$this->db->query("INSERT INTO " . DB_PREFIX . "setting SET store_id = '" . (int)$store_id . "', `code` = '" . $this->db->escape($code) . "', `key` = '" . $this->db->escape($key) . "', `value` = '" . $this->db->escape($value) . "'");
} else {
$this->db->query("INSERT INTO " . DB_PREFIX . "setting SET store_id = '" . (int)$store_id . "', `code` = '" . $this->db->escape($code) . "', `key` = '" . $this->db->escape($key) . "', `value` = '" . $this->db->escape(json_encode($value, true)) . "', serialized = '1'");
}
}
}
}
private function getOrder($id)
{
return array(
'payments' => array(
array(
'id' => 1,
'status' => 'not-paid',
'type' => 'cod',
'externalId' => $id,
'amount' => '100'
)
)
);
}
}