CustomerRegistryTest.php 4.25 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
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace Magento\Customer\Model;

use Magento\Framework\Exception\NoSuchEntityException;
use Magento\TestFramework\Helper\Bootstrap;

/**
 * Test for \Magento\Customer\Model\CustomerRegistry
 */
class CustomerRegistryTest extends \PHPUnit\Framework\TestCase
{
    /**
     * @var \Magento\Customer\Model\CustomerRegistry
     */
    protected $_model;

    /**#@+
     * Data set in customer fixture
     */
    const CUSTOMER_ID = 1;
    const CUSTOMER_EMAIL = 'customer@example.com';
    const WEBSITE_ID = 1;

    /**
     * Initialize SUT
     */
    protected function setUp()
    {
        $this->_model = Bootstrap::getObjectManager()
            ->create(\Magento\Customer\Model\CustomerRegistry::class);
    }

    /**
     * @magentoDataFixture Magento/Customer/_files/customer.php
     */
    public function testRetrieve()
    {
        $customer = $this->_model->retrieve(self::CUSTOMER_ID);
        $this->assertInstanceOf(\Magento\Customer\Model\Customer::class, $customer);
        $this->assertEquals(self::CUSTOMER_ID, $customer->getId());
    }

    /**
     * @magentoDataFixture Magento/Customer/_files/customer.php
     */
    public function testRetrieveByEmail()
    {
        $customer = $this->_model->retrieveByEmail('customer@example.com', self::WEBSITE_ID);
        $this->assertInstanceOf(\Magento\Customer\Model\Customer::class, $customer);
        $this->assertEquals(self::CUSTOMER_EMAIL, $customer->getEmail());
    }

    /**
     * @magentoDataFixture Magento/Customer/_files/customer.php
     * @magentoAppArea adminhtml
     */
    public function testRetrieveCached()
    {
        //Setup customer in the id and email registries
        $customerBeforeDeletion = $this->_model->retrieve(self::CUSTOMER_ID);
        //Delete the customer from db
        Bootstrap::getObjectManager()->create(
            \Magento\Customer\Model\Customer::class
        )->load(self::CUSTOMER_ID)->delete();
        //Verify presence of Customer in registry
        $this->assertEquals($customerBeforeDeletion, $this->_model->retrieve(self::CUSTOMER_ID));
        //Verify presence of Customer in email registry
        $this->assertEquals($customerBeforeDeletion, $this->_model
                ->retrieveByEmail(self::CUSTOMER_EMAIL, self::WEBSITE_ID));
    }

    /**
     * @expectedException \Magento\Framework\Exception\NoSuchEntityException
     * @expectedExceptionMessage No such entity with customerId = 1
     */
    public function testRetrieveException()
    {
        $this->_model->retrieve(self::CUSTOMER_ID);
    }

    public function testRetrieveEmailException()
    {
        try {
            $this->_model->retrieveByEmail(self::CUSTOMER_EMAIL, self::WEBSITE_ID);
            $this->fail("NoSuchEntityException was not thrown as expected.");
        } catch (NoSuchEntityException $e) {
            $expectedParams = [
                'fieldName' => 'email',
                'fieldValue' => 'customer@example.com',
                'field2Name' => 'websiteId',
                'field2Value' => 1,
            ];
            $this->assertEquals($expectedParams, $e->getParameters());
        }
    }

    /**
     * @magentoDataFixture Magento/Customer/_files/customer.php
     * @expectedException \Magento\Framework\Exception\NoSuchEntityException
     * @magentoAppArea adminhtml
     */
    public function testRemove()
    {
        $customer = $this->_model->retrieve(self::CUSTOMER_ID);
        $this->assertInstanceOf(\Magento\Customer\Model\Customer::class, $customer);
        $customer->delete();
        $this->_model->remove(self::CUSTOMER_ID);
        $this->_model->retrieve(self::CUSTOMER_ID);
    }

    /**
     * @magentoDataFixture Magento/Customer/_files/customer.php
     * @expectedException \Magento\Framework\Exception\NoSuchEntityException
     * @magentoAppArea adminhtml
     */
    public function testRemoveByEmail()
    {
        $customer = $this->_model->retrieve(self::CUSTOMER_ID);
        $this->assertInstanceOf(\Magento\Customer\Model\Customer::class, $customer);
        $customer->delete();
        $this->_model->removeByEmail(self::CUSTOMER_EMAIL, self::WEBSITE_ID);
        $this->_model->retrieveByEmail(self::CUSTOMER_EMAIL, $customer->getWebsiteId());
    }
}