tree-test.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. <?php
  2. /**
  3. * This file is part of the Propel package.
  4. * For the full copyright and license information, please view the LICENSE
  5. * file that was distributed with this source code.
  6. *
  7. * @license MIT License
  8. */
  9. if (!isset($argc)) echo "<pre>";
  10. error_reporting(E_ALL);
  11. $conf_path = realpath(dirname(__FILE__) . '/fixtures/treetest/build/conf/treetest-conf.php');
  12. if (!file_exists($conf_path)) {
  13. echo "Make sure that you specify properties in conf/treetest.properties and "
  14. ."build propel before running this script.";
  15. exit;
  16. }
  17. // Add PHP_CLASSPATH, if set
  18. if (getenv("PHP_CLASSPATH")) {
  19. set_include_path(getenv("PHP_CLASSPATH") . PATH_SEPARATOR . get_include_path());
  20. }
  21. // Add build/classes/ and classes/ to path
  22. set_include_path(
  23. realpath(dirname(__FILE__) . '/fixtures/treetest/build/classes') . PATH_SEPARATOR .
  24. dirname(__FILE__) . '/../runtime/classes' . PATH_SEPARATOR .
  25. get_include_path()
  26. );
  27. // Require classes.
  28. require_once 'propel/Propel.php';
  29. require_once 'treetest/TestNodePeer.php';
  30. function dumpTree($node, $querydb = false, $con = null)
  31. {
  32. $opts = array('querydb' => $querydb,
  33. 'con' => $con);
  34. $node->setIteratorOptions('pre', $opts);
  35. $indent = 0;
  36. $lastLevel = $node->getNodeLevel();
  37. foreach ($node as $n)
  38. {
  39. $nodeLevel = $n->getNodeLevel();
  40. $indent += $nodeLevel - $lastLevel;
  41. echo str_repeat(' ', $indent);
  42. echo $n->getNodePath() . " -> " . $n->getLabel();
  43. echo "\n";
  44. $lastLevel = $nodeLevel;
  45. }
  46. }
  47. try {
  48. // Initialize Propel
  49. Propel::init($conf_path);
  50. } catch (Exception $e) {
  51. die("Error initializing propel: ". $e->__toString());
  52. }
  53. try {
  54. $nodeKeySep = TestNodePeer::NPATH_SEP;
  55. echo "\nCreating initial tree:\n";
  56. echo "-------------------------------------\n";
  57. $a = new Test();
  58. $a->setLabel("a");
  59. $a = TestNodePeer::createNewRootNode($a);
  60. echo "Created 'a' as new root\n";
  61. $b = new TestNode();
  62. $b->setLabel('b');
  63. $a->addChildNode($b);
  64. echo "Added 'b' as first child of 'a'\n";
  65. $c = new TestNode();
  66. $c->setLabel('c');
  67. $a->addChildNode($c);
  68. echo "Added 'c' as second child of 'a'\n";
  69. $f = new TestNode();
  70. $f->setLabel('f');
  71. $b->addChildNode($f);
  72. echo "Added 'f' as first child of 'b'\n";
  73. $d = new TestNode();
  74. $d->setLabel('d');
  75. $b->addChildNode($d, $f);
  76. echo "Added 'd' as first child of 'b' before 'f' (insert before first child test - f is now second child)\n";
  77. $e = new TestNode();
  78. $e->setLabel('e');
  79. $b->addChildNode($e, $f);
  80. echo "Added 'e' as second child of 'b' before 'f' (insert before last child test - f is now third child)\n";
  81. $g = new TestNode();
  82. $g->setLabel('g');
  83. $c->addChildNode($g);
  84. echo "Added 'g' as first child of 'c'\n";
  85. $h = new TestNode();
  86. $h->setLabel('h');
  87. $c->addChildNode($h);
  88. echo "Added 'h' as second child of 'c'\n";
  89. $i = new TestNode();
  90. $i->setLabel('i');
  91. $d->addChildNode($i);
  92. echo "Added 'i' as first child of 'd'\n";
  93. $j = new TestNode();
  94. $j->setLabel('j');
  95. $f->addChildNode($j);
  96. echo "Added 'j' as first child of 'f'\n";
  97. $k = new TestNode();
  98. $k->setLabel('k');
  99. $j->addChildNode($k);
  100. echo "Added 'k' as first child of 'j'\n";
  101. $l = new TestNode();
  102. $l->setLabel('l');
  103. $j->addChildNode($l);
  104. echo "Added 'l' as second child of 'j'\n";
  105. dumpTree($a);
  106. echo "\n\nDeleting 'd' node sub-tree:\n";
  107. echo "-------------------------------------\n";
  108. $d->delete();
  109. dumpTree($a);
  110. echo "\n\nMove node tests:\n";
  111. echo "-------------------------------------\n";
  112. echo "Move 'j' sub-tree to 'b' before 'e' (move tree/insert before first child test):\n";
  113. $b->addChildNode($j, $e);
  114. dumpTree($a);
  115. echo "\nMove 'j' sub-tree to 'c' (move tree after last child test):\n";
  116. $c->addChildNode($j);
  117. dumpTree($a);
  118. echo "\nMove 'j' sub-tree to 'g' (move tree to first child test):\n";
  119. $g->addChildNode($j);
  120. dumpTree($a);
  121. echo "\n\nCreating new (in-memory) sub-tree:\n";
  122. echo "-------------------------------------\n";
  123. $m = new TestNode();
  124. $m->setLabel('m');
  125. echo "Created 'm' as root of new sub-tree\n";
  126. $n = new TestNode();
  127. $n->setLabel('n');
  128. $m->addChildNode($n);
  129. echo "Added 'n' as first child of 'm'\n";
  130. $o = new TestNode();
  131. $o->setLabel('o');
  132. $m->addChildNode($o);
  133. echo "Added 'o' as second child of 'm'\n";
  134. $r = new TestNode();
  135. $r->setLabel('r');
  136. $n->addChildNode($r);
  137. echo "Added 'r' as first child of 'n'\n";
  138. $p = new TestNode();
  139. $p->setLabel('p');
  140. $n->addChildNode($p, $r);
  141. echo "Added 'p' as first child of 'n' before 'r' (insert before first child test - r is now second child)\n";
  142. $q = new TestNode();
  143. $q->setLabel('q');
  144. $n->addChildNode($q, $r);
  145. echo "Added 'q' as second child of 'n' before 'r' (insert before last child test - r is now third child)\n";
  146. $s = new TestNode();
  147. $s->setLabel('s');
  148. $o->addChildNode($s);
  149. echo "Added 's' as first child of 'o'\n";
  150. $t = new TestNode();
  151. $t->setLabel('t');
  152. $o->addChildNode($t);
  153. echo "Added 't' as second child of 'o'\n";
  154. $u = new TestNode();
  155. $u->setLabel('u');
  156. $p->addChildNode($u);
  157. echo "Added 'u' as first child of 'p'\n";
  158. $v = new TestNode();
  159. $v->setLabel('v');
  160. $r->addChildNode($v);
  161. echo "Added 'v' as first child of 'r'\n";
  162. $w = new TestNode();
  163. $w->setLabel('w');
  164. $v->addChildNode($w);
  165. echo "Added 'w' as first child of 'v'\n";
  166. $x = new TestNode();
  167. $x->setLabel('x');
  168. $v->addChildNode($x);
  169. echo "Added 'x' as second child of 'v'\n";
  170. dumpTree($m);
  171. echo "\n\nDeleting in-memory 'p' node sub-tree:\n";
  172. echo "-------------------------------------\n";
  173. $p->delete();
  174. dumpTree($m);
  175. echo "\n\nMove in-memory node tests:\n";
  176. echo "-------------------------------------\n";
  177. echo "Move 'v' sub-tree to 'n' before 'q' (move tree/insert before first child test):\n";
  178. $n->addChildNode($v, $q);
  179. dumpTree($m);
  180. echo "\nMove 'v' sub-tree to 'o' (move tree after last child test):\n";
  181. $o->addChildNode($v);
  182. dumpTree($m);
  183. echo "\nMove 'v' sub-tree to 's' (move tree to first child test):\n";
  184. $s->addChildNode($v);
  185. dumpTree($m);
  186. echo "\n\nAdd in-memory 'm' sub-tree to 'a':\n";
  187. echo "-------------------------------------\n";
  188. $a->addChildNode($m);
  189. dumpTree($a);
  190. echo "\n\nInsert new root node 'z' and retrieve descendants on demand (via querydb param in iterator):\n";
  191. echo "-------------------------------------\n";
  192. $z = new Test();
  193. $z->setLabel("z");
  194. $z = TestNodePeer::insertNewRootNode($z);
  195. dumpTree($z, true);
  196. } catch (Exception $e) {
  197. die("Error creating initial tree: " . $e->__toString());
  198. }
  199. try {
  200. echo "\n\nTest retrieveRootNode() (without descendants)\n";
  201. echo "-------------------------------------\n";
  202. $root = TestNodePeer::retrieveRootNode(false);
  203. dumpTree($root);
  204. echo "\n\nTest retrieveRootNode() (with descendants)\n";
  205. echo "-------------------------------------\n";
  206. $root = TestNodePeer::retrieveRootNode(true);
  207. dumpTree($root);
  208. $m_addr = array(1,1,3);
  209. echo "\n\nTest retrieveNodeByNP() for 'm' (without descendants)\n";
  210. echo "-------------------------------------\n";
  211. $node = TestNodePeer::retrieveNodeByNP(implode($nodeKeySep, $m_addr), false, false);
  212. dumpTree($node);
  213. echo "\n\nTest retrieveNodeByNP() for 'm' (with descendants)\n";
  214. echo "-------------------------------------\n";
  215. $node = TestNodePeer::retrieveNodeByNP(implode($nodeKeySep, $m_addr), false, true);
  216. dumpTree($node);
  217. echo "\n\nTest getAncestors() for 'x' in one query:\n";
  218. echo "-------------------------------------\n";
  219. $criteria = new Criteria();
  220. $criteria->add(TestPeer::LABEL, 'x', Criteria::EQUAL);
  221. $nodes = TestNodePeer::retrieveNodes($criteria, true, false);
  222. $ancestors = $nodes[0]->getAncestors(false);
  223. foreach ($ancestors as $ancestor)
  224. echo $ancestor->getNodePath() . " -> " . $ancestor->getLabel() . "\n";
  225. echo "\n\nTest retrieveNodeByNP() for 'o' (with ancestors and descendants in one query):\n";
  226. echo "-------------------------------------\n";
  227. $o_addr = array(1,1,3,2);
  228. $node = TestNodePeer::retrieveNodeByNP(implode($nodeKeySep, $o_addr), true, true);
  229. echo "ancestors:\n";
  230. foreach ($node->getAncestors(false) as $ancestor)
  231. echo $ancestor->getNodePath() . " -> " . $ancestor->getLabel() . "\n";
  232. echo "\ndescendants:\n";
  233. dumpTree($node);
  234. echo "\n\nTest retrieveNodes() between 'b' and 'g' (without descendants)\n";
  235. echo "-------------------------------------\n";
  236. $criteria = new Criteria();
  237. $criteria->add(TestPeer::LABEL, 'b', Criteria::GREATER_EQUAL);
  238. $criteria->addAnd(TestPeer::LABEL, 'g', Criteria::LESS_EQUAL);
  239. $criteria->addAscendingOrderByColumn(TestPeer::LABEL);
  240. $nodes = TestNodePeer::retrieveNodes($criteria, false, false);
  241. foreach ($nodes as $node)
  242. echo $node->getNodePath() . " -> " . $node->getLabel() . "\n";
  243. echo "\n\nTest retrieveNodes() between 'b' and 'g' (with descendants)\n";
  244. echo "-------------------------------------\n";
  245. $criteria = new Criteria();
  246. $criteria->add(TestPeer::LABEL, 'b', Criteria::GREATER_EQUAL);
  247. $criteria->addAnd(TestPeer::LABEL, 'g', Criteria::LESS_EQUAL);
  248. $criteria->addAscendingOrderByColumn(TestPeer::LABEL);
  249. $nodes = TestNodePeer::retrieveNodes($criteria, false, true);
  250. foreach ($nodes as $node)
  251. {
  252. dumpTree($node);
  253. echo "\n";
  254. }
  255. } catch (Exception $e) {
  256. die("Error retrieving nodes: " . $e->__toString());
  257. }
  258. try {
  259. echo "\nCreating new tree:\n";
  260. echo "-------------------------------------\n";
  261. $a = new Test();
  262. $a->setLabel("a");
  263. $a = TestNodePeer::createNewRootNode($a);
  264. echo "Created 'a' as new root\n";
  265. echo "\nAdding 10 child nodes:\n";
  266. echo "-------------------------------------\n";
  267. $b = new TestNode();
  268. $b->setLabel('b');
  269. $a->addChildNode($b);
  270. $c = new TestNode();
  271. $c->setLabel('c');
  272. $a->addChildNode($c);
  273. $d = new TestNode();
  274. $d->setLabel('d');
  275. $a->addChildNode($d);
  276. $e = new TestNode();
  277. $e->setLabel('e');
  278. $a->addChildNode($e);
  279. $f = new TestNode();
  280. $f->setLabel('f');
  281. $a->addChildNode($f);
  282. $g = new TestNode();
  283. $g->setLabel('g');
  284. $a->addChildNode($g);
  285. $h = new TestNode();
  286. $h->setLabel('h');
  287. $a->addChildNode($h);
  288. $i = new TestNode();
  289. $i->setLabel('i');
  290. $a->addChildNode($i);
  291. $j = new TestNode();
  292. $j->setLabel('j');
  293. $a->addChildNode($j);
  294. $k = new TestNode();
  295. $k->setLabel('k');
  296. $a->addChildNode($k);
  297. echo "\ndescendants:\n";
  298. dumpTree($a);
  299. echo "\nRetrieving last node:\n";
  300. echo "-------------------------------------\n";
  301. $last = $a->getLastChildNode(true);
  302. echo "Last child node is '" . $last->getLabel() . "' (" . $last->getNodePath() . ")\n";
  303. } catch (Exception $e) {
  304. die("Error creating tree with > 10 nodes: " . $e->__toString());
  305. }
  306. if (!isset($argc)) echo "</pre>";