77 $this->usageFactory = $this->createPartialMock(
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,
87 $this->coupon = $this->createPartialMock(
88 \
Magento\SalesRule\Model\Coupon::class,
94 'getUsagePerCustomer',
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']
103 $this->rule = $this->createPartialMock(\
Magento\SalesRule\Model\Rule::class, [
104 'hasIsValidForAddress',
105 'getIsValidForAddress',
106 'setIsValidForAddress',
112 $this->address = $this->createPartialMock(\
Magento\Quote\Model\Quote\Address::class, [
115 'setIsValidForAddress',
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',
133 $this->priceCurrency = $this->getMockBuilder(\
Magento\Framework\Pricing\PriceCurrencyInterface::class)
135 $this->utility = new \Magento\SalesRule\Model\Utility(
137 $this->couponFactory,
138 $this->customerFactory,
139 $this->objectFactory,
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));
173 $this->rule->setCouponType(\
Magento\SalesRule\Model\Rule::COUPON_TYPE_SPECIFIC);
176 $this->address->expects($this->once())
178 ->will($this->returnValue($this->quote));
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())
189 ->will($this->returnSelf());
190 $this->couponFactory->expects($this->once())
192 ->will($this->returnValue($this->coupon));
193 $this->coupon->expects($this->once())
195 ->will($this->returnValue($couponId));
196 $this->assertFalse($this->utility->canProcessRule($this->rule, $this->address));
211 $this->rule->setCouponType(\
Magento\SalesRule\Model\Rule::COUPON_TYPE_SPECIFIC);
215 $this->address->expects($this->atLeastOnce())
217 ->will($this->returnValue($this->quote));
219 $this->coupon->expects($this->atLeastOnce())
220 ->method(
'getUsagePerCustomer')
221 ->will($this->returnValue($usageLimit));
222 $this->coupon->expects($this->once())
225 ->will($this->returnSelf());
226 $this->coupon->expects($this->atLeastOnce())
228 ->will($this->returnValue($couponId));
229 $this->couponFactory->expects($this->once())
231 ->will($this->returnValue($this->coupon));
233 $couponUsage = new \Magento\Framework\DataObject();
234 $this->objectFactory->expects($this->once())
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())
241 ->will($this->returnValue($couponUsageModel));
242 $this->assertFalse($this->utility->canProcessRule($this->rule, $this->address));
255 $this->rule->setUsesPerCustomer($usageLimit);
257 $this->address->expects($this->atLeastOnce())
259 ->will($this->returnValue($this->quote));
261 $this->customer->setTimesUsed($timesUsed);
262 $this->customerFactory->expects($this->once())
264 ->will($this->returnValue($this->customer));
266 $this->assertFalse($this->utility->canProcessRule($this->rule, $this->address));
274 $this->rule->setCouponType(\
Magento\SalesRule\Model\Rule::COUPON_TYPE_NO_COUPON);
275 $this->assertFalse($this->utility->canProcessRule($this->rule, $this->address));
283 $this->rule->setCouponType(\
Magento\SalesRule\Model\Rule::COUPON_TYPE_NO_COUPON);
284 $this->rule->expects($this->once())
286 ->will($this->returnValue(
true));
287 $this->assertTrue($this->utility->canProcessRule($this->rule, $this->address));
293 $this->assertEquals(
$price, $this->utility->getItemPrice($this->item));
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));
312 $this->assertEquals(
$price, $this->utility->getItemBasePrice($this->item));
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));
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));
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));
362 $this->assertEquals($expected, $this->utility->mergeIds($a1, $a2, $isSting));
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']
386 $fixedBaseAmount = 24;
389 $this->item->setDiscountAmount(
$amount);
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')
398 $discountData->expects($this->once())
399 ->method(
'setAmount')
400 ->with($fixedAmount);
401 $discountData->expects($this->once())
402 ->method(
'setBaseAmount')
403 ->with($fixedBaseAmount);
405 $this->assertNull($this->utility->minFix($discountData, $this->item, $qty));
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));
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));
443 $discountAmount = 10.003;
444 $baseDiscountAmount = 12.465;
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);
453 $this->item->expects($this->any())
455 ->will($this->returnValue($this->quote));
457 $store = $this->createMock(\
Magento\Store\Model\Store::class);
458 $this->priceCurrency->expects($this->any())
460 ->will($this->returnValueMap([
461 [$discountAmount, $roundedDiscount],
462 [$baseDiscountAmount, $roundedBaseDiscount],
463 [$discountAmount + $delta, $secondRoundedDiscount],
464 [$baseDiscountAmount + $baseDelta, $secondRoundedBaseDiscount],
467 $this->quote->expects($this->any())
469 ->will($this->returnValue(
$store));
471 $this->item->setDiscountPercent(
$percent);
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));
481 $discountData->expects($this->at(2))
482 ->method(
'setAmount')
483 ->with($roundedDiscount);
484 $discountData->expects($this->at(3))
485 ->method(
'setBaseAmount')
486 ->with($roundedBaseDiscount);
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));
495 $discountData->expects($this->at(6))
496 ->method(
'setAmount')
497 ->with($secondRoundedDiscount);
498 $discountData->expects($this->at(7))
499 ->method(
'setBaseAmount')
500 ->with($secondRoundedBaseDiscount);
502 $this->assertEquals($this->utility, $this->utility->deltaRoundingFix($discountData, $this->item));
503 $this->assertEquals($this->utility, $this->utility->deltaRoundingFix($discountData, $this->item));
508 $this->assertNull($this->utility->resetRoundingDeltas());
testCanProcessRuleInvalidConditions()
if($this->helper('Magento\Tax\Helper\Data') ->displayFullSummary()) foreach( $block->getTotal() ->getFullInfo() as $info)(isset($info['hidden']) && $info['hidden']) $percent
testResetRoundingDeltas()
testGetBaseItemPriceCalculation()
testCanProcessRuleCouponUsageLimitFail()
testMergeIds($a1, $a2, $isSting, $expected)
testCanProcessRuleCouponUsagePerCustomerFail()
testCanProcessRuleValidAddress()
testCanProcessRuleUsagePerCustomer()