CustomerManagementTest.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Quote\Test\Unit\Model;
  7. /**
  8. * Class CustomerManagementTest
  9. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  10. */
  11. class CustomerManagementTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var \Magento\Quote\Model\CustomerManagement
  15. */
  16. protected $customerManagement;
  17. /**
  18. * @var \Magento\Customer\Api\CustomerRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $customerRepositoryMock;
  21. /**
  22. * @var \Magento\Customer\Api\AccountManagementInterface|\PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $accountManagementMock;
  25. /**
  26. * @var \Magento\Customer\Api\AddressRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
  27. */
  28. protected $customerAddressRepositoryMock;
  29. /**
  30. * @var \Magento\Quote\Model\Quote|\PHPUnit_Framework_MockObject_MockObject
  31. */
  32. protected $quoteMock;
  33. /**
  34. * @var \Magento\Quote\Model\Quote\Address|\PHPUnit_Framework_MockObject_MockObject
  35. */
  36. protected $quoteAddressMock;
  37. /**
  38. * @var \Magento\Customer\Api\Data\CustomerInterface|\PHPUnit_Framework_MockObject_MockObject
  39. */
  40. protected $customerMock;
  41. /**
  42. * @var \Magento\Customer\Api\Data\AddressInterface|\PHPUnit_Framework_MockObject_MockObject
  43. */
  44. protected $customerAddressMock;
  45. /**
  46. * @var \PHPUnit_Framework_MockObject_MockObject
  47. */
  48. private $validatorFactoryMock;
  49. /**
  50. * @var \PHPUnit_Framework_MockObject_MockObject
  51. */
  52. private $addressFactoryMock;
  53. protected function setUp()
  54. {
  55. $this->customerRepositoryMock = $this->getMockForAbstractClass(
  56. \Magento\Customer\Api\CustomerRepositoryInterface::class,
  57. [],
  58. '',
  59. false,
  60. true,
  61. true,
  62. ['getById']
  63. );
  64. $this->customerAddressRepositoryMock = $this->getMockForAbstractClass(
  65. \Magento\Customer\Api\AddressRepositoryInterface::class,
  66. [],
  67. '',
  68. false,
  69. true,
  70. true,
  71. ['getById']
  72. );
  73. $this->accountManagementMock = $this->getMockForAbstractClass(
  74. \Magento\Customer\Api\AccountManagementInterface::class,
  75. [],
  76. '',
  77. false,
  78. true,
  79. true,
  80. []
  81. );
  82. $this->quoteMock = $this->createPartialMock(
  83. \Magento\Quote\Model\Quote::class,
  84. ['getId', 'getCustomer', 'getBillingAddress', 'getShippingAddress', 'setCustomer', 'getPasswordHash']
  85. );
  86. $this->quoteAddressMock = $this->createMock(\Magento\Quote\Model\Quote\Address::class);
  87. $this->customerMock = $this->getMockForAbstractClass(
  88. \Magento\Customer\Api\Data\CustomerInterface::class,
  89. [],
  90. '',
  91. false,
  92. true,
  93. true,
  94. ['getId', 'getDefaultBilling']
  95. );
  96. $this->customerAddressMock = $this->getMockForAbstractClass(
  97. \Magento\Customer\Api\Data\AddressInterface::class,
  98. [],
  99. '',
  100. false,
  101. true,
  102. true,
  103. []
  104. );
  105. $this->addressFactoryMock = $this->getMockBuilder(\Magento\Customer\Model\AddressFactory::class)
  106. ->disableOriginalConstructor()
  107. ->setMethods(['create'])
  108. ->getMock();
  109. $this->validatorFactoryMock = $this->getMockBuilder(\Magento\Framework\Validator\Factory::class)
  110. ->disableOriginalConstructor()
  111. ->getMock();
  112. $this->customerManagement = new \Magento\Quote\Model\CustomerManagement(
  113. $this->customerRepositoryMock,
  114. $this->customerAddressRepositoryMock,
  115. $this->accountManagementMock,
  116. $this->validatorFactoryMock,
  117. $this->addressFactoryMock
  118. );
  119. }
  120. public function testPopulateCustomerInfo()
  121. {
  122. $this->quoteMock->expects($this->once())
  123. ->method('getCustomer')
  124. ->willReturn($this->customerMock);
  125. $this->customerMock->expects($this->atLeastOnce())
  126. ->method('getId')
  127. ->willReturn(null);
  128. $this->customerMock->expects($this->atLeastOnce())
  129. ->method('getDefaultBilling')
  130. ->willReturn(100500);
  131. $this->quoteMock->expects($this->atLeastOnce())
  132. ->method('getBillingAddress')
  133. ->willReturn($this->quoteAddressMock);
  134. $this->quoteMock->expects($this->atLeastOnce())
  135. ->method('getShippingAddress')
  136. ->willReturn($this->quoteAddressMock);
  137. $this->quoteMock->expects($this->atLeastOnce())
  138. ->method('setCustomer')
  139. ->with($this->customerMock)
  140. ->willReturnSelf();
  141. $this->quoteMock->expects($this->once())
  142. ->method('getPasswordHash')
  143. ->willReturn('password hash');
  144. $this->quoteAddressMock->expects($this->atLeastOnce())
  145. ->method('getId')
  146. ->willReturn(null);
  147. $this->customerAddressRepositoryMock->expects($this->atLeastOnce())
  148. ->method('getById')
  149. ->with(100500)
  150. ->willReturn($this->customerAddressMock);
  151. $this->quoteAddressMock->expects($this->atLeastOnce())
  152. ->method('importCustomerAddressData')
  153. ->willReturnSelf();
  154. $this->accountManagementMock->expects($this->once())
  155. ->method('createAccountWithPasswordHash')
  156. ->with($this->customerMock, 'password hash')
  157. ->willReturn($this->customerMock);
  158. $this->customerManagement->populateCustomerInfo($this->quoteMock);
  159. }
  160. public function testPopulateCustomerInfoForExistingCustomer()
  161. {
  162. $this->quoteMock->expects($this->once())
  163. ->method('getCustomer')
  164. ->willReturn($this->customerMock);
  165. $this->customerMock->expects($this->atLeastOnce())
  166. ->method('getId')
  167. ->willReturn(1);
  168. $this->customerMock->expects($this->atLeastOnce())
  169. ->method('getDefaultBilling')
  170. ->willReturn(100500);
  171. $this->quoteMock->expects($this->atLeastOnce())
  172. ->method('getBillingAddress')
  173. ->willReturn($this->quoteAddressMock);
  174. $this->quoteMock->expects($this->atLeastOnce())
  175. ->method('getShippingAddress')
  176. ->willReturn($this->quoteAddressMock);
  177. $this->quoteAddressMock->expects($this->atLeastOnce())
  178. ->method('getId')
  179. ->willReturn(null);
  180. $this->customerAddressRepositoryMock->expects($this->atLeastOnce())
  181. ->method('getById')
  182. ->with(100500)
  183. ->willReturn($this->customerAddressMock);
  184. $this->quoteAddressMock->expects($this->atLeastOnce())
  185. ->method('importCustomerAddressData')
  186. ->willReturnSelf();
  187. $this->customerManagement->populateCustomerInfo($this->quoteMock);
  188. }
  189. public function testValidateAddresses()
  190. {
  191. $this->quoteMock
  192. ->expects($this->exactly(2))
  193. ->method('getBillingAddress')
  194. ->willReturn($this->quoteAddressMock);
  195. $this->quoteMock
  196. ->expects($this->exactly(2))
  197. ->method('getShippingAddress')
  198. ->willReturn($this->quoteAddressMock);
  199. $this->quoteAddressMock->expects($this->any())->method('getCustomerAddressId')->willReturn(2);
  200. $this->customerAddressRepositoryMock
  201. ->expects($this->any())
  202. ->method('getById')
  203. ->willReturn($this->customerAddressMock);
  204. $validatorMock = $this->getMockBuilder(\Magento\Framework\Validator::class)
  205. ->disableOriginalConstructor()
  206. ->getMock();
  207. $addressMock = $this->getMockBuilder(\Magento\Customer\Model\Address::class)
  208. ->disableOriginalConstructor()
  209. ->getMock();
  210. $this->addressFactoryMock->expects($this->exactly(2))->method('create')->willReturn($addressMock);
  211. $this->validatorFactoryMock
  212. ->expects($this->exactly(2))
  213. ->method('createValidator')
  214. ->with('customer_address', 'save', null)
  215. ->willReturn($validatorMock);
  216. $validatorMock->expects($this->exactly(2))->method('isValid')->with($addressMock)->willReturn(true);
  217. $this->customerManagement->validateAddresses($this->quoteMock);
  218. }
  219. public function testValidateAddressesNotSavedInAddressBook()
  220. {
  221. $this->quoteMock
  222. ->expects($this->once())
  223. ->method('getBillingAddress')
  224. ->willReturn($this->quoteAddressMock);
  225. $this->quoteMock
  226. ->expects($this->once())
  227. ->method('getShippingAddress')
  228. ->willReturn($this->quoteAddressMock);
  229. $this->quoteAddressMock->expects($this->any())->method('getCustomerAddressId')->willReturn(null);
  230. $this->validatorFactoryMock
  231. ->expects($this->never())
  232. ->method('createValidator')
  233. ->with('customer_address', 'save', null);
  234. $this->customerManagement->validateAddresses($this->quoteMock);
  235. }
  236. }