/** * @test * @dataProvider getSomeEvaluationTestValues * @param string $comparison * @param boolean $expected */ public function testSomeEvaluations($comparison, $expected, $variables = array()) { $parser = new BooleanParser(); $this->assertEquals($expected, BooleanNode::convertToBoolean($parser->evaluate($comparison, $variables), $this->renderingContext), 'Expression: ' . $comparison); $compiledEvaluation = $parser->compile($comparison); $functionName = 'expression_' . md5($comparison . rand(0, 100000)); eval('function ' . $functionName . '($context) {return ' . $compiledEvaluation . ';}'); $this->assertEquals($expected, BooleanNode::convertToBoolean($functionName($variables), $this->renderingContext), 'compiled Expression: ' . $compiledEvaluation); }
/** * @param RenderingContextInterface $renderingContext * @param string $expression * @param array $matches * @return mixed */ public static function evaluateExpression(RenderingContextInterface $renderingContext, $expression, array $matches) { $parts = preg_split('/([\\?:])/s', $expression); $parts = array_map(array(__CLASS__, 'trimPart'), $parts); list($check, $then, $else) = $parts; $checkResult = Parser\SyntaxTree\BooleanNode::convertToBoolean(parent::getTemplateVariableOrValueItself($check, $renderingContext)); if ($checkResult) { return parent::getTemplateVariableOrValueItself($then, $renderingContext); } else { return parent::getTemplateVariableOrValueItself($else, $renderingContext); } }
/** * @test */ public function convertToBooleanProperlyConvertsObjects() { $this->assertFalse(BooleanNode::convertToBoolean(NULL, $this->renderingContext)); $this->assertTrue(BooleanNode::convertToBoolean(new \stdClass(), $this->renderingContext)); }
/** * @test */ public function convertToBooleanProperlyConvertsObjects() { $this->assertFalse(BooleanNode::convertToBoolean(NULL)); $this->assertTrue(BooleanNode::convertToBoolean(new \stdClass())); }