FirstOrderTest.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace Dotdigitalgroup\Email\Test\Integration\Automation;
  3. /**
  4. * Class FirstOrderTest
  5. * @package Dotdigitalgroup\Email\Test\Integration\Automation
  6. *
  7. * @magentoDBIsolation disabled
  8. */
  9. class FirstOrderTest extends \PHPUnit\Framework\TestCase
  10. {
  11. /**
  12. * @var object
  13. */
  14. public $objectManager;
  15. /**
  16. * @var int
  17. */
  18. public $orderIncrementId;
  19. /**
  20. * @return void
  21. */
  22. public function setUp()
  23. {
  24. $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  25. $this->orderIncrementId = '100000001';
  26. }
  27. /**
  28. * @magentoDataFixture Magento/Sales/_files/order_with_customer.php
  29. * @magentoConfigFixture default_store connector_api_credentials/api/enabled 1
  30. * @magentoConfigFixture default_store connector_api_credentials/api/username dummyusername
  31. * @magentoConfigFixture default_store connector_api_credentials/api/password dummypassword
  32. * @magentoConfigFixture default_store connector_automation/visitor_automation/first_order_automation 123
  33. *
  34. * @return null
  35. */
  36. public function testFirstCustomerAutomation()
  37. {
  38. $this->objectManager->create(\Magento\Customer\Model\Customer::class);
  39. $order = $this->objectManager->create(\Magento\Sales\Model\Order::class);
  40. $order->loadByIncrementId($this->orderIncrementId);
  41. $orderEmail = $order->getCustomerEmail();
  42. //set new state and status for order
  43. $order->setState(\Magento\Sales\Model\Order::STATE_NEW);
  44. $order->setStatus($order->getConfig()->getStateDefaultStatus(\Magento\Sales\Model\Order::STATE_NEW));
  45. $order->setCustomerId(1);
  46. $order->save();
  47. $automation = $this->objectManager->create(
  48. \Dotdigitalgroup\Email\Model\ResourceModel\Automation\Collection::class
  49. );
  50. $automation->addFieldToFilter('email', $orderEmail);
  51. $automation->addFieldToFilter('automation_type', 'first_order_automation');
  52. $customerOrders = $this->objectManager->create(\Magento\Sales\Model\ResourceModel\Order\Collection::class);
  53. $customerOrders->addFieldToFilter('customer_email', $orderEmail);
  54. //update to new state
  55. $this->assertEquals('new', $order->getState(), 'order state not new');
  56. $order = $this->createInvoice($order);
  57. //check order is processing
  58. $this->assertEquals('processing', $order->getState(), 'fail to create invoice for the order ');
  59. //order save should not create duplicate automation enrollment
  60. $this->assertEquals(1, $automation->getSize(), 'duplicate automation for first order');
  61. }
  62. /**
  63. * @param \Magento\Sales\Model\Order $order
  64. *
  65. * @return \Magento\Sales\Model\Order $order
  66. */
  67. public function createInvoice($order)
  68. {
  69. $orderService = \Magento\TestFramework\ObjectManager::getInstance()->create(
  70. \Magento\Sales\Api\InvoiceManagementInterface::class
  71. );
  72. $invoice = $orderService->prepareInvoice($order);
  73. $invoice->register();
  74. $order = $invoice->getOrder();
  75. $order->setIsInProcess(true);
  76. $order->save();
  77. return $order;
  78. }
  79. }