1
0
mirror of synced 2024-11-22 13:06:07 +03:00

Tests, travis

Src directory
This commit is contained in:
Akolzin Dmitry 2018-05-04 16:25:15 +03:00
parent 3b6430cf7d
commit 0e40ad1b5f
55 changed files with 753 additions and 85 deletions

26
.travis.yml Normal file
View File

@ -0,0 +1,26 @@
language: php
sudo: false
env:
global:
- DB_USER=root
- DB_HOST=localhost
- DB_NAME=magento2_test
- ADMIN_FIRSTNAME=admin_firstname
- ADMIN_LASTNAME=admin_lastname
- ADMIN_EMAIL=example@email.com
- ADMIN_USER=admin
- ADMIN_PASS=admin123
matrix:
include:
- php: 7.0
env: BRANCH=2.2-develop
before_script:
- bash bin/install.sh
script:
- cd ../magento2
- php vendor/phpunit/phpunit/phpunit -c dev/tests/unit/phpunit.xml.dist app/code/Retailcrm/Retailcrm/Test/Unit/

53
bin/install.sh Normal file
View File

@ -0,0 +1,53 @@
#!/usr/bin/env bash
if [ -z $TRAVIS_BUILD_DIR ]; then
exit 0;
fi
MAGE_ROOT=$TRAVIS_BUILD_DIR/../magento2
create_db() {
mysqladmin create magento_test --user="$DB_USER" --password="$DB_PASS"
}
magento_clone() {
cd ..
git clone https://github.com/magento/magento2
cd magento2
git checkout $BRANCH
composer install
composer require retailcrm/api-client-php
}
magento_install() {
cd $MAGE_ROOT
php bin/magento setup:install \
--db-host="$DB_HOST" \
--db-name="$DB_NAME" \
--db-user="$DB_USER" \
--db-password="$DB_PASS" \
--admin-firstname="$ADMIN_FIRSTNAME" \
--admin-lastname="$ADMIN_LASTNAME" \
--admin-email="$ADMIN_EMAIL" \
--admin-user="$ADMIN_USER" \
--admin-password="$ADMIN_PASS" \
--language="en_US" \
--currency="USD" \
--timezone="Europe/Moscow"
}
module_install() {
cd $MAGE_ROOT
mkdir -p app/code/Retailcrm/Retailcrm
cp -R $TRAVIS_BUILD_DIR/src/* app/code/Retailcrm/Retailcrm
php bin/magento module:enable Retailcrm_Retailcrm
php bin/magento setup:upgrade
php bin/magento setup:di:compile
}
create_db
magento_clone
magento_install
module_install

View File

@ -45,14 +45,14 @@ class Payment extends \Magento\Config\Block\System\Config\Form\Field
return $htmlError; return $htmlError;
} }
foreach (array_keys($activePaymentMethods) as $k => $payment) { foreach ($activePaymentMethods as $code => $payment) {
$html .= '<table id="' . $element->getId() . '_table">'; $html .= '<table id="' . $element->getId() . '_table">';
$html .= '<tr id="row_retailcrm_payment_'.$payment.'">'; $html .= '<tr id="row_retailcrm_payment_' . $code . '">';
$html .= '<td class="label">'.$payment.'</td>'; $html .= '<td class="label">' . $payment->getTitle() . '</td>';
$html .= '<td>'; $html .= '<td>';
$html .= '<select id="1" name="groups[Payment][fields][' . $payment . '][value]">'; $html .= '<select id="1" name="groups[Payment][fields][' . $code . '][value]">';
$selected = $this->config->getValue('retailcrm/Payment/' . $payment); $selected = $this->config->getValue('retailcrm/Payment/' . $code);
foreach ($paymentTypes as $k => $value) { foreach ($paymentTypes as $k => $value) {
if (!empty($selected) && $selected == $value['code']) { if (!empty($selected) && $selected == $value['code']) {

View File

@ -45,14 +45,14 @@ class Shipping extends \Magento\Config\Block\System\Config\Form\Field
return $htmlError; return $htmlError;
} }
foreach (array_keys($deliveryMethods) as $k => $delivery) { foreach ($deliveryMethods as $code => $delivery) {
$html .= '<table id="' . $element->getId() . '_table">'; $html .= '<table id="' . $element->getId() . '_table">';
$html .= '<tr id="row_retailcrm_shipping_'.$delivery.'">'; $html .= '<tr id="row_retailcrm_shipping_'. $code . '">';
$html .= '<td class="label">'.$delivery.'</td>'; $html .= '<td class="label">' . $delivery->getConfigData('title') . '</td>';
$html .= '<td>'; $html .= '<td>';
$html .= '<select id="1" name="groups[Shipping][fields]['.$delivery.'][value]">'; $html .= '<select id="1" name="groups[Shipping][fields][' . $code . '][value]">';
$selected = $this->config->getValue('retailcrm/Shipping/'.$delivery); $selected = $this->config->getValue('retailcrm/Shipping/' . $code);
foreach ($deliveryTypes as $k => $value) { foreach ($deliveryTypes as $k => $value) {
if (!empty($selected) && $selected == $value['code']) { if (!empty($selected) && $selected == $value['code']) {

View File

@ -8,6 +8,7 @@ class Customer implements \Magento\Framework\Event\ObserverInterface
{ {
private $api; private $api;
private $registry; private $registry;
private $customer;
public function __construct( public function __construct(
\Magento\Framework\Registry $registry, \Magento\Framework\Registry $registry,
@ -15,6 +16,7 @@ class Customer implements \Magento\Framework\Event\ObserverInterface
) { ) {
$this->api = $api; $this->api = $api;
$this->registry = $registry; $this->registry = $registry;
$this->customer = [];
} }
public function execute(\Magento\Framework\Event\Observer $observer) public function execute(\Magento\Framework\Event\Observer $observer)
@ -27,7 +29,7 @@ class Customer implements \Magento\Framework\Event\ObserverInterface
$data = $observer->getEvent()->getCustomer(); $data = $observer->getEvent()->getCustomer();
$customer = [ $this->customer = [
'externalId' => $data->getId(), 'externalId' => $data->getId(),
'email' => $data->getEmail(), 'email' => $data->getEmail(),
'firstName' => $data->getFirstname(), 'firstName' => $data->getFirstname(),
@ -36,14 +38,22 @@ class Customer implements \Magento\Framework\Event\ObserverInterface
'createdAt' => date('Y-m-d H:i:s', strtotime($data->getCreatedAt())) 'createdAt' => date('Y-m-d H:i:s', strtotime($data->getCreatedAt()))
]; ];
$response = $this->api->customersEdit($customer); $response = $this->api->customersEdit($this->customer);
if ($response === false) { if ($response === false) {
return; return;
} }
if (!$response->isSuccessful() && $response->errorMsg == $this->api->getErrorText('errorNotFound')) { if (!$response->isSuccessful() && $response->errorMsg == $this->api->getErrorText('errorNotFound')) {
$this->api->customersCreate($customer); $this->api->customersCreate($this->customer);
} }
} }
/**
* @return array
*/
public function getCustomer()
{
return $this->customer;
}
} }

