SingleOrderSyncTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. namespace Dotdigitalgroup\Email\Model\Sync;
  3. /**
  4. * Class SingleOrderSyncTest
  5. *
  6. * @package Dotdigitalgroup\Email\Controller\Customer
  7. * @magentoDBIsolation enabled
  8. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  9. */
  10. class SingleOrderSyncTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var \Magento\Framework\ObjectManagerInterface
  14. */
  15. public $objectManager;
  16. /**
  17. * @var string
  18. */
  19. public $storeId;
  20. /**
  21. * @var string
  22. */
  23. public $orderStatus;
  24. /**
  25. * @var \Dotdigitalgroup\Email\Model\ResourceModel\Importer\Collection
  26. */
  27. public $importerCollection;
  28. /**
  29. * @return void
  30. */
  31. public function setup()
  32. {
  33. $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  34. $this->importerCollection = $this->objectManager->create(
  35. \Dotdigitalgroup\Email\Model\ResourceModel\Importer\Collection::class
  36. );
  37. }
  38. /**
  39. * @return array
  40. */
  41. public function prep()
  42. {
  43. /** @var \Magento\Store\Model\Store $store */
  44. $store = $this->objectManager->create(\Magento\Store\Model\Store::class);
  45. $store->load($this->storeId);
  46. /** @var \Dotdigitalgroup\Email\Helper\Data|\PHPUnit_Framework_MockObject_MockObject $helper */
  47. $helper = $this->getMockBuilder(\Dotdigitalgroup\Email\Helper\Data::class)
  48. ->disableOriginalConstructor()
  49. ->getMock();
  50. $helper->method('isEnabled')->willReturn(true);
  51. $helper->method('getWebsites')->willReturn([$store->getWebsite()]);
  52. $helper->method('getApiUsername')->willReturn('apiuser-dummy@apiconnector.com');
  53. $helper->method('getApiPassword')->willReturn('dummypass');
  54. $helper->method('getWebsiteConfig')->willReturn('1');
  55. $helper->method('getConfigSelectedStatus')->willReturn($this->orderStatus);
  56. $helper->storeManager = $this->objectManager->create(\Magento\Store\Model\StoreManagerInterface::class);
  57. $orderSync = new \Dotdigitalgroup\Email\Model\Sync\Order(
  58. $this->objectManager->create(\Dotdigitalgroup\Email\Model\ImporterFactory::class),
  59. $this->objectManager->create(\Dotdigitalgroup\Email\Model\OrderFactory::class),
  60. $this->objectManager->create(\Dotdigitalgroup\Email\Model\Connector\AccountFactory::class),
  61. $this->objectManager->create(\Dotdigitalgroup\Email\Model\Connector\OrderFactory::class),
  62. $this->objectManager->create(\Dotdigitalgroup\Email\Model\ResourceModel\Contact::class),
  63. $this->objectManager->create(\Dotdigitalgroup\Email\Model\ResourceModel\Contact\CollectionFactory::class),
  64. $this->objectManager->create(\Dotdigitalgroup\Email\Model\ResourceModel\Order::class),
  65. $helper,
  66. $this->objectManager->create(\Magento\Sales\Model\OrderFactory::class)
  67. );
  68. return $orderSync->sync();
  69. }
  70. /**
  71. * @magentoDataFixture Magento/Sales/_files/order.php
  72. * @magentoConfigFixture default_store sync_settings/sync/order_enabled 1
  73. * @magentoConfigFixture default_store connector_api_credentials/api/enabled 1
  74. *
  75. * @return null
  76. */
  77. public function testSingleOrderIsTypeOrderAndModeSingle()
  78. {
  79. $this->createModifiedEmailOrder();
  80. $this->prep();
  81. $item = $this->importerCollection
  82. ->addFieldToFilter('import_type', \Dotdigitalgroup\Email\Model\Importer::IMPORT_TYPE_ORDERS)
  83. ->addFieldToFilter('import_mode', \Dotdigitalgroup\Email\Model\Importer::MODE_SINGLE)
  84. ->getFirstItem();
  85. $this->assertEquals(
  86. \Dotdigitalgroup\Email\Model\Importer::IMPORT_TYPE_ORDERS,
  87. $item->getImportType(),
  88. 'Item is not type of order'
  89. );
  90. $this->assertEquals(
  91. \Dotdigitalgroup\Email\Model\Importer::MODE_SINGLE,
  92. $item->getImportMode(),
  93. 'Item is not single mode'
  94. );
  95. }
  96. /**
  97. * @magentoDataFixture Magento/Sales/_files/order.php
  98. * @magentoConfigFixture default_store sync_settings/sync/order_enabled 1
  99. * @magentoConfigFixture default_store connector_api_credentials/api/enabled 1
  100. *
  101. * @return null
  102. */
  103. public function testSingleOrderTypeIsObject()
  104. {
  105. $this->createModifiedEmailOrder();
  106. $this->prep();
  107. $item = $this->importerCollection->getFirstItem();
  108. $this->assertInternalType('object', json_decode($item->getImportData()), 'Import data is not of object type');
  109. }
  110. /**
  111. * @return null
  112. */
  113. public function createModifiedEmailOrder()
  114. {
  115. /** @var \Magento\Sales\Model\ResourceModel\Order\Collection $orderCollection */
  116. $orderCollection = $this->objectManager->create(\Magento\Sales\Model\ResourceModel\Order\Collection::class);
  117. /** @var \Magento\Sales\Model\Order $order */
  118. $order = $orderCollection->getFirstItem();
  119. $this->storeId = $order->getStoreId();
  120. $this->orderStatus = [$order->getStatus()];
  121. $emailOrder = $this->objectManager->create(\Dotdigitalgroup\Email\Model\Order::class)
  122. ->setOrderId($order->getId())
  123. ->setOrderStatus($order->getStatus())
  124. ->setQuoteId($order->getQuoteId())
  125. ->setStoreId($this->storeId)
  126. ->setEmailImported('1')
  127. ->setModified('1');
  128. $emailOrder->save();
  129. }
  130. /**
  131. * @magentoDataFixture Magento/Sales/_files/order.php
  132. */
  133. public function testSingleOrderSync()
  134. {
  135. $this->createModifiedEmailOrder();
  136. $orderResponse = $this->prep();
  137. $expected = 'Orders updated 1';
  138. $this->assertEquals($expected, $orderResponse['message']);
  139. }
  140. /**
  141. * @magentoDataFixture Magento/Sales/_files/order.php
  142. */
  143. public function testSyncOrderWithoutPaymentInfoSync()
  144. {
  145. $this->createOrderWithoutPayment();
  146. $orderResponse = $this->prep();
  147. $expected = 'Orders updated 1';
  148. $this->assertEquals($expected, $orderResponse['message']);
  149. }
  150. private function createOrderWithoutPayment()
  151. {
  152. /** @var \Magento\Sales\Model\ResourceModel\Order\Collection $orderCollection */
  153. $orderCollection = $this->objectManager->create(\Magento\Sales\Model\ResourceModel\Order\Collection::class);
  154. $orderResource = $this->objectManager->create(\Magento\Sales\Model\ResourceModel\Order::class);
  155. /** @var \Magento\Sales\Model\Order $order */
  156. $order = $orderCollection->getFirstItem();
  157. $payment = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
  158. \Magento\Sales\Model\Order\Payment::class
  159. );
  160. $order->setPayment($payment);
  161. $orderResource->save($order);
  162. $this->storeId = $order->getStoreId();
  163. $this->orderStatus = [$order->getStatus()];
  164. $emailOrder = $this->objectManager->create(\Dotdigitalgroup\Email\Model\Order::class)
  165. ->setOrderId($order->getId())
  166. ->setOrderStatus($order->getStatus())
  167. ->setQuoteId($order->getQuoteId())
  168. ->setStoreId($this->storeId)
  169. ->setEmailImported('1')
  170. ->setModified('1');
  171. $emailOrder->save();
  172. }
  173. }