mirror of
https://github.com/retailcrm/prestashop-module.git
synced 2025-03-02 19:33:14 +03:00
Added middleware to receive order number (CRM->PS)
This commit is contained in:
parent
f9a730208a
commit
7a8be5f637
@ -368,7 +368,6 @@ class RetailcrmExport
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param int $id
|
* @param int $id
|
||||||
* @param false $receiveOrderNumber
|
|
||||||
*
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
*
|
*
|
||||||
@ -376,7 +375,7 @@ class RetailcrmExport
|
|||||||
* @throws PrestaShopDatabaseException
|
* @throws PrestaShopDatabaseException
|
||||||
* @throws PrestaShopException
|
* @throws PrestaShopException
|
||||||
*/
|
*/
|
||||||
public static function exportOrder($id, $receiveOrderNumber = false)
|
public static function exportOrder($id)
|
||||||
{
|
{
|
||||||
if (!static::$api) {
|
if (!static::$api) {
|
||||||
return false;
|
return false;
|
||||||
@ -410,12 +409,6 @@ class RetailcrmExport
|
|||||||
|
|
||||||
if (empty($existingOrder)) {
|
if (empty($existingOrder)) {
|
||||||
$response = static::$api->ordersCreate($crmOrder);
|
$response = static::$api->ordersCreate($crmOrder);
|
||||||
|
|
||||||
if ($receiveOrderNumber && $response instanceof RetailcrmApiResponse && $response->isSuccessful()) {
|
|
||||||
$crmOrder = $response->order;
|
|
||||||
$object->reference = $crmOrder['number'];
|
|
||||||
$object->update();
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
$response = static::$api->ordersEdit($crmOrder);
|
$response = static::$api->ordersEdit($crmOrder);
|
||||||
|
|
||||||
|
@ -52,22 +52,7 @@ class RetailcrmProxy
|
|||||||
{
|
{
|
||||||
$this->client = new RetailcrmApiClientV5($url, $key);
|
$this->client = new RetailcrmApiClientV5($url, $key);
|
||||||
|
|
||||||
$this->pipeline = new RetailcrmPipeline();
|
$this->buildPipeline();
|
||||||
$this->pipeline
|
|
||||||
->setMiddlewares(
|
|
||||||
RetailcrmTools::filter(
|
|
||||||
'RetailcrmFilterMiddlewares',
|
|
||||||
[
|
|
||||||
RetailcrmExceptionMiddleware::class,
|
|
||||||
RetailcrmLoggerMiddleware::class,
|
|
||||||
]
|
|
||||||
)
|
|
||||||
)
|
|
||||||
->setAction(function ($request) {
|
|
||||||
return call_user_func_array([$this->client, $request->getMethod()], $request->getData());
|
|
||||||
})
|
|
||||||
->build()
|
|
||||||
;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __call($method, $arguments)
|
public function __call($method, $arguments)
|
||||||
@ -81,4 +66,32 @@ class RetailcrmProxy
|
|||||||
|
|
||||||
return $this->pipeline->run($request);
|
return $this->pipeline->run($request);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function setClient($client)
|
||||||
|
{
|
||||||
|
$this->client = $client;
|
||||||
|
|
||||||
|
$this->buildPipeline();
|
||||||
|
}
|
||||||
|
|
||||||
|
private function buildPipeline()
|
||||||
|
{
|
||||||
|
$this->pipeline = new RetailcrmPipeline();
|
||||||
|
$this->pipeline
|
||||||
|
->setMiddlewares(
|
||||||
|
RetailcrmTools::filter(
|
||||||
|
'RetailcrmFilterMiddlewares',
|
||||||
|
[
|
||||||
|
RetailcrmExceptionMiddleware::class,
|
||||||
|
RetailcrmLoggerMiddleware::class,
|
||||||
|
RetailcrmReferenceMiddleware::class,
|
||||||
|
]
|
||||||
|
)
|
||||||
|
)
|
||||||
|
->setAction(function ($request) {
|
||||||
|
return call_user_func_array([$this->client, $request->getMethod()], $request->getData());
|
||||||
|
})
|
||||||
|
->build()
|
||||||
|
;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,71 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* MIT License
|
||||||
|
*
|
||||||
|
* Copyright (c) 2020 DIGITAL RETAIL TECHNOLOGIES SL
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
* DISCLAIMER
|
||||||
|
*
|
||||||
|
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
|
||||||
|
* versions in the future. If you wish to customize PrestaShop for your
|
||||||
|
* needs please refer to http://www.prestashop.com for more information.
|
||||||
|
*
|
||||||
|
* @author DIGITAL RETAIL TECHNOLOGIES SL <mail@simlachat.com>
|
||||||
|
* @copyright 2021 DIGITAL RETAIL TECHNOLOGIES SL
|
||||||
|
* @license https://opensource.org/licenses/MIT The MIT License
|
||||||
|
*
|
||||||
|
* Don't forget to prefix your containers with your own identifier
|
||||||
|
* to avoid any conflicts with others containers.
|
||||||
|
*/
|
||||||
|
class RetailcrmReferenceMiddleware implements RetailcrmMiddlewareInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public function __invoke(RetailcrmApiRequest $request, callable $next = null)
|
||||||
|
{
|
||||||
|
/** @var RetailcrmApiResponse $response */
|
||||||
|
$response = $next($request);
|
||||||
|
|
||||||
|
if (
|
||||||
|
$response->isSuccessful()
|
||||||
|
&& (
|
||||||
|
'ordersCreate' === $request->getMethod()
|
||||||
|
|| 'ordersEdit' === $request->getMethod()
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
$receiveOrderNumber = (bool) (Configuration::get(RetailCRM::ENABLE_ORDER_NUMBER_RECEIVING));
|
||||||
|
$crmOrder = $response->order;
|
||||||
|
|
||||||
|
if (
|
||||||
|
$receiveOrderNumber
|
||||||
|
&& isset($crmOrder['externalId'], $crmOrder['number'])
|
||||||
|
) {
|
||||||
|
$object = new Order($crmOrder['externalId']);
|
||||||
|
$object->reference = $crmOrder['number'];
|
||||||
|
$object->update();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $response;
|
||||||
|
}
|
||||||
|
}
|
@ -356,13 +356,12 @@ class RetailCRM extends Module
|
|||||||
$isSuccessful = true;
|
$isSuccessful = true;
|
||||||
$skippedOrders = [];
|
$skippedOrders = [];
|
||||||
RetailcrmExport::$api = $this->api;
|
RetailcrmExport::$api = $this->api;
|
||||||
$receiveOrderNumber = (bool) (Configuration::get(RetailCRM::ENABLE_ORDER_NUMBER_RECEIVING));
|
|
||||||
|
|
||||||
foreach ($orderIds as $orderId) {
|
foreach ($orderIds as $orderId) {
|
||||||
$response = false;
|
$response = false;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$response = RetailcrmExport::exportOrder($orderId, $receiveOrderNumber);
|
$response = RetailcrmExport::exportOrder($orderId);
|
||||||
} catch (PrestaShopObjectNotFoundExceptionCore $e) {
|
} catch (PrestaShopObjectNotFoundExceptionCore $e) {
|
||||||
$skippedOrders[] = $orderId;
|
$skippedOrders[] = $orderId;
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
@ -736,13 +735,12 @@ class RetailCRM extends Module
|
|||||||
}
|
}
|
||||||
|
|
||||||
$status = json_decode(Configuration::get(static::STATUS), true);
|
$status = json_decode(Configuration::get(static::STATUS), true);
|
||||||
$receiveOrderNumber = (bool) (Configuration::get(RetailCRM::ENABLE_ORDER_NUMBER_RECEIVING));
|
|
||||||
|
|
||||||
if (isset($params['orderStatus'])) {
|
if (isset($params['orderStatus'])) {
|
||||||
try {
|
try {
|
||||||
RetailcrmExport::$api = $this->api;
|
RetailcrmExport::$api = $this->api;
|
||||||
|
|
||||||
return RetailcrmExport::exportOrder($params['order']->id, $receiveOrderNumber);
|
return RetailcrmExport::exportOrder($params['order']->id);
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
RetailcrmLogger::writeCaller(__METHOD__, $e->getMessage());
|
RetailcrmLogger::writeCaller(__METHOD__, $e->getMessage());
|
||||||
RetailcrmLogger::writeNoCaller($e->getTraceAsString());
|
RetailcrmLogger::writeNoCaller($e->getTraceAsString());
|
||||||
|
@ -3,7 +3,6 @@
|
|||||||
class RetailCRMTest extends RetailcrmTestCase
|
class RetailCRMTest extends RetailcrmTestCase
|
||||||
{
|
{
|
||||||
private $retailcrmModule;
|
private $retailcrmModule;
|
||||||
private $apiMock;
|
|
||||||
|
|
||||||
protected function setUp()
|
protected function setUp()
|
||||||
{
|
{
|
||||||
@ -11,54 +10,78 @@ class RetailCRMTest extends RetailcrmTestCase
|
|||||||
|
|
||||||
$this->setConfig();
|
$this->setConfig();
|
||||||
|
|
||||||
$this->apiMock = $this->apiMockBuilder()->getMock();
|
|
||||||
|
|
||||||
$this->retailcrmModule = new RetailCRM();
|
$this->retailcrmModule = new RetailCRM();
|
||||||
$this->retailcrmModule->api = $this->apiMock;
|
$this->retailcrmModule->api = $this->getApiMock(
|
||||||
}
|
[
|
||||||
|
'customersCreate',
|
||||||
private function apiMockBuilder()
|
'customersEdit',
|
||||||
{
|
'customersGet',
|
||||||
return $this->getMockBuilder('RetailcrmProxy')
|
'ordersCreate',
|
||||||
->disableOriginalConstructor()
|
'ordersEdit',
|
||||||
->setMethods(
|
'ordersGet',
|
||||||
[
|
'ordersPaymentEdit',
|
||||||
'customersCreate',
|
'ordersPaymentCreate',
|
||||||
'customersEdit',
|
]
|
||||||
'customersGet',
|
);
|
||||||
'ordersCreate',
|
|
||||||
'ordersEdit',
|
|
||||||
'ordersGet',
|
|
||||||
'ordersPaymentEdit',
|
|
||||||
'ordersPaymentCreate',
|
|
||||||
]
|
|
||||||
)
|
|
||||||
;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testUploadOrders()
|
public function testUploadOrders()
|
||||||
{
|
{
|
||||||
Configuration::updateValue(RetailCRM::API_URL, 'https://test.test');
|
Configuration::updateValue(RetailCRM::API_URL, 'https://test.test');
|
||||||
Configuration::updateValue(RetailCRM::API_KEY, 'test_key');
|
Configuration::updateValue(RetailCRM::API_KEY, 'test_key');
|
||||||
|
|
||||||
$order = new Order(1);
|
$order = new Order(1);
|
||||||
$reference = $order->reference;
|
$reference = $order->reference;
|
||||||
$updReference = 'test';
|
$updReference = 'test';
|
||||||
$this->apiMock->expects($this->any())->method('ordersGet')->willReturn(new RetailcrmApiResponse(
|
|
||||||
|
$this->apiClientMock->expects($this->any())->method('ordersGet')->willReturn(new RetailcrmApiResponse(
|
||||||
200,
|
200,
|
||||||
json_encode([
|
json_encode([
|
||||||
'success' => true,
|
'success' => true,
|
||||||
'order' => [],
|
'order' => [],
|
||||||
])
|
])
|
||||||
));
|
));
|
||||||
$this->apiMock->expects($this->any())->method('ordersCreate')->willReturn(new RetailcrmApiResponse(
|
$this->apiClientMock->expects($this->any())->method('ordersCreate')->willReturn(new RetailcrmApiResponse(
|
||||||
200,
|
200,
|
||||||
json_encode([
|
json_encode([
|
||||||
'success' => true,
|
'success' => true,
|
||||||
'order' => [
|
'order' => [
|
||||||
|
'externalId' => 1,
|
||||||
'number' => $updReference,
|
'number' => $updReference,
|
||||||
],
|
],
|
||||||
])
|
])
|
||||||
));
|
));
|
||||||
|
$this->apiClientMock->expects($this->any())->method('ordersEdit')->willReturn(new RetailcrmApiResponse(
|
||||||
|
200,
|
||||||
|
json_encode([
|
||||||
|
'success' => true,
|
||||||
|
'order' => [
|
||||||
|
'externalId' => 1,
|
||||||
|
'number' => $updReference,
|
||||||
|
],
|
||||||
|
])
|
||||||
|
));
|
||||||
|
$this->apiClientMock->expects($this->any())->method('customersGet')->willReturn(new RetailcrmApiResponse(
|
||||||
|
200,
|
||||||
|
json_encode([
|
||||||
|
'success' => true,
|
||||||
|
'customer' => [],
|
||||||
|
])
|
||||||
|
));
|
||||||
|
$this->apiClientMock->expects($this->any())->method('customersCreate')->willReturn(new RetailcrmApiResponse(
|
||||||
|
200,
|
||||||
|
json_encode([
|
||||||
|
'success' => true,
|
||||||
|
'customer' => [],
|
||||||
|
])
|
||||||
|
));
|
||||||
|
$this->apiClientMock->expects($this->any())->method('ordersPaymentCreate')->willReturn(new RetailcrmApiResponse(
|
||||||
|
200,
|
||||||
|
json_encode([
|
||||||
|
'success' => true,
|
||||||
|
'payment' => [],
|
||||||
|
])
|
||||||
|
));
|
||||||
|
|
||||||
Configuration::updateValue(RetailCRM::ENABLE_ORDER_NUMBER_RECEIVING, false);
|
Configuration::updateValue(RetailCRM::ENABLE_ORDER_NUMBER_RECEIVING, false);
|
||||||
$this->retailcrmModule->uploadOrders([1]);
|
$this->retailcrmModule->uploadOrders([1]);
|
||||||
@ -78,6 +101,21 @@ class RetailCRMTest extends RetailcrmTestCase
|
|||||||
$newCustomer = new Customer(1);
|
$newCustomer = new Customer(1);
|
||||||
$params = ['newCustomer' => $newCustomer];
|
$params = ['newCustomer' => $newCustomer];
|
||||||
|
|
||||||
|
$this->apiClientMock->expects($this->any())->method('customersGet')->willReturn(new RetailcrmApiResponse(
|
||||||
|
200,
|
||||||
|
json_encode([
|
||||||
|
'success' => true,
|
||||||
|
'customer' => [],
|
||||||
|
])
|
||||||
|
));
|
||||||
|
$this->apiClientMock->expects($this->any())->method('customersCreate')->willReturn(new RetailcrmApiResponse(
|
||||||
|
200,
|
||||||
|
json_encode([
|
||||||
|
'success' => true,
|
||||||
|
'customer' => [],
|
||||||
|
])
|
||||||
|
));
|
||||||
|
|
||||||
$this->assertTrue($this->retailcrmModule->hookActionCustomerAccountAdd($params));
|
$this->assertTrue($this->retailcrmModule->hookActionCustomerAccountAdd($params));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -86,6 +124,23 @@ class RetailCRMTest extends RetailcrmTestCase
|
|||||||
$customer = new Customer(1);
|
$customer = new Customer(1);
|
||||||
$params = ['customer' => $customer];
|
$params = ['customer' => $customer];
|
||||||
|
|
||||||
|
$this->apiClientMock->expects($this->any())->method('customersGet')->willReturn(new RetailcrmApiResponse(
|
||||||
|
200,
|
||||||
|
json_encode([
|
||||||
|
'success' => true,
|
||||||
|
'customer' => [
|
||||||
|
'phones' => [],
|
||||||
|
],
|
||||||
|
])
|
||||||
|
));
|
||||||
|
$this->apiClientMock->expects($this->any())->method('customersEdit')->willReturn(new RetailcrmApiResponse(
|
||||||
|
200,
|
||||||
|
json_encode([
|
||||||
|
'success' => true,
|
||||||
|
'customer' => [],
|
||||||
|
])
|
||||||
|
));
|
||||||
|
|
||||||
$this->assertTrue($this->retailcrmModule->hookActionCustomerAccountUpdate($params));
|
$this->assertTrue($this->retailcrmModule->hookActionCustomerAccountUpdate($params));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -97,15 +152,7 @@ class RetailCRMTest extends RetailcrmTestCase
|
|||||||
$reference = $order->reference;
|
$reference = $order->reference;
|
||||||
$updReference = 'test';
|
$updReference = 'test';
|
||||||
|
|
||||||
$this->apiMock->expects($this->any())->method('ordersGet')->willReturn(new RetailcrmApiResponse(
|
$this->apiClientMock->expects($this->any())->method('ordersGet')->willReturn(new RetailcrmApiResponse(
|
||||||
200,
|
|
||||||
json_encode([
|
|
||||||
'success' => true,
|
|
||||||
'order' => [],
|
|
||||||
])
|
|
||||||
));
|
|
||||||
|
|
||||||
$this->apiMock->expects($this->any())->method('ordersCreate')->willReturn(new RetailcrmApiResponse(
|
|
||||||
200,
|
200,
|
||||||
json_encode([
|
json_encode([
|
||||||
'success' => true,
|
'success' => true,
|
||||||
@ -114,15 +161,56 @@ class RetailCRMTest extends RetailcrmTestCase
|
|||||||
],
|
],
|
||||||
])
|
])
|
||||||
));
|
));
|
||||||
|
$this->apiClientMock->expects($this->any())->method('ordersCreate')->willReturn(new RetailcrmApiResponse(
|
||||||
|
200,
|
||||||
|
json_encode([
|
||||||
|
'success' => true,
|
||||||
|
'order' => [
|
||||||
|
'number' => $updReference,
|
||||||
|
],
|
||||||
|
])
|
||||||
|
));
|
||||||
|
$this->apiClientMock->expects($this->any())->method('ordersEdit')->willReturn(new RetailcrmApiResponse(
|
||||||
|
200,
|
||||||
|
json_encode([
|
||||||
|
'success' => true,
|
||||||
|
'order' => [
|
||||||
|
'number' => $updReference,
|
||||||
|
],
|
||||||
|
])
|
||||||
|
));
|
||||||
|
$this->apiClientMock->expects($this->any())->method('customersGet')->willReturn(new RetailcrmApiResponse(
|
||||||
|
200,
|
||||||
|
json_encode([
|
||||||
|
'success' => true,
|
||||||
|
'customer' => [],
|
||||||
|
])
|
||||||
|
));
|
||||||
|
$this->apiClientMock->expects($this->any())->method('customersCreate')->willReturn(new RetailcrmApiResponse(
|
||||||
|
200,
|
||||||
|
json_encode([
|
||||||
|
'success' => true,
|
||||||
|
'customer' => [],
|
||||||
|
])
|
||||||
|
));
|
||||||
|
$this->apiClientMock->expects($this->any())->method('ordersPaymentCreate')->willReturn(new RetailcrmApiResponse(
|
||||||
|
200,
|
||||||
|
json_encode([
|
||||||
|
'success' => true,
|
||||||
|
'payment' => [],
|
||||||
|
])
|
||||||
|
));
|
||||||
|
|
||||||
Configuration::updateValue(RetailCRM::ENABLE_ORDER_NUMBER_RECEIVING, false);
|
Configuration::updateValue(RetailCRM::ENABLE_ORDER_NUMBER_RECEIVING, false);
|
||||||
|
|
||||||
$this->assertTrue($this->retailcrmModule->hookActionOrderEdited($params));
|
$this->assertTrue($this->retailcrmModule->hookActionOrderEdited($params));
|
||||||
|
$order = new Order(1);
|
||||||
$this->assertEquals($reference, $order->reference);
|
$this->assertEquals($reference, $order->reference);
|
||||||
|
|
||||||
Configuration::updateValue(RetailCRM::ENABLE_ORDER_NUMBER_RECEIVING, true);
|
Configuration::updateValue(RetailCRM::ENABLE_ORDER_NUMBER_RECEIVING, true);
|
||||||
|
|
||||||
$this->assertTrue($this->retailcrmModule->hookActionOrderEdited($params));
|
$this->assertTrue($this->retailcrmModule->hookActionOrderEdited($params));
|
||||||
|
$order = new Order(1);
|
||||||
$this->assertEquals($updReference, $order->reference);
|
$this->assertEquals($updReference, $order->reference);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -158,7 +246,7 @@ class RetailCRMTest extends RetailcrmTestCase
|
|||||||
'cart' => $cart,
|
'cart' => $cart,
|
||||||
];
|
];
|
||||||
|
|
||||||
$this->apiMock->expects($this->any())->method('ordersGet')->willReturn(new RetailcrmApiResponse(
|
$this->apiClientMock->expects($this->any())->method('ordersGet')->willReturn(new RetailcrmApiResponse(
|
||||||
200,
|
200,
|
||||||
json_encode([
|
json_encode([
|
||||||
'success' => true,
|
'success' => true,
|
||||||
@ -167,15 +255,47 @@ class RetailCRMTest extends RetailcrmTestCase
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->apiMock->expects($this->any())->method('ordersCreate')->willReturn(new RetailcrmApiResponse(
|
$this->apiClientMock->expects($this->any())->method('ordersCreate')->willReturn(new RetailcrmApiResponse(
|
||||||
200,
|
200,
|
||||||
json_encode([
|
json_encode([
|
||||||
'success' => true,
|
'success' => true,
|
||||||
'order' => [
|
'order' => [
|
||||||
|
'externalId' => 1,
|
||||||
'number' => $updReference,
|
'number' => $updReference,
|
||||||
],
|
],
|
||||||
])
|
])
|
||||||
));
|
));
|
||||||
|
$this->apiClientMock->expects($this->any())->method('ordersEdit')->willReturn(new RetailcrmApiResponse(
|
||||||
|
200,
|
||||||
|
json_encode([
|
||||||
|
'success' => true,
|
||||||
|
'order' => [
|
||||||
|
'externalId' => 1,
|
||||||
|
'number' => $updReference,
|
||||||
|
],
|
||||||
|
])
|
||||||
|
));
|
||||||
|
$this->apiClientMock->expects($this->any())->method('customersGet')->willReturn(new RetailcrmApiResponse(
|
||||||
|
200,
|
||||||
|
json_encode([
|
||||||
|
'success' => true,
|
||||||
|
'customer' => [],
|
||||||
|
])
|
||||||
|
));
|
||||||
|
$this->apiClientMock->expects($this->any())->method('customersCreate')->willReturn(new RetailcrmApiResponse(
|
||||||
|
200,
|
||||||
|
json_encode([
|
||||||
|
'success' => true,
|
||||||
|
'customer' => [],
|
||||||
|
])
|
||||||
|
));
|
||||||
|
$this->apiClientMock->expects($this->any())->method('ordersPaymentCreate')->willReturn(new RetailcrmApiResponse(
|
||||||
|
200,
|
||||||
|
json_encode([
|
||||||
|
'success' => true,
|
||||||
|
'payment' => [],
|
||||||
|
])
|
||||||
|
));
|
||||||
|
|
||||||
Configuration::updateValue(RetailCRM::ENABLE_ORDER_NUMBER_RECEIVING, false);
|
Configuration::updateValue(RetailCRM::ENABLE_ORDER_NUMBER_RECEIVING, false);
|
||||||
|
|
||||||
@ -204,10 +324,25 @@ class RetailCRMTest extends RetailcrmTestCase
|
|||||||
'cart' => $cart,
|
'cart' => $cart,
|
||||||
];
|
];
|
||||||
|
|
||||||
|
$this->apiClientMock->expects($this->any())->method('ordersPaymentCreate')->willReturn(new RetailcrmApiResponse(
|
||||||
|
200,
|
||||||
|
json_encode([
|
||||||
|
'success' => true,
|
||||||
|
'payment' => [],
|
||||||
|
])
|
||||||
|
));
|
||||||
|
$this->apiClientMock->expects($this->any())->method('ordersGet')->willReturn(new RetailcrmApiResponse(
|
||||||
|
200,
|
||||||
|
json_encode([
|
||||||
|
'success' => true,
|
||||||
|
'order' => [],
|
||||||
|
])
|
||||||
|
));
|
||||||
|
|
||||||
$referenceMock = $this->createMock('RetailcrmReferences');
|
$referenceMock = $this->createMock('RetailcrmReferences');
|
||||||
$referenceMock->expects($this->once())->method('getSystemPaymentModules')->willReturn($this->getSystemPaymentModules());
|
$referenceMock->expects($this->once())->method('getSystemPaymentModules')->willReturn($this->getSystemPaymentModules());
|
||||||
$this->retailcrmModule->reference = $referenceMock;
|
$this->retailcrmModule->reference = $referenceMock;
|
||||||
$this->apiMock->expects($this->any())->method('ordersGet')->willReturn($ordersGet);
|
$this->apiClientMock->expects($this->any())->method('ordersGet')->willReturn($ordersGet);
|
||||||
|
|
||||||
$result = $this->retailcrmModule->hookActionPaymentCCAdd($params);
|
$result = $this->retailcrmModule->hookActionPaymentCCAdd($params);
|
||||||
|
|
||||||
|
@ -6,6 +6,10 @@ if (class_exists('LegacyTests\Unit\ContextMocker')) {
|
|||||||
|
|
||||||
abstract class RetailcrmTestCase extends \PHPUnit\Framework\TestCase
|
abstract class RetailcrmTestCase extends \PHPUnit\Framework\TestCase
|
||||||
{
|
{
|
||||||
|
private $apiMock;
|
||||||
|
|
||||||
|
protected $apiClientMock;
|
||||||
|
|
||||||
protected $contextMock;
|
protected $contextMock;
|
||||||
|
|
||||||
protected function setUp()
|
protected function setUp()
|
||||||
@ -18,6 +22,16 @@ abstract class RetailcrmTestCase extends \PHPUnit\Framework\TestCase
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function getApiMock(array $methods)
|
||||||
|
{
|
||||||
|
$this->apiClientMock = $this->apiMockBuilder($methods)->getMock();
|
||||||
|
|
||||||
|
$this->apiMock = new RetailcrmProxy('https://test.test', 'test_key');
|
||||||
|
$this->apiMock->setClient($this->apiClientMock);
|
||||||
|
|
||||||
|
return $this->apiMock;
|
||||||
|
}
|
||||||
|
|
||||||
protected function setConfig()
|
protected function setConfig()
|
||||||
{
|
{
|
||||||
$delivery = json_encode(
|
$delivery = json_encode(
|
||||||
@ -46,4 +60,12 @@ abstract class RetailcrmTestCase extends \PHPUnit\Framework\TestCase
|
|||||||
Configuration::updateValue('RETAILCRM_API_STATUS', $status);
|
Configuration::updateValue('RETAILCRM_API_STATUS', $status);
|
||||||
Configuration::updateValue('RETAILCRM_API_PAYMENT', $payment);
|
Configuration::updateValue('RETAILCRM_API_PAYMENT', $payment);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function apiMockBuilder(array $methods)
|
||||||
|
{
|
||||||
|
return $this->getMockBuilder('RetailcrmApiClientV5')
|
||||||
|
->disableOriginalConstructor()
|
||||||
|
->setMethods($methods)
|
||||||
|
;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -135,7 +135,11 @@ class RetailcrmHistoryTest extends RetailcrmTestCase
|
|||||||
'200',
|
'200',
|
||||||
json_encode(
|
json_encode(
|
||||||
[
|
[
|
||||||
'order' => [],
|
'success' => true,
|
||||||
|
'order' => [
|
||||||
|
'externalId' => $order->id,
|
||||||
|
'number' => $updReference,
|
||||||
|
],
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
94
tests/lib/api/RetailcrmReferenceMiddlewareTest.php
Normal file
94
tests/lib/api/RetailcrmReferenceMiddlewareTest.php
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
class RetailcrmReferenceMiddlewareTest extends RetailcrmTestCase
|
||||||
|
{
|
||||||
|
private $apiMock;
|
||||||
|
|
||||||
|
protected function setUp()
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
$this->apiMock = $this->getApiMock(
|
||||||
|
[
|
||||||
|
'ordersCreate',
|
||||||
|
'ordersEdit',
|
||||||
|
'ordersGet',
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getRequests()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
[
|
||||||
|
'method' => 'ordersGet',
|
||||||
|
'params' => [[]],
|
||||||
|
'reference' => 'reference',
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'method' => 'ordersEdit',
|
||||||
|
'params' => [['number' => 'test', 'externalId' => 1]],
|
||||||
|
'reference' => 'test',
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'method' => 'ordersCreate',
|
||||||
|
'params' => [['number' => 'test', 'externalId' => 1]],
|
||||||
|
'reference' => 'test',
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider getRequests
|
||||||
|
*/
|
||||||
|
public function testRequest($method, $params, $reference)
|
||||||
|
{
|
||||||
|
$this->apiClientMock->expects($this->any())->method('ordersCreate')->willReturn(new RetailcrmApiResponse(
|
||||||
|
200,
|
||||||
|
json_encode([
|
||||||
|
'success' => true,
|
||||||
|
'order' => [
|
||||||
|
'number' => 'test',
|
||||||
|
'externalId' => 1,
|
||||||
|
],
|
||||||
|
])
|
||||||
|
));
|
||||||
|
$this->apiClientMock->expects($this->any())->method('ordersEdit')->willReturn(new RetailcrmApiResponse(
|
||||||
|
200,
|
||||||
|
json_encode([
|
||||||
|
'success' => true,
|
||||||
|
'order' => [
|
||||||
|
'number' => 'test',
|
||||||
|
'externalId' => 1,
|
||||||
|
],
|
||||||
|
])
|
||||||
|
));
|
||||||
|
$this->apiClientMock->expects($this->any())->method('ordersGet')->willReturn(new RetailcrmApiResponse(
|
||||||
|
200,
|
||||||
|
json_encode([
|
||||||
|
'success' => true,
|
||||||
|
'order' => [
|
||||||
|
'number' => 'test',
|
||||||
|
'externalId' => 1,
|
||||||
|
],
|
||||||
|
])
|
||||||
|
));
|
||||||
|
|
||||||
|
Configuration::updateValue(RetailCRM::ENABLE_ORDER_NUMBER_RECEIVING, true);
|
||||||
|
|
||||||
|
$order = new Order(1);
|
||||||
|
$order->reference = 'reference';
|
||||||
|
$order->update();
|
||||||
|
unset($order);
|
||||||
|
|
||||||
|
/** @var RetailcrmApiResponse $response */
|
||||||
|
$response = $this->apiMock->$method($params);
|
||||||
|
|
||||||
|
$this->assertInstanceOf(RetailcrmApiResponse::class, $response);
|
||||||
|
$this->assertTrue($response->isSuccessful());
|
||||||
|
|
||||||
|
$order = new Order(1);
|
||||||
|
|
||||||
|
$this->assertEquals($reference, $order->reference);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user