View File

@ -12,6 +12,7 @@ class OrderCreate implements \Magento\Framework\Event\ObserverInterface
private $logger; private $logger;
private $registry; private $registry;
private $product; private $product;
private $order;
/** /**
* Constructor * Constructor
@ -32,6 +33,7 @@ class OrderCreate implements \Magento\Framework\Event\ObserverInterface
$this->registry = $registry; $this->registry = $registry;
$this->product = $product; $this->product = $product;
$this->api = $api; $this->api = $api;
$this->order = [];
} }
/** /**
@ -39,7 +41,7 @@ class OrderCreate implements \Magento\Framework\Event\ObserverInterface
* *
* @param Observer $observer * @param Observer $observer
* *
* @return $this * @return null
*/ */
public function execute(Observer $observer) public function execute(Observer $observer)
{ {
@ -59,7 +61,7 @@ class OrderCreate implements \Magento\Framework\Event\ObserverInterface
$shippingCode = $this->getShippingCode($order->getShippingMethod()); $shippingCode = $this->getShippingCode($order->getShippingMethod());
$preparedOrder = [ $this->order = [
'site' => $order->getStore()->getCode(), 'site' => $order->getStore()->getCode(),
'externalId' => $order->getId(), 'externalId' => $order->getId(),
'number' => $order->getRealOrderId(), 'number' => $order->getRealOrderId(),
@ -101,16 +103,16 @@ class OrderCreate implements \Magento\Framework\Event\ObserverInterface
]; ];
if ($addressObj->getData('country_id')) { if ($addressObj->getData('country_id')) {
$preparedOrder['countryIso'] = $addressObj->getData('country_id'); $this->order['countryIso'] = $addressObj->getData('country_id');
} }
if ($this->api->getVersion() == 'v4') { if ($this->api->getVersion() == 'v4') {
$preparedOrder['paymentType'] = $this->config->getValue( $this->order['paymentType'] = $this->config->getValue(
'retailcrm/Payment/' . $order->getPayment()->getMethodInstance()->getCode() 'retailcrm/Payment/' . $order->getPayment()->getMethodInstance()->getCode()
); );
$preparedOrder['discount'] = abs($order->getDiscountAmount()); $this->order['discount'] = abs($order->getDiscountAmount());
} elseif ($this->api->getVersion() == 'v5') { } elseif ($this->api->getVersion() == 'v5') {
$preparedOrder['discountManualAmount'] = abs($order->getDiscountAmount()); $this->order['discountManualAmount'] = abs($order->getDiscountAmount());
$payment = [ $payment = [
'type' => $this->config->getValue( 'type' => $this->config->getValue(
@ -126,54 +128,50 @@ class OrderCreate implements \Magento\Framework\Event\ObserverInterface
$payment['status'] = 'paid'; $payment['status'] = 'paid';
} }
$preparedOrder['payments'][] = $payment; $this->order['payments'][] = $payment;
} }
if (trim($preparedOrder['delivery']['code']) == '') { if (trim($this->order['delivery']['code']) == '') {
unset($preparedOrder['delivery']['code']); unset($this->order['delivery']['code']);
} }
if (isset($preparedOrder['paymentType']) && trim($preparedOrder['paymentType']) == '') { if (isset($this->order['paymentType']) && trim($this->order['paymentType']) == '') {
unset($preparedOrder['paymentType']); unset($this->order['paymentType']);
} }
if (trim($preparedOrder['status']) == '') { if (trim($this->order['status']) == '') {
unset($preparedOrder['status']); unset($this->order['status']);
} }
$this->setCustomer( $this->setCustomer(
$order, $order,
$addressObj, $addressObj
$preparedOrder
); );
\Retailcrm\Retailcrm\Helper\Data::filterRecursive($preparedOrder); \Retailcrm\Retailcrm\Helper\Data::filterRecursive($this->order);
$this->logger->writeDump($preparedOrder, 'CreateOrder'); $this->logger->writeDump($this->order, 'CreateOrder');
$this->api->ordersCreate($preparedOrder); $this->api->ordersCreate($this->order);
return $this;
} }
/** /**
* @param $order * @param $order
* @param $addressObj * @param $addressObj
* @param $preparedOrder
*/ */
private function setCustomer($order, $addressObj, &$preparedOrder) private function setCustomer($order, $addressObj)
{ {
if ($order->getCustomerIsGuest() == 1) { if ($order->getCustomerIsGuest() == 1) {
$customer = $this->getCustomerByEmail($order->getCustomerEmail()); $customer = $this->getCustomerByEmail($order->getCustomerEmail());
if ($customer !== false) { if ($customer !== false) {
$preparedOrder['customer']['id'] = $customer['id']; $this->order['customer']['id'] = $customer['id'];
} }
} }
if ($order->getCustomerIsGuest() == 0) { if ($order->getCustomerIsGuest() == 0) {
if ($this->existsInCrm($order->getCustomerId(), 'customersGet')) { if ($this->existsInCrm($order->getCustomerId(), 'customersGet')) {
$preparedOrder['customer']['externalId'] = $order->getCustomerId(); $this->order['customer']['externalId'] = $order->getCustomerId();
} else { } else {
$preparedCustomer = [ $preparedCustomer = [
'externalId' => $order->getCustomerId(), 'externalId' => $order->getCustomerId(),
@ -189,7 +187,7 @@ class OrderCreate implements \Magento\Framework\Event\ObserverInterface
} }
if ($this->api->customersCreate($preparedCustomer)) { if ($this->api->customersCreate($preparedCustomer)) {
$preparedOrder['customer']['externalId'] = $order->getCustomerId(); $this->order['customer']['externalId'] = $order->getCustomerId();
} }
} }
} }
@ -296,4 +294,12 @@ class OrderCreate implements \Magento\Framework\Event\ObserverInterface
return false; return false;
} }
/**
* @return array
*/
public function getOrder()
{
return $this->order;
}
} }

