root/trunk/generator/test/tree-test.php

Revision 833, 10.0 kB (checked in by hans, 1 year ago)

Merging in changes from 1.3 branch. svn merge -r 534:832

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id Rev Date Author HeadURL Revision
Line 
1 <?php
2
3 if (!isset($argc)) echo "<pre>";
4
5 error_reporting(E_ALL);
6
7 $conf_path = realpath(dirname(__FILE__) . '/../projects/treetest/build/conf/treetest-conf.php');
8 if (!file_exists($conf_path)) {
9     echo "Make sure that you specify properties in conf/treetest.properties and "
10     ."build propel before running this script.";
11     exit;
12 }
13
14 // Add PHP_CLASSPATH, if set
15 if (getenv("PHP_CLASSPATH")) {
16     set_include_path(getenv("PHP_CLASSPATH") . PATH_SEPARATOR . get_include_path());
17 }
18
19  // Add build/classes/ and classes/ to path
20 set_include_path(
21     realpath(dirname(__FILE__) . '/../projects/treetest/build/classes') . PATH_SEPARATOR .
22     dirname(__FILE__) . '/../../runtime/classes' . PATH_SEPARATOR .
23     get_include_path()
24 );
25
26
27 // Require classes.
28 require_once 'propel/Propel.php';
29 require_once 'treetest/TestNodePeer.php';
30
31 function dumpTree($node, $querydb = false, $con = null)
32 {
33     $opts = array('querydb' => $querydb,
34                   'con' => $con);
35
36     $node->setIteratorOptions('pre', $opts);
37
38     $indent = 0;
39     $lastLevel = $node->getNodeLevel();
40
41     foreach ($node as $n)
42     {
43         $nodeLevel = $n->getNodeLevel();
44         $indent += $nodeLevel - $lastLevel;
45         echo str_repeat('  '$indent);
46         echo $n->getNodePath() . " -> " . $n->getLabel();
47         echo "\n";
48         $lastLevel = $nodeLevel;
49     }
50 }
51
52 try {
53     // Initialize Propel
54      Propel::init($conf_path);
55 } catch (Exception $e) {
56     die("Error initializing propel: ". $e->__toString());
57 }
58
59 try {
60
61     $nodeKeySep = TestNodePeer::NPATH_SEP;
62
63     echo "\nCreating initial tree:\n";
64     echo "-------------------------------------\n";
65
66     $a = new Test();
67     $a->setLabel("a");
68     $a = TestNodePeer::createNewRootNode($a);
69     echo "Created 'a' as new root\n";
70
71     $b = new TestNode();
72     $b->setLabel('b');
73     $a->addChildNode($b);
74     echo "Added 'b' as first child of 'a'\n";
75
76     $c = new TestNode();
77     $c->setLabel('c');
78     $a->addChildNode($c);
79     echo "Added 'c' as second child of 'a'\n";
80
81     $f = new TestNode();
82     $f->setLabel('f');
83     $b->addChildNode($f);
84     echo "Added 'f' as first child of 'b'\n";
85
86     $d = new TestNode();
87     $d->setLabel('d');
88     $b->addChildNode($d, $f);
89     echo "Added 'd' as first child of 'b' before 'f' (insert before first child test - f is now second child)\n";
90
91     $e = new TestNode();
92     $e->setLabel('e');
93     $b->addChildNode($e, $f);
94     echo "Added 'e' as second child of 'b' before 'f' (insert before last child test - f is now third child)\n";
95
96     $g = new TestNode();
97     $g->setLabel('g');
98     $c->addChildNode($g);
99     echo "Added 'g' as first child of 'c'\n";
100
101     $h = new TestNode();
102     $h->setLabel('h');
103     $c->addChildNode($h);
104     echo "Added 'h' as second child of 'c'\n";
105
106     $i = new TestNode();
107     $i->setLabel('i');
108     $d->addChildNode($i);
109     echo "Added 'i' as first child of 'd'\n";
110
111     $j = new TestNode();
112     $j->setLabel('j');
113     $f->addChildNode($j);
114     echo "Added 'j' as first child of 'f'\n";
115
116     $k = new TestNode();
117     $k->setLabel('k');
118     $j->addChildNode($k);
119     echo "Added 'k' as first child of 'j'\n";
120
121     $l = new TestNode();
122     $l->setLabel('l');
123     $j->addChildNode($l);
124     echo "Added 'l' as second child of 'j'\n";
125
126     dumpTree($a);
127
128
129     echo "\n\nDeleting 'd' node sub-tree:\n";
130     echo "-------------------------------------\n";
131
132     $d->delete();
133
134     dumpTree($a);
135
136
137     echo "\n\nMove node tests:\n";
138     echo "-------------------------------------\n";
139
140     echo "Move 'j' sub-tree to 'b' before 'e' (move tree/insert before first child test):\n";
141     $b->addChildNode($j, $e);
142     dumpTree($a);
143
144     echo "\nMove 'j' sub-tree to 'c' (move tree after last child test):\n";
145     $c->addChildNode($j);
146     dumpTree($a);
147
148     echo "\nMove 'j' sub-tree to 'g' (move tree to first child test):\n";
149     $g->addChildNode($j);
150     dumpTree($a);
151
152
153     echo "\n\nCreating new (in-memory) sub-tree:\n";
154     echo "-------------------------------------\n";
155
156     $m = new TestNode();
157     $m->setLabel('m');
158     echo "Created 'm' as root of new sub-tree\n";
159
160     $n = new TestNode();
161     $n->setLabel('n');
162     $m->addChildNode($n);
163     echo "Added 'n' as first child of 'm'\n";
164
165     $o = new TestNode();
166     $o->setLabel('o');
167     $m->addChildNode($o);
168     echo "Added 'o' as second child of 'm'\n";
169
170     $r = new TestNode();
171     $r->setLabel('r');
172     $n->addChildNode($r);
173     echo "Added 'r' as first child of 'n'\n";
174
175     $p = new TestNode();
176     $p->setLabel('p');
177     $n->addChildNode($p, $r);
178     echo "Added 'p' as first child of 'n' before 'r' (insert before first child test - r is now second child)\n";
179
180     $q = new TestNode();
181     $q->setLabel('q');
182     $n->addChildNode($q, $r);
183     echo "Added 'q' as second child of 'n' before 'r' (insert before last child test - r is now third child)\n";
184
185     $s = new TestNode();
186     $s->setLabel('s');
187     $o->addChildNode($s);
188     echo "Added 's' as first child of 'o'\n";
189
190     $t = new TestNode();
191     $t->setLabel('t');
192     $o->addChildNode($t);
193     echo "Added 't' as second child of 'o'\n";
194
195     $u = new TestNode();
196     $u->setLabel('u');
197     $p->addChildNode($u);
198     echo "Added 'u' as first child of 'p'\n";
199
200     $v = new TestNode();
201     $v->setLabel('v');
202     $r->addChildNode($v);
203     echo "Added 'v' as first child of 'r'\n";
204
205     $w = new TestNode();
206     $w->setLabel('w');
207     $v->addChildNode($w);
208     echo "Added 'w' as first child of 'v'\n";
209
210     $x = new TestNode();
211     $x->setLabel('x');
212     $v->addChildNode($x);
213     echo "Added 'x' as second child of 'v'\n";
214
215     dumpTree($m);
216
217
218     echo "\n\nDeleting in-memory 'p' node sub-tree:\n";
219     echo "-------------------------------------\n";
220
221     $p->delete();
222
223     dumpTree($m);
224
225
226     echo "\n\nMove in-memory node tests:\n";
227     echo "-------------------------------------\n";
228
229     echo "Move 'v' sub-tree to 'n' before 'q' (move tree/insert before first child test):\n";
230     $n->addChildNode($v, $q);
231     dumpTree($m);
232
233     echo "\nMove 'v' sub-tree to 'o' (move tree after last child test):\n";
234     $o->addChildNode($v);
235     dumpTree($m);
236
237     echo "\nMove 'v' sub-tree to 's' (move tree to first child test):\n";
238     $s->addChildNode($v);
239     dumpTree($m);
240
241
242     echo "\n\nAdd in-memory 'm' sub-tree to 'a':\n";
243     echo "-------------------------------------\n";
244
245     $a->addChildNode($m);
246
247     dumpTree($a);
248
249
250     echo "\n\nInsert new root node 'z' and retrieve descendants on demand (via querydb param in iterator):\n";
251     echo "-------------------------------------\n";
252     $z = new Test();
253     $z->setLabel("z");
254     $z = TestNodePeer::insertNewRootNode($z);
255
256     dumpTree($z, true);
257
258 } catch (Exception $e) {
259     die("Error creating initial tree: " . $e->__toString());
260 }
261
262 try {
263
264     echo "\n\nTest retrieveRootNode() (without descendants)\n";
265     echo "-------------------------------------\n";
266     $root = TestNodePeer::retrieveRootNode(false);
267     dumpTree($root);
268
269
270     echo "\n\nTest retrieveRootNode() (with descendants)\n";
271     echo "-------------------------------------\n";
272     $root = TestNodePeer::retrieveRootNode(true);
273     dumpTree($root);
274
275     $m_addr = array(1,1,3);
276
277     echo "\n\nTest retrieveNodeByNP() for 'm' (without descendants)\n";
278     echo "-------------------------------------\n";
279     $node = TestNodePeer::retrieveNodeByNP(implode($nodeKeySep, $m_addr), false, false);
280     dumpTree($node);
281
282
283     echo "\n\nTest retrieveNodeByNP() for 'm' (with descendants)\n";
284     echo "-------------------------------------\n";
285     $node = TestNodePeer::retrieveNodeByNP(implode($nodeKeySep, $m_addr), false, true);
286     dumpTree($node);
287
288
289     echo "\n\nTest getAncestors() for 'x' in one query:\n";
290     echo "-------------------------------------\n";
291
292     $criteria = new Criteria();
293     $criteria->add(TestPeer::LABEL, 'x', Criteria::EQUAL);
294
295     $nodes = TestNodePeer::retrieveNodes($criteria, true, false);
296     $ancestors = $nodes[0]->getAncestors(false);
297
298     foreach ($ancestors as $ancestor)
299         echo $ancestor->getNodePath() . " -> " . $ancestor->getLabel() . "\n";
300
301
302     echo "\n\nTest retrieveNodeByNP() for 'o' (with ancestors and descendants in one query):\n";
303     echo "-------------------------------------\n";
304
305     $o_addr = array(1,1,3,2);
306
307     $node = TestNodePeer::retrieveNodeByNP(implode($nodeKeySep, $o_addr), true, true);
308
309     echo "ancestors:\n";
310     foreach ($node->getAncestors(false) as $ancestor)
311         echo $ancestor->getNodePath() . " -> " . $ancestor->getLabel() . "\n";
312
313     echo "\ndescendants:\n";
314     dumpTree($node);
315
316
317     echo "\n\nTest retrieveNodes() between 'b' and 'g' (without descendants)\n";
318     echo "-------------------------------------\n";
319
320     $criteria = new Criteria();
321     $criteria->add(TestPeer::LABEL, 'b', Criteria::GREATER_EQUAL);
322     $criteria->addAnd(TestPeer::LABEL, 'g', Criteria::LESS_EQUAL);
323     $criteria->addAscendingOrderByColumn(TestPeer::LABEL);
324
325     $nodes = TestNodePeer::retrieveNodes($criteria, false, false);
326
327     foreach ($nodes as $node)
328         echo $node->getNodePath() . " -> " . $node->getLabel() . "\n";
329
330
331     echo "\n\nTest retrieveNodes() between 'b' and 'g' (with descendants)\n";
332     echo "-------------------------------------\n";
333
334     $criteria = new Criteria();
335     $criteria->add(TestPeer::LABEL, 'b', Criteria::GREATER_EQUAL);
336     $criteria->addAnd(TestPeer::LABEL, 'g', Criteria::LESS_EQUAL);
337     $criteria->addAscendingOrderByColumn(TestPeer::LABEL);
338
339     $nodes = TestNodePeer::retrieveNodes($criteria, false, true);
340
341     foreach ($nodes as $node)
342     {
343         dumpTree($node);
344         echo "\n";
345     }
346
347
348 } catch (Exception $e) {
349     die("Error retrieving nodes: " . $e->__toString());
350 }
351
352 try {
353
354     echo "\nCreating new tree:\n";
355     echo "-------------------------------------\n";
356
357     $a = new Test();
358     $a->setLabel("a");
359     $a = TestNodePeer::createNewRootNode($a);
360     echo "Created 'a' as new root\n";
361
362     echo "\nAdding 10 child nodes:\n";
363     echo "-------------------------------------\n";
364
365     $b = new TestNode();
366     $b->setLabel('b');
367     $a->addChildNode($b);
368
369     $c = new TestNode();
370     $c->setLabel('c');
371     $a->addChildNode($c);
372
373     $d = new TestNode();
374     $d->setLabel('d');
375     $a->addChildNode($d);
376
377     $e = new TestNode();
378     $e->setLabel('e');
379     $a->addChildNode($e);
380
381     $f = new TestNode();
382     $f->setLabel('f');
383     $a->addChildNode($f);
384
385     $g = new TestNode();
386     $g->setLabel('g');
387     $a->addChildNode($g);
388
389     $h = new TestNode();
390     $h->setLabel('h');
391     $a->addChildNode($h);
392
393     $i = new TestNode();
394     $i->setLabel('i');
395     $a->addChildNode($i);
396
397     $j = new TestNode();
398     $j->setLabel('j');
399     $a->addChildNode($j);
400
401     $k = new TestNode();
402     $k->setLabel('k');
403     $a->addChildNode($k);
404
405     echo "\ndescendants:\n";
406     dumpTree($a);
407
408     echo "\nRetrieving last node:\n";
409     echo "-------------------------------------\n";
410
411     $last = $a->getLastChildNode(true);
412     echo "Last child node is '" . $last->getLabel() . "' (" . $last->getNodePath() . ")\n";
413
414 } catch (Exception $e) {
415     die("Error creating tree with > 10 nodes: " . $e->__toString());
416 }
417
418 if (!isset($argc)) echo "</pre>";
419
Note: See TracBrowser for help on using the browser.