CreateUpdateContactTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace Dotdigitalgroup\Email\Observer\Customer;
  3. use Dotdigitalgroup\Email\Helper\Data;
  4. /**
  5. * @magentoDbIsolation enabled
  6. * @magentoAdminConfigFixture connector/api/endpoint https://r1-api.dotmailer.com
  7. * @magentoAdminConfigFixture connector_api_credentials/api/enabled 1
  8. * @magentoAdminConfigFixture connector_api_credentials/api/username dummyusername
  9. * @magentoAdminConfigFixture connector_api_credentials/api/password dummypassword
  10. */
  11. class CreateUpdateContactTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var \Magento\TestFramework\ObjectManager
  15. */
  16. public $objectManager;
  17. /**
  18. * @var \Magento\Customer\Model\CustomerFactory
  19. */
  20. public $customerFactory;
  21. /**
  22. * @var \Dotdigitalgroup\Email\Model\ContactFactory
  23. */
  24. public $contactFactory;
  25. /**
  26. * @var string
  27. */
  28. public $email;
  29. /**
  30. * @var \Magento\Customer\Model\Customer
  31. */
  32. public $customerModel;
  33. /**
  34. * @var int
  35. */
  36. public $customerId ;
  37. /**
  38. * @var \Dotdigitalgroup\Email\Helper\Data
  39. */
  40. private $helper;
  41. /**
  42. * @return void
  43. */
  44. public function setup()
  45. {
  46. $this->objectManager = \Magento\TestFramework\ObjectManager::getInstance();
  47. $this->customerFactory = $this->objectManager->create(\Magento\Customer\Model\CustomerFactory::class);
  48. $this->contactFactory = $this->objectManager->create(\Dotdigitalgroup\Email\Model\ContactFactory::class);
  49. $this->helper = $this->objectManager->create(\Dotdigitalgroup\Email\Helper\Data::class);
  50. }
  51. /**
  52. * @return void
  53. */
  54. public function prepareCustomerData()
  55. {
  56. /** @var \Magento\Store\Model\StoreManagerInterface $storeManager */
  57. $storeManager = $this->objectManager->create(\Magento\Store\Model\StoreManagerInterface::class);
  58. $store = $storeManager->getStore();
  59. $website = $store->getWebsite();
  60. $num = rand(500, 5000);
  61. $this->email = 'dummy' . $num . 'new@dotmailer.com';
  62. //pass the helper is enabled
  63. $mockHelper = $this->getMockBuilder(Data::class)
  64. ->disableOriginalConstructor()
  65. ->getMock();
  66. $this->objectManager->addSharedInstance($mockHelper, Data::class);
  67. $mockHelper->method('isEnabled')->willReturn(1);
  68. $customerModel = $this->customerFactory->create();
  69. $customerModel->setStore($store);
  70. $customerModel->setWebsiteId($website->getId());
  71. $customerModel->setEmail($this->email);
  72. $customerModel->setFirstname('Firstname');
  73. $customerModel->setLastname('Lastname');
  74. $customerModel->setPassword('dummypassword');
  75. $customerModel->save();
  76. $this->customerId = $customerModel->getId();
  77. $this->customerModel = $customerModel;
  78. }
  79. /**
  80. * @return void
  81. */
  82. public function testContactCreatedAndUpdatedSuccessfully()
  83. {
  84. $this->prepareCustomerData();
  85. //update the email and save contact
  86. $updatedEmail = str_replace('new', 'updated', $this->email);
  87. //save new customer which will trigger the observer and will create new contact
  88. $contact = $this->loadContactByCustomerId();
  89. $emailCreated = $contact->getEmail();
  90. $this->updateCustomerEmail($updatedEmail);
  91. $contact = $this->loadContactByCustomerId();
  92. $emailUpdated = $contact->getEmail();
  93. //check contact created after the customer was saved
  94. $this->assertEquals($this->email, $emailCreated, 'Contact was not found');
  95. $this->assertEquals($updatedEmail, $emailUpdated, 'Updated contact was not found');
  96. }
  97. /**
  98. * @param string $updatedEmail
  99. *
  100. * @return \Magento\Customer\Model\Customer
  101. */
  102. private function updateCustomerEmail($updatedEmail)
  103. {
  104. return $this->customerModel
  105. ->setEmail($updatedEmail)
  106. ->save();
  107. }
  108. /**
  109. * @return \Dotdigitalgroup\Email\Model\Contact
  110. */
  111. public function loadContactByCustomerId()
  112. {
  113. $contact = $this->contactFactory->create()
  114. ->loadByCustomerId($this->customerId);
  115. return $contact;
  116. }
  117. }