View File

@ -10,6 +10,7 @@ class OrderUpdate implements \Magento\Framework\Event\ObserverInterface
private $api; private $api;
private $config; private $config;
private $registry; private $registry;
private $order;
/** /**
* Constructor * Constructor
@ -26,6 +27,7 @@ class OrderUpdate implements \Magento\Framework\Event\ObserverInterface
$this->config = $config; $this->config = $config;
$this->registry = $registry; $this->registry = $registry;
$this->api = $api; $this->api = $api;
$this->order = [];
} }
/** /**
@ -46,14 +48,14 @@ class OrderUpdate implements \Magento\Framework\Event\ObserverInterface
$order = $observer->getEvent()->getOrder(); $order = $observer->getEvent()->getOrder();
if ($order) { if ($order) {
$preparedOrder = [ $this->order = [
'externalId' => $order->getId(), 'externalId' => $order->getId(),
'status' => $this->config->getValue('retailcrm/Status/' . $order->getStatus()) 'status' => $this->config->getValue('retailcrm/Status/' . $order->getStatus())
]; ];
if ($order->getBaseTotalDue() == 0) { if ($order->getBaseTotalDue() == 0) {
if ($this->api->getVersion() == 'v4') { if ($this->api->getVersion() == 'v4') {
$preparedOrder['paymentStatus'] = 'paid'; $this->order['paymentStatus'] = 'paid';
} elseif ($this->api->getVersion() == 'v5') { } elseif ($this->api->getVersion() == 'v5') {
$payment = [ $payment = [
'externalId' => $order->getPayment()->getId(), 'externalId' => $order->getPayment()->getId(),
@ -64,8 +66,16 @@ class OrderUpdate implements \Magento\Framework\Event\ObserverInterface
} }
} }
\Retailcrm\Retailcrm\Helper\Data::filterRecursive($preparedOrder); \Retailcrm\Retailcrm\Helper\Data::filterRecursive($this->order);
$this->api->ordersEdit($preparedOrder); $this->api->ordersEdit($this->order);
} }
} }
/**
* @return mixed
*/
public function getOrder()
{
return $this->order;
}
} }

1
src/README.md Normal file
View File

@ -0,0 +1 @@
README

View File

