1
0
mirror of synced 2024-11-29 08:46:09 +03:00

Merge pull request #60 from iyzoer/master

Tests
This commit is contained in:
Alex Lushpai 2019-02-28 17:06:37 +03:00 committed by GitHub
commit 8c360e512a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 2133 additions and 126 deletions

4
.env.dist Normal file
View File

@ -0,0 +1,4 @@
DB_BITRIX_HOST=""
DB_BITRIX_LOGIN=""
DB_BITRIX_PASS=""
DB_BITRIX_NAME=""

5
.gitignore vendored
View File

@ -7,5 +7,8 @@
.idea/* .idea/*
/.idea /.idea
/.idea/* /.idea/*
.travis.yml
/vendor/
.env
.phpunit.result.cache

25
.travis.yml Normal file
View File

@ -0,0 +1,25 @@
language: php
sudo: false
php:
- 7.1
- 7.2
env:
global:
- DB_BITRIX_HOST=localhost
- DB_BITRIX_LOGIN=root
- DB_BITRIX_PASS=root
- DB_BITRIX_NAME=bitrix
- BITRIX_PATH=$TRAVIS_BUILD_DIR/bitrix
before_script:
# Change MySQL root password
- echo "USE mysql;\nUPDATE user SET password=PASSWORD('root') WHERE user='root';\nFLUSH PRIVILEGES;\n" | mysql -u root
- composer install
- bash tests/bin/before_script.sh
script:
- make test

5
Makefile Normal file
View File

@ -0,0 +1,5 @@
test: prepare_module
@php -d short_open_tag=On vendor/bin/phpunit -c phpunit.xml.dist
prepare_module:
@composer pre-module-install

View File

@ -1,3 +1,4 @@
[![Build Status](https://img.shields.io/travis/retailcrm/bitrix-module/master.svg?style=flat-square)](https://travis-ci.org/retailcrm/bitrix-module)
[![GitHub release](https://img.shields.io/github/release/retailcrm/bitrix-module.svg?style=flat-square)](https://github.com/retailcrm/bitrix-module/releases) [![GitHub release](https://img.shields.io/github/release/retailcrm/bitrix-module.svg?style=flat-square)](https://github.com/retailcrm/bitrix-module/releases)
[![PHP version](https://img.shields.io/badge/PHP->=5.3-blue.svg?style=flat-square)](https://php.net/) [![PHP version](https://img.shields.io/badge/PHP->=5.3-blue.svg?style=flat-square)](https://php.net/)

21
composer.json Normal file
View File

@ -0,0 +1,21 @@
{
"name": "retailcrm/bitrix-module",
"scripts": {
"pre-module-install": "cp -R intaro.retailcrm $BITRIX_PATH/bitrix/modules"
},
"description": "Integration module for Bitrix & RetailCRM",
"type": "bitrix-module",
"authors": [
{
"name": "RetailDriver LLC",
"email": "integration@retailcrm.ru"
}
],
"require": {
"ext-json": "*"
},
"require-dev": {
"phpunit/phpunit": "^7",
"vlucas/phpdotenv": "^3.3"
}
}

1591
composer.lock generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,17 +1,25 @@
<?php <?php
/**
* Class RetailCrmCollector
*/
class RetailCrmCollector class RetailCrmCollector
{ {
public static $MODULE_ID = 'intaro.retailcrm'; public static $MODULE_ID = 'intaro.retailcrm';
public static $CRM_COLL_KEY = 'coll_key'; public static $CRM_COLL_KEY = 'coll_key';
public static $CRM_COLL = 'collector'; public static $CRM_COLL = 'collector';
/**
* Add Daemon Collector script
*
* @return bool
*/
public static function add() public static function add()
{ {
$keys = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_COLL_KEY, 0)); $keys = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_COLL_KEY, 0));
$collector = COption::GetOptionString(self::$MODULE_ID, self::$CRM_COLL, 0); $collector = COption::GetOptionString(self::$MODULE_ID, self::$CRM_COLL, 0);
if ($collector === 'Y' && !empty($keys[SITE_ID]) && ADMIN_SECTION !== true) { if ($collector === 'Y' && !empty($keys[SITE_ID]) && ADMIN_SECTION !== true) {
global $APPLICATION;
global $USER; global $USER;
$params = array(); $params = array();
@ -24,7 +32,9 @@ class RetailCrmCollector
_rc('create', '" . $keys[SITE_ID] . "', " . json_encode((object) $params) . "); _rc('create', '" . $keys[SITE_ID] . "', " . json_encode((object) $params) . ");
_rc('send', 'pageView'); _rc('send', 'pageView');
</script>"; </script>";
$APPLICATION->AddHeadString($str, true); \Bitrix\Main\Page\Asset::getInstance()->addString($str, true);
return true;
} else { } else {
return false; return false;
} }

