CustomerRegistryTest.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Test\Unit\Model;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  8. /**
  9. * Test for CustomerRegistry
  10. *
  11. */
  12. class CustomerRegistryTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var \Magento\Customer\Model\CustomerRegistry
  16. */
  17. private $customerRegistry;
  18. /**
  19. * @var \Magento\Customer\Model\CustomerFactory|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. private $customerFactory;
  22. /**
  23. * @var \Magento\Customer\Model\Customer|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. private $customer;
  26. /**#@+
  27. * Sample customer data
  28. */
  29. const CUSTOMER_ID = 1;
  30. const CUSTOMER_EMAIL = 'customer@example.com';
  31. const WEBSITE_ID = 1;
  32. protected function setUp()
  33. {
  34. $this->customerFactory = $this->getMockBuilder(\Magento\Customer\Model\CustomerFactory::class)
  35. ->setMethods(['create'])
  36. ->disableOriginalConstructor()
  37. ->getMock();
  38. $objectManager = new ObjectManager($this);
  39. $this->customerRegistry = $objectManager->getObject(
  40. \Magento\Customer\Model\CustomerRegistry::class,
  41. ['customerFactory' => $this->customerFactory]
  42. );
  43. $this->customer = $this->getMockBuilder(\Magento\Customer\Model\Customer::class)
  44. ->disableOriginalConstructor()
  45. ->setMethods(
  46. [
  47. 'load',
  48. 'getId',
  49. 'getEmail',
  50. 'getWebsiteId',
  51. '__wakeup',
  52. 'setEmail',
  53. 'setWebsiteId',
  54. 'loadByEmail',
  55. ]
  56. )
  57. ->getMock();
  58. }
  59. public function testRetrieve()
  60. {
  61. $this->customer->expects($this->once())
  62. ->method('load')
  63. ->with(self::CUSTOMER_ID)
  64. ->will($this->returnValue($this->customer));
  65. $this->customer->expects($this->any())
  66. ->method('getId')
  67. ->will($this->returnValue(self::CUSTOMER_ID));
  68. $this->customerFactory->expects($this->once())
  69. ->method('create')
  70. ->will($this->returnValue($this->customer));
  71. $actual = $this->customerRegistry->retrieve(self::CUSTOMER_ID);
  72. $this->assertEquals($this->customer, $actual);
  73. $actualCached = $this->customerRegistry->retrieve(self::CUSTOMER_ID);
  74. $this->assertEquals($this->customer, $actualCached);
  75. }
  76. public function testRetrieveByEmail()
  77. {
  78. $this->customer->expects($this->once())
  79. ->method('loadByEmail')
  80. ->with(self::CUSTOMER_EMAIL)
  81. ->will($this->returnValue($this->customer));
  82. $this->customer->expects($this->any())
  83. ->method('getId')
  84. ->will($this->returnValue(self::CUSTOMER_ID));
  85. $this->customer->expects($this->any())
  86. ->method('getEmail')
  87. ->will($this->returnValue(self::CUSTOMER_EMAIL));
  88. $this->customer->expects($this->any())
  89. ->method('getWebsiteId')
  90. ->will($this->returnValue(self::WEBSITE_ID));
  91. $this->customer->expects($this->any())
  92. ->method('setEmail')
  93. ->will($this->returnValue($this->customer));
  94. $this->customer->expects($this->any())
  95. ->method('setWebsiteId')
  96. ->will($this->returnValue($this->customer));
  97. $this->customerFactory->expects($this->once())
  98. ->method('create')
  99. ->will($this->returnValue($this->customer));
  100. $actual = $this->customerRegistry->retrieveByEmail(self::CUSTOMER_EMAIL, self::WEBSITE_ID);
  101. $this->assertEquals($this->customer, $actual);
  102. $actualCached = $this->customerRegistry->retrieveByEmail(self::CUSTOMER_EMAIL, self::WEBSITE_ID);
  103. $this->assertEquals($this->customer, $actualCached);
  104. }
  105. /**
  106. * @expectedException \Magento\Framework\Exception\NoSuchEntityException
  107. */
  108. public function testRetrieveException()
  109. {
  110. $this->customer->expects($this->once())
  111. ->method('load')
  112. ->with(self::CUSTOMER_ID)
  113. ->will($this->returnValue($this->customer));
  114. $this->customer->expects($this->any())
  115. ->method('getId')
  116. ->will($this->returnValue(null));
  117. $this->customerFactory->expects($this->once())
  118. ->method('create')
  119. ->will($this->returnValue($this->customer));
  120. $this->customerRegistry->retrieve(self::CUSTOMER_ID);
  121. }
  122. /**
  123. * @expectedException \Magento\Framework\Exception\NoSuchEntityException
  124. */
  125. public function testRetrieveByEmailException()
  126. {
  127. $this->customer->expects($this->once())
  128. ->method('loadByEmail')
  129. ->with(self::CUSTOMER_EMAIL)
  130. ->will($this->returnValue($this->customer));
  131. $this->customer->expects($this->any())
  132. ->method('getEmail')
  133. ->will($this->returnValue(null));
  134. $this->customer->expects($this->any())
  135. ->method('getWebsiteId')
  136. ->will($this->returnValue(null));
  137. $this->customer->expects($this->any())
  138. ->method('setEmail')
  139. ->will($this->returnValue($this->customer));
  140. $this->customer->expects($this->any())
  141. ->method('setWebsiteId')
  142. ->will($this->returnValue($this->customer));
  143. $this->customerFactory->expects($this->once())
  144. ->method('create')
  145. ->will($this->returnValue($this->customer));
  146. $this->customerRegistry->retrieveByEmail(self::CUSTOMER_EMAIL, self::WEBSITE_ID);
  147. }
  148. public function testRemove()
  149. {
  150. $this->customer->expects($this->exactly(2))
  151. ->method('load')
  152. ->with(self::CUSTOMER_ID)
  153. ->will($this->returnValue($this->customer));
  154. $this->customer->expects($this->any())
  155. ->method('getId')
  156. ->will($this->returnValue(self::CUSTOMER_ID));
  157. $this->customerFactory->expects($this->exactly(2))
  158. ->method('create')
  159. ->will($this->returnValue($this->customer));
  160. $actual = $this->customerRegistry->retrieve(self::CUSTOMER_ID);
  161. $this->assertEquals($this->customer, $actual);
  162. $this->customerRegistry->remove(self::CUSTOMER_ID);
  163. $actual = $this->customerRegistry->retrieve(self::CUSTOMER_ID);
  164. $this->assertEquals($this->customer, $actual);
  165. }
  166. public function testRemoveByEmail()
  167. {
  168. $this->customer->expects($this->exactly(2))
  169. ->method('loadByEmail')
  170. ->with(self::CUSTOMER_EMAIL)
  171. ->will($this->returnValue($this->customer));
  172. $this->customer->expects($this->any())
  173. ->method('getId')
  174. ->will($this->returnValue(self::CUSTOMER_ID));
  175. $this->customer->expects($this->any())
  176. ->method('getEmail')
  177. ->will($this->returnValue(self::CUSTOMER_EMAIL));
  178. $this->customer->expects($this->any())
  179. ->method('getWebsiteId')
  180. ->will($this->returnValue(self::WEBSITE_ID));
  181. $this->customer->expects($this->any())
  182. ->method('setEmail')
  183. ->will($this->returnValue($this->customer));
  184. $this->customer->expects($this->any())
  185. ->method('setWebsiteId')
  186. ->will($this->returnValue($this->customer));
  187. $this->customerFactory->expects($this->exactly(2))
  188. ->method('create')
  189. ->will($this->returnValue($this->customer));
  190. $actual = $this->customerRegistry->retrieveByEmail(self::CUSTOMER_EMAIL, self::WEBSITE_ID);
  191. $this->assertEquals($this->customer, $actual);
  192. $this->customerRegistry->removeByEmail(self::CUSTOMER_EMAIL, self::WEBSITE_ID);
  193. $actual = $this->customerRegistry->retrieveByEmail(self::CUSTOMER_EMAIL, self::WEBSITE_ID);
  194. $this->assertEquals($this->customer, $actual);
  195. }
  196. }