public function testInitTotalsCanApplyDiscount()
 {
     $address = $this->getMock('Magento\\Sales\\Model\\Quote\\Address', [], [], '', false);
     $rule = $this->getMock('Magento\\SalesRule\\Model\\Rule', ['getSimpleAction', 'getActions', 'getId'], [], '', false);
     $item1 = $this->getMockForAbstractClass('Magento\\Sales\\Model\\Quote\\Item\\AbstractItem', [], '', false, true, true, ['__clone', 'getDiscountCalculationPrice', 'getBaseDiscountCalculationPrice', 'getCalculationPrice']);
     $item2 = clone $item1;
     $items = [$item1, $item2];
     $rule->expects($this->any())->method('getSimpleAction')->willReturn(\Magento\SalesRule\Model\Rule::CART_FIXED_ACTION);
     $iterator = new \ArrayIterator([$rule]);
     $this->ruleCollection->expects($this->once())->method('getIterator')->willReturn($iterator);
     $validator = $this->getMockBuilder('Magento\\Framework\\Validator\\AbstractValidator')->setMethods(['isValid'])->disableOriginalConstructor()->getMockForAbstractClass();
     $this->validators->expects($this->atLeastOnce())->method('getValidators')->with('discount')->willReturn([$validator]);
     $validator->expects($this->at(0))->method('isValid')->with($item1)->willReturn(false);
     $validator->expects($this->at(1))->method('isValid')->with($item2)->willReturn(true);
     $item1->expects($this->any())->method('getParentItemId')->willReturn(false);
     $item1->expects($this->never())->method('getDiscountCalculationPrice');
     $item1->expects($this->never())->method('getBaseDiscountCalculationPrice');
     $item2->expects($this->any())->method('getParentItemId')->willReturn(false);
     $item2->expects($this->any())->method('getDiscountCalculationPrice')->willReturn(50);
     $item2->expects($this->once())->method('getBaseDiscountCalculationPrice')->willReturn(50);
     $this->utility->expects($this->once())->method('getItemQty')->willReturn(1);
     $this->utility->expects($this->any())->method('canProcessRule')->willReturn(true);
     $actionsCollection = $this->getMock('Magento\\Rule\\Model\\Action\\Collection', ['validate'], [], '', false);
     $actionsCollection->expects($this->at(0))->method('validate')->with($item1)->willReturn(true);
     $actionsCollection->expects($this->at(1))->method('validate')->with($item2)->willReturn(true);
     $rule->expects($this->any())->method('getActions')->willReturn($actionsCollection);
     $rule->expects($this->any())->method('getId')->willReturn(1);
     $this->model->init($this->model->getWebsiteId(), $this->model->getCustomerGroupId(), $this->model->getCouponCode());
     $this->model->initTotals($items, $address);
     $this->assertArrayHasKey('items_price', $this->model->getRuleItemTotalsInfo($rule->getId()));
     $this->assertArrayHasKey('base_items_price', $this->model->getRuleItemTotalsInfo($rule->getId()));
     $this->assertArrayHasKey('items_count', $this->model->getRuleItemTotalsInfo($rule->getId()));
     $this->assertEquals(1, $this->model->getRuleItemTotalsInfo($rule->getId())['items_count']);
 }
Example #2
0
 /**
  * Check if we can apply discount to current QuoteItem
  *
  * @param AbstractItem $item
  * @return bool
  */
 public function canApplyDiscount(AbstractItem $item)
 {
     $result = true;
     /** @var \Zend_Validate_Interface $validator */
     foreach ($this->validators->getValidators('discount') as $validator) {
         $result = $validator->isValid($item);
         if (!$result) {
             break;
         }
     }
     return $result;
 }