/**
  * Checks if given Coupon is valid. To determine its validity, this service
  * will evaluate all rules if there are any.
  *
  * @param CartInterface   $cart   Cart
  * @param CouponInterface $coupon Coupon
  *
  * @return boolean Coupon is valid
  */
 public function checkCouponValidity(CartInterface $cart, CouponInterface $coupon)
 {
     $rules = $coupon->getRules();
     return $rules->forAll(function ($_, AbstractRuleInterface $rule) use($cart, $coupon) {
         return $this->ruleManager->evaluateByRule($rule, ['cart' => $cart, 'coupon' => $coupon]);
     });
 }
Example #2
0
 /**
  * Evaluate compound rules.
  *
  * @dataProvider providerEvaluateCompoundRule
  *
  * @param int  $amount
  * @param int  $quantity
  * @param bool $expected
  */
 public function testEvaluateCompoundRule($amount, $quantity, $expected)
 {
     $rule = new Rule();
     $rule->setExpression('rule("cart_valuable_items")');
     $cart = $this->getMock('Elcodi\\Component\\Cart\\Entity\\Interfaces\\CartInterface');
     $cart->expects($this->any())->method('getAmount')->willReturn($amount);
     $cart->expects($this->any())->method('getQuantity')->willReturn($quantity);
     $context = ['cart' => $cart];
     $this->assertEquals($expected, $this->ruleManager->evaluate($rule, $context));
 }
Example #3
0
 /**
  * Get functions
  *
  * @return ExpressionFunction[] An array of Function instances
  */
 public function getFunctions()
 {
     return [new ExpressionFunction('rule', function () {
         throw new RuntimeException('Function "rule" can\'t be compiled.');
     }, function (array $context, $value) {
         /**
          * @var Rule $rule
          */
         $rule = $this->ruleRepository->findOneBy(['name' => $value]);
         return $rule ? $this->ruleManager->evaluate($rule, $context) : false;
     })];
 }
Example #4
0
 /**
  * Evaluate rule false
  */
 public function testEvaluateRuleCodeFalse()
 {
     $this->assertFalse($this->ruleManager->evaluateByCode('rule-false'));
 }