Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CalculatorFactoryTest.php
Go to the documentation of this file.
1 <?php
7 
8 use \Magento\Tax\Model\Calculation\CalculatorFactory;
9 
10 use Magento\Customer\Api\Data\AddressInterface as CustomerAddress;
12 
16 class CalculatorFactoryTest extends \PHPUnit\Framework\TestCase
17 {
22 
23  protected function setUp()
24  {
25  $this->objectManager = new ObjectManager($this);
26  }
27 
40  public function testCreate(
41  $type,
42  $storeId,
45  $customerTaxClassId,
47  $expectedInstanceType
48  ) {
49  $instanceMock = $this->getMockBuilder($expectedInstanceType)->disableOriginalConstructor()->getMock();
50  $objectManagerMock = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
51 
52  // Verify create() is called with correct concrete type
53  $objectManagerMock->expects($this->once())
54  ->method('create')
55  ->with($expectedInstanceType, ['storeId' => $storeId])
56  ->will($this->returnValue($instanceMock));
57 
59  $calculatorFactory = $this->objectManager->getObject(
60  \Magento\Tax\Model\Calculation\CalculatorFactory::class,
61  ['objectManager' => $objectManagerMock]
62  );
63 
64  // Verify billing is set correctly if passed in
65  if ($billingAddress != null) {
66  $instanceMock->expects($this->once())
67  ->method('setBillingAddress')
68  ->with($billingAddress);
69  } else {
70  $instanceMock->expects($this->never())
71  ->method('setBillingAddress');
72  }
73 
74  // Verify shipping is set correctly if passed in
75  if ($shippingAddress != null) {
76  $instanceMock->expects($this->once())
77  ->method('setShippingAddress')
78  ->with($shippingAddress);
79  } else {
80  $instanceMock->expects($this->never())
81  ->method('setShippingAddress');
82  }
83 
84  // Verify customerTaxClassId is set correctly if passed in
85  if ($customerTaxClassId != null) {
86  $instanceMock->expects($this->once())
87  ->method('setCustomerTaxClassId')
88  ->with($customerTaxClassId);
89  } else {
90  $instanceMock->expects($this->never())
91  ->method('setCustomerTaxClassId');
92  }
93 
94  // Verify customerId is set correctly if passed in
95  if ($customerId != null) {
96  $instanceMock->expects($this->once())
97  ->method('setCustomerId')
98  ->with($customerId);
99  } else {
100  $instanceMock->expects($this->never())
101  ->method('setCustomerId');
102  }
103 
104  // Call create()
105  $calculator = $calculatorFactory
106  ->create($type, $storeId, $billingAddress, $shippingAddress, $customerTaxClassId, $customerId);
107 
108  // Verify correct type is returned
109  $this->assertInstanceOf($expectedInstanceType, $calculator);
110  }
111 
117  public function createDataProvider()
118  {
119  $billingAddressMock = $this->getMockBuilder(\Magento\Customer\Api\Data\AddressInterface::class)
120  ->disableOriginalConstructor()
121  ->getMockForAbstractClass();
122  $shippingAddressMock = $this->getMockBuilder(\Magento\Customer\Api\Data\AddressInterface::class)
123  ->disableOriginalConstructor()
124  ->getMockForAbstractClass();
125 
126  return [
127  'Unit' => [
129  1,
130  null,
131  null,
132  null,
133  null, \Magento\Tax\Model\Calculation\UnitBaseCalculator::class,
134  ],
135  'Row HasBilling' => [
137  2,
138  $billingAddressMock,
139  null,
140  null,
141  null, \Magento\Tax\Model\Calculation\RowBaseCalculator::class,
142  ],
143  'Row HasCustomerTaxClassId' => [
145  3,
146  null,
147  null,
148  123,
149  null, \Magento\Tax\Model\Calculation\RowBaseCalculator::class,
150  ],
151  'Total HasShipping' => [
153  1,
154  null,
155  $shippingAddressMock,
156  null,
157  null, \Magento\Tax\Model\Calculation\TotalBaseCalculator::class,
158  ],
159  'Total HasShipping HasBilling HasCustomerTaxClassId' => [
161  1,
162  $billingAddressMock,
163  $shippingAddressMock,
164  1,
165  null, \Magento\Tax\Model\Calculation\TotalBaseCalculator::class,
166  ],
167  'Total HasShipping HasBilling HasCustomerTaxClassId, HasCustomer' => [
169  1,
170  $billingAddressMock,
171  $shippingAddressMock,
172  1,
173  1, \Magento\Tax\Model\Calculation\TotalBaseCalculator::class,
174  ],
175  ];
176  }
177 
182  public function testCreateInvalid()
183  {
185  $calculatorFactory = $this->objectManager->getObject(
186  \Magento\Tax\Model\Calculation\CalculatorFactory::class
187  );
188 
189  // Call create() with a bad type to generate exception
190  $calculatorFactory->create('NOT_A_TYPE', 1);
191  }
192 }
$billingAddress
Definition: order.php:25
$shippingAddress
Definition: order.php:40
$type
Definition: item.phtml:13