Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
TaxTest.php
Go to the documentation of this file.
1 <?php
7 
13 class TaxTest extends \PHPUnit\Framework\TestCase
14 {
18  protected $model;
19 
23  protected $context;
24 
28  protected $registry;
29 
33  protected $attributeFactory;
34 
38  protected $storeManager;
39 
44 
48  protected $customerSession;
49 
53  protected $accountManagement;
54 
58  protected $taxData;
59 
63  protected $resource;
64 
68  protected $weeeConfig;
69 
73  protected $priceCurrency;
74 
79 
83  protected $data;
84 
88  protected $objectManager;
89 
93  protected function setUp()
94  {
95  $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
96 
97  $className = \Magento\Framework\Model\Context::class;
98  $this->context = $this->createMock($className);
99 
100  $className = \Magento\Framework\Registry::class;
101  $this->registry = $this->createMock($className);
102 
103  $className = \Magento\Eav\Model\Entity\AttributeFactory::class;
104  $this->attributeFactory = $this->createPartialMock($className, ['create']);
105 
106  $className = \Magento\Store\Model\StoreManagerInterface::class;
107  $this->storeManager = $this->createMock($className);
108 
109  $className = \Magento\Tax\Model\CalculationFactory::class;
110  $this->calculationFactory = $this->createPartialMock($className, ['create']);
111 
112  $className = \Magento\Customer\Model\Session::class;
113  $this->customerSession = $this->createPartialMock(
114  $className,
115  ['getCustomerId', 'getDefaultTaxShippingAddress', 'getDefaultTaxBillingAddress', 'getCustomerTaxClassId']
116  );
117  $this->customerSession->expects($this->any())->method('getCustomerId')->willReturn(null);
118  $this->customerSession->expects($this->any())->method('getDefaultTaxShippingAddress')->willReturn(null);
119  $this->customerSession->expects($this->any())->method('getDefaultTaxBillingAddress')->willReturn(null);
120  $this->customerSession->expects($this->any())->method('getCustomerTaxClassId')->willReturn(null);
121 
122  $className = \Magento\Customer\Api\AccountManagementInterface::class;
123  $this->accountManagement = $this->createMock($className);
124 
125  $className = \Magento\Tax\Helper\Data::class;
126  $this->taxData = $this->createMock($className);
127 
128  $className = \Magento\Weee\Model\ResourceModel\Tax::class;
129  $this->resource = $this->createMock($className);
130 
131  $className = \Magento\Weee\Model\Config::class;
132  $this->weeeConfig = $this->createMock($className);
133 
134  $className = \Magento\Framework\Pricing\PriceCurrencyInterface::class;
135  $this->priceCurrency = $this->createMock($className);
136 
137  $className = \Magento\Framework\Data\Collection\AbstractDb::class;
138  $this->resourceCollection = $this->createMock($className);
139 
140  $this->model = $this->objectManager->getObject(
141  \Magento\Weee\Model\Tax::class,
142  [
143  'context' => $this->context,
144  'registry' => $this->registry,
145  'attributeFactory' => $this->attributeFactory,
146  'storeManager' => $this->storeManager,
147  'calculationFactory' => $this->calculationFactory,
148  'customerSession' => $this->customerSession,
149  'accountManagement' => $this->accountManagement,
150  'taxData' => $this->taxData,
151  'resource' => $this->resource,
152  'weeeConfig' => $this->weeeConfig,
153  'priceCurrency' => $this->priceCurrency,
154  'resourceCollection' => $this->resourceCollection,
155  ]
156  );
157  }
158 
167  array $weeeTaxCalculationsByEntity,
168  $websitePassed,
169  string $expectedFptLabel
170  ): void {
171  $product = $this->createMock(\Magento\Catalog\Model\Product::class);
172  $website = $this->createMock(\Magento\Store\Model\Website::class);
173  $store = $this->createMock(\Magento\Store\Model\Store::class);
174  $group = $this->createMock(\Magento\Store\Model\Group::class);
175 
176  $attribute = $this->createMock(\Magento\Eav\Model\Entity\Attribute::class);
177  $calculation = $this->createMock(\Magento\Tax\Model\Calculation::class);
178 
179  $obj = new \Magento\Framework\DataObject(['country' => 'US', 'region' => 'TX']);
180  $calculation->expects($this->once())
181  ->method('getRateRequest')
182  ->willReturn($obj);
183  $calculation->expects($this->once())
184  ->method('getDefaultRateRequest')
185  ->willReturn($obj);
186  $calculation->expects($this->any())
187  ->method('getRate')
188  ->willReturn('10');
189 
190  $attribute->expects($this->once())
191  ->method('getAttributeCodesByFrontendType')
192  ->willReturn(['0'=>'fpt']);
193 
194  $this->storeManager->expects($this->any())
195  ->method('getWebsite')
196  ->willReturn($website);
197  $website->expects($this->any())
198  ->method('getId')
199  ->willReturn($websitePassed);
200  $website->expects($this->any())
201  ->method('getDefaultGroup')
202  ->willReturn($group);
203  $group->expects($this->any())
204  ->method('getDefaultStore')
205  ->willReturn($store);
206  $store->expects($this->any())
207  ->method('getId')
208  ->willReturn(1);
209 
210  if ($websitePassed) {
211  $product->expects($this->never())
212  ->method('getStore')
213  ->willReturn($store);
214  } else {
215  $product->expects($this->once())
216  ->method('getStore')
217  ->willReturn($store);
218  $store->expects($this->once())
219  ->method('getWebsiteId')
220  ->willReturn(1);
221  }
222 
223  $product->expects($this->any())
224  ->method('getId')
225  ->willReturn(1);
226 
227  $this->weeeConfig->expects($this->any())
228  ->method('isEnabled')
229  ->willReturn(true);
230 
231  $this->weeeConfig->expects($this->any())
232  ->method('isTaxable')
233  ->willReturn(true);
234 
235  $this->attributeFactory->expects($this->any())
236  ->method('create')
237  ->willReturn($attribute);
238 
239  $this->calculationFactory->expects($this->any())
240  ->method('create')
241  ->willReturn($calculation);
242 
243  $this->priceCurrency->expects($this->any())
244  ->method('round')
245  ->with(0.1)
246  ->willReturn(0.25);
247 
248  $this->resource->expects($this->any())
249  ->method('fetchWeeeTaxCalculationsByEntity')
250  ->willReturn([
251  0 => $weeeTaxCalculationsByEntity
252  ]);
253 
254  $result = $this->model->getProductWeeeAttributes($product, null, null, $websitePassed, true);
255  $this->assertTrue(is_array($result));
256  $this->assertArrayHasKey(0, $result);
257  $obj = $result[0];
258  $this->assertEquals(1, $obj->getAmount());
259  $this->assertEquals(0.25, $obj->getTaxAmount());
260  $this->assertEquals($weeeTaxCalculationsByEntity['attribute_code'], $obj->getCode());
261  $this->assertEquals(__($expectedFptLabel), $obj->getName());
262  }
263 
271  public function testGetWeeeAmountExclTax($productTypeId, $productPriceType)
272  {
273  $product = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
274  ->disableOriginalConstructor()
275  ->setMethods(['getTypeId', 'getPriceType'])
276  ->getMock();
277  $product->expects($this->any())->method('getTypeId')->willReturn($productTypeId);
278  $product->expects($this->any())->method('getPriceType')->willReturn($productPriceType);
279  $weeeDataHelper = $this->getMockBuilder(\Magento\Framework\DataObject::class)
280  ->disableOriginalConstructor()
281  ->setMethods(['getAmountExclTax'])
282  ->getMock();
283  $weeeDataHelper->expects($this->at(0))->method('getAmountExclTax')->willReturn(10);
284  $weeeDataHelper->expects($this->at(1))->method('getAmountExclTax')->willReturn(30);
285  $tax = $this->getMockBuilder(\Magento\Weee\Model\Tax::class)
286  ->disableOriginalConstructor()
287  ->setMethods(['getProductWeeeAttributes'])
288  ->getMock();
289  $tax->expects($this->once())->method('getProductWeeeAttributes')
290  ->willReturn([$weeeDataHelper, $weeeDataHelper]);
291  $this->assertEquals(40, $tax->getWeeeAmountExclTax($product));
292  }
293 
298  {
299  $product = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
300  ->disableOriginalConstructor()
301  ->setMethods(['getTypeId', 'getPriceType'])
302  ->getMock();
303  $product->expects($this->once())->method('getTypeId')->willReturn('bundle');
304  $product->expects($this->once())->method('getPriceType')->willReturn(0);
305  $weeeDataHelper = $this->getMockBuilder(\Magento\Framework\DataObject::class)
306  ->disableOriginalConstructor()
307  ->getMock();
308  $tax = $this->getMockBuilder(\Magento\Weee\Model\Tax::class)
309  ->disableOriginalConstructor()
310  ->setMethods(['getProductWeeeAttributes'])
311  ->getMock();
312  $tax->expects($this->once())->method('getProductWeeeAttributes')->willReturn([$weeeDataHelper]);
313  $this->assertEquals(0, $tax->getWeeeAmountExclTax($product));
314  }
315 
320  {
321  return [
322  'store_label_defined' => [
323  'weeeTaxCalculationsByEntity' => [
324  'weee_value' => 1,
325  'label_value' => 'fpt_label',
326  'frontend_label' => 'fpt_label_frontend',
327  'attribute_code' => 'fpt_code',
328  ],
329  'websitePassed' => 1,
330  'expectedFptLabel' => 'fpt_label',
331  ],
332  'store_label_not_defined' => [
333  'weeeTaxCalculationsByEntity' => [
334  'weee_value' => 1,
335  'label_value' => '',
336  'frontend_label' => 'fpt_label_frontend',
337  'attribute_code' => 'fpt_code',
338  ],
339  'websitePassed' => 1,
340  'expectedFptLabel' => 'fpt_label_frontend',
341  ],
342  'website_not_passed' => [
343  'weeeTaxCalculationsByEntity' => [
344  'weee_value' => 1,
345  'label_value' => '',
346  'frontend_label' => 'fpt_label_frontend',
347  'attribute_code' => 'fpt_code',
348  ],
349  'websitePassed' => null,
350  'expectedFptLabel' => 'fpt_label_frontend',
351  ],
352  ];
353  }
354 
359  {
360  return [
361  [
362  'bundle', 1
363  ],
364  [
365  'simple', 0
366  ],
367  [
368  'simple', 1
369  ]
370  ];
371  }
372 }
$group
Definition: sections.phtml:16
__()
Definition: __.php:13
testGetProductWeeeAttributes(array $weeeTaxCalculationsByEntity, $websitePassed, string $expectedFptLabel)
Definition: TaxTest.php:166
testGetWeeeAmountExclTax($productTypeId, $productPriceType)
Definition: TaxTest.php:271
if($currentSelectedMethod==$_code) $className
Definition: form.phtml:31