CustomerRegistryTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Model;
  7. use Magento\Framework\Exception\NoSuchEntityException;
  8. use Magento\TestFramework\Helper\Bootstrap;
  9. /**
  10. * Test for \Magento\Customer\Model\CustomerRegistry
  11. */
  12. class CustomerRegistryTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var \Magento\Customer\Model\CustomerRegistry
  16. */
  17. protected $_model;
  18. /**#@+
  19. * Data set in customer fixture
  20. */
  21. const CUSTOMER_ID = 1;
  22. const CUSTOMER_EMAIL = 'customer@example.com';
  23. const WEBSITE_ID = 1;
  24. /**
  25. * Initialize SUT
  26. */
  27. protected function setUp()
  28. {
  29. $this->_model = Bootstrap::getObjectManager()
  30. ->create(\Magento\Customer\Model\CustomerRegistry::class);
  31. }
  32. /**
  33. * @magentoDataFixture Magento/Customer/_files/customer.php
  34. */
  35. public function testRetrieve()
  36. {
  37. $customer = $this->_model->retrieve(self::CUSTOMER_ID);
  38. $this->assertInstanceOf(\Magento\Customer\Model\Customer::class, $customer);
  39. $this->assertEquals(self::CUSTOMER_ID, $customer->getId());
  40. }
  41. /**
  42. * @magentoDataFixture Magento/Customer/_files/customer.php
  43. */
  44. public function testRetrieveByEmail()
  45. {
  46. $customer = $this->_model->retrieveByEmail('customer@example.com', self::WEBSITE_ID);
  47. $this->assertInstanceOf(\Magento\Customer\Model\Customer::class, $customer);
  48. $this->assertEquals(self::CUSTOMER_EMAIL, $customer->getEmail());
  49. }
  50. /**
  51. * @magentoDataFixture Magento/Customer/_files/customer.php
  52. * @magentoAppArea adminhtml
  53. */
  54. public function testRetrieveCached()
  55. {
  56. //Setup customer in the id and email registries
  57. $customerBeforeDeletion = $this->_model->retrieve(self::CUSTOMER_ID);
  58. //Delete the customer from db
  59. Bootstrap::getObjectManager()->create(
  60. \Magento\Customer\Model\Customer::class
  61. )->load(self::CUSTOMER_ID)->delete();
  62. //Verify presence of Customer in registry
  63. $this->assertEquals($customerBeforeDeletion, $this->_model->retrieve(self::CUSTOMER_ID));
  64. //Verify presence of Customer in email registry
  65. $this->assertEquals($customerBeforeDeletion, $this->_model
  66. ->retrieveByEmail(self::CUSTOMER_EMAIL, self::WEBSITE_ID));
  67. }
  68. /**
  69. * @expectedException \Magento\Framework\Exception\NoSuchEntityException
  70. * @expectedExceptionMessage No such entity with customerId = 1
  71. */
  72. public function testRetrieveException()
  73. {
  74. $this->_model->retrieve(self::CUSTOMER_ID);
  75. }
  76. public function testRetrieveEmailException()
  77. {
  78. try {
  79. $this->_model->retrieveByEmail(self::CUSTOMER_EMAIL, self::WEBSITE_ID);
  80. $this->fail("NoSuchEntityException was not thrown as expected.");
  81. } catch (NoSuchEntityException $e) {
  82. $expectedParams = [
  83. 'fieldName' => 'email',
  84. 'fieldValue' => 'customer@example.com',
  85. 'field2Name' => 'websiteId',
  86. 'field2Value' => 1,
  87. ];
  88. $this->assertEquals($expectedParams, $e->getParameters());
  89. }
  90. }
  91. /**
  92. * @magentoDataFixture Magento/Customer/_files/customer.php
  93. * @expectedException \Magento\Framework\Exception\NoSuchEntityException
  94. * @magentoAppArea adminhtml
  95. */
  96. public function testRemove()
  97. {
  98. $customer = $this->_model->retrieve(self::CUSTOMER_ID);
  99. $this->assertInstanceOf(\Magento\Customer\Model\Customer::class, $customer);
  100. $customer->delete();
  101. $this->_model->remove(self::CUSTOMER_ID);
  102. $this->_model->retrieve(self::CUSTOMER_ID);
  103. }
  104. /**
  105. * @magentoDataFixture Magento/Customer/_files/customer.php
  106. * @expectedException \Magento\Framework\Exception\NoSuchEntityException
  107. * @magentoAppArea adminhtml
  108. */
  109. public function testRemoveByEmail()
  110. {
  111. $customer = $this->_model->retrieve(self::CUSTOMER_ID);
  112. $this->assertInstanceOf(\Magento\Customer\Model\Customer::class, $customer);
  113. $customer->delete();
  114. $this->_model->removeByEmail(self::CUSTOMER_EMAIL, self::WEBSITE_ID);
  115. $this->_model->retrieveByEmail(self::CUSTOMER_EMAIL, $customer->getWebsiteId());
  116. }
  117. }