@ -0,0 +1,160 @@
<?php
namespace Retailcrm\Retailcrm\Test\Unit\Block\Adminhtml\System\Config\Form\Field;
class PaymentTest extends \PHPUnit\Framework\TestCase
{
private $objectManager;
private $testElementId = 'test_element_id';
public function setUp()
{
$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
}
/**
* @param boolean $isSuccessful
* @param boolean $isConfigured
* @dataProvider dataProvider
*/
public function testRender($isSuccessful, $isConfigured)
{
// element mock
$elementMock = $this->getMockBuilder(\Magento\Framework\Data\Form\Element\AbstractElement::class)
->disableOriginalConstructor()
->setMethods(['getId'])
->getMock();
$elementMock->expects($this->any())
->method('getId')
->willReturn($this->testElementId);
// response
$response = $this->objectManager->getObject(
\RetailCrm\Response\ApiResponse::class,
[
'statusCode' => $isSuccessful ? 200 : 404,
'responseBody' => json_encode($this->getTestResponse())
]
);
// api client mock
$client = $this->getMockBuilder(\Retailcrm\Retailcrm\Helper\Proxy::class)
->disableOriginalConstructor()
->setMethods(
[
'isConfigured',
'paymentTypesList'
]
)
->getMock();
$client->expects($this->any())
->method('isConfigured')
->willReturn($isConfigured);
$client->expects($this->any())
->method('paymentTypesList')
->willReturn($response);
// payment config mock
$paymentConfig = $this->getMockBuilder(\Magento\Payment\Model\Config::class)
->disableOriginalConstructor()
->getMock();
$paymentConfig->expects($this->any())
->method('getActiveMethods')
->willReturn($this->getTestActiveMethods());
$payment = $this->objectManager->getObject(
\Retailcrm\Retailcrm\Block\Adminhtml\System\Config\Form\Field\Payment::class,
[
'client' => $client,
'paymentConfig' => $paymentConfig
]
);
$html = $payment->render($elementMock);
if (!$isConfigured || !$isSuccessful) {
$this->assertEquals($html, $this->getHtml(true));
}
if ($isConfigured && $isSuccessful) {
$this->assertEquals($html, $this->getHtml(false));
}
}
private function getTestActiveMethods()
{
$payment = $this->getMockBuilder(\Magento\Payment\Model\MethodInterface::class)
->disableOriginalConstructor()
->getMockForAbstractClass();
$payment->expects($this->any())
->method('getTitle')
->willReturn('Test Payment');
return ['test_payment' => $payment];
}
private function getTestResponse()
{
return [
'success' => true,
'paymentTypes' => [
[
'code' => 'payment',
'name' => 'Test payment type'
]
]
];
}
private function getHtml($error)
{
$html = '';
$paymentTypes = $this->getTestResponse();
foreach ($this->getTestActiveMethods() as $code => $paymentType) {
$html .= '<table id="' . $this->testElementId . '_table">';
$html .= '<tr id="row_retailcrm_payment_' . $code . '">';
$html .= '<td class="label">' . $paymentType->getTitle() . '</td>';
$html .= '<td>';
$html .= '<select id="1" name="groups[Payment][fields][' . $code . '][value]">';
foreach ($paymentTypes['paymentTypes'] as $k => $value) {
$html .= '<option value="' . $value['code'] . '"> ' . $value['name'] . '</option>';
}
$html .= '</select>';
$html .= '</td>';
$html .= '</tr>';
$html .= '</table>';
}
if ($error) {
return '<div style="margin-left: 15px;"><b><i>Please check your API Url & API Key</i></b></div>';
}
return $html;
}
public function dataProvider()
{
return [
[
'is_successful' => true,
'is_configured' => true
],
[
'is_successful' => false,
'is_configured' => false
],
[
'is_successful' => true,
'is_configured' => false
],
[
'is_successful' => false,
'is_configured' => true
]
];
}
}

View File

@ -0,0 +1,161 @@
<?php
namespace Retailcrm\Retailcrm\Test\Unit\Block\Adminhtml\System\Config\Form\Field;
class ShippingTest extends \PHPUnit\Framework\TestCase
{
private $objectManager;
private $testElementId = 'test_element_id';
public function setUp()
{
$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
}
/**
* @param $isSuccessful
* @param $isConfigured
* @dataProvider dataProvider
*/
public function testRender($isSuccessful, $isConfigured)
{
// element mock
$elementMock = $this->getMockBuilder(\Magento\Framework\Data\Form\Element\AbstractElement::class)
->disableOriginalConstructor()
->setMethods(['getId'])
->getMock();
$elementMock->expects($this->any())
->method('getId')
->willReturn($this->testElementId);
// response
$response = $this->objectManager->getObject(
\RetailCrm\Response\ApiResponse::class,
[
'statusCode' => $isSuccessful ? 200 : 404,
'responseBody' => json_encode($this->getTestResponse())
]
);
// api client mock
$client = $this->getMockBuilder(\Retailcrm\Retailcrm\Helper\Proxy::class)
->disableOriginalConstructor()
->setMethods(
[
'isConfigured',
'deliveryTypesList'
]
)
->getMock();
$client->expects($this->any())
->method('isConfigured')
->willReturn($isConfigured);
$client->expects($this->any())
->method('deliveryTypesList')
->willReturn($response);
// shipping config mock
$shippingConfig = $this->getMockBuilder(\Magento\Shipping\Model\Config::class)
->disableOriginalConstructor()
->getMock();
$shippingConfig->expects($this->any())
->method('getActiveCarriers')
->willReturn($this->getTestActiveCarriers());
$shipping = $this->objectManager->getObject(
\Retailcrm\Retailcrm\Block\Adminhtml\System\Config\Form\Field\Shipping::class,
[
'client' => $client,
'shippingConfig' => $shippingConfig
]
);
$html = $shipping->render($elementMock);
if (!$isConfigured || !$isSuccessful) {
$this->assertEquals($html, $this->getHtml(true));
}
if ($isConfigured && $isSuccessful) {
$this->assertEquals($html, $this->getHtml(false));
}
}
private function getTestActiveCarriers()
{
$shipping = $this->getMockBuilder(\Magento\Shipping\Model\Carrier\AbstractCarrierInterface::class)
->disableOriginalConstructor()
->getMockForAbstractClass();
$shipping->expects($this->any())
->method('getConfigData')
->with('title')
->willReturn('Test Shipping');
return ['test_shipping' => $shipping];
}
private function getTestResponse()
{
return [
'success' => true,
'deliveryTypes' => [
[
'code' => 'delivery',
'name' => 'Test delivery type'
]
]
];
}
private function getHtml($error)
{
$html = '';
$deliveryTypes = $this->getTestResponse();
foreach ($this->getTestActiveCarriers() as $code => $deliveryType) {
$html .= '<table id="' . $this->testElementId . '_table">';
$html .= '<tr id="row_retailcrm_shipping_' . $code . '">';
$html .= '<td class="label">' . $deliveryType->getConfigData('title') . '</td>';
$html .= '<td>';
$html .= '<select id="1" name="groups[Shipping][fields][' . $code . '][value]">';
foreach ($deliveryTypes['deliveryTypes'] as $k => $value) {
$html .= '<option value="' . $value['code'] . '"> ' . $value['name'] . '</option>';
}
$html .= '</select>';
$html .= '</td>';
$html .= '</tr>';
$html .= '</table>';
}
if ($error) {
return '<div style="margin-left: 15px;"><b><i>Please check your API Url & API Key</i></b></div>';
}
return $html;
}
public function dataProvider()
{
return [
[
'is_successful' => true,
'is_configured' => true
],
[
'is_successful' => false,
'is_configured' => false
],
[
'is_successful' => true,
'is_configured' => false
],
[
'is_successful' => false,
'is_configured' => true
]
];
}
}

