64 protected function setUp()
66 $this->helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
67 $this->rulesApplier = $this->createPartialMock(
68 \
Magento\SalesRule\Model\RulesApplier::class,
69 [
'setAppliedRuleIds',
'applyRules',
'addDiscountDescription',
'__wakeup']
72 $this->addressMock = $this->getMockBuilder(\
Magento\Quote\Model\Quote\Address::class)
73 ->disableOriginalConstructor()
76 'getShippingAmountForDiscount',
78 'getCustomAttributesCodes',
85 $this->item = $this->createPartialMock(
86 \
Magento\Quote\Model\Quote\Item::class,
87 [
'__wakeup',
'getAddress',
'getParentItemId']
89 $this->item->expects($this->any())
90 ->method(
'getAddress')
91 ->willReturn($this->addressMock);
93 $context = $this->createMock(\
Magento\Framework\Model\Context::class);
95 $this->catalogData = $this->createMock(\
Magento\Catalog\Helper\Data::class);
96 $this->utility = $this->createMock(\
Magento\SalesRule\Model\Utility::class);
97 $this->validators = $this->createPartialMock(\
Magento\SalesRule\Model\
Validator\Pool::class, [
'getValidators']);
98 $this->messageManager = $this->createMock(\
Magento\Framework\Message\Manager::class);
99 $this->ruleCollection = $this->getMockBuilder(\
Magento\SalesRule\Model\
ResourceModel\Rule\Collection::class)
100 ->disableOriginalConstructor()
105 $this->model = $this->helper->getObject(
106 \
Magento\SalesRule\Model\Validator::class,
108 'context' => $context,
110 'collectionFactory' => $ruleCollectionFactoryMock,
111 'catalogData' => $this->catalogData,
112 'utility' => $this->utility,
113 'rulesApplier' => $this->rulesApplier,
114 'validators' => $this->validators,
115 'messageManager' => $this->messageManager
118 $this->model->setWebsiteId(1);
119 $this->model->setCustomerGroupId(2);
120 $this->model->setCouponCode(
'code');
121 $this->ruleCollection->expects($this->any())
122 ->method(
'setValidationFilter')
124 $this->model->getWebsiteId(),
125 $this->model->getCustomerGroupId(),
126 $this->model->getCouponCode(),
136 protected function getQuoteItemMock()
138 $fixturePath =
__DIR__ .
'/_files/';
139 $itemDownloadable = $this->createPartialMock(
140 \
Magento\Quote\Model\Quote\Item::class,
141 [
'getAddress',
'__wakeup']
143 $itemDownloadable->expects($this->any())->method(
'getAddress')->will($this->returnValue($this->addressMock));
145 $itemSimple = $this->createPartialMock(\
Magento\Quote\Model\Quote\Item::class, [
'getAddress',
'__wakeup']);
146 $itemSimple->expects($this->any())->method(
'getAddress')->will($this->returnValue($this->addressMock));
149 $quote = $this->createPartialMock(\
Magento\Quote\Model\Quote::class, [
'getStoreId',
'__wakeup']);
150 $quote->expects($this->any())->method(
'getStoreId')->will($this->returnValue(1));
152 $itemData = include $fixturePath .
'quote_item_downloadable.php';
153 $itemDownloadable->addData($itemData);
154 $quote->addItem($itemDownloadable);
156 $itemData = include $fixturePath .
'quote_item_simple.php';
157 $itemSimple->addData($itemData);
158 $quote->addItem($itemSimple);
160 return $itemDownloadable;
166 $this->model->getWebsiteId(),
167 $this->model->getCustomerGroupId(),
168 $this->model->getCouponCode()
170 $item = $this->getQuoteItemMock();
171 $rule = $this->createMock(\
Magento\SalesRule\Model\Rule::class);
172 $actionsCollection = $this->createPartialMock(\
Magento\Rule\Model\Action\Collection::class, [
'validate']);
173 $actionsCollection->expects($this->any())
177 $rule->expects($this->any())
178 ->method(
'getActions')
179 ->willReturn($actionsCollection);
181 $this->ruleCollection->expects($this->any())
182 ->method(
'getIterator')
185 $this->utility->expects($this->any())
186 ->method(
'canProcessRule')
187 ->with(
$rule, $this->anything())
192 $quote->setVirtualItemsQty(1);
194 $this->assertTrue($this->model->canApplyRules(
$item));
197 $quote->setVirtualItemsQty(2);
199 $this->assertTrue($this->model->canApplyRules(
$item));
206 $this->item->setDiscountCalculationPrice($negativePrice);
207 $this->item->setData(
'calculation_price', $negativePrice);
209 $this->rulesApplier->expects($this->never())->method(
'applyRules');
212 $this->model->getWebsiteId(),
213 $this->model->getCustomerGroupId(),
214 $this->model->getCouponCode()
216 $this->model->process($this->item);
222 $nonZeroDiscount = 123;
224 $this->model->getWebsiteId(),
225 $this->model->getCustomerGroupId(),
226 $this->model->getCouponCode()
229 $this->item->setDiscountCalculationPrice($negativePrice);
230 $this->item->setData(
'calculation_price', $negativePrice);
232 $this->item->setDiscountAmount($nonZeroDiscount);
233 $this->item->setBaseDiscountAmount($nonZeroDiscount);
234 $this->item->setDiscountPercent($nonZeroDiscount);
236 $this->model->process($this->item);
238 $this->assertEquals(0, $this->item->getDiscountAmount());
239 $this->assertEquals(0, $this->item->getBaseDiscountAmount());
240 $this->assertEquals(0, $this->item->getDiscountPercent());
248 $expectedRuleIds = [$ruleId1 => $ruleId1, $ruleId2 => $ruleId2];
250 $this->model->getWebsiteId(),
251 $this->model->getCustomerGroupId(),
252 $this->model->getCouponCode()
255 $this->item->setDiscountCalculationPrice($positivePrice);
256 $this->item->setData(
'calculation_price', $positivePrice);
257 $this->model->setSkipActionsValidation(
true);
259 $this->rulesApplier->expects($this->once())
260 ->method(
'applyRules')
262 $this->equalTo($this->item),
263 $this->equalTo($this->ruleCollection),
267 ->will($this->returnValue($expectedRuleIds));
268 $this->rulesApplier->expects($this->once())
269 ->method(
'setAppliedRuleIds')
275 $this->model->process($this->item);
280 $this->assertInstanceOf(
281 \
Magento\SalesRule\Model\Validator::class,
283 $this->model->getWebsiteId(),
284 $this->model->getCustomerGroupId(),
285 $this->model->getCouponCode()
292 $validator = $this->getMockBuilder(\
Magento\Framework\
Validator\AbstractValidator::class)
293 ->setMethods([
'isValid'])
294 ->disableOriginalConstructor()
295 ->getMockForAbstractClass();
297 $this->validators->expects($this->any())
298 ->method(
'getValidators')
300 ->willReturn([$validator]);
301 $validator->expects($this->any())
307 $this->model->getWebsiteId(),
308 $this->model->getCustomerGroupId(),
309 $this->model->getCouponCode()
311 $this->assertFalse($this->model->canApplyDiscount($this->item));
316 $rule = $this->createPartialMock(
317 \
Magento\SalesRule\Model\Rule::class,
318 [
'getSimpleAction',
'getActions',
'getId']
320 $item1 = $this->getMockForAbstractClass(
321 \
Magento\Quote\Model\Quote\Item\AbstractItem::class,
329 'getDiscountCalculationPrice',
330 'getBaseDiscountCalculationPrice',
331 'getCalculationPrice',
335 $item2 = clone $item1;
336 $items = [$item1, $item2];
338 $rule->expects($this->any())
339 ->method(
'getSimpleAction')
340 ->willReturn(\
Magento\SalesRule\Model\Rule::CART_FIXED_ACTION);
342 $this->ruleCollection->expects($this->once())->method(
'getIterator')->willReturn(
$iterator);
343 $validator = $this->getMockBuilder(\
Magento\Framework\
Validator\AbstractValidator::class)
344 ->setMethods([
'isValid'])
345 ->disableOriginalConstructor()
346 ->getMockForAbstractClass();
348 $this->validators->expects($this->atLeastOnce())->method(
'getValidators')->with(
'discount')
349 ->willReturn([$validator]);
350 $validator->expects($this->at(0))->method(
'isValid')->with($item1)->willReturn(
false);
351 $validator->expects($this->at(1))->method(
'isValid')->with($item2)->willReturn(
true);
353 $item1->expects($this->any())->method(
'getParentItemId')->willReturn(
false);
354 $item1->expects($this->never())->method(
'getDiscountCalculationPrice');
355 $item1->expects($this->never())->method(
'getBaseDiscountCalculationPrice');
356 $item2->expects($this->any())->method(
'getParentItemId')->willReturn(
false);
357 $item2->expects($this->any())->method(
'getDiscountCalculationPrice')->willReturn(50);
358 $item2->expects($this->once())->method(
'getBaseDiscountCalculationPrice')->willReturn(50);
359 $this->utility->expects($this->once())->method(
'getItemQty')->willReturn(1);
360 $this->utility->expects($this->any())->method(
'canProcessRule')->willReturn(
true);
362 $actionsCollection = $this->createPartialMock(\
Magento\Rule\Model\Action\Collection::class, [
'validate']);
363 $actionsCollection->expects($this->at(0))->method(
'validate')->with($item1)->willReturn(
true);
364 $actionsCollection->expects($this->at(1))->method(
'validate')->with($item2)->willReturn(
true);
365 $rule->expects($this->any())->method(
'getActions')->willReturn($actionsCollection);
366 $rule->expects($this->any())->method(
'getId')->willReturn(1);
369 $this->model->getWebsiteId(),
370 $this->model->getCustomerGroupId(),
371 $this->model->getCouponCode()
373 $this->model->initTotals(
$items, $this->addressMock);
374 $this->assertArrayHasKey(
'items_price', $this->model->getRuleItemTotalsInfo(
$rule->getId()));
375 $this->assertArrayHasKey(
'base_items_price', $this->model->getRuleItemTotalsInfo(
$rule->getId()));
376 $this->assertArrayHasKey(
'items_count', $this->model->getRuleItemTotalsInfo(
$rule->getId()));
377 $this->assertEquals(1, $this->model->getRuleItemTotalsInfo(
$rule->getId())[
'items_count']);
382 $address = $this->createMock(\
Magento\Quote\Model\Quote\Address::class);
383 $this->item->expects($this->never())
384 ->method(
'getParentItemId');
386 $this->model->getWebsiteId(),
387 $this->model->getCustomerGroupId(),
388 $this->model->getCouponCode()
390 $this->model->initTotals([],
$address);
399 $this->ruleCollection->expects($this->any())
400 ->method(
'addFieldToFilter')
401 ->with(
'is_active', 1)
402 ->will($this->returnSelf());
403 $this->ruleCollection->expects($this->any())
405 ->will($this->returnSelf());
407 $ruleCollectionFactoryMock =
409 ->disableOriginalConstructor()
410 ->setMethods([
'create'])
412 $ruleCollectionFactoryMock->expects($this->any())
415 return $ruleCollectionFactoryMock;
421 $this->ruleCollection->expects($this->any())
422 ->method(
'getIterator')
425 $this->model->getWebsiteId(),
426 $this->model->getCustomerGroupId(),
427 $this->model->getCouponCode()
429 $this->assertInstanceOf(
430 \
Magento\SalesRule\Model\Validator::class,
431 $this->model->processShippingAmount($this->setupAddressMock())
437 $ruleMock = $this->getMockBuilder(\
Magento\SalesRule\Model\Rule::class)
438 ->disableOriginalConstructor()
441 $iterator = new \ArrayIterator([$ruleMock]);
442 $this->ruleCollection->expects($this->any())
443 ->method(
'getIterator')
446 $this->model->getWebsiteId(),
447 $this->model->getCustomerGroupId(),
448 $this->model->getCouponCode()
450 $this->assertInstanceOf(
451 \
Magento\SalesRule\Model\Validator::class,
452 $this->model->processShippingAmount($this->setupAddressMock())
462 $discountAmount = 50;
464 $ruleMock = $this->getMockBuilder(\
Magento\SalesRule\Model\Rule::class)
465 ->disableOriginalConstructor()
466 ->setMethods([
'getApplyToShipping',
'getSimpleAction',
'getDiscountAmount'])
468 $ruleMock->expects($this->any())
469 ->method(
'getApplyToShipping')
471 $ruleMock->expects($this->any())
472 ->method(
'getDiscountAmount')
473 ->willReturn($discountAmount);
474 $ruleMock->expects($this->any())
475 ->method(
'getSimpleAction')
476 ->willReturn($action);
478 $iterator = new \ArrayIterator([$ruleMock]);
479 $this->ruleCollection->expects($this->any())
480 ->method(
'getIterator')
483 $this->utility->expects($this->any())
484 ->method(
'canProcessRule')
488 $this->model->getWebsiteId(),
489 $this->model->getCustomerGroupId(),
490 $this->model->getCouponCode()
492 $this->assertInstanceOf(
493 \
Magento\SalesRule\Model\Validator::class,
494 $this->model->processShippingAmount($this->setupAddressMock(5))
518 $storeMock = $this->getMockBuilder(\
Magento\Store\Model\Store::class)
519 ->disableOriginalConstructor()
522 $quoteMock = $this->getMockBuilder(\
Magento\Quote\Model\Quote::class)
523 ->disableOriginalConstructor()
524 ->setMethods([
'setAppliedRuleIds',
'getStore'])
526 $quoteMock->expects($this->any())
528 ->willReturn($storeMock);
529 $quoteMock->expects($this->any())
530 ->method(
'setAppliedRuleIds')
533 $this->addressMock->expects($this->any())
534 ->method(
'getShippingAmountForDiscount')
536 $this->addressMock->expects($this->any())
538 ->willReturn($quoteMock);
539 $this->addressMock->expects($this->any())
540 ->method(
'getCustomAttributesCodes')
547 $this->utility->expects($this->once())
548 ->method(
'resetRoundingDeltas');
549 $quoteMock = $this->getMockBuilder(\
Magento\Quote\Model\Quote::class)
550 ->disableOriginalConstructor()
553 ->disableOriginalConstructor()
557 ->willReturn($quoteMock);
559 $this->model->getWebsiteId(),
560 $this->model->getCustomerGroupId(),
561 $this->model->getCouponCode()
563 $this->assertInstanceOf(\
Magento\SalesRule\Model\Validator::class, $this->model->reset(
$addressMock));
testProcessWhenItemPriceIsNegativeDiscountsAreZeroed()
testProcessShippingAmountActions($action)
testInitTotalsCanApplyDiscount()
defined('TESTS_BP')||define('TESTS_BP' __DIR__
static dataProviderActions()
setupAddressMock($shippingAmount=null)
testProcessShippingAmountNoRules()
testProcessShippingAmountProcessDisabled()
testApplyRulesThatAppliedRuleIdsAreCollected()
prepareRuleCollectionMock($ruleCollection)