CreateUpdateContactTest.php 4.09 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
<?php

namespace Dotdigitalgroup\Email\Observer\Customer;

use Dotdigitalgroup\Email\Helper\Data;

/**
 * @magentoDbIsolation enabled
 * @magentoAdminConfigFixture connector/api/endpoint https://r1-api.dotmailer.com
 * @magentoAdminConfigFixture connector_api_credentials/api/enabled 1
 * @magentoAdminConfigFixture connector_api_credentials/api/username dummyusername
 * @magentoAdminConfigFixture connector_api_credentials/api/password dummypassword
 */
class CreateUpdateContactTest extends \PHPUnit\Framework\TestCase
{
    /**
     * @var \Magento\TestFramework\ObjectManager
     */
    public $objectManager;

    /**
     * @var \Magento\Customer\Model\CustomerFactory
     */
    public $customerFactory;

    /**
     * @var \Dotdigitalgroup\Email\Model\ContactFactory
     */
    public $contactFactory;

    /**
     * @var string
     */
    public $email;

    /**
     * @var \Magento\Customer\Model\Customer
     */
    public $customerModel;

    /**
     * @var int
     */
    public $customerId ;

    /**
     * @var \Dotdigitalgroup\Email\Helper\Data
     */
    private $helper;

    /**
     * @return void
     */
    public function setup()
    {
        $this->objectManager = \Magento\TestFramework\ObjectManager::getInstance();
        $this->customerFactory = $this->objectManager->create(\Magento\Customer\Model\CustomerFactory::class);
        $this->contactFactory = $this->objectManager->create(\Dotdigitalgroup\Email\Model\ContactFactory::class);
        $this->helper = $this->objectManager->create(\Dotdigitalgroup\Email\Helper\Data::class);
    }

    /**
     * @return void
     */
    public function prepareCustomerData()
    {
        /** @var \Magento\Store\Model\StoreManagerInterface $storeManager */
        $storeManager = $this->objectManager->create(\Magento\Store\Model\StoreManagerInterface::class);
        $store = $storeManager->getStore();
        $website = $store->getWebsite();
        $num = rand(500, 5000);
        $this->email = 'dummy' . $num . 'new@dotmailer.com';

        //pass the helper is enabled
        $mockHelper = $this->getMockBuilder(Data::class)
            ->disableOriginalConstructor()
            ->getMock();
        $this->objectManager->addSharedInstance($mockHelper, Data::class);
        $mockHelper->method('isEnabled')->willReturn(1);

        $customerModel = $this->customerFactory->create();
        $customerModel->setStore($store);
        $customerModel->setWebsiteId($website->getId());
        $customerModel->setEmail($this->email);
        $customerModel->setFirstname('Firstname');
        $customerModel->setLastname('Lastname');
        $customerModel->setPassword('dummypassword');
        $customerModel->save();

        $this->customerId = $customerModel->getId();
        $this->customerModel = $customerModel;
    }

    /**
     * @return void
     */
    public function testContactCreatedAndUpdatedSuccessfully()
    {
        $this->prepareCustomerData();
        //update the email and save contact
        $updatedEmail = str_replace('new', 'updated', $this->email);

        //save new customer which will trigger the observer and will create new contact
        $contact = $this->loadContactByCustomerId();
        $emailCreated = $contact->getEmail();

        $this->updateCustomerEmail($updatedEmail);
        $contact = $this->loadContactByCustomerId();
        $emailUpdated = $contact->getEmail();

        //check contact created after the customer was saved
        $this->assertEquals($this->email, $emailCreated, 'Contact was not found');

        $this->assertEquals($updatedEmail, $emailUpdated, 'Updated contact was not found');
    }

    /**
     * @param string $updatedEmail
     *
     * @return \Magento\Customer\Model\Customer
     */
    private function updateCustomerEmail($updatedEmail)
    {
        return $this->customerModel
            ->setEmail($updatedEmail)
            ->save();
    }

    /**
     * @return \Dotdigitalgroup\Email\Model\Contact
     */
    public function loadContactByCustomerId()
    {
        $contact = $this->contactFactory->create()
            ->loadByCustomerId($this->customerId);

        return $contact;
    }
}