View File

@ -1,9 +1,7 @@
<?php <?php
/** /**
* RCrmEvent * Class RetailCrmEvent
*/ */
use \Bitrix\Main\Event;
class RetailCrmEvent class RetailCrmEvent
{ {
protected static $MODULE_ID = 'intaro.retailcrm'; protected static $MODULE_ID = 'intaro.retailcrm';
@ -23,29 +21,28 @@ class RetailCrmEvent
protected static $CRM_SITES_LIST = 'sites_list'; protected static $CRM_SITES_LIST = 'sites_list';
/** /**
* OnAfterUserUpdate * @param $arFields
* *
* @param mixed $arFields - User arFields * @return bool
* * @throws InvalidArgumentException
* @return mixed
*/ */
function OnAfterUserUpdate($arFields) function OnAfterUserUpdate($arFields)
{ {
if (isset($GLOBALS['RETAIL_CRM_HISTORY']) && $GLOBALS['RETAIL_CRM_HISTORY']) { if (isset($GLOBALS['RETAIL_CRM_HISTORY']) && $GLOBALS['RETAIL_CRM_HISTORY']) {
return; return false;
} }
if (!$arFields['RESULT']) { if (!$arFields['RESULT']) {
return; return false;
} }
$api_host = COption::GetOptionString(self::$MODULE_ID, self::$CRM_API_HOST_OPTION, 0); $api_host = COption::GetOptionString(self::$MODULE_ID, self::$CRM_API_HOST_OPTION, 0);
$api_key = COption::GetOptionString(self::$MODULE_ID, self::$CRM_API_KEY_OPTION, 0); $api_key = COption::GetOptionString(self::$MODULE_ID, self::$CRM_API_KEY_OPTION, 0);
$api = new RetailCrm\ApiClient($api_host, $api_key);
$optionsSitesList = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_SITES_LIST, 0)); $optionsSitesList = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_SITES_LIST, 0));
$api = new RetailCrm\ApiClient($api_host, $api_key);
$resultOrder = RetailCrmUser::customerEdit($arFields, $api, $optionsSitesList); $resultOrder = RetailCrmUser::customerEdit($arFields, $api, $optionsSitesList);
if (!$resultOrder) { if (!$resultOrder) {
RCrmActions::eventLog('RetailCrmEvent::OnAfterUserUpdate', 'RetailCrmUser::customerEdit', 'error update customer'); RCrmActions::eventLog('RetailCrmEvent::OnAfterUserUpdate', 'RetailCrmUser::customerEdit', 'error update customer');
} }
@ -84,31 +81,36 @@ class RetailCrmEvent
} }
/** /**
* orderSave * @param $event
*
* @param object $event - Order object
* *
* @return bool * @return bool
* @throws InvalidArgumentException
*/ */
function orderSave($event) function orderSave($event)
{ {
if ($GLOBALS['RETAILCRM_ORDER_OLD_EVENT'] !== false && $GLOBALS['RETAIL_CRM_HISTORY'] !== true && $GLOBALS['RETAILCRM_ORDER_DELETE'] !== true) { if ($GLOBALS['RETAILCRM_ORDER_OLD_EVENT'] === false
&& $GLOBALS['RETAIL_CRM_HISTORY'] === true
&& $GLOBALS['RETAILCRM_ORDER_DELETE'] === true
) {
return false;
}
if (!CModule::IncludeModule('iblock')) { if (!CModule::IncludeModule('iblock')) {
RCrmActions::eventLog('RetailCrmEvent::orderSave', 'iblock', 'module not found'); RCrmActions::eventLog('RetailCrmEvent::orderSave', 'iblock', 'module not found');
return true; return false;
} }
if (!CModule::IncludeModule("sale")) { if (!CModule::IncludeModule("sale")) {
RCrmActions::eventLog('RetailCrmEvent::orderSave', 'sale', 'module not found'); RCrmActions::eventLog('RetailCrmEvent::orderSave', 'sale', 'module not found');
return true; return false;
} }
if (!CModule::IncludeModule("catalog")) { if (!CModule::IncludeModule("catalog")) {
RCrmActions::eventLog('RetailCrmEvent::orderSave', 'catalog', 'module not found'); RCrmActions::eventLog('RetailCrmEvent::orderSave', 'catalog', 'module not found');
return true; return false;
} }
//exists getParameter("ENTITY") //exists getParameter("ENTITY")
@ -119,7 +121,7 @@ class RetailCrmEvent
} else { } else {
RCrmActions::eventLog('RetailCrmEvent::orderSave', 'events', 'event error'); RCrmActions::eventLog('RetailCrmEvent::orderSave', 'events', 'event error');
return true; return false;
} }
$arOrder = RetailCrmOrder::orderObjToArr($obOrder); $arOrder = RetailCrmOrder::orderObjToArr($obOrder);
@ -159,7 +161,7 @@ class RetailCrmEvent
if (array_key_exists($arOrder['LID'], $optionsSitesList) && $optionsSitesList[$arOrder['LID']] !== null) { if (array_key_exists($arOrder['LID'], $optionsSitesList) && $optionsSitesList[$arOrder['LID']] !== null) {
$site = $optionsSitesList[$arOrder['LID']]; $site = $optionsSitesList[$arOrder['LID']];
} else { } else {
return; return false;
} }
} elseif (!$optionsSitesList) { } elseif (!$optionsSitesList) {
$site = null; $site = null;
@ -182,7 +184,7 @@ class RetailCrmEvent
if (!$resultUser) { if (!$resultUser) {
RCrmActions::eventLog('RetailCrmEvent::orderSave', 'RetailCrmUser::customerSend', 'error during creating customer'); RCrmActions::eventLog('RetailCrmEvent::orderSave', 'RetailCrmUser::customerSend', 'error during creating customer');
return true; return false;
} }
} }
@ -191,19 +193,18 @@ class RetailCrmEvent
if (!$resultOrder) { if (!$resultOrder) {
RCrmActions::eventLog('RetailCrmEvent::orderSave', 'RetailCrmOrder::orderSend', 'error during creating order'); RCrmActions::eventLog('RetailCrmEvent::orderSave', 'RetailCrmOrder::orderSend', 'error during creating order');
return true; return false;
}
return true;
} }
return true; return true;
} }
/** /**
* paymentSave * @param \Bitrix\Sale\Payment $event
* *
* @param \Bitrix\Sale\Payment $event - Payment object * @throws InvalidArgumentException
*
* @return bool
*/ */
function paymentSave($event) function paymentSave($event)
{ {
@ -216,7 +217,7 @@ class RetailCrmEvent
|| $apiVersion != 'v5' || $apiVersion != 'v5'
|| $order->isNew() || $order->isNew()
) { ) {
return; return false;
} }
$optionsSitesList = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_SITES_LIST, 0)); $optionsSitesList = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_SITES_LIST, 0));
@ -237,7 +238,7 @@ class RetailCrmEvent
if (array_key_exists($arPayment['LID'], $optionsSitesList) && $optionsSitesList[$arPayment['LID']] !== null) { if (array_key_exists($arPayment['LID'], $optionsSitesList) && $optionsSitesList[$arPayment['LID']] !== null) {
$site = $optionsSitesList[$arPayment['LID']]; $site = $optionsSitesList[$arPayment['LID']];
} else { } else {
return; return false;
} }
} elseif (!$optionsSitesList) { } elseif (!$optionsSitesList) {
$site = null; $site = null;
@ -288,7 +289,7 @@ class RetailCrmEvent
} }
} else { } else {
RCrmActions::eventLog('RetailCrmEvent::paymentSave', 'payments', 'OrderID = ' . $arPayment['ID'] . '. Payment not found.'); RCrmActions::eventLog('RetailCrmEvent::paymentSave', 'payments', 'OrderID = ' . $arPayment['ID'] . '. Payment not found.');
return; return false;
} }
if (!array_key_exists($arPayment['ID'], $paymentsExternalIds)) { if (!array_key_exists($arPayment['ID'], $paymentsExternalIds)) {
@ -299,12 +300,14 @@ class RetailCrmEvent
RCrmActions::apiMethod($api, 'ordersPaymentDelete', __METHOD__, $paymentsExternalIds[$arPayment['ID']]['id']); RCrmActions::apiMethod($api, 'ordersPaymentDelete', __METHOD__, $paymentsExternalIds[$arPayment['ID']]['id']);
RCrmActions::apiMethod($api, 'ordersPaymentCreate', __METHOD__, $paymentToCrm, $site); RCrmActions::apiMethod($api, 'ordersPaymentCreate', __METHOD__, $paymentToCrm, $site);
} }
return true;
} }
/** /**
* paymentDelete * @param \Bitrix\Sale\Payment $event
* *
* @param object $event - Payment object * @throws InvalidArgumentException
*/ */
function paymentDelete($event) function paymentDelete($event)
{ {

18
phpunit.xml.dist Normal file
View File

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
bootstrap="tests/phpunit/bootstrap.php"
backupGlobals="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
verbose="true"
processIsolation="false"
stopOnFailure="false"
>
<testsuites>
<testsuite name="Retailcrm Bitrix module Test Suite">
<directory suffix=".php">tests/phpunit</directory>
</testsuite>
</testsuites>
</phpunit>

21
tests/bin/before_script.sh Executable file
View File

@ -0,0 +1,21 @@
#!/usr/bin/env bash
if [ -d $TRAVIS_BUILD_DIR ]; then
BITRIX_PATH="$TRAVIS_BUILD_DIR/bitrix"
fi
download() {
wget http://download.retailcrm.pro/modules/bitrix/bitrix.tar.gz
mkdir $BITRIX_PATH
tar -xf $TRAVIS_BUILD_DIR/bitrix.tar.gz -C $BITRIX_PATH
rm $TRAVIS_BUILD_DIR/bitrix.tar.gz
}
create_db() {
mysqladmin create $DB_BITRIX_NAME --user="$DB_BITRIX_LOGIN" --password="$DB_BITRIX_PASS"
mysql --user="$DB_BITRIX_LOGIN" --password="$DB_BITRIX_PASS" $DB_BITRIX_NAME < $BITRIX_PATH/dump.sql
}
download
create_db

View File

@ -0,0 +1,33 @@
<?php
/**
* PHP version 7.0
*
* @category Integration
* @author RetailCRM <integration@retailcrm.ru>
* @license http://retailcrm.ru Proprietary
* @link http://retailcrm.ru
* @see http://help.retailcrm.ru
*/
if (file_exists(__DIR__ . '/../../.env')) {
$dotenv = Dotenv\Dotenv::create(__DIR__ . '/../../');
$dotenv->load();
}
if (getenv('TRAVIS_BUILD_DIR')) {
$_SERVER['DOCUMENT_ROOT'] = getenv('TRAVIS_BUILD_DIR') . '/bitrix';
} else {
$_SERVER['DOCUMENT_ROOT'] = getenv('BITRIX_PATH');
}
define('NOT_CHECK_PERMISSIONS', true);
define('NO_AGENT_CHECK', true);
require($_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/main/include/prolog_before.php');
global $USER;
$USER->Authorize(1);
if (!CModule::IncludeModule('intaro.retailcrm')) {
RegisterModule('intaro.retailcrm');
}

View File

@ -0,0 +1,32 @@
<?php
class RetailCrmCollectorTest extends \PHPUnit\Framework\TestCase
{
const TEST_KEY = 'RC-XXXXXXXXXX-X';
public function setUp()
{
parent::setUp();
COption::SetOptionString(
RetailCrmCollector::$MODULE_ID,
RetailCrmCollector::$CRM_COLL_KEY,
serialize([SITE_ID => self::TEST_KEY])
);
COption::SetOptionString(
RetailCrmCollector::$MODULE_ID,
RetailCrmCollector::$CRM_COLL,
'Y'
);
}
public function testAdd()
{
RetailCrmCollector::add();
$strings = \Bitrix\Main\Page\Asset::getInstance()->getStrings();
$this->assertContains(self::TEST_KEY, $strings);
$this->assertContains('customerId', $strings);
}
}

View File

@ -0,0 +1,240 @@
<?php
/**
* Class RetailCrmEventTest
*/
class RetailCrmEventTest extends PHPUnit\Framework\TestCase
{
/**
* @var RetailCrmEvent
*/
private $retailcrmEvent;
public function setUp()
{
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'] = [];
}
$result = $this->retailcrmEvent->OnAfterUserUpdate($arFields);
$this->assertEquals(false, $result);
}
/**
* @param $history
* @param $v5
* @param $new
*
* @throws InvalidArgumentException
* @throws \PHPUnit\Framework\Exception
* @throws \PHPUnit\Framework\ExpectationFailedException
* @throws \PHPUnit\Framework\MockObject\RuntimeException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*
* @dataProvider paymentSaveDataProvider
*/
public function testPaymentSave($history, $v5, $new)
{
$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;
}
if ($v5 === false) {
COption::SetOptionString('intaro.retailcrm', 'api_version', 'v4');
}
$result = $this->retailcrmEvent->paymentSave($event);
$this->assertEquals(false, $result);
}
/**
* @param $history
* @param $v5
*
* @throws InvalidArgumentException
* @throws \PHPUnit\Framework\Exception
* @throws \PHPUnit\Framework\ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*
* @dataProvider paymentDeleteDataProvider
*/
public function testPaymentDelete($history, $v5)
{
$event = $this->createMock(\Bitrix\Sale\Payment::class);
if ($history === true) {
$GLOBALS['RETAIL_CRM_HISTORY'] = true;
}
if ($v5 === false) {
COption::SetOptionString('intaro.retailcrm', 'api_version', 'v4');
}
$result = $this->retailcrmEvent->paymentDelete($event);
$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);
$result = $this->retailcrmEvent->orderSave($event);
$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);
$result = $this->retailcrmEvent->OrderDelete($event);
$this->assertEquals(null, $result);
}
/**
* @throws \PHPUnit\Framework\ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*/
public function testOnUpdateOrder()
{
$arFields = [];
$result = $this->retailcrmEvent->OnUpdateOrder(1, $arFields);
$this->assertEquals(null, $result);
}
/**
* @return array
*/
public function userUpdateDataProvider()
{
return [
[
'history' => true,
'emptyData' => false
],
[
'history' => false,
'emptyData' => true
]
];
}
/**
* @return array
*/
public function paymentSaveDataProvider()
{
return [
[
'history' => true,
'v5' => true,
'new' => false
],
[
'history' => false,
'v5' => false,
'new' => false
],
[
'history' => false,
'v5' => true,
'new' => true
]
];
}
/**
* @return array
*/
public function paymentDeleteDataProvider()
{
return [
[
'history' => true,
'v5' => true
],
[
'history' => false,
'v5' => false
],
[
'history' => false,
'v5' => true
]
];
}
public function tearDown()
{
parent::tearDown();
$GLOBALS['RETAIL_CRM_HISTORY'] = false;
$GLOBALS['RETAILCRM_ORDER_OLD_EVENT'] = true;
$GLOBALS['RETAILCRM_ORDER_DELETE'] = false;
}
}