Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
UtilityTest.php
Go to the documentation of this file.
1 <?php
7 
13 class UtilityTest extends \PHPUnit\Framework\TestCase
14 {
18  protected $usageFactory;
19 
23  protected $couponFactory;
24 
28  protected $coupon;
29 
33  protected $quote;
34 
38  protected $customerFactory;
39 
43  protected $customer;
44 
48  protected $address;
49 
53  protected $rule;
54 
58  protected $objectFactory;
59 
63  protected $item;
64 
68  protected $utility;
69 
73  protected $priceCurrency;
74 
75  protected function setUp()
76  {
77  $this->usageFactory = $this->createPartialMock(
78  \Magento\SalesRule\Model\ResourceModel\Coupon\UsageFactory::class,
79  ['create']
80  );
81  $this->couponFactory = $this->createPartialMock(\Magento\SalesRule\Model\CouponFactory::class, ['create']);
82  $this->objectFactory = $this->createPartialMock(\Magento\Framework\DataObjectFactory::class, ['create']);
83  $this->customerFactory = $this->createPartialMock(
84  \Magento\SalesRule\Model\Rule\CustomerFactory::class,
85  ['create']
86  );
87  $this->coupon = $this->createPartialMock(
88  \Magento\SalesRule\Model\Coupon::class,
89  [
90  'load',
91  'getId',
92  'getUsageLimit',
93  'getTimesUsed',
94  'getUsagePerCustomer',
95  '__wakeup'
96  ]
97  );
98  $this->quote = $this->createPartialMock(\Magento\Quote\Model\Quote::class, ['__wakeup', 'getStore']);
99  $this->customer = $this->createPartialMock(
100  \Magento\SalesRule\Model\Rule\Customer::class,
101  ['loadByCustomerRule', '__wakeup']
102  );
103  $this->rule = $this->createPartialMock(\Magento\SalesRule\Model\Rule::class, [
104  'hasIsValidForAddress',
105  'getIsValidForAddress',
106  'setIsValidForAddress',
107  '__wakeup',
108  'validate',
109  'afterLoad',
110  'getDiscountQty'
111  ]);
112  $this->address = $this->createPartialMock(\Magento\Quote\Model\Quote\Address::class, [
113  'isObjectNew',
114  'getQuote',
115  'setIsValidForAddress',
116  '__wakeup',
117  'validate',
118  'afterLoad'
119  ]);
120  $this->address->setQuote($this->quote);
121  $this->item = $this->createPartialMock(\Magento\Quote\Model\Quote\Item\AbstractItem::class, [
122  'getDiscountCalculationPrice',
123  'getCalculationPrice',
124  'getBaseDiscountCalculationPrice',
125  'getBaseCalculationPrice',
126  'getQuote',
127  'getAddress',
128  'getOptionByCode',
129  'getTotalQty',
130  '__wakeup'
131  ]);
132 
133  $this->priceCurrency = $this->getMockBuilder(\Magento\Framework\Pricing\PriceCurrencyInterface::class)
134  ->getMock();
135  $this->utility = new \Magento\SalesRule\Model\Utility(
136  $this->usageFactory,
137  $this->couponFactory,
138  $this->customerFactory,
139  $this->objectFactory,
140  $this->priceCurrency
141  );
142  }
143 
148  {
149  $this->rule->expects($this->once())
150  ->method('hasIsValidForAddress')
151  ->with($this->address)
152  ->will($this->returnValue(true));
153  $this->rule->expects($this->once())
154  ->method('getIsValidForAddress')
155  ->with($this->address)
156  ->will($this->returnValue(true));
157  $this->address->expects($this->once())
158  ->method('isObjectNew')
159  ->will($this->returnValue(false));
160  $this->assertTrue($this->utility->canProcessRule($this->rule, $this->address));
161  }
162 
167  {
168  $couponCode = 111;
169  $couponId = 4;
170  $quoteId = 4;
171  $usageLimit = 1;
172  $timesUsed = 2;
173  $this->rule->setCouponType(\Magento\SalesRule\Model\Rule::COUPON_TYPE_SPECIFIC);
174  $this->quote->setCouponCode($couponCode);
175  $this->quote->setId($quoteId);
176  $this->address->expects($this->once())
177  ->method('getQuote')
178  ->will($this->returnValue($this->quote));
179 
180  $this->coupon->expects($this->atLeastOnce())
181  ->method('getUsageLimit')
182  ->will($this->returnValue($usageLimit));
183  $this->coupon->expects($this->once())
184  ->method('getTimesUsed')
185  ->will($this->returnValue($timesUsed));
186  $this->coupon->expects($this->once())
187  ->method('load')
188  ->with($couponCode, 'code')
189  ->will($this->returnSelf());
190  $this->couponFactory->expects($this->once())
191  ->method('create')
192  ->will($this->returnValue($this->coupon));
193  $this->coupon->expects($this->once())
194  ->method('getId')
195  ->will($this->returnValue($couponId));
196  $this->assertFalse($this->utility->canProcessRule($this->rule, $this->address));
197  }
198 
203  {
204  $couponCode = 111;
205  $couponId = 4;
206  $quoteId = 4;
207  $customerId = 1;
208  $usageLimit = 1;
209  $timesUsed = 2;
210 
211  $this->rule->setCouponType(\Magento\SalesRule\Model\Rule::COUPON_TYPE_SPECIFIC);
212  $this->quote->setCouponCode($couponCode);
213  $this->quote->setId($quoteId);
214  $this->quote->setCustomerId($customerId);
215  $this->address->expects($this->atLeastOnce())
216  ->method('getQuote')
217  ->will($this->returnValue($this->quote));
218 
219  $this->coupon->expects($this->atLeastOnce())
220  ->method('getUsagePerCustomer')
221  ->will($this->returnValue($usageLimit));
222  $this->coupon->expects($this->once())
223  ->method('load')
224  ->with($couponCode, 'code')
225  ->will($this->returnSelf());
226  $this->coupon->expects($this->atLeastOnce())
227  ->method('getId')
228  ->will($this->returnValue($couponId));
229  $this->couponFactory->expects($this->once())
230  ->method('create')
231  ->will($this->returnValue($this->coupon));
232 
233  $couponUsage = new \Magento\Framework\DataObject();
234  $this->objectFactory->expects($this->once())
235  ->method('create')
236  ->will($this->returnValue($couponUsage));
237  $couponUsageModel = $this->createMock(\Magento\SalesRule\Model\ResourceModel\Coupon\Usage::class);
238  $couponUsage->setData(['coupon_id' => $couponId, 'times_used' => $timesUsed]);
239  $this->usageFactory->expects($this->once())
240  ->method('create')
241  ->will($this->returnValue($couponUsageModel));
242  $this->assertFalse($this->utility->canProcessRule($this->rule, $this->address));
243  }
244 
249  {
250  $customerId = 1;
251  $usageLimit = 1;
252  $timesUsed = 2;
253  $ruleId = 4;
254  $this->rule->setId($ruleId);
255  $this->rule->setUsesPerCustomer($usageLimit);
256  $this->quote->setCustomerId($customerId);
257  $this->address->expects($this->atLeastOnce())
258  ->method('getQuote')
259  ->will($this->returnValue($this->quote));
260  $this->customer->setId($customerId);
261  $this->customer->setTimesUsed($timesUsed);
262  $this->customerFactory->expects($this->once())
263  ->method('create')
264  ->will($this->returnValue($this->customer));
265 
266  $this->assertFalse($this->utility->canProcessRule($this->rule, $this->address));
267  }
268 
273  {
274  $this->rule->setCouponType(\Magento\SalesRule\Model\Rule::COUPON_TYPE_NO_COUPON);
275  $this->assertFalse($this->utility->canProcessRule($this->rule, $this->address));
276  }
277 
281  public function testCanProcessRule()
282  {
283  $this->rule->setCouponType(\Magento\SalesRule\Model\Rule::COUPON_TYPE_NO_COUPON);
284  $this->rule->expects($this->once())
285  ->method('validate')
286  ->will($this->returnValue(true));
287  $this->assertTrue($this->utility->canProcessRule($this->rule, $this->address));
288  }
289 
290  public function testGetItemPrice()
291  {
292  $price = $this->getItemPrice();
293  $this->assertEquals($price, $this->utility->getItemPrice($this->item));
294  }
295 
296  public function testGetItemPriceNull()
297  {
298  $price = 4;
299 
300  $this->item->expects($this->once())
301  ->method('getDiscountCalculationPrice')
302  ->will($this->returnValue($price));
303  $this->item->expects($this->once())
304  ->method('getCalculationPrice')
305  ->will($this->returnValue(null));
306  $this->assertEquals($price, $this->utility->getItemPrice($this->item));
307  }
308 
309  public function testGetItemBasePrice()
310  {
311  $price = $this->getItemBasePrice();
312  $this->assertEquals($price, $this->utility->getItemBasePrice($this->item));
313  }
314 
316  {
317  $calcPrice = 5;
318  $this->item->expects($this->once())
319  ->method('getDiscountCalculationPrice')
320  ->will($this->returnValue(null));
321  $this->item->expects($this->any())
322  ->method('getBaseCalculationPrice')
323  ->will($this->returnValue($calcPrice));
324  $this->assertEquals($calcPrice, $this->utility->getItemBasePrice($this->item));
325  }
326 
327  public function testGetItemQtyMin()
328  {
329  $qty = 7;
330  $discountQty = 4;
331  $this->item->expects($this->once())
332  ->method('getTotalQty')
333  ->will($this->returnValue($qty));
334  $this->rule->expects($this->once())
335  ->method('getDiscountQty')
336  ->will($this->returnValue($discountQty));
337  $this->assertEquals(min($discountQty, $qty), $this->utility->getItemQty($this->item, $this->rule));
338  }
339 
340  public function testGetItemQty()
341  {
342  $qty = 7;
343  $this->item->expects($this->once())
344  ->method('getTotalQty')
345  ->will($this->returnValue($qty));
346  $this->rule->expects($this->once())
347  ->method('getDiscountQty')
348  ->will($this->returnValue(null));
349  $this->assertEquals($qty, $this->utility->getItemQty($this->item, $this->rule));
350  }
351 
360  public function testMergeIds($a1, $a2, $isSting, $expected)
361  {
362  $this->assertEquals($expected, $this->utility->mergeIds($a1, $a2, $isSting));
363  }
364 
368  public function mergeIdsDataProvider()
369  {
370  return [
371  ['id1,id2', '', true, 'id1,id2'],
372  ['id1,id2', '', false, ['id1', 'id2']],
373  ['', 'id3,id4', false, ['id3', 'id4']],
374  ['', 'id3,id4', true, 'id3,id4'],
375  [['id1', 'id2'], ['id3', 'id4'], false, ['id1', 'id2', 'id3', 'id4']],
376  [['id1', 'id2'], ['id3', 'id4'], true, 'id1,id2,id3,id4']
377  ];
378  }
379 
380  public function testMinFix()
381  {
382  $qty = 13;
383  $amount = 10;
384  $baseAmount = 12;
385  $fixedAmount = 20;
386  $fixedBaseAmount = 24;
387  $this->getItemPrice();
388  $this->getItemBasePrice();
389  $this->item->setDiscountAmount($amount);
390  $this->item->setBaseDiscountAmount($baseAmount);
391  $discountData = $this->createMock(\Magento\SalesRule\Model\Rule\Action\Discount\Data::class);
392  $discountData->expects($this->atLeastOnce())
393  ->method('getAmount')
394  ->will($this->returnValue($amount));
395  $discountData->expects($this->atLeastOnce())
396  ->method('getBaseAmount')
397  ->will($this->returnValue($baseAmount));
398  $discountData->expects($this->once())
399  ->method('setAmount')
400  ->with($fixedAmount);
401  $discountData->expects($this->once())
402  ->method('setBaseAmount')
403  ->with($fixedBaseAmount);
404 
405  $this->assertNull($this->utility->minFix($discountData, $this->item, $qty));
406  }
407 
411  protected function getItemPrice()
412  {
413  $price = 4;
414  $calcPrice = 5;
415 
416  $this->item->expects($this->atLeastOnce())
417  ->method('getDiscountCalculationPrice')
418  ->will($this->returnValue($price));
419  $this->item->expects($this->once())
420  ->method('getCalculationPrice')
421  ->will($this->returnValue($calcPrice));
422  return $price;
423  }
424 
428  protected function getItemBasePrice()
429  {
430  $price = 4;
431  $calcPrice = 5;
432  $this->item->expects($this->atLeastOnce())
433  ->method('getDiscountCalculationPrice')
434  ->will($this->returnValue($calcPrice));
435  $this->item->expects($this->any())
436  ->method('getBaseDiscountCalculationPrice')
437  ->will($this->returnValue($price));
438  return $price;
439  }
440 
441  public function testDeltaRoundignFix()
442  {
443  $discountAmount = 10.003;
444  $baseDiscountAmount = 12.465;
445  $percent = 15;
446  $roundedDiscount = round($discountAmount, 2);
447  $roundedBaseDiscount = round($baseDiscountAmount, 2);
448  $delta = $discountAmount - $roundedDiscount;
449  $baseDelta = $baseDiscountAmount - $roundedBaseDiscount;
450  $secondRoundedDiscount = round($discountAmount + $delta);
451  $secondRoundedBaseDiscount = round($baseDiscountAmount + $baseDelta);
452 
453  $this->item->expects($this->any())
454  ->method('getQuote')
455  ->will($this->returnValue($this->quote));
456 
457  $store = $this->createMock(\Magento\Store\Model\Store::class);
458  $this->priceCurrency->expects($this->any())
459  ->method('round')
460  ->will($this->returnValueMap([
461  [$discountAmount, $roundedDiscount],
462  [$baseDiscountAmount, $roundedBaseDiscount],
463  [$discountAmount + $delta, $secondRoundedDiscount], //?
464  [$baseDiscountAmount + $baseDelta, $secondRoundedBaseDiscount], //?
465  ]));
466 
467  $this->quote->expects($this->any())
468  ->method('getStore')
469  ->will($this->returnValue($store));
470 
471  $this->item->setDiscountPercent($percent);
472 
473  $discountData = $this->createMock(\Magento\SalesRule\Model\Rule\Action\Discount\Data::class);
474  $discountData->expects($this->at(0))
475  ->method('getAmount')
476  ->will($this->returnValue($discountAmount));
477  $discountData->expects($this->at(1))
478  ->method('getBaseAmount')
479  ->will($this->returnValue($baseDiscountAmount));
480 
481  $discountData->expects($this->at(2))
482  ->method('setAmount')
483  ->with($roundedDiscount);
484  $discountData->expects($this->at(3))
485  ->method('setBaseAmount')
486  ->with($roundedBaseDiscount);
487 
488  $discountData->expects($this->at(4))
489  ->method('getAmount')
490  ->will($this->returnValue($discountAmount));
491  $discountData->expects($this->at(5))
492  ->method('getBaseAmount')
493  ->will($this->returnValue($baseDiscountAmount));
494 
495  $discountData->expects($this->at(6))
496  ->method('setAmount')
497  ->with($secondRoundedDiscount);
498  $discountData->expects($this->at(7))
499  ->method('setBaseAmount')
500  ->with($secondRoundedBaseDiscount);
501 
502  $this->assertEquals($this->utility, $this->utility->deltaRoundingFix($discountData, $this->item));
503  $this->assertEquals($this->utility, $this->utility->deltaRoundingFix($discountData, $this->item));
504  }
505 
506  public function testResetRoundingDeltas()
507  {
508  $this->assertNull($this->utility->resetRoundingDeltas());
509  }
510 }
if($this->helper('Magento\Tax\Helper\Data') ->displayFullSummary()) foreach( $block->getTotal() ->getFullInfo() as $info)(isset($info['hidden']) && $info['hidden']) $percent
Definition: tax.phtml:33
$price
$baseAmount
Definition: tax.phtml:46
$amount
Definition: order.php:14
testMergeIds($a1, $a2, $isSting, $expected)