public function testApplyRulesWhenRuleWithStopRulesProcessingIsUsed() { $positivePrice = 1; $skipValidation = true; $item = $this->getPreparedItem(); $couponCode = 111; $ruleId = 1; $appliedRuleIds = [$ruleId => $ruleId]; /** * @var \Magento\SalesRule\Model\Rule|\PHPUnit_Framework_MockObject_MockObject $ruleWithStopFurtherProcessing */ $ruleWithStopFurtherProcessing = $this->getMock('Magento\\SalesRule\\Model\\Rule', ['getStoreLabel', 'getCouponType', 'getRuleId', '__wakeup'], [], '', false); /** @var \Magento\SalesRule\Model\Rule|\PHPUnit_Framework_MockObject_MockObject $ruleThatShouldNotBeRun */ $ruleThatShouldNotBeRun = $this->getMock('Magento\\SalesRule\\Model\\Rule', ['getStopRulesProcessing', '__wakeup'], [], '', false); $ruleWithStopFurtherProcessing->setName('ruleWithStopFurtherProcessing'); $ruleThatShouldNotBeRun->setName('ruleThatShouldNotBeRun'); $rules = [$ruleWithStopFurtherProcessing, $ruleThatShouldNotBeRun]; $item->setDiscountCalculationPrice($positivePrice); $item->setData('calculation_price', $positivePrice); $this->validatorUtility->expects($this->atLeastOnce())->method('canProcessRule')->will($this->returnValue(true)); $ruleWithStopFurtherProcessing->expects($this->any())->method('getRuleId')->will($this->returnValue($ruleId)); $this->applyRule($item, $ruleWithStopFurtherProcessing); $ruleWithStopFurtherProcessing->setStopRulesProcessing(true); $ruleThatShouldNotBeRun->expects($this->never())->method('getStopRulesProcessing'); $result = $this->rulesApplier->applyRules($item, $rules, $skipValidation, $couponCode); $this->assertEquals($appliedRuleIds, $result); }
/** * Quote item discount calculation process * * @param AbstractItem $item * @return $this */ public function process(AbstractItem $item) { $item->setDiscountAmount(0); $item->setBaseDiscountAmount(0); $item->setDiscountPercent(0); $itemPrice = $this->getItemPrice($item); if ($itemPrice < 0) { return $this; } $appliedRuleIds = $this->rulesApplier->applyRules($item, $this->_getRules(), $this->_skipActionsValidation, $this->getCouponCode()); $this->rulesApplier->setAppliedRuleIds($item, $appliedRuleIds); return $this; }
/** * @param bool $isChildren * @param bool $isContinue * * @dataProvider dataProviderChildren */ public function testApplyRulesWhenRuleWithStopRulesProcessingIsUsed($isChildren, $isContinue) { $positivePrice = 1; $skipValidation = false; $item = $this->getPreparedItem(); $couponCode = 111; $ruleId = 1; $appliedRuleIds = [$ruleId => $ruleId]; /** * @var \Magento\SalesRule\Model\Rule|\PHPUnit_Framework_MockObject_MockObject $ruleWithStopFurtherProcessing */ $ruleWithStopFurtherProcessing = $this->getMock('Magento\\SalesRule\\Model\\Rule', ['getStoreLabel', 'getCouponType', 'getRuleId', '__wakeup', 'getActions'], [], '', false); /** @var \Magento\SalesRule\Model\Rule|\PHPUnit_Framework_MockObject_MockObject $ruleThatShouldNotBeRun */ $ruleThatShouldNotBeRun = $this->getMock('Magento\\SalesRule\\Model\\Rule', ['getStopRulesProcessing', '__wakeup'], [], '', false); $actionMock = $this->getMock('Magento\\Rule\\Model\\Action\\Collection', ['validate'], [], '', false); $ruleWithStopFurtherProcessing->setName('ruleWithStopFurtherProcessing'); $ruleThatShouldNotBeRun->setName('ruleThatShouldNotBeRun'); $rules = [$ruleWithStopFurtherProcessing, $ruleThatShouldNotBeRun]; $item->setDiscountCalculationPrice($positivePrice); $item->setData('calculation_price', $positivePrice); $this->validatorUtility->expects($this->atLeastOnce())->method('canProcessRule')->will($this->returnValue(true)); $ruleWithStopFurtherProcessing->expects($this->atLeastOnce())->method('getActions')->willReturn($actionMock); $actionMock->expects($this->at(0))->method('validate')->with($item)->willReturn(!$isChildren); // if there are child elements, check them if ($isChildren) { $item->expects($this->atLeastOnce())->method('getChildren')->willReturn([$item]); $actionMock->expects($this->at(1))->method('validate')->with($item)->willReturn(!$isContinue); } // if (!$isContinue || !$isChildren) { $ruleWithStopFurtherProcessing->expects($this->any())->method('getRuleId')->will($this->returnValue($ruleId)); $this->applyRule($item, $ruleWithStopFurtherProcessing); $ruleWithStopFurtherProcessing->setStopRulesProcessing(true); $ruleThatShouldNotBeRun->expects($this->never())->method('getStopRulesProcessing'); } $result = $this->rulesApplier->applyRules($item, $rules, $skipValidation, $couponCode); $this->assertEquals($appliedRuleIds, $result); }