Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
TaxAddressManagerTest.php
Go to the documentation of this file.
1 <?php
7 
10 use \PHPUnit_Framework_MockObject_MockObject as MockObject;
11 
12 class TaxAddressManagerTest extends \PHPUnit\Framework\TestCase
13 {
17  private $objectManager;
18 
22  private $manager;
23 
27  private $customerSessionMock;
28 
29  protected function setUp()
30  {
31  $this->objectManager = new ObjectManager($this);
32 
33  $this->customerSessionMock = $this->getMockBuilder(\Magento\Customer\Model\Session::class)
34  ->disableOriginalConstructor()
35  ->setMethods(['setDefaultTaxBillingAddress', 'setDefaultTaxShippingAddress'])
36  ->getMock();
37 
38  $this->manager = $this->objectManager->getObject(
39  TaxAddressManager::class,
40  [
41  'customerSession' => $this->customerSessionMock,
42  ]
43  );
44  }
45 
57  $addressId,
58  $billingInfo,
59  $shippingInfo,
60  $needSetShipping,
61  $needSetBilling
62  ) {
63  list($customerDefBillAddId, $isPrimaryBilling, $isDefaultBilling) = $billingInfo;
64  list($customerDefShipAddId, $isPrimaryShipping, $isDefaultShipping) = $shippingInfo;
65 
66  /* @var \Magento\Customer\Model\Address|\PHPUnit_Framework_MockObject_MockObject $address */
67  $address = $this->getMockBuilder(\Magento\Customer\Model\Address::class)
68  ->setMethods([
69  'getId',
70  'getCustomer',
71  'getIsPrimaryBilling',
72  'getIsDefaultBilling',
73  'getIsPrimaryShipping',
74  'getIsDefaultShipping',
75  'getCountryId',
76  'getRegion',
77  'getPostcode',
78  ])
79  ->disableOriginalConstructor()
80  ->getMock();
81 
82  $address->expects($this->any())->method('getCountryId')->willReturn(1);
83  $address->expects($this->any())->method('getRegion')->willReturn(null);
84  $address->expects($this->any())->method('getPostcode')->willReturn('11111');
85 
86  $address->expects($this->any())->method('getId')->willReturn($addressId);
87  $address->expects($this->any())->method('getIsPrimaryBilling')->willReturn($isPrimaryBilling);
88  $address->expects($this->any())->method('getIsDefaultBilling')->willReturn($isDefaultBilling);
89  $address->expects($this->any())->method('getIsPrimaryShipping')->willReturn($isPrimaryShipping);
90  $address->expects($this->any())->method('getIsDefaultShipping')->willReturn($isDefaultShipping);
91 
92  /* @var \Magento\Customer\Model\Customer|\PHPUnit_Framework_MockObject_MockObject $customer */
93  $customer = $this->getMockBuilder(\Magento\Customer\Model\Customer::class)
94  ->setMethods(['getDefaultBilling', 'getDefaultShipping'])
95  ->disableOriginalConstructor()
96  ->getMock();
97  $customer->expects($this->any())->method('getDefaultBilling')->willReturn($customerDefBillAddId);
98  $customer->expects($this->any())->method('getDefaultShipping')->willReturn($customerDefShipAddId);
99 
100  $address->expects($this->any())->method('getCustomer')->willReturn($customer);
101 
102  $this->customerSessionMock->expects($needSetShipping ? $this->once() : $this->never())
103  ->method('setDefaultTaxShippingAddress')
104  ->with(['country_id' => 1, 'region_id' => null, 'postcode' => 11111]);
105  $this->customerSessionMock->expects($needSetBilling ? $this->once() : $this->never())
106  ->method('setDefaultTaxBillingAddress')
107  ->with(['country_id' => 1, 'region_id' => null, 'postcode' => 11111]);
108 
109  $this->manager->setDefaultAddressAfterSave($address);
110  }
111 
116  {
117  return [
118  [1, [1, false, false], [1, false, false], true, true],
119  [1, [2, false, false], [2, false, false], false, false],
120  [1, [2, false, true], [2, false, true], true, true],
121  [1, [2, true, false], [2, true, false], true, true],
122  ];
123  }
124 
133  $isAddressDefaultBilling,
134  $isAddressDefaultShipping
135  ) {
136  /* @var \Magento\Customer\Api\Data\AddressInterface|\PHPUnit_Framework_MockObject_MockObject $address */
137  $address = $this->getMockBuilder(\Magento\Customer\Api\Data\AddressInterface::class)
138  ->disableOriginalConstructor()
139  ->getMock();
140 
141  $address->expects($this->any())->method('getCountryId')->willReturn(1);
142  $address->expects($this->any())->method('getRegion')->willReturn(null);
143  $address->expects($this->any())->method('getPostcode')->willReturn('11111');
144  $address->expects($this->any())->method('isDefaultShipping')->willReturn($isAddressDefaultShipping);
145  $address->expects($this->any())->method('isDefaultBilling')->willReturn($isAddressDefaultBilling);
146 
147  $this->customerSessionMock->expects($isAddressDefaultShipping ? $this->once() : $this->never())
148  ->method('setDefaultTaxShippingAddress')
149  ->with(['country_id' => 1, 'region_id' => null, 'postcode' => 11111]);
150  $this->customerSessionMock->expects($isAddressDefaultBilling ? $this->once() : $this->never())
151  ->method('setDefaultTaxBillingAddress')
152  ->with(['country_id' => 1, 'region_id' => null, 'postcode' => 11111]);
153 
154  $this->manager->setDefaultAddressAfterLogIn([$address]);
155  }
156 
161  {
162  return [
163  [false, false],
164  [false, true],
165  [true, false],
166  [true, true],
167  ];
168  }
169 }
$customer
Definition: customers.php:11
$address
Definition: customer.php:38
testSetDefaultAddressAfterLogIn( $isAddressDefaultBilling, $isAddressDefaultShipping)
testSetDefaultAddressAfterSave( $addressId, $billingInfo, $shippingInfo, $needSetShipping, $needSetBilling)