Skip to content
ConditionParserTest.php 5.02 KiB
Newer Older
<?php

/**
 * Class ConditionParserTest
 * @package Test\ConditionParser
 *
 * Date: 22.03.13
 * Time: 16:08
 * @author Thomas Joußen <tjoussen@databay.de>
 */
Michael Jansen's avatar
Michael Jansen committed
class ConditionParserTest extends \PHPUnit_Framework_TestCase
{
    protected $key_constraint;
    protected $type_constraint;
    protected $array_constraint;

    /**
     * @var null|ConditionParserProxy
     */
    protected $parser = null;

    protected static $CONDITION = "Q5 = +32+ & (Q74 <= %9879% | Q5 >= #5#)";
    protected static $EXPECTED_EXPRESSIONS = array("Q5","+32+","Q74","%9879%","Q5","#5#");
    protected static $EXPECTED_OPERATORS = array("=","&","<=","|",">=");
    protected static $CANNONICALIZED_CONDITION = "n o n o (n o n o n o n)";

    public function setUp()
    {
        include_once 'ConditionParserProxy.php';
        include_once __DIR__ . '/../classes/AbstractComposite.php';
        $this->parser = new ConditionParserProxy();


        $this->key_constraint = $this->logicalOr(
            $this->equalTo("type"),
            $this->equalTo("value"),
            $this->equalTo("negated")
        );
        $this->type_constraint = $this->logicalOr(
            $this->equalTo("group"),
            $this->equalTo("expression"),
            $this->equalTo("operator")
        );
        $this->array_constraint = $this->logicalOr(
            $this->equalTo("nodes"),
            $this->isType("integer")
        );
    }

    public function test_construction()
    {
        $this->assertInstanceOf('ConditionParser', $this->parser);
    }

    public function test_FetchExpressions()
    {
        $this->parser->setCondition(self::$CONDITION);
        $this->parser->fetchExpressions();

        foreach (self::$EXPECTED_EXPRESSIONS as $expression) {
            $this->assertContains($expression, $this->parser->getExpressions());
        }
    }

    public function test_FetchOperators()
    {
        $this->parser->setCondition(self::$CONDITION);
        $this->parser->fetchOperators();

        foreach (self::$EXPECTED_OPERATORS as $operator) {
            $this->assertContains($operator, $this->parser->getOperators());
        }
    }

    public function test_CannonicalizeCondition()
    {
        $this->parser->setCondition(self::$CONDITION);
        $this->parser->fetchExpressions();
        $this->parser->fetchOperators();
        $this->parser->cannonicalizeCondition();
        $this->assertEquals(self::$CANNONICALIZED_CONDITION, $this->parser->getCondition());
    }

    public function test_CreateNodeArray()
    {
        $this->parser->setCondition(self::$CONDITION);
        $this->parser->fetchExpressions();
        $this->parser->fetchOperators();
        $this->parser->cannonicalizeCondition();
        $node_array = $this->parser->createNodeArray();

        $this->recursiveNodeTest($node_array);
    }

    public function test_Parse()
    {
        $composite = $this->parser->parse(self::$CONDITION);
        $this->assertInstanceOf("AbstractComposite", $composite);
    }

    /**
     * @expectedException UnableToParseCondition
     */
    public function test_Nonsense()
    {
        $composite = $this->parser->parse("1 = 1");
    }

    public function test_DescribeComposite()
    {
        $composite = $this->parser->parse("Q1[2] & (Q2[1] | Q2[2])");
        $this->assertInstanceOf("AbstractComposite", $composite);
        $description = $composite->describe();
        $expected = "(Frage 1 mit Anwort 2 beantwortet und (Frage 2 mit Anwort 1 beantwortet oder Frage 2 mit Anwort 2 beantwortet ) ) ";
        $this->assertEquals($expected, $description);
    }

    /**
     * @expectedException ConditionParserException
     */
    public function test_Issue1338()
    {
        $condition = "Q85[1]=~nach oben geöffnete Parabel~ &|Q85[2]=#0# | Q85[8]=+2+ !Q85[9]=+2+";
        $this->parser->setCondition($condition);
        $this->parser->fetchExpressions();
        $this->parser->getOperators();
        $this->parser->cannonicalizeCondition();
        $this->parser->parse($condition);
    }

    public function test_Replacing()
    {
        $condition = "Q5 = +2+ & !Q2 < #3# | !Q5 <= %100%";
        $this->parser->setCondition($condition);
        $this->parser->fetchExpressions();
        $this->parser->getOperators();
        $this->parser->cannonicalizeCondition();

        $this->assertEquals("n o n o !(n o n) o !(n o n)", $this->parser->getCondition());
    }

    private function recursiveNodeTest($nodes)
    {
        foreach ($nodes as $key => $node) {
            if (!is_array($node)) {
                $this->assertThat($key, $this->key_constraint);
                if ($key == "type") {
                    $this->assertThat($node, $this->type_constraint);
                }
            } else {
                $this->assertThat($key, $this->array_constraint);
                $this->recursiveNodeTest($node);
            }
        }
    }

    public function test_missingSpaces()
    {
        $condition = "Q1=%100%";
        $composite = $this->parser->parse($condition);
        $this->assertInstanceOf("AbstractComposite", $composite);
    }