EmulateCustomerObserverTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Persistent\Test\Unit\Observer;
  8. class EmulateCustomerObserverTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var \Magento\Persistent\Observer\EmulateCustomerObserver
  12. */
  13. protected $model;
  14. /**
  15. * @var \PHPUnit_Framework_MockObject_MockObject
  16. */
  17. protected $customerRepositoryMock;
  18. /**
  19. * @var \PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $customerSessionMock;
  22. /**
  23. * @var \PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $sessionHelperMock;
  26. /**
  27. * @var \PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $helperMock;
  30. /**
  31. * @var \PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected $observerMock;
  34. /**
  35. * @var \PHPUnit_Framework_MockObject_MockObject
  36. */
  37. protected $addressRepositoryMock;
  38. protected function setUp()
  39. {
  40. $this->customerRepositoryMock = $this->getMockForAbstractClass(
  41. \Magento\Customer\Api\CustomerRepositoryInterface::class,
  42. [],
  43. '',
  44. false
  45. );
  46. $methods = [
  47. 'setDefaultTaxShippingAddress',
  48. 'setDefaultTaxBillingAddress',
  49. 'setCustomerId',
  50. 'setCustomerGroupId',
  51. 'isLoggedIn',
  52. 'setIsCustomerEmulated',
  53. '__wakeUp'
  54. ];
  55. $this->customerSessionMock = $this->createPartialMock(\Magento\Customer\Model\Session::class, $methods);
  56. $this->sessionHelperMock = $this->createMock(\Magento\Persistent\Helper\Session::class);
  57. $this->helperMock = $this->createMock(\Magento\Persistent\Helper\Data::class);
  58. $this->observerMock = $this->createMock(\Magento\Framework\Event\Observer::class);
  59. $this->addressRepositoryMock = $this->createMock(\Magento\Customer\Api\AddressRepositoryInterface::class);
  60. $this->model = new \Magento\Persistent\Observer\EmulateCustomerObserver(
  61. $this->sessionHelperMock,
  62. $this->helperMock,
  63. $this->customerSessionMock,
  64. $this->customerRepositoryMock,
  65. $this->addressRepositoryMock
  66. );
  67. }
  68. public function testExecuteWhenCannotProcessPersistentData()
  69. {
  70. $this->helperMock
  71. ->expects($this->once())
  72. ->method('canProcess')
  73. ->with($this->observerMock)
  74. ->will($this->returnValue(false));
  75. $this->helperMock->expects($this->never())->method('isShoppingCartPersist');
  76. $this->sessionHelperMock->expects($this->never())->method('isPersistent');
  77. $this->model->execute($this->observerMock);
  78. }
  79. public function testExecuteWhenShoppingCartNotPersist()
  80. {
  81. $this->helperMock
  82. ->expects($this->once())
  83. ->method('canProcess')
  84. ->with($this->observerMock)
  85. ->will($this->returnValue(true));
  86. $this->helperMock->expects($this->once())->method('isShoppingCartPersist')->will($this->returnValue(false));
  87. $this->sessionHelperMock->expects($this->never())->method('isPersistent');
  88. $this->model->execute($this->observerMock);
  89. }
  90. public function testExecuteWhenSessionPersistAndCustomerNotLoggedIn()
  91. {
  92. $customerId = 1;
  93. $customerGroupId = 2;
  94. $countryId = 3;
  95. $regionId = 4;
  96. $postcode = 90210;
  97. $sessionMock = $this->createPartialMock(
  98. \Magento\Persistent\Model\Session::class,
  99. ['getCustomerId', '__wakeUp']
  100. );
  101. $methods = ['getCountryId', 'getRegion', 'getRegionId', 'getPostcode'];
  102. $defaultShippingAddressMock = $this->createPartialMock(\Magento\Customer\Model\Address::class, $methods);
  103. $defaultBillingAddressMock = $this->createPartialMock(\Magento\Customer\Model\Address::class, $methods);
  104. $customerMock = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
  105. $customerMock
  106. ->expects($this->once())
  107. ->method('getDefaultShipping')
  108. ->willReturn('shippingId');
  109. $customerMock
  110. ->expects($this->once())
  111. ->method('getDefaultBilling')
  112. ->willReturn('billingId');
  113. $valueMap = [
  114. ['shippingId', $defaultShippingAddressMock],
  115. ['billingId', $defaultBillingAddressMock]
  116. ];
  117. $this->addressRepositoryMock->expects($this->any())->method('getById')->willReturnMap($valueMap);
  118. $this->customerSessionMock
  119. ->expects($this->once())
  120. ->method('setDefaultTaxShippingAddress')
  121. ->with(
  122. [
  123. 'country_id' => $countryId,
  124. 'region_id' => $regionId,
  125. 'postcode' => $postcode
  126. ]
  127. );
  128. $defaultBillingAddressMock->expects($this->once())->method('getCountryId')->willReturn($countryId);
  129. $defaultBillingAddressMock->expects($this->once())->method('getRegion')->willReturn('California');
  130. $defaultBillingAddressMock->expects($this->once())->method('getRegionId')->willReturn($regionId);
  131. $defaultBillingAddressMock->expects($this->once())->method('getPostcode')->willReturn($postcode);
  132. $defaultShippingAddressMock->expects($this->once())->method('getCountryId')->willReturn($countryId);
  133. $defaultShippingAddressMock->expects($this->once())->method('getRegion')->willReturn('California');
  134. $defaultShippingAddressMock->expects($this->once())->method('getRegionId')->willReturn($regionId);
  135. $defaultShippingAddressMock->expects($this->once())->method('getPostcode')->willReturn($postcode);
  136. $this->helperMock
  137. ->expects($this->once())
  138. ->method('canProcess')
  139. ->with($this->observerMock)
  140. ->will($this->returnValue(true));
  141. $this->helperMock->expects($this->once())->method('isShoppingCartPersist')->will($this->returnValue(true));
  142. $this->sessionHelperMock->expects($this->once())->method('isPersistent')->will($this->returnValue(true));
  143. $this->customerSessionMock->expects($this->once())->method('isLoggedIn')->will($this->returnValue(false));
  144. $this->sessionHelperMock->expects($this->once())->method('getSession')->will($this->returnValue($sessionMock));
  145. $sessionMock->expects($this->once())->method('getCustomerId')->will($this->returnValue($customerId));
  146. $this->customerRepositoryMock
  147. ->expects($this->once())
  148. ->method('getById')
  149. ->with(1)
  150. ->will($this->returnValue($customerMock));
  151. $customerMock->expects($this->once())->method('getId')->will($this->returnValue($customerId));
  152. $customerMock->expects($this->once())->method('getGroupId')->will($this->returnValue($customerGroupId));
  153. $this->customerSessionMock
  154. ->expects($this->once())
  155. ->method('setCustomerId')
  156. ->with($customerId)
  157. ->will($this->returnSelf());
  158. $this->customerSessionMock
  159. ->expects($this->once())
  160. ->method('setCustomerGroupId')
  161. ->with($customerGroupId)->will($this->returnSelf());
  162. $this->customerSessionMock
  163. ->expects($this->once())
  164. ->method('setIsCustomerEmulated')
  165. ->with(true)
  166. ->will($this->returnSelf());
  167. $this->model->execute($this->observerMock);
  168. }
  169. public function testExecuteWhenSessionNotPersist()
  170. {
  171. $this->helperMock
  172. ->expects($this->once())
  173. ->method('canProcess')
  174. ->with($this->observerMock)
  175. ->will($this->returnValue(true));
  176. $this->helperMock->expects($this->once())->method('isShoppingCartPersist')->will($this->returnValue(true));
  177. $this->sessionHelperMock->expects($this->once())->method('isPersistent')->will($this->returnValue(true));
  178. $this->customerSessionMock->expects($this->once())->method('isLoggedIn')->will($this->returnValue(true));
  179. $this->customerRepositoryMock
  180. ->expects($this->never())
  181. ->method('get');
  182. $this->model->execute($this->observerMock);
  183. }
  184. }