public function testValidate() { $age = 25; $context = ['user' => ['age' => $age]]; $modifier = Mockery::mock('Ve\\LogicProcessor\\AbstractAssertion'); $modifier->shouldReceive('run')->with($age)->once()->andReturn(true); $rule = new NameRuleStub(); $rule->setAssertion($modifier); $this->set->setRule($rule); $this->assertTrue($this->set->isValid($context)); }
/** * Performs an actual logical check without mocks as a form of integration test. */ protected function testFullRuleRun() { $set = new RuleSet(); // Set up and add a rule $rule = new HasProdRule(); $modifier = new Modifier\Equal(); $modifier->setTargetValue(10); $rule->setAssertion($modifier); $set->setRule($rule); // Set up and add a result $result = new OrderDiscountResult(); $result->setValue(10.0); $set->addResult($result); $context = ['products' => [['id' => 1, 'qty' => 10, 'price' => 10.0]]]; $target = new stdClass(); $target->total = 50.0; // Check the rule can be valid $this->processor->addRuleSet($rule); $this->processor->run($context, $target); $this->assertEquals(10.0, $target->total); // Check the rule can be false $target->total = 50.0; $context['products'][0]['qty'] = 5; $this->processor->run($context, $target); $this->assertEquals(50.0, $target->total); }
/** * Builds a full rule set with a rule and results. * * @param array $data * * @return RuleSet */ public function buildRuleSet($data) { $ruleSet = new RuleSet(); if (isset($data['identifier'])) { $ruleSet->setIdentifier($data['identifier']); } // Build the rule if (isset($data['rule'])) { $ruleSet->setRule($this->buildRule($data['rule'])); } // Loop over each result and build that too if (isset($data['result'])) { foreach ($data['result'] as $result) { $ruleSet->addResult($this->buildResult($result)); } } return $ruleSet; }