Newer
Older
<?php
/**
* Class ConditionParserTest
* @package Test\ConditionParser
*
* Date: 22.03.13
* Time: 16:08
* @author Thomas Joußen <tjoussen@databay.de>
*/
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
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);
}