2019-02-28 14:45:39 +03:00
|
|
|
<?php
|
|
|
|
|
2024-01-24 15:01:13 +03:00
|
|
|
use Intaro\RetailCrm\Component\Constants;
|
|
|
|
|
2019-02-28 14:45:39 +03:00
|
|
|
/**
|
|
|
|
* Class RetailCrmEventTest
|
|
|
|
*/
|
|
|
|
class RetailCrmEventTest extends PHPUnit\Framework\TestCase
|
|
|
|
{
|
2023-03-15 11:55:04 +03:00
|
|
|
public function setUp(): void
|
2019-02-28 14:45:39 +03:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
COption::SetOptionString('intaro.retailcrm', 'api_version', 'v5');
|
|
|
|
CModule::IncludeModule('intaro.retailcrm');
|
|
|
|
|
|
|
|
$this->retailcrmEvent = new RetailCrmEvent();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $history
|
|
|
|
* @param $emptyData
|
|
|
|
*
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
* @throws \PHPUnit\Framework\ExpectationFailedException
|
|
|
|
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
|
|
|
|
*
|
|
|
|
* @dataProvider userUpdateDataProvider
|
|
|
|
*/
|
|
|
|
public function testOnAfterUserUpdate($history, $emptyData)
|
|
|
|
{
|
|
|
|
$arFields = [
|
|
|
|
'ID' => 1
|
|
|
|
];
|
|
|
|
|
|
|
|
if ($history === true) {
|
|
|
|
$GLOBALS['RETAIL_CRM_HISTORY'] = $history;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($emptyData === true) {
|
|
|
|
$arFields['RESULT'] = [];
|
|
|
|
}
|
|
|
|
|
2023-03-15 11:55:04 +03:00
|
|
|
$result = RetailCrmEvent::OnAfterUserUpdate($arFields);
|
2019-02-28 14:45:39 +03:00
|
|
|
|
|
|
|
$this->assertEquals(false, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $history
|
|
|
|
* @param $new
|
|
|
|
*
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
* @throws \PHPUnit\Framework\Exception
|
|
|
|
* @throws \PHPUnit\Framework\ExpectationFailedException
|
|
|
|
* @throws \PHPUnit\Framework\MockObject\RuntimeException
|
|
|
|
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
|
|
|
|
*
|
2024-01-24 15:01:13 +03:00
|
|
|
* @runInSeparateProcess
|
|
|
|
* @preserveGlobalState disabled
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function testIntegrationPaymentSave()
|
|
|
|
{
|
|
|
|
RetailcrmConfigProvider::setSyncIntegrationPayment('N');
|
|
|
|
RetailcrmConfigProvider::setIntegrationPaymentTypes(['testPayment']);
|
|
|
|
|
|
|
|
$event = $this->createMock(\Bitrix\Sale\Payment::class);
|
|
|
|
$date = \Bitrix\Main\Type\DateTime::createFromPhp(new DateTime('2000-01-01'))->format('Y-m-d H:i:s');
|
|
|
|
$order = $this->createMock(\Bitrix\Sale\Order::class);
|
|
|
|
|
|
|
|
$order->expects($this->any())
|
|
|
|
->method('isNew')
|
|
|
|
->willReturn(false);
|
|
|
|
|
|
|
|
$paymentCollection = $this->createMock(\Bitrix\Sale\PaymentCollection::class);
|
|
|
|
$paymentCollection->expects($this->any())
|
|
|
|
->method('getOrder')
|
|
|
|
->willReturn($order);
|
|
|
|
|
|
|
|
$event->method('getCollection')->willReturn($paymentCollection);
|
|
|
|
$event->method('getId')->willReturn(11);
|
|
|
|
$event->method('getField')->willReturnCallback(function ($field) use ($date){
|
|
|
|
switch ($field) {
|
|
|
|
case 'ORDER_ID': return 11;
|
|
|
|
case 'PAID': return 'paid';
|
|
|
|
case 'PAY_SYSTEM_ID': return 1;
|
|
|
|
case 'SUM': return '500';
|
|
|
|
case 'DATE_PAID': return $date;
|
|
|
|
default: return null;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$api = new RetailCrm\ApiClient(RetailcrmConfigProvider::getApiUrl(), RetailcrmConfigProvider::getApiKey());
|
|
|
|
$spy = \Mockery::spy('overload:' . RCrmActions::class); //Добавление слежки за классом
|
|
|
|
|
|
|
|
$GLOBALS['RETAIL_CRM_HISTORY'] = false;
|
|
|
|
|
|
|
|
$result = RetailCrmEvent::paymentSave($event);
|
|
|
|
|
|
|
|
//Проверка вызова класса и передачи определенных параметров
|
|
|
|
$spy->shouldReceive('apiMethod')->with(
|
|
|
|
$api,
|
|
|
|
'ordersPaymentCreate',
|
|
|
|
'RetailCrmEvent::paymentSave',
|
|
|
|
[
|
|
|
|
'externalId' => null,
|
|
|
|
'order' => ['externalId' => 11],
|
|
|
|
'type' => 'testPayment'
|
|
|
|
],
|
|
|
|
null
|
|
|
|
)->once();
|
|
|
|
|
|
|
|
$this->assertEquals(true, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-02-28 14:45:39 +03:00
|
|
|
* @dataProvider paymentSaveDataProvider
|
|
|
|
*/
|
2024-01-24 15:01:13 +03:00
|
|
|
public function testSavePaymentWithHistoryAndCreateOrder($history, $new)
|
2019-02-28 14:45:39 +03:00
|
|
|
{
|
|
|
|
$event = $this->createMock(\Bitrix\Sale\Payment::class);
|
|
|
|
|
|
|
|
$order = $this->createMock(\Bitrix\Sale\Order::class);
|
|
|
|
$order->expects($this->any())
|
|
|
|
->method('isNew')
|
|
|
|
->willReturn($new);
|
|
|
|
|
|
|
|
$paymentCollection = $this->createMock(\Bitrix\Sale\PaymentCollection::class);
|
|
|
|
$paymentCollection->expects($this->any())
|
|
|
|
->method('getOrder')
|
|
|
|
->willReturn($order);
|
|
|
|
|
|
|
|
$event->method('getCollection')->willReturn($paymentCollection);
|
|
|
|
|
|
|
|
if ($history === true) {
|
|
|
|
$GLOBALS['RETAIL_CRM_HISTORY'] = true;
|
|
|
|
}
|
|
|
|
|
2023-03-15 11:55:04 +03:00
|
|
|
$result = RetailCrmEvent::paymentSave($event);
|
2019-02-28 14:45:39 +03:00
|
|
|
|
|
|
|
$this->assertEquals(false, $result);
|
|
|
|
}
|
|
|
|
|
2024-01-24 15:01:13 +03:00
|
|
|
/**
|
|
|
|
* @runInSeparateProcess
|
|
|
|
* @preserveGlobalState disabled
|
|
|
|
*/
|
|
|
|
public function testPaymentSaveWithSyncIntegrationPayment()
|
|
|
|
{
|
|
|
|
RetailcrmConfigProvider::setSyncIntegrationPayment('Y');
|
|
|
|
RetailcrmConfigProvider::setIntegrationPaymentTypes(['testPayment']);
|
|
|
|
|
|
|
|
$event = $this->createMock(\Bitrix\Sale\Payment::class);
|
|
|
|
$date = \Bitrix\Main\Type\DateTime::createFromPhp(new DateTime('2000-01-01'))->format('Y-m-d H:i:s');
|
|
|
|
$order = $this->createMock(\Bitrix\Sale\Order::class);
|
|
|
|
|
|
|
|
$order->expects($this->any())
|
|
|
|
->method('isNew')
|
|
|
|
->willReturn(false);
|
|
|
|
|
|
|
|
$paymentCollection = $this->createMock(\Bitrix\Sale\PaymentCollection::class);
|
|
|
|
$paymentCollection->expects($this->any())
|
|
|
|
->method('getOrder')
|
|
|
|
->willReturn($order);
|
|
|
|
|
|
|
|
$event->method('getCollection')->willReturn($paymentCollection);
|
|
|
|
$event->method('getId')->willReturn(11);
|
|
|
|
$event->method('getField')->willReturnCallback(function ($field) use ($date){
|
|
|
|
switch ($field) {
|
|
|
|
case 'ORDER_ID': return 11;
|
|
|
|
case 'PAID': return 'paid';
|
|
|
|
case 'PAY_SYSTEM_ID': return 1;
|
|
|
|
case 'SUM': return '500';
|
|
|
|
case 'DATE_PAID': return $date;
|
|
|
|
default: return null;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$api = new RetailCrm\ApiClient(RetailcrmConfigProvider::getApiUrl(), RetailcrmConfigProvider::getApiKey());
|
|
|
|
$spy = \Mockery::spy('overload:' . RCrmActions::class);
|
|
|
|
|
|
|
|
$GLOBALS['RETAIL_CRM_HISTORY'] = false;
|
|
|
|
|
|
|
|
$result = RetailCrmEvent::paymentSave($event);
|
|
|
|
|
|
|
|
$spy->shouldReceive('apiMethod')->with(
|
|
|
|
$api,
|
|
|
|
'ordersPaymentCreate',
|
|
|
|
'RetailCrmEvent::paymentSave',
|
|
|
|
[
|
|
|
|
'externalId' => null,
|
|
|
|
'order' => ['externalId' => 11],
|
|
|
|
'type' => 'testPayment' . Constants::CRM_PART_SUBSTITUTED_PAYMENT_CODE,
|
|
|
|
'status' => 'paid',
|
|
|
|
'paidAt' => $date
|
|
|
|
],
|
|
|
|
null
|
|
|
|
)->once();
|
|
|
|
|
|
|
|
$this->assertEquals(true, $result);
|
|
|
|
}
|
|
|
|
|
2019-02-28 14:45:39 +03:00
|
|
|
/**
|
|
|
|
* @param $history
|
|
|
|
*
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
* @throws \PHPUnit\Framework\Exception
|
|
|
|
* @throws \PHPUnit\Framework\ExpectationFailedException
|
|
|
|
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
|
|
|
|
*
|
|
|
|
* @dataProvider paymentDeleteDataProvider
|
|
|
|
*/
|
2023-09-01 14:06:23 +03:00
|
|
|
public function testPaymentDelete($history)
|
2019-02-28 14:45:39 +03:00
|
|
|
{
|
|
|
|
$event = $this->createMock(\Bitrix\Sale\Payment::class);
|
|
|
|
|
|
|
|
if ($history === true) {
|
|
|
|
$GLOBALS['RETAIL_CRM_HISTORY'] = true;
|
|
|
|
}
|
|
|
|
|
2023-03-15 11:55:04 +03:00
|
|
|
$result = RetailCrmEvent::paymentDelete($event);
|
2019-02-28 14:45:39 +03:00
|
|
|
|
|
|
|
$this->assertEquals(false, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
* @throws \PHPUnit\Framework\Exception
|
|
|
|
* @throws \PHPUnit\Framework\ExpectationFailedException
|
|
|
|
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function testOrderSave()
|
|
|
|
{
|
|
|
|
$GLOBALS['RETAIL_CRM_HISTORY'] = true;
|
|
|
|
$GLOBALS['RETAILCRM_ORDER_OLD_EVENT'] = false;
|
|
|
|
$GLOBALS['RETAILCRM_ORDER_DELETE'] = true;
|
|
|
|
|
|
|
|
$event = $this->createMock(\Bitrix\Main\Event::class);
|
|
|
|
|
2022-03-02 15:40:53 +03:00
|
|
|
$result = RetailCrmEvent::orderSave($event);
|
2019-02-28 14:45:39 +03:00
|
|
|
|
|
|
|
$this->assertEquals(false, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
* @throws \PHPUnit\Framework\Exception
|
|
|
|
* @throws \PHPUnit\Framework\ExpectationFailedException
|
|
|
|
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function testOrderDelete()
|
|
|
|
{
|
|
|
|
$event = $this->createMock(\Bitrix\Main\Event::class);
|
|
|
|
|
2023-03-15 11:55:04 +03:00
|
|
|
$result = RetailCrmEvent::OrderDelete($event);
|
2019-02-28 14:45:39 +03:00
|
|
|
|
2022-07-20 12:46:32 +03:00
|
|
|
$this->assertEquals(true, $GLOBALS['RETAILCRM_ORDER_DELETE']);
|
2019-02-28 14:45:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws \PHPUnit\Framework\ExpectationFailedException
|
|
|
|
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function testOnUpdateOrder()
|
|
|
|
{
|
2022-07-20 12:46:32 +03:00
|
|
|
$arFields = [
|
|
|
|
'CANCELED' => 'Y',
|
|
|
|
'BASKET_ITEMS' => [],
|
|
|
|
'ORDER_PROP' => []
|
|
|
|
];
|
2019-02-28 14:45:39 +03:00
|
|
|
|
2023-03-15 11:55:04 +03:00
|
|
|
$result = RetailCrmEvent::OnUpdateOrder(1, $arFields);
|
2019-02-28 14:45:39 +03:00
|
|
|
|
2022-07-20 12:46:32 +03:00
|
|
|
$this->assertEquals(true, $GLOBALS['RETAILCRM_ORDER_OLD_EVENT']);
|
|
|
|
|
|
|
|
$this->assertEquals(true, $GLOBALS['ORDER_DELETE_USER_ADMIN']);
|
2019-02-28 14:45:39 +03:00
|
|
|
}
|
|
|
|
|
2023-04-11 11:57:06 +03:00
|
|
|
/**
|
|
|
|
* @runInSeparateProcess
|
|
|
|
* @preserveGlobalState disabled
|
|
|
|
*/
|
|
|
|
public function testSetBasket(): void
|
|
|
|
{
|
|
|
|
$arBasket = $this->getBasket();
|
|
|
|
$crmBasket = $this->getCrmCart();
|
|
|
|
|
|
|
|
$actionsMock = Mockery::mock('alias:' . RCrmActions::class);
|
|
|
|
|
|
|
|
$actionsMock->shouldReceive('apiMethod')->withAnyArgs()->andReturn($crmBasket, ['success' => true]);
|
|
|
|
|
|
|
|
$result = RetailCrmCart::handlerCart($arBasket);
|
|
|
|
|
|
|
|
self::assertTrue($result['success']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @runInSeparateProcess
|
|
|
|
* @preserveGlobalState disabled
|
|
|
|
*/
|
|
|
|
public function testClearBasket(): void
|
|
|
|
{
|
|
|
|
$arBasket = ['LID' => 's1', 'USER_ID' => '1'];
|
|
|
|
$crmBasket = $this->getCrmCart();
|
|
|
|
|
|
|
|
$actionsMock = Mockery::mock('alias:' . RCrmActions::class);
|
|
|
|
|
|
|
|
$actionsMock->shouldReceive('apiMethod')->withAnyArgs()->andReturn($crmBasket, ['success' => true]);
|
|
|
|
|
|
|
|
$result = RetailCrmCart::handlerCart($arBasket);
|
|
|
|
|
|
|
|
self::assertTrue($result['success']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @runInSeparateProcess
|
|
|
|
* @preserveGlobalState disabled
|
|
|
|
*/
|
|
|
|
public function testIgnoreChangeBasket()
|
|
|
|
{
|
|
|
|
$arBasket = ['LID' => 's1', 'USER_ID' => '1'];
|
|
|
|
$crmBasket = [];
|
|
|
|
|
|
|
|
$actionsMock = Mockery::mock('alias:' . RCrmActions::class);
|
|
|
|
|
|
|
|
$actionsMock->shouldReceive('apiMethod')->withAnyArgs()->andReturn($crmBasket);
|
|
|
|
|
|
|
|
$result = RetailCrmCart::handlerCart($arBasket);
|
|
|
|
|
|
|
|
self::assertNull($result);
|
|
|
|
}
|
|
|
|
|
2019-02-28 14:45:39 +03:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function userUpdateDataProvider()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
'history' => true,
|
|
|
|
'emptyData' => false
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'history' => false,
|
|
|
|
'emptyData' => true
|
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function paymentSaveDataProvider()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
'history' => true,
|
|
|
|
'new' => false
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'history' => false,
|
|
|
|
'new' => true
|
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function paymentDeleteDataProvider()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
'history' => true,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'history' => false,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'history' => false,
|
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2023-03-15 11:55:04 +03:00
|
|
|
public function tearDown(): void
|
2019-02-28 14:45:39 +03:00
|
|
|
{
|
|
|
|
parent::tearDown();
|
|
|
|
|
|
|
|
$GLOBALS['RETAIL_CRM_HISTORY'] = false;
|
|
|
|
$GLOBALS['RETAILCRM_ORDER_OLD_EVENT'] = true;
|
|
|
|
$GLOBALS['RETAILCRM_ORDER_DELETE'] = false;
|
|
|
|
}
|
2023-04-11 11:57:06 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getBasket(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'LID' => 's1',
|
|
|
|
'USER_ID' => '1',
|
|
|
|
'BASKET' => [
|
|
|
|
[
|
|
|
|
'QUANTITY' => 2,
|
|
|
|
'PRICE' => 100,
|
|
|
|
'DATE_INSERT' => new DateTime('now'),
|
|
|
|
'DATE_UPDATE' => new DateTime('now'),
|
|
|
|
'PRODUCT_ID' => '10'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'QUANTITY' => 1,
|
|
|
|
'PRICE' => 300,
|
|
|
|
'DATE_INSERT' => new DateTime('now'),
|
|
|
|
'DATE_UPDATE' => new DateTime('now'),
|
|
|
|
'PRODUCT_ID' => '2'
|
|
|
|
],
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getCrmCart(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'cart' => [
|
|
|
|
'items' => 'items'
|
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
2019-02-28 14:45:39 +03:00
|
|
|
}
|