View File

@ -0,0 +1,159 @@
<?php
namespace Retailcrm\Retailcrm\Test\Unit\Block\Adminhtml\System\Config\Form\Field;
class StatusTest extends \PHPUnit\Framework\TestCase
{
private $objectManager;
private $testElementId = 'test_element_id';
public function setUp()
{
$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
}
/**
* @param boolean $isSuccessful
* @param boolean $isConfigured
* @dataProvider dataProvider
*/
public function testRender($isSuccessful, $isConfigured)
{
// element mock
$elementMock = $this->getMockBuilder(\Magento\Framework\Data\Form\Element\AbstractElement::class)
->disableOriginalConstructor()
->setMethods(['getId'])
->getMock();
$elementMock->expects($this->any())
->method('getId')
->willReturn($this->testElementId);
// response
$response = $this->objectManager->getObject(
\RetailCrm\Response\ApiResponse::class,
[
'statusCode' => $isSuccessful ? 200 : 404,
'responseBody' => json_encode($this->getTestResponse())
]
);
// api client mock
$client = $this->getMockBuilder(\Retailcrm\Retailcrm\Helper\Proxy::class)
->disableOriginalConstructor()
->setMethods(
[
'isConfigured',
'statusesList'
]
)
->getMock();
$client->expects($this->any())
->method('isConfigured')
->willReturn($isConfigured);
$client->expects($this->any())
->method('statusesList')
->willReturn($response);
// status collection mock
$statusCollection = $this->getMockBuilder(\Magento\Sales\Model\ResourceModel\Order\Status\Collection::class)
->disableOriginalConstructor()
->getMock();
$statusCollection->expects($this->any())
->method('toOptionArray')
->willReturn($this->getTestStatuses());
$status = $this->objectManager->getObject(
\Retailcrm\Retailcrm\Block\Adminhtml\System\Config\Form\Field\Status::class,
[
'client' => $client,
'statusCollection' => $statusCollection
]
);
$html = $status->render($elementMock);
if (!$isConfigured || !$isSuccessful) {
$this->assertEquals($html, $this->getHtml(true));
}
if ($isConfigured && $isSuccessful) {
$this->assertEquals($html, $this->getHtml(false));
}
}
private function getTestStatuses()
{
$status = [
'label' => 'Test Status',
'value' => 'Test status'
];
return ['test_status' => $status];
}
private function getTestResponse()
{
return [
'success' => true,
'statuses' => [
[
'code' => 'status',
'name' => 'Test status'
]
]
];
}
private function getHtml($error)
{
$html = '';
$statuses = $this->getTestResponse();
foreach ($this->getTestStatuses() as $code => $status) {
$html .= '<table id="' . $this->testElementId . '_table">';
$html .= '<tr id="row_retailcrm_status_' . $status['label'] . '">';
$html .= '<td class="label">' . $status['label'] . '</td>';
$html .= '<td>';
$html .= '<select name="groups[Status][fields][' . $status['value'] . '][value]">';
$html .= '<option value=""> Select status </option>';
foreach ($statuses['statuses'] as $k => $value) {
$html .= '<option value="' . $value['code'] . '"> ' . $value['name'] . '</option>';
}
$html .= '</select>';
$html .= '</td>';
$html .= '</tr>';
$html .= '</table>';
}
if ($error) {
return '<div style="margin-left: 15px;"><b><i>Please check your API Url & API Key</i></b></div>';
}
return $html;
}
public function dataProvider()
{
return [
[
'is_successful' => true,
'is_configured' => true
],
[
'is_successful' => false,
'is_configured' => false
],
[
'is_successful' => true,
'is_configured' => false
],
[
'is_successful' => false,
'is_configured' => true
]
];
}
}

View File

