-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfreemind-tests.php
222 lines (187 loc) · 6.1 KB
/
freemind-tests.php
1
2
3
4
5
6
7
8
9
10
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
error_reporting(0);
if (isset($_SERVER['argv']) && count($_SERVER['argv']) > 2) {
$doc = file_get_contents($_SERVER['argv'][1]);
$xml = new SimpleXMLElement($doc);
// print_as_tree($xml);
if (count($_SERVER['argv']) == 4) {
$cycles = $_SERVER['argv'][3];
} else {
$cycles = 1;
}
generate_test_cases($xml, $_SERVER['argv'][2], $cycles);
} else {
echo "Path to mindmap and root node missed\n";
exit();
}
function traverse($xml, $fn, $level = 0) {
$result = $fn($xml, $level);
if ($result !== false) {
return $result;
}
if ($xml->count() > 0) {
foreach ($xml->children() as $child) {
$result = traverse($child, $fn, $level+2);
if ($result !== false) {
return $result;
}
}
}
return false;
}
function print_as_tree($xml) {
traverse($xml, function ($node, $level) {
if (trim($node['TEXT']) !== '') {
echo str_repeat(' ', $level) . $node['TEXT'] . "\n";
}
return false;
});
}
function find_node($xml, $searchNode) {
return traverse($xml, function ($node, $level) use ($searchNode) {
$curNode = new FlowNode($node['TEXT']);
// var_dump($curNode);
if ($curNode->getTitle() === $searchNode) {
return $node;
}
return false;
});
}
class FlowNode {
const TYPE_END = 1;
const TYPE_REF = 2;
const TYPE_CASE = 3;
private $nodeTitle;
private $title;
private $type;
private $ref;
public function __construct($nodeTitle) {
$this->nodeTitle = $nodeTitle;
$title = trim(preg_replace('/[\(\[].*$/', '', $nodeTitle));
$this->title = $title;
if (strtoupper($title) === 'END') {
$this->type = self::TYPE_END;
} elseif (stripos($title, 'GOTO:') === 0) {
$this->type = self::TYPE_REF;
$this->ref = trim(substr($title, strlen('GOTO:')));
} else {
$this->type = self::TYPE_CASE;
}
}
/**
* @return mixed
*/
public function getNodeTitle() {
return $this->nodeTitle;
}
public function getType() {
return $this->type;
}
/**
* @return string
*/
public function getTitle() {
return $this->title;
}
/**
* @return bool|string
*/
public function getRef() {
return $this->ref;
}
}
class TestCaseDetector {
private $rootXml; // XML of flo that has to be processed
private $xml; // full XML
private $cyclesAllowed = 1;
const RESULT_OK = 'Ok';
const RESULT_NOT_TERMINATED = 'NotTerminated';
const RESULT_CYCLES = 'Cycles';
private $resultCycles = [];
private $resultOk = [];
private $resultNotTerminated = [];
public function __construct($rootXml, $cyclesAllowed, $xml) {
$this->rootXml = $rootXml;
$this->cyclesAllowed = $cyclesAllowed;
$this->xml = $xml;
}
public function process() {
$this->resultCycles = [];
$this->resultOk = [];
$this->resultNotTerminated = [];
$this->collectTestCases();
return [
self::RESULT_CYCLES => $this->resultCycles,
self::RESULT_OK => $this->resultOk,
self::RESULT_NOT_TERMINATED => $this->resultNotTerminated
];
}
private function collectTestCases($xml = null, $out = []) {
if ($xml === null) {
$xml = $this->rootXml;
}
if (!isset($xml['TEXT'])) {
// ignore node if it has no text
return;
}
$out[] = (string) $xml['TEXT'];
if ($xml->count() > 0) {
foreach ($xml->children() as $child) {
$this->collectTestCases($child, $out);
}
} else {
$this->validateTestCase($out);
}
}
private function validateTestCase($testCases) {
// echo "\n--\n";
// echo implode("\n", $testCases);
// var_dump($testCases);
if (count($testCases) === 0) {
throw new Exception("Testcase is empty");
return;
}
$flowNodes = [];
foreach ($testCases as $nodeTitle) {
$flowNode = new FlowNode($nodeTitle);
if (!isset($flowNodes[$flowNode->getTitle()])) {
$flowNodes[$flowNode->getTitle()] = 1;
} else {
$flowNodes[$flowNode->getTitle()] += 1;
}
if ($flowNodes[$flowNode->getTitle()] > $this->cyclesAllowed) {
// echo "WARN: Cycle found\n";
$this->resultCycles[] = $testCases;
return;
}
}
$lastNode = new FlowNode($testCases[count($testCases) - 1]);
if ($lastNode->getType() === FlowNode::TYPE_REF) {
$node = find_node($this->xml, $lastNode->getRef());
if ($node === false) {
// echo "ERROR: Unknown reference: {$lastNode->getNodeTitle()}\n";
throw new Exception("Unknown reference: {$lastNode->getNodeTitle()}");
}
$this->collectTestCases($node, $testCases);
} elseif ($lastNode->getType() !== FlowNode::TYPE_END) {
echo "ERROR: Flow was not terminated properly in {$lastNode->getTitle()}\n";
$this->resultNotTerminated[] = $testCases;
} else {
// echo "ok\n";
$this->resultOk[] = $testCases;
}
}
}
function generate_test_cases($xml, $rootNode, $cycles) {
$flowNode = find_node($xml, $rootNode);
if ($flowNode === null) {
echo 'Flow node not found';
return;
}
$detector = new TestCaseDetector($flowNode, $cycles, $xml);
$result = $detector->process();
foreach ($result[TestCaseDetector::RESULT_OK] as $k => $testCase) {
echo "TestCase #" . ($k + 1) . "\n - ";
echo implode("\n - ", $testCase) . "\n\n";
}
}