TaxAddressManagerTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Tax\Test\Unit\Model;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  8. use Magento\Tax\Model\TaxAddressManager;
  9. use \PHPUnit_Framework_MockObject_MockObject as MockObject;
  10. class TaxAddressManagerTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var ObjectManager
  14. */
  15. private $objectManager;
  16. /**
  17. * @var TaxAddressManager
  18. */
  19. private $manager;
  20. /**
  21. * @var \Magento\Customer\Model\Session|MockObject
  22. */
  23. private $customerSessionMock;
  24. protected function setUp()
  25. {
  26. $this->objectManager = new ObjectManager($this);
  27. $this->customerSessionMock = $this->getMockBuilder(\Magento\Customer\Model\Session::class)
  28. ->disableOriginalConstructor()
  29. ->setMethods(['setDefaultTaxBillingAddress', 'setDefaultTaxShippingAddress'])
  30. ->getMock();
  31. $this->manager = $this->objectManager->getObject(
  32. TaxAddressManager::class,
  33. [
  34. 'customerSession' => $this->customerSessionMock,
  35. ]
  36. );
  37. }
  38. /**
  39. * @test
  40. * @dataProvider setAddressCustomerSessionAddressSaveDataProvider
  41. *
  42. * @param array $addressId
  43. * @param array $billingInfo
  44. * @param array $shippingInfo
  45. * @param bool $needSetShipping
  46. * @param bool $needSetBilling
  47. */
  48. public function testSetDefaultAddressAfterSave(
  49. $addressId,
  50. $billingInfo,
  51. $shippingInfo,
  52. $needSetShipping,
  53. $needSetBilling
  54. ) {
  55. list($customerDefBillAddId, $isPrimaryBilling, $isDefaultBilling) = $billingInfo;
  56. list($customerDefShipAddId, $isPrimaryShipping, $isDefaultShipping) = $shippingInfo;
  57. /* @var \Magento\Customer\Model\Address|\PHPUnit_Framework_MockObject_MockObject $address */
  58. $address = $this->getMockBuilder(\Magento\Customer\Model\Address::class)
  59. ->setMethods([
  60. 'getId',
  61. 'getCustomer',
  62. 'getIsPrimaryBilling',
  63. 'getIsDefaultBilling',
  64. 'getIsPrimaryShipping',
  65. 'getIsDefaultShipping',
  66. 'getCountryId',
  67. 'getRegion',
  68. 'getPostcode',
  69. ])
  70. ->disableOriginalConstructor()
  71. ->getMock();
  72. $address->expects($this->any())->method('getCountryId')->willReturn(1);
  73. $address->expects($this->any())->method('getRegion')->willReturn(null);
  74. $address->expects($this->any())->method('getPostcode')->willReturn('11111');
  75. $address->expects($this->any())->method('getId')->willReturn($addressId);
  76. $address->expects($this->any())->method('getIsPrimaryBilling')->willReturn($isPrimaryBilling);
  77. $address->expects($this->any())->method('getIsDefaultBilling')->willReturn($isDefaultBilling);
  78. $address->expects($this->any())->method('getIsPrimaryShipping')->willReturn($isPrimaryShipping);
  79. $address->expects($this->any())->method('getIsDefaultShipping')->willReturn($isDefaultShipping);
  80. /* @var \Magento\Customer\Model\Customer|\PHPUnit_Framework_MockObject_MockObject $customer */
  81. $customer = $this->getMockBuilder(\Magento\Customer\Model\Customer::class)
  82. ->setMethods(['getDefaultBilling', 'getDefaultShipping'])
  83. ->disableOriginalConstructor()
  84. ->getMock();
  85. $customer->expects($this->any())->method('getDefaultBilling')->willReturn($customerDefBillAddId);
  86. $customer->expects($this->any())->method('getDefaultShipping')->willReturn($customerDefShipAddId);
  87. $address->expects($this->any())->method('getCustomer')->willReturn($customer);
  88. $this->customerSessionMock->expects($needSetShipping ? $this->once() : $this->never())
  89. ->method('setDefaultTaxShippingAddress')
  90. ->with(['country_id' => 1, 'region_id' => null, 'postcode' => 11111]);
  91. $this->customerSessionMock->expects($needSetBilling ? $this->once() : $this->never())
  92. ->method('setDefaultTaxBillingAddress')
  93. ->with(['country_id' => 1, 'region_id' => null, 'postcode' => 11111]);
  94. $this->manager->setDefaultAddressAfterSave($address);
  95. }
  96. /**
  97. * @return array
  98. */
  99. public function setAddressCustomerSessionAddressSaveDataProvider()
  100. {
  101. return [
  102. [1, [1, false, false], [1, false, false], true, true],
  103. [1, [2, false, false], [2, false, false], false, false],
  104. [1, [2, false, true], [2, false, true], true, true],
  105. [1, [2, true, false], [2, true, false], true, true],
  106. ];
  107. }
  108. /**
  109. * @test
  110. * @dataProvider setAddressCustomerSessionLogInDataProvider
  111. *
  112. * @param bool $isAddressDefaultBilling
  113. * @param bool $isAddressDefaultShipping
  114. */
  115. public function testSetDefaultAddressAfterLogIn(
  116. $isAddressDefaultBilling,
  117. $isAddressDefaultShipping
  118. ) {
  119. /* @var \Magento\Customer\Api\Data\AddressInterface|\PHPUnit_Framework_MockObject_MockObject $address */
  120. $address = $this->getMockBuilder(\Magento\Customer\Api\Data\AddressInterface::class)
  121. ->disableOriginalConstructor()
  122. ->getMock();
  123. $address->expects($this->any())->method('getCountryId')->willReturn(1);
  124. $address->expects($this->any())->method('getRegion')->willReturn(null);
  125. $address->expects($this->any())->method('getPostcode')->willReturn('11111');
  126. $address->expects($this->any())->method('isDefaultShipping')->willReturn($isAddressDefaultShipping);
  127. $address->expects($this->any())->method('isDefaultBilling')->willReturn($isAddressDefaultBilling);
  128. $this->customerSessionMock->expects($isAddressDefaultShipping ? $this->once() : $this->never())
  129. ->method('setDefaultTaxShippingAddress')
  130. ->with(['country_id' => 1, 'region_id' => null, 'postcode' => 11111]);
  131. $this->customerSessionMock->expects($isAddressDefaultBilling ? $this->once() : $this->never())
  132. ->method('setDefaultTaxBillingAddress')
  133. ->with(['country_id' => 1, 'region_id' => null, 'postcode' => 11111]);
  134. $this->manager->setDefaultAddressAfterLogIn([$address]);
  135. }
  136. /**
  137. * @return array
  138. */
  139. public function setAddressCustomerSessionLogInDataProvider()
  140. {
  141. return [
  142. [false, false],
  143. [false, true],
  144. [true, false],
  145. [true, true],
  146. ];
  147. }
  148. }