diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..5ef1caf --- /dev/null +++ b/.travis.yml @@ -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/ diff --git a/bin/install.sh b/bin/install.sh new file mode 100644 index 0000000..bded0b6 --- /dev/null +++ b/bin/install.sh @@ -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 diff --git a/Api/ConfigManagerInterface.php b/src/Api/ConfigManagerInterface.php similarity index 100% rename from Api/ConfigManagerInterface.php rename to src/Api/ConfigManagerInterface.php diff --git a/ApiClient/ApiClientFactory.php b/src/ApiClient/ApiClientFactory.php similarity index 100% rename from ApiClient/ApiClientFactory.php rename to src/ApiClient/ApiClientFactory.php diff --git a/Block/Adminhtml/System/Config/Button.php b/src/Block/Adminhtml/System/Config/Button.php similarity index 100% rename from Block/Adminhtml/System/Config/Button.php rename to src/Block/Adminhtml/System/Config/Button.php diff --git a/Block/Adminhtml/System/Config/Form/Field/Attributes.php b/src/Block/Adminhtml/System/Config/Form/Field/Attributes.php similarity index 100% rename from Block/Adminhtml/System/Config/Form/Field/Attributes.php rename to src/Block/Adminhtml/System/Config/Form/Field/Attributes.php diff --git a/Block/Adminhtml/System/Config/Form/Field/ListMode.php b/src/Block/Adminhtml/System/Config/Form/Field/ListMode.php similarity index 100% rename from Block/Adminhtml/System/Config/Form/Field/ListMode.php rename to src/Block/Adminhtml/System/Config/Form/Field/ListMode.php diff --git a/Block/Adminhtml/System/Config/Form/Field/Payment.php b/src/Block/Adminhtml/System/Config/Form/Field/Payment.php similarity index 89% rename from Block/Adminhtml/System/Config/Form/Field/Payment.php rename to src/Block/Adminhtml/System/Config/Form/Field/Payment.php index 5f03640..9448ebf 100644 --- a/Block/Adminhtml/System/Config/Form/Field/Payment.php +++ b/src/Block/Adminhtml/System/Config/Form/Field/Payment.php @@ -45,14 +45,14 @@ class Payment extends \Magento\Config\Block\System\Config\Form\Field return $htmlError; } - foreach (array_keys($activePaymentMethods) as $k => $payment) { + foreach ($activePaymentMethods as $code => $payment) { $html .= ''; - $html .= ''; - $html .= ''; + $html .= ''; + $html .= ''; $html .= '
'.$payment.'
' . $payment->getTitle() . ''; - $html .= ''; - $selected = $this->config->getValue('retailcrm/Payment/' . $payment); + $selected = $this->config->getValue('retailcrm/Payment/' . $code); foreach ($paymentTypes as $k => $value) { if (!empty($selected) && $selected == $value['code']) { diff --git a/Block/Adminhtml/System/Config/Form/Field/Shipping.php b/src/Block/Adminhtml/System/Config/Form/Field/Shipping.php similarity index 89% rename from Block/Adminhtml/System/Config/Form/Field/Shipping.php rename to src/Block/Adminhtml/System/Config/Form/Field/Shipping.php index 48780ea..81a74bd 100644 --- a/Block/Adminhtml/System/Config/Form/Field/Shipping.php +++ b/src/Block/Adminhtml/System/Config/Form/Field/Shipping.php @@ -45,14 +45,14 @@ class Shipping extends \Magento\Config\Block\System\Config\Form\Field return $htmlError; } - foreach (array_keys($deliveryMethods) as $k => $delivery) { + foreach ($deliveryMethods as $code => $delivery) { $html .= ''; - $html .= ''; - $html .= ''; + $html .= ''; + $html .= ''; $html .= '
'.$delivery.'
' . $delivery->getConfigData('title') . ''; - $html .= ''; - $selected = $this->config->getValue('retailcrm/Shipping/'.$delivery); + $selected = $this->config->getValue('retailcrm/Shipping/' . $code); foreach ($deliveryTypes as $k => $value) { if (!empty($selected) && $selected == $value['code']) { diff --git a/Block/Adminhtml/System/Config/Form/Field/Status.php b/src/Block/Adminhtml/System/Config/Form/Field/Status.php similarity index 100% rename from Block/Adminhtml/System/Config/Form/Field/Status.php rename to src/Block/Adminhtml/System/Config/Form/Field/Status.php diff --git a/Block/Display.php b/src/Block/Display.php similarity index 100% rename from Block/Display.php rename to src/Block/Display.php diff --git a/Controller/Adminhtml/System/Config/Button.php b/src/Controller/Adminhtml/System/Config/Button.php similarity index 100% rename from Controller/Adminhtml/System/Config/Button.php rename to src/Controller/Adminhtml/System/Config/Button.php diff --git a/Controller/Index/Display.php b/src/Controller/Index/Display.php similarity index 100% rename from Controller/Index/Display.php rename to src/Controller/Index/Display.php diff --git a/Cron/Icml.php b/src/Cron/Icml.php similarity index 100% rename from Cron/Icml.php rename to src/Cron/Icml.php diff --git a/Cron/OrderHistory.php b/src/Cron/OrderHistory.php similarity index 100% rename from Cron/OrderHistory.php rename to src/Cron/OrderHistory.php diff --git a/Helper/Data.php b/src/Helper/Data.php similarity index 100% rename from Helper/Data.php rename to src/Helper/Data.php diff --git a/Helper/Proxy.php b/src/Helper/Proxy.php similarity index 100% rename from Helper/Proxy.php rename to src/Helper/Proxy.php diff --git a/LICENSE b/src/LICENSE similarity index 100% rename from LICENSE rename to src/LICENSE diff --git a/Model/Config/Backend/ApiUrl.php b/src/Model/Config/Backend/ApiUrl.php similarity index 100% rename from Model/Config/Backend/ApiUrl.php rename to src/Model/Config/Backend/ApiUrl.php diff --git a/Model/Config/Backend/ApiVersion.php b/src/Model/Config/Backend/ApiVersion.php similarity index 100% rename from Model/Config/Backend/ApiVersion.php rename to src/Model/Config/Backend/ApiVersion.php diff --git a/Model/History/Exchange.php b/src/Model/History/Exchange.php similarity index 100% rename from Model/History/Exchange.php rename to src/Model/History/Exchange.php diff --git a/Model/Icml/Icml.php b/src/Model/Icml/Icml.php similarity index 100% rename from Model/Icml/Icml.php rename to src/Model/Icml/Icml.php diff --git a/Model/Logger/Logger.php b/src/Model/Logger/Logger.php similarity index 100% rename from Model/Logger/Logger.php rename to src/Model/Logger/Logger.php diff --git a/Model/Observer/Customer.php b/src/Model/Observer/Customer.php similarity index 79% rename from Model/Observer/Customer.php rename to src/Model/Observer/Customer.php index 8fdfa79..8e4465c 100644 --- a/Model/Observer/Customer.php +++ b/src/Model/Observer/Customer.php @@ -8,6 +8,7 @@ class Customer implements \Magento\Framework\Event\ObserverInterface { private $api; private $registry; + private $customer; public function __construct( \Magento\Framework\Registry $registry, @@ -15,6 +16,7 @@ class Customer implements \Magento\Framework\Event\ObserverInterface ) { $this->api = $api; $this->registry = $registry; + $this->customer = []; } public function execute(\Magento\Framework\Event\Observer $observer) @@ -27,7 +29,7 @@ class Customer implements \Magento\Framework\Event\ObserverInterface $data = $observer->getEvent()->getCustomer(); - $customer = [ + $this->customer = [ 'externalId' => $data->getId(), 'email' => $data->getEmail(), '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())) ]; - $response = $this->api->customersEdit($customer); + $response = $this->api->customersEdit($this->customer); if ($response === false) { return; } 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; + } } diff --git a/Model/Observer/OrderCreate.php b/src/Model/Observer/OrderCreate.php similarity index 84% rename from Model/Observer/OrderCreate.php rename to src/Model/Observer/OrderCreate.php index 28805e7..5f1350f 100644 --- a/Model/Observer/OrderCreate.php +++ b/src/Model/Observer/OrderCreate.php @@ -12,6 +12,7 @@ class OrderCreate implements \Magento\Framework\Event\ObserverInterface private $logger; private $registry; private $product; + private $order; /** * Constructor @@ -32,6 +33,7 @@ class OrderCreate implements \Magento\Framework\Event\ObserverInterface $this->registry = $registry; $this->product = $product; $this->api = $api; + $this->order = []; } /** @@ -39,7 +41,7 @@ class OrderCreate implements \Magento\Framework\Event\ObserverInterface * * @param Observer $observer * - * @return $this + * @return null */ public function execute(Observer $observer) { @@ -50,7 +52,7 @@ class OrderCreate implements \Magento\Framework\Event\ObserverInterface } $order = $observer->getEvent()->getOrder(); - + if ($this->existsInCrm($order->getId()) === true) { return; } @@ -59,7 +61,7 @@ class OrderCreate implements \Magento\Framework\Event\ObserverInterface $shippingCode = $this->getShippingCode($order->getShippingMethod()); - $preparedOrder = [ + $this->order = [ 'site' => $order->getStore()->getCode(), 'externalId' => $order->getId(), 'number' => $order->getRealOrderId(), @@ -101,16 +103,16 @@ class OrderCreate implements \Magento\Framework\Event\ObserverInterface ]; if ($addressObj->getData('country_id')) { - $preparedOrder['countryIso'] = $addressObj->getData('country_id'); + $this->order['countryIso'] = $addressObj->getData('country_id'); } if ($this->api->getVersion() == 'v4') { - $preparedOrder['paymentType'] = $this->config->getValue( + $this->order['paymentType'] = $this->config->getValue( 'retailcrm/Payment/' . $order->getPayment()->getMethodInstance()->getCode() ); - $preparedOrder['discount'] = abs($order->getDiscountAmount()); + $this->order['discount'] = abs($order->getDiscountAmount()); } elseif ($this->api->getVersion() == 'v5') { - $preparedOrder['discountManualAmount'] = abs($order->getDiscountAmount()); + $this->order['discountManualAmount'] = abs($order->getDiscountAmount()); $payment = [ 'type' => $this->config->getValue( @@ -126,54 +128,50 @@ class OrderCreate implements \Magento\Framework\Event\ObserverInterface $payment['status'] = 'paid'; } - $preparedOrder['payments'][] = $payment; + $this->order['payments'][] = $payment; } - if (trim($preparedOrder['delivery']['code']) == '') { - unset($preparedOrder['delivery']['code']); + if (trim($this->order['delivery']['code']) == '') { + unset($this->order['delivery']['code']); } - if (isset($preparedOrder['paymentType']) && trim($preparedOrder['paymentType']) == '') { - unset($preparedOrder['paymentType']); + if (isset($this->order['paymentType']) && trim($this->order['paymentType']) == '') { + unset($this->order['paymentType']); } - if (trim($preparedOrder['status']) == '') { - unset($preparedOrder['status']); + if (trim($this->order['status']) == '') { + unset($this->order['status']); } $this->setCustomer( $order, - $addressObj, - $preparedOrder + $addressObj ); - \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); - - return $this; + $this->api->ordersCreate($this->order); } /** * @param $order * @param $addressObj - * @param $preparedOrder */ - private function setCustomer($order, $addressObj, &$preparedOrder) + private function setCustomer($order, $addressObj) { if ($order->getCustomerIsGuest() == 1) { $customer = $this->getCustomerByEmail($order->getCustomerEmail()); if ($customer !== false) { - $preparedOrder['customer']['id'] = $customer['id']; + $this->order['customer']['id'] = $customer['id']; } } if ($order->getCustomerIsGuest() == 0) { if ($this->existsInCrm($order->getCustomerId(), 'customersGet')) { - $preparedOrder['customer']['externalId'] = $order->getCustomerId(); + $this->order['customer']['externalId'] = $order->getCustomerId(); } else { $preparedCustomer = [ 'externalId' => $order->getCustomerId(), @@ -189,7 +187,7 @@ class OrderCreate implements \Magento\Framework\Event\ObserverInterface } 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 array + */ + public function getOrder() + { + return $this->order; + } } diff --git a/Model/Observer/OrderUpdate.php b/src/Model/Observer/OrderUpdate.php similarity index 82% rename from Model/Observer/OrderUpdate.php rename to src/Model/Observer/OrderUpdate.php index 5940130..760ea34 100644 --- a/Model/Observer/OrderUpdate.php +++ b/src/Model/Observer/OrderUpdate.php @@ -10,6 +10,7 @@ class OrderUpdate implements \Magento\Framework\Event\ObserverInterface private $api; private $config; private $registry; + private $order; /** * Constructor @@ -26,6 +27,7 @@ class OrderUpdate implements \Magento\Framework\Event\ObserverInterface $this->config = $config; $this->registry = $registry; $this->api = $api; + $this->order = []; } /** @@ -46,14 +48,14 @@ class OrderUpdate implements \Magento\Framework\Event\ObserverInterface $order = $observer->getEvent()->getOrder(); if ($order) { - $preparedOrder = [ + $this->order = [ 'externalId' => $order->getId(), 'status' => $this->config->getValue('retailcrm/Status/' . $order->getStatus()) ]; if ($order->getBaseTotalDue() == 0) { if ($this->api->getVersion() == 'v4') { - $preparedOrder['paymentStatus'] = 'paid'; + $this->order['paymentStatus'] = 'paid'; } elseif ($this->api->getVersion() == 'v5') { $payment = [ 'externalId' => $order->getPayment()->getId(), @@ -64,8 +66,16 @@ class OrderUpdate implements \Magento\Framework\Event\ObserverInterface } } - \Retailcrm\Retailcrm\Helper\Data::filterRecursive($preparedOrder); - $this->api->ordersEdit($preparedOrder); + \Retailcrm\Retailcrm\Helper\Data::filterRecursive($this->order); + $this->api->ordersEdit($this->order); } } + + /** + * @return mixed + */ + public function getOrder() + { + return $this->order; + } } diff --git a/Model/Order/OrderNumber.php b/src/Model/Order/OrderNumber.php similarity index 100% rename from Model/Order/OrderNumber.php rename to src/Model/Order/OrderNumber.php diff --git a/Model/Service/ConfigManager.php b/src/Model/Service/ConfigManager.php similarity index 100% rename from Model/Service/ConfigManager.php rename to src/Model/Service/ConfigManager.php diff --git a/Model/Setting/ApiVersions.php b/src/Model/Setting/ApiVersions.php similarity index 100% rename from Model/Setting/ApiVersions.php rename to src/Model/Setting/ApiVersions.php diff --git a/Model/Setting/Attribute.php b/src/Model/Setting/Attribute.php similarity index 100% rename from Model/Setting/Attribute.php rename to src/Model/Setting/Attribute.php diff --git a/Model/Setting/Shipping.php b/src/Model/Setting/Shipping.php similarity index 100% rename from Model/Setting/Shipping.php rename to src/Model/Setting/Shipping.php diff --git a/Model/Setting/Status.php b/src/Model/Setting/Status.php similarity index 100% rename from Model/Setting/Status.php rename to src/Model/Setting/Status.php diff --git a/src/README.md b/src/README.md new file mode 100644 index 0000000..100b938 --- /dev/null +++ b/src/README.md @@ -0,0 +1 @@ +README \ No newline at end of file diff --git a/src/Test/Unit/Block/Adminhtml/System/Config/Form/Field/PaymentTest.php b/src/Test/Unit/Block/Adminhtml/System/Config/Form/Field/PaymentTest.php new file mode 100644 index 0000000..95c631d --- /dev/null +++ b/src/Test/Unit/Block/Adminhtml/System/Config/Form/Field/PaymentTest.php @@ -0,0 +1,160 @@ +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 .= ''; + $html .= ''; + $html .= ''; + $html .= ''; + $html .= ''; + $html .= '
' . $paymentType->getTitle() . ''; + $html .= ''; + $html .= '
'; + } + + if ($error) { + return '
Please check your API Url & API Key
'; + } + + 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 + ] + ]; + } +} diff --git a/src/Test/Unit/Block/Adminhtml/System/Config/Form/Field/ShippingTest.php b/src/Test/Unit/Block/Adminhtml/System/Config/Form/Field/ShippingTest.php new file mode 100644 index 0000000..ac53639 --- /dev/null +++ b/src/Test/Unit/Block/Adminhtml/System/Config/Form/Field/ShippingTest.php @@ -0,0 +1,161 @@ +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 .= ''; + $html .= ''; + $html .= ''; + $html .= ''; + $html .= ''; + $html .= '
' . $deliveryType->getConfigData('title') . ''; + $html .= ''; + $html .= '
'; + } + + if ($error) { + return '
Please check your API Url & API Key
'; + } + + 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 + ] + ]; + } +} diff --git a/src/Test/Unit/Block/Adminhtml/System/Config/Form/Field/StatusTest.php b/src/Test/Unit/Block/Adminhtml/System/Config/Form/Field/StatusTest.php new file mode 100644 index 0000000..56d091b --- /dev/null +++ b/src/Test/Unit/Block/Adminhtml/System/Config/Form/Field/StatusTest.php @@ -0,0 +1,159 @@ +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 .= ''; + $html .= ''; + $html .= ''; + $html .= ''; + $html .= ''; + $html .= '
' . $status['label'] . ''; + $html .= ''; + $html .= '
'; + } + + if ($error) { + return '
Please check your API Url & API Key
'; + } + + 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 + ] + ]; + } +} diff --git a/Test/Unit/Observer/CustomerTest.php b/src/Test/Unit/Model/Observer/CustomerTest.php similarity index 68% rename from Test/Unit/Observer/CustomerTest.php rename to src/Test/Unit/Model/Observer/CustomerTest.php index 137041c..952fd4c 100644 --- a/Test/Unit/Observer/CustomerTest.php +++ b/src/Test/Unit/Model/Observer/CustomerTest.php @@ -10,6 +10,7 @@ class CustomerTest extends \PHPUnit\Framework\TestCase private $mockObserver; private $mockEvent; private $mockCustomer; + private $unit; public function setUp() { @@ -17,7 +18,8 @@ class CustomerTest extends \PHPUnit\Framework\TestCase ->disableOriginalConstructor() ->setMethods([ 'customersEdit', - 'customersCreate' + 'customersCreate', + 'isConfigured' ]) ->getMock(); @@ -54,19 +56,16 @@ class CustomerTest extends \PHPUnit\Framework\TestCase $this->registry, $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 $isConfigured * @dataProvider dataProviderCustomer */ public function testExecute( - $isSuccessful + $isSuccessful, + $isConfigured ) { $testData = $this->getAfterSaveCustomerTestData(); @@ -86,38 +85,54 @@ class CustomerTest extends \PHPUnit\Framework\TestCase ->method('customersCreate') ->willReturn($this->mockResponse); + $this->mockApi->expects($this->any()) + ->method('isConfigured') + ->willReturn($isConfigured); + // mock Customer - $this->mockCustomer->expects($this->once()) + $this->mockCustomer->expects($this->any()) ->method('getId') ->willReturn($testData['id']); - $this->mockCustomer->expects($this->once()) + $this->mockCustomer->expects($this->any()) ->method('getEmail') ->willReturn($testData['email']); - $this->mockCustomer->expects($this->once()) + $this->mockCustomer->expects($this->any()) ->method('getFirstname') ->willReturn($testData['firstname']); - $this->mockCustomer->expects($this->once()) + $this->mockCustomer->expects($this->any()) ->method('getMiddlename') ->willReturn($testData['middlename']); - $this->mockCustomer->expects($this->once()) + $this->mockCustomer->expects($this->any()) ->method('getLastname') ->willReturn($testData['lastname']); // mock Event - $this->mockEvent->expects($this->once()) + $this->mockEvent->expects($this->any()) ->method('getCustomer') ->willReturn($this->mockCustomer); // mock Observer - $this->mockObserver->expects($this->once()) + $this->mockObserver->expects($this->any()) ->method('getEvent') ->willReturn($this->mockEvent); $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 [ [ - '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 ] ]; } diff --git a/Test/Unit/Observer/OrderCreateTest.php b/src/Test/Unit/Model/Observer/OrderCreateTest.php similarity index 84% rename from Test/Unit/Observer/OrderCreateTest.php rename to src/Test/Unit/Model/Observer/OrderCreateTest.php index 4a43788..fa38252 100644 --- a/Test/Unit/Observer/OrderCreateTest.php +++ b/src/Test/Unit/Model/Observer/OrderCreateTest.php @@ -20,6 +20,7 @@ class OrderCreateTest extends \PHPUnit\Framework\TestCase private $mockResponse; private $mockPayment; private $mockPaymentMethod; + private $logger; public function setUp() { @@ -31,7 +32,8 @@ class OrderCreateTest extends \PHPUnit\Framework\TestCase 'customersGet', 'customersCreate', 'customersList', - 'getVersion' + 'getVersion', + 'isConfigured' ]) ->getMock(); @@ -131,13 +133,15 @@ class OrderCreateTest extends \PHPUnit\Framework\TestCase * @param string $errorMsg * @param int $customerIsGuest * @param string $apiVersion + * @param boolean $isConfigured * @dataProvider dataProviderOrderCreate */ public function testExecute( $isSuccessful, $errorMsg, $customerIsGuest, - $apiVersion + $apiVersion, + $isConfigured ) { $testData = $this->getAfterSaveOrderTestData(); @@ -173,6 +177,10 @@ class OrderCreateTest extends \PHPUnit\Framework\TestCase ->method('getVersion') ->willReturn($apiVersion); + $this->mockApi->expects($this->any()) + ->method('isConfigured') + ->willReturn($isConfigured); + // billing address mock set data $this->mockBillingAddress->expects($this->any()) ->method('getTelephone') @@ -287,16 +295,39 @@ class OrderCreateTest extends \PHPUnit\Framework\TestCase ->willReturn($this->mockPaymentMethod); // mock Event - $this->mockEvent->expects($this->once()) + $this->mockEvent->expects($this->any()) ->method('getOrder') ->willReturn($this->mockOrder); // mock Observer - $this->mockObserver->expects($this->once()) + $this->mockObserver->expects($this->any()) ->method('getEvent') ->willReturn($this->mockEvent); $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, 'error_msg' => 'Not found', 'customer_is_guest' => 1, - 'api_version' => 'v4' + 'api_version' => 'v4', + 'is_configured' => true ], [ 'is_successful' => true, 'error_msg' => 'Not found', 'customer_is_guest' => 0, - 'api_version' => 'v4' + 'api_version' => 'v4', + 'is_configured' => false ], [ 'is_successful' => false, 'error_msg' => 'Not found', 'customer_is_guest' => 1, - 'api_version' => 'v4' + 'api_version' => 'v4', + 'is_configured' => true ], [ 'is_successful' => false, 'error_msg' => 'Not found', 'customer_is_guest' => 0, - 'api_version' => 'v4' + 'api_version' => 'v4', + 'is_configured' => false ], [ 'is_successful' => true, 'error_msg' => 'Not found', 'customer_is_guest' => 1, - 'api_version' => 'v5' + 'api_version' => 'v5', + 'is_configured' => true ], [ 'is_successful' => true, 'error_msg' => 'Not found', 'customer_is_guest' => 0, - 'api_version' => 'v5' + 'api_version' => 'v5', + 'is_configured' => false ], [ 'is_successful' => false, 'error_msg' => 'Not found', 'customer_is_guest' => 1, - 'api_version' => 'v5' + 'api_version' => 'v5', + 'is_configured' => true ], [ 'is_successful' => false, 'error_msg' => 'Not found', 'customer_is_guest' => 0, - 'api_version' => 'v5' + 'api_version' => 'v5', + 'is_configured' => false ] ]; } diff --git a/Test/Unit/Observer/OrderUpdateTest.php b/src/Test/Unit/Model/Observer/OrderUpdateTest.php similarity index 77% rename from Test/Unit/Observer/OrderUpdateTest.php rename to src/Test/Unit/Model/Observer/OrderUpdateTest.php index fe5132e..e0779ad 100644 --- a/Test/Unit/Observer/OrderUpdateTest.php +++ b/src/Test/Unit/Model/Observer/OrderUpdateTest.php @@ -21,7 +21,8 @@ class OrderUpdateTest extends \PHPUnit\Framework\TestCase ->setMethods([ 'ordersEdit', 'ordersPaymentsEdit', - 'getVersion' + 'getVersion', + 'isConfigured' ]) ->getMock(); @@ -68,11 +69,13 @@ class OrderUpdateTest extends \PHPUnit\Framework\TestCase /** * @param int $getBaseTotalDue * @param string $apiVersion + * @param boolean $isConfigured * @dataProvider dataProviderOrderUpdate */ public function testExecute( $getBaseTotalDue, - $apiVersion + $apiVersion, + $isConfigured ) { $testData = $this->getAfterUpdateOrderTestData(); @@ -82,15 +85,15 @@ class OrderUpdateTest extends \PHPUnit\Framework\TestCase ->willReturn(1); // mock Order - $this->mockOrder->expects($this->once()) + $this->mockOrder->expects($this->any()) ->method('getId') ->willReturn($testData['order.id']); - $this->mockOrder->expects($this->once()) + $this->mockOrder->expects($this->any()) ->method('getStatus') ->willReturn($testData['order.status']); - $this->mockOrder->expects($this->once()) + $this->mockOrder->expects($this->any()) ->method('getBaseTotalDue') ->willReturn($getBaseTotalDue); @@ -103,17 +106,29 @@ class OrderUpdateTest extends \PHPUnit\Framework\TestCase ->method('getVersion') ->willReturn($apiVersion); + $this->mockApi->expects($this->any()) + ->method('isConfigured') + ->willReturn($isConfigured); + // mock Event - $this->mockEvent->expects($this->once()) + $this->mockEvent->expects($this->any()) ->method('getOrder') ->willReturn($this->mockOrder); // mock Observer - $this->mockObserver->expects($this->once()) + $this->mockObserver->expects($this->any()) ->method('getEvent') ->willReturn($this->mockEvent); $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 [ [ 'get_base_total_due' => 0, - 'api_version' => 'v4' + 'api_version' => 'v4', + 'is_configured' => false ], [ 'get_base_total_due' => 1, - 'api_version' => 'v4' + 'api_version' => 'v4', + 'is_configured' => true ], [ 'get_base_total_due' => 0, - 'api_version' => 'v5' + 'api_version' => 'v5', + 'is_configured' => true ], [ 'get_base_total_due' => 1, - 'api_version' => 'v5' + 'api_version' => 'v5', + 'is_configured' => false ] ]; } diff --git a/composer.json b/src/composer.json similarity index 93% rename from composer.json rename to src/composer.json index bed5c26..bc6ba84 100644 --- a/composer.json +++ b/src/composer.json @@ -2,7 +2,6 @@ "name": "retailcrm/retailcrm", "description": "Retailcrm", "require": { - "php": "~5.5.0|~5.6.0|~7.0.0", "retailcrm/api-client-php": "~5.0" }, "type": "magento2-module", diff --git a/etc/adminhtml/menu.xml b/src/etc/adminhtml/menu.xml similarity index 100% rename from etc/adminhtml/menu.xml rename to src/etc/adminhtml/menu.xml diff --git a/etc/adminhtml/routes.xml b/src/etc/adminhtml/routes.xml similarity index 100% rename from etc/adminhtml/routes.xml rename to src/etc/adminhtml/routes.xml diff --git a/etc/adminhtml/system.xml b/src/etc/adminhtml/system.xml similarity index 97% rename from etc/adminhtml/system.xml rename to src/etc/adminhtml/system.xml index 657ec96..41c4587 100644 --- a/etc/adminhtml/system.xml +++ b/src/etc/adminhtml/system.xml @@ -33,8 +33,8 @@ - - attributes to export into icml + + Attributes to export into icml Retailcrm\Retailcrm\Model\Setting\Attribute