@ -10,6 +10,7 @@ class CustomerTest extends \PHPUnit\Framework\TestCase
private $mockObserver; private $mockObserver;
private $mockEvent; private $mockEvent;
private $mockCustomer; private $mockCustomer;
private $unit;
public function setUp() public function setUp()
{ {
@ -17,7 +18,8 @@ class CustomerTest extends \PHPUnit\Framework\TestCase
->disableOriginalConstructor() ->disableOriginalConstructor()
->setMethods([ ->setMethods([
'customersEdit', 'customersEdit',
'customersCreate' 'customersCreate',
'isConfigured'
]) ])
->getMock(); ->getMock();
@ -54,19 +56,16 @@ class CustomerTest extends \PHPUnit\Framework\TestCase
$this->registry, $this->registry,
$this->mockApi $this->mockApi
); );
$reflection = new \ReflectionClass($this->unit);
$reflection_property = $reflection->getProperty('api');
$reflection_property->setAccessible(true);
$reflection_property->setValue($this->unit, $this->mockApi);
} }
/** /**
* @param boolean $isSuccessful * @param boolean $isSuccessful
* @param boolean $isConfigured
* @dataProvider dataProviderCustomer * @dataProvider dataProviderCustomer
*/ */
public function testExecute( public function testExecute(
$isSuccessful $isSuccessful,
$isConfigured
) { ) {
$testData = $this->getAfterSaveCustomerTestData(); $testData = $this->getAfterSaveCustomerTestData();
@ -86,38 +85,54 @@ class CustomerTest extends \PHPUnit\Framework\TestCase
->method('customersCreate') ->method('customersCreate')
->willReturn($this->mockResponse); ->willReturn($this->mockResponse);
$this->mockApi->expects($this->any())
->method('isConfigured')
->willReturn($isConfigured);
// mock Customer // mock Customer
$this->mockCustomer->expects($this->once()) $this->mockCustomer->expects($this->any())
->method('getId') ->method('getId')
->willReturn($testData['id']); ->willReturn($testData['id']);
$this->mockCustomer->expects($this->once()) $this->mockCustomer->expects($this->any())
->method('getEmail') ->method('getEmail')
->willReturn($testData['email']); ->willReturn($testData['email']);
$this->mockCustomer->expects($this->once()) $this->mockCustomer->expects($this->any())
->method('getFirstname') ->method('getFirstname')
->willReturn($testData['firstname']); ->willReturn($testData['firstname']);
$this->mockCustomer->expects($this->once()) $this->mockCustomer->expects($this->any())
->method('getMiddlename') ->method('getMiddlename')
->willReturn($testData['middlename']); ->willReturn($testData['middlename']);
$this->mockCustomer->expects($this->once()) $this->mockCustomer->expects($this->any())
->method('getLastname') ->method('getLastname')
->willReturn($testData['lastname']); ->willReturn($testData['lastname']);
// mock Event // mock Event
$this->mockEvent->expects($this->once()) $this->mockEvent->expects($this->any())
->method('getCustomer') ->method('getCustomer')
->willReturn($this->mockCustomer); ->willReturn($this->mockCustomer);
// mock Observer // mock Observer
$this->mockObserver->expects($this->once()) $this->mockObserver->expects($this->any())
->method('getEvent') ->method('getEvent')
->willReturn($this->mockEvent); ->willReturn($this->mockEvent);
$this->unit->execute($this->mockObserver); $this->unit->execute($this->mockObserver);
if ($isConfigured) {
$this->assertNotEmpty($this->unit->getCustomer());
$this->assertArrayHasKey('externalId', $this->unit->getCustomer());
$this->assertArrayHasKey('email', $this->unit->getCustomer());
$this->assertArrayHasKey('firstName', $this->unit->getCustomer());
$this->assertArrayHasKey('lastName', $this->unit->getCustomer());
$this->assertArrayHasKey('patronymic', $this->unit->getCustomer());
$this->assertArrayHasKey('createdAt', $this->unit->getCustomer());
} else {
$this->assertEmpty($this->unit->getCustomer());
}
} }
/** /**
@ -140,10 +155,20 @@ class CustomerTest extends \PHPUnit\Framework\TestCase
{ {
return [ return [
[ [
'is_successful' => true 'is_successful' => true,
'is_configured' => true
], ],
[ [
'is_successful' => false 'is_successful' => false,
'is_configured' => false
],
[
'is_successful' => false,
'is_configured' => true
],
[
'is_successful' => true,
'is_configured' => false
] ]
]; ];
} }

View File

@ -20,6 +20,7 @@ class OrderCreateTest extends \PHPUnit\Framework\TestCase
private $mockResponse; private $mockResponse;
private $mockPayment; private $mockPayment;
private $mockPaymentMethod; private $mockPaymentMethod;
private $logger;
public function setUp() public function setUp()
{ {
@ -31,7 +32,8 @@ class OrderCreateTest extends \PHPUnit\Framework\TestCase
'customersGet', 'customersGet',
'customersCreate', 'customersCreate',
'customersList', 'customersList',
'getVersion' 'getVersion',
'isConfigured'
]) ])
->getMock(); ->getMock();
@ -131,13 +133,15 @@ class OrderCreateTest extends \PHPUnit\Framework\TestCase
* @param string $errorMsg * @param string $errorMsg
* @param int $customerIsGuest * @param int $customerIsGuest
* @param string $apiVersion * @param string $apiVersion
* @param boolean $isConfigured
* @dataProvider dataProviderOrderCreate * @dataProvider dataProviderOrderCreate
*/ */
public function testExecute( public function testExecute(
$isSuccessful, $isSuccessful,
$errorMsg, $errorMsg,
$customerIsGuest, $customerIsGuest,
$apiVersion $apiVersion,
$isConfigured
) { ) {
$testData = $this->getAfterSaveOrderTestData(); $testData = $this->getAfterSaveOrderTestData();
@ -173,6 +177,10 @@ class OrderCreateTest extends \PHPUnit\Framework\TestCase
->method('getVersion') ->method('getVersion')
->willReturn($apiVersion); ->willReturn($apiVersion);
$this->mockApi->expects($this->any())
->method('isConfigured')
->willReturn($isConfigured);
// billing address mock set data // billing address mock set data
$this->mockBillingAddress->expects($this->any()) $this->mockBillingAddress->expects($this->any())
->method('getTelephone') ->method('getTelephone')
@ -287,16 +295,39 @@ class OrderCreateTest extends \PHPUnit\Framework\TestCase
->willReturn($this->mockPaymentMethod); ->willReturn($this->mockPaymentMethod);
// mock Event // mock Event
$this->mockEvent->expects($this->once()) $this->mockEvent->expects($this->any())
->method('getOrder') ->method('getOrder')
->willReturn($this->mockOrder); ->willReturn($this->mockOrder);
// mock Observer // mock Observer
$this->mockObserver->expects($this->once()) $this->mockObserver->expects($this->any())
->method('getEvent') ->method('getEvent')
->willReturn($this->mockEvent); ->willReturn($this->mockEvent);
$this->unit->execute($this->mockObserver); $this->unit->execute($this->mockObserver);
if ($isConfigured && !$isSuccessful) {
$this->assertNotEmpty($this->unit->getOrder());
$this->assertArrayHasKey('externalId', $this->unit->getOrder());
$this->assertArrayHasKey('number', $this->unit->getOrder());
$this->assertArrayHasKey('createdAt', $this->unit->getOrder());
$this->assertArrayHasKey('lastName', $this->unit->getOrder());
$this->assertArrayHasKey('firstName', $this->unit->getOrder());
$this->assertArrayHasKey('patronymic', $this->unit->getOrder());
$this->assertArrayHasKey('email', $this->unit->getOrder());
$this->assertArrayHasKey('phone', $this->unit->getOrder());
// $this->assertArrayHasKey('status', $this->unit->getOrder());
$this->assertArrayHasKey('items', $this->unit->getOrder());
$this->assertArrayHasKey('delivery', $this->unit->getOrder());
if ($apiVersion == 'v5') {
$this->assertArrayHasKey('payments', $this->unit->getOrder());
} else {
$this->assertArrayHasKey('paymentType', $this->unit->getOrder());
}
} elseif (!$isConfigured || $isSuccessful) {
$this->assertEmpty($this->unit->getOrder());
}
} }
/** /**
@ -365,49 +396,57 @@ class OrderCreateTest extends \PHPUnit\Framework\TestCase
'is_successful' => true, 'is_successful' => true,
'error_msg' => 'Not found', 'error_msg' => 'Not found',
'customer_is_guest' => 1, 'customer_is_guest' => 1,
'api_version' => 'v4' 'api_version' => 'v4',
'is_configured' => true
], ],
[ [
'is_successful' => true, 'is_successful' => true,
'error_msg' => 'Not found', 'error_msg' => 'Not found',
'customer_is_guest' => 0, 'customer_is_guest' => 0,
'api_version' => 'v4' 'api_version' => 'v4',
'is_configured' => false
], ],
[ [
'is_successful' => false, 'is_successful' => false,
'error_msg' => 'Not found', 'error_msg' => 'Not found',
'customer_is_guest' => 1, 'customer_is_guest' => 1,
'api_version' => 'v4' 'api_version' => 'v4',
'is_configured' => true
], ],
[ [
'is_successful' => false, 'is_successful' => false,
'error_msg' => 'Not found', 'error_msg' => 'Not found',
'customer_is_guest' => 0, 'customer_is_guest' => 0,
'api_version' => 'v4' 'api_version' => 'v4',
'is_configured' => false
], ],
[ [
'is_successful' => true, 'is_successful' => true,
'error_msg' => 'Not found', 'error_msg' => 'Not found',
'customer_is_guest' => 1, 'customer_is_guest' => 1,
'api_version' => 'v5' 'api_version' => 'v5',
'is_configured' => true
], ],
[ [
'is_successful' => true, 'is_successful' => true,
'error_msg' => 'Not found', 'error_msg' => 'Not found',
'customer_is_guest' => 0, 'customer_is_guest' => 0,
'api_version' => 'v5' 'api_version' => 'v5',
'is_configured' => false
], ],
[ [
'is_successful' => false, 'is_successful' => false,
'error_msg' => 'Not found', 'error_msg' => 'Not found',
'customer_is_guest' => 1, 'customer_is_guest' => 1,
'api_version' => 'v5' 'api_version' => 'v5',
'is_configured' => true
], ],
[ [
'is_successful' => false, 'is_successful' => false,
'error_msg' => 'Not found', 'error_msg' => 'Not found',
'customer_is_guest' => 0, 'customer_is_guest' => 0,
'api_version' => 'v5' 'api_version' => 'v5',
'is_configured' => false
] ]
]; ];
} }

View File

@ -21,7 +21,8 @@ class OrderUpdateTest extends \PHPUnit\Framework\TestCase
->setMethods([ ->setMethods([
'ordersEdit', 'ordersEdit',
'ordersPaymentsEdit', 'ordersPaymentsEdit',
'getVersion' 'getVersion',
'isConfigured'
]) ])
->getMock(); ->getMock();
@ -68,11 +69,13 @@ class OrderUpdateTest extends \PHPUnit\Framework\TestCase
/** /**
* @param int $getBaseTotalDue * @param int $getBaseTotalDue
* @param string $apiVersion * @param string $apiVersion
* @param boolean $isConfigured
* @dataProvider dataProviderOrderUpdate * @dataProvider dataProviderOrderUpdate
*/ */
public function testExecute( public function testExecute(
$getBaseTotalDue, $getBaseTotalDue,
$apiVersion $apiVersion,
$isConfigured
) { ) {
$testData = $this->getAfterUpdateOrderTestData(); $testData = $this->getAfterUpdateOrderTestData();
@ -82,15 +85,15 @@ class OrderUpdateTest extends \PHPUnit\Framework\TestCase
->willReturn(1); ->willReturn(1);
// mock Order // mock Order
$this->mockOrder->expects($this->once()) $this->mockOrder->expects($this->any())
->method('getId') ->method('getId')
->willReturn($testData['order.id']); ->willReturn($testData['order.id']);
$this->mockOrder->expects($this->once()) $this->mockOrder->expects($this->any())
->method('getStatus') ->method('getStatus')
->willReturn($testData['order.status']); ->willReturn($testData['order.status']);
$this->mockOrder->expects($this->once()) $this->mockOrder->expects($this->any())
->method('getBaseTotalDue') ->method('getBaseTotalDue')
->willReturn($getBaseTotalDue); ->willReturn($getBaseTotalDue);
@ -103,17 +106,29 @@ class OrderUpdateTest extends \PHPUnit\Framework\TestCase
->method('getVersion') ->method('getVersion')
->willReturn($apiVersion); ->willReturn($apiVersion);
$this->mockApi->expects($this->any())
->method('isConfigured')
->willReturn($isConfigured);
// mock Event // mock Event
$this->mockEvent->expects($this->once()) $this->mockEvent->expects($this->any())
->method('getOrder') ->method('getOrder')
->willReturn($this->mockOrder); ->willReturn($this->mockOrder);
// mock Observer // mock Observer
$this->mockObserver->expects($this->once()) $this->mockObserver->expects($this->any())
->method('getEvent') ->method('getEvent')
->willReturn($this->mockEvent); ->willReturn($this->mockEvent);
$this->unit->execute($this->mockObserver); $this->unit->execute($this->mockObserver);
if ($isConfigured) {
$this->assertNotEmpty($this->unit->getOrder());
$this->assertArrayHasKey('externalId', $this->unit->getOrder());
$this->assertArrayHasKey('status', $this->unit->getOrder());
} else {
$this->assertEmpty($this->unit->getOrder());
}
} }
/** /**
@ -136,19 +151,23 @@ class OrderUpdateTest extends \PHPUnit\Framework\TestCase
return [ return [
[ [
'get_base_total_due' => 0, 'get_base_total_due' => 0,
'api_version' => 'v4' 'api_version' => 'v4',
'is_configured' => false
], ],
[ [
'get_base_total_due' => 1, 'get_base_total_due' => 1,
'api_version' => 'v4' 'api_version' => 'v4',
'is_configured' => true
], ],
[ [
'get_base_total_due' => 0, 'get_base_total_due' => 0,
'api_version' => 'v5' 'api_version' => 'v5',
'is_configured' => true
], ],
[ [
'get_base_total_due' => 1, 'get_base_total_due' => 1,
'api_version' => 'v5' 'api_version' => 'v5',
'is_configured' => false
] ]
]; ];
} }

View File

@ -2,7 +2,6 @@
"name": "retailcrm/retailcrm", "name": "retailcrm/retailcrm",
"description": "Retailcrm", "description": "Retailcrm",
"require": { "require": {
"php": "~5.5.0|~5.6.0|~7.0.0",
"retailcrm/api-client-php": "~5.0" "retailcrm/api-client-php": "~5.0"
}, },
"type": "magento2-module", "type": "magento2-module",

View File

@ -33,8 +33,8 @@
<group id="Misc" translate="label" type="text" sortOrder="20" showInDefault="2" showInWebsite="0" showInStore="0"> <group id="Misc" translate="label" type="text" sortOrder="20" showInDefault="2" showInWebsite="0" showInStore="0">
<label>Misc</label> <label>Misc</label>
<field id="attributes_to_export_into_icml" translate="label" type="multiselect" sortOrder="2" showInDefault="1" showInWebsite="0" showInStore="0"> <field id="attributes_to_export_into_icml" translate="label" type="multiselect" sortOrder="2" showInDefault="1" showInWebsite="0" showInStore="0">
<label>attributes to export into icml</label> <label>Attributes to export into icml</label>
<comment>attributes to export into icml</comment> <comment>Attributes to export into icml</comment>
<source_model>Retailcrm\Retailcrm\Model\Setting\Attribute</source_model> <source_model>Retailcrm\Retailcrm\Model\Setting\Attribute</source_model>
<!-- <!--