Beispiel #1
0
 public function testPrepareValue()
 {
     $values = array('one' => 'Foo', 'two' => 'BAR', 'three' => function () {
         return 'baz';
     });
     $context = new Context($values);
     $varA = new Variable('four', 'qux');
     $this->assertInstanceOf('Ruler\\Value', $varA->prepareValue($context));
     $this->assertEquals('qux', $varA->prepareValue($context)->getValue(), "Variables should return the default value if it's missing from the context.");
     $varB = new Variable('one', 'FAIL');
     $this->assertEquals('Foo', $varB->prepareValue($context)->getValue());
     $varC = new Variable('three', 'FAIL');
     $this->assertEquals('baz', $varC->prepareValue($context)->getValue());
     $varD = new Variable(null, 'qux');
     $this->assertInstanceOf('Ruler\\Value', $varD->prepareValue($context));
     $this->assertEquals('qux', $varD->prepareValue($context)->getValue(), "Anonymous variables don't require a name to prepare value");
 }
Beispiel #2
0
    public function testFluentInterfaceHelpersAndAnonymousVariables()
    {
        $context = new Context(array(
            'a' => 1,
            'b' => 2,
        ));
    
        $varA = new Variable('a');
        $varB = new Variable('b');

        $this->assertInstanceOf('Ruler\Operator\ComparisonOperator', $varA->greaterThan(0));

        $this->assertInstanceOf('Ruler\Operator\GreaterThan', $varA->greaterThan(0));
        $this->assertTrue($varA->greaterThan(0)->evaluate($context));
        $this->assertFalse($varA->greaterThan(2)->evaluate($context));

        $this->assertInstanceOf('Ruler\Operator\GreaterThanOrEqualTo', $varA->greaterThanOrEqualTo(0));
        $this->assertTrue($varA->greaterThanOrEqualTo(0)->evaluate($context));
        $this->assertTrue($varA->greaterThanOrEqualTo(1)->evaluate($context));
        $this->assertFalse($varA->greaterThanOrEqualTo(2)->evaluate($context));

        $this->assertInstanceOf('Ruler\Operator\LessThan', $varA->lessThan(0));
        $this->assertTrue($varA->lessThan(2)->evaluate($context));
        $this->assertFalse($varA->lessThan(0)->evaluate($context));

        $this->assertInstanceOf('Ruler\Operator\LessThanOrEqualTo', $varA->lessThanOrEqualTo(0));
        $this->assertTrue($varA->lessThanOrEqualTo(1)->evaluate($context));
        $this->assertTrue($varA->lessThanOrEqualTo(2)->evaluate($context));
        $this->assertFalse($varA->lessThanOrEqualTo(0)->evaluate($context));

        $this->assertInstanceOf('Ruler\Operator\EqualTo', $varA->equalTo(0));
        $this->assertTrue($varA->equalTo(1)->evaluate($context));
        $this->assertFalse($varA->equalTo(0)->evaluate($context));
        $this->assertFalse($varA->equalTo(2)->evaluate($context));

        $this->assertInstanceOf('Ruler\Operator\NotEqualTo', $varA->notEqualTo(0));
        $this->assertFalse($varA->notEqualTo(1)->evaluate($context));
        $this->assertTrue($varA->notEqualTo(0)->evaluate($context));
        $this->assertTrue($varA->notEqualTo(2)->evaluate($context));

        $this->assertFalse($varA->greaterThan($varB)->evaluate($context));
        $this->assertTrue($varA->lessThan($varB)->evaluate($context));
    }
 /**
  * VariableProperty class constructor.
  *
  * @param BaseVariable $parent Parent Variable instance
  * @param string       $name   Property name
  * @param mixed        $value  Default Property value (default: null)
  */
 public function __construct(BaseVariable $parent, $name, $value = null)
 {
     $this->parent = $parent;
     parent::__construct($parent->getRuleBuilder(), $name, $value);
 }
Beispiel #4
0
 /**
  * RuleBuilder Variable constructor.
  *
  * @param RuleBuilder $ruleBuilder
  * @param string      $name        Variable name (default: null)
  * @param mixed       $value       Default Variable value (default: null)
  */
 public function __construct(RuleBuilder $ruleBuilder, $name = null, $value = null)
 {
     $this->ruleBuilder = $ruleBuilder;
     parent::__construct($name, $value);
 }