PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/test/testsuite/generator/behavior/nestedset/NestedSetBehaviorQueryBuilderModifierTest.php

https://github.com/mattleff/propel
PHP | 283 lines | 181 code | 15 blank | 87 comment | 0 complexity | 62ec4597f02996eaec329730e9e1e411 MD5 | raw file
  1. <?php
  2. /*
  3. * $Id: NestedSetBehaviorQueryBuilderModifierTest.php 1347 2009-12-03 21:06:36Z francois $
  4. * This file is part of the Propel package.
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. *
  8. * @license MIT License
  9. */
  10. require_once dirname(__FILE__) . '/../../../../tools/helpers/bookstore/behavior/BookstoreNestedSetTestBase.php';
  11. /**
  12. * Tests for NestedSetBehaviorQueryBuilderModifier class
  13. *
  14. * @author François Zaninotto
  15. * @version $Revision: 1834 $
  16. * @package generator.behavior.nestedset
  17. */
  18. class NestedSetBehaviorQueryBuilderModifierTest extends BookstoreNestedSetTestBase
  19. {
  20. public function testDescendantsOf()
  21. {
  22. list($t1, $t2, $t3, $t4, $t5, $t6, $t7) = $this->initTree();
  23. /* Tree used for tests
  24. t1
  25. | \
  26. t2 t3
  27. | \
  28. t4 t5
  29. | \
  30. t6 t7
  31. */
  32. $objs = Table9Query::create()
  33. ->descendantsOf($t7)
  34. ->orderByBranch()
  35. ->find();
  36. $coll = $this->buildCollection(array());
  37. $this->assertEquals($coll, $objs, 'decendantsOf() filters by descendants');
  38. $objs = Table9Query::create()
  39. ->descendantsOf($t3)
  40. ->orderByBranch()
  41. ->find();
  42. $coll = $this->buildCollection(array($t4, $t5, $t6, $t7));
  43. $this->assertEquals($coll, $objs, 'decendantsOf() filters by descendants');
  44. }
  45. public function testBranchOf()
  46. {
  47. list($t1, $t2, $t3, $t4, $t5, $t6, $t7) = $this->initTree();
  48. /* Tree used for tests
  49. t1
  50. | \
  51. t2 t3
  52. | \
  53. t4 t5
  54. | \
  55. t6 t7
  56. */
  57. $objs = Table9Query::create()
  58. ->branchOf($t7)
  59. ->orderByBranch()
  60. ->find();
  61. $coll = $this->buildCollection(array($t7));
  62. $this->assertEquals($coll, $objs, 'branchOf() filters by descendants and includes object passed as parameter');
  63. $objs = Table9Query::create()
  64. ->branchOf($t3)
  65. ->orderByBranch()
  66. ->find();
  67. $coll = $this->buildCollection(array($t3, $t4, $t5, $t6, $t7));
  68. $this->assertEquals($coll, $objs, 'branchOf() filters by descendants and includes object passed as parameter');
  69. $objs = Table9Query::create()
  70. ->branchOf($t1)
  71. ->orderByBranch()
  72. ->find();
  73. $coll = $this->buildCollection(array($t1, $t2, $t3, $t4, $t5, $t6, $t7));
  74. $this->assertEquals($coll, $objs, 'branchOf() returns the whole tree for the root node');
  75. }
  76. public function testChildrenOf()
  77. {
  78. list($t1, $t2, $t3, $t4, $t5, $t6, $t7) = $this->initTree();
  79. /* Tree used for tests
  80. t1
  81. | \
  82. t2 t3
  83. | \
  84. t4 t5
  85. | \
  86. t6 t7
  87. */
  88. $objs = Table9Query::create()
  89. ->childrenOf($t6)
  90. ->orderByBranch()
  91. ->find();
  92. $coll = $this->buildCollection(array());
  93. $this->assertEquals($coll, $objs, 'childrenOf() returns empty collection for leaf nodes');
  94. $objs = Table9Query::create()
  95. ->childrenOf($t5)
  96. ->orderByBranch()
  97. ->find();
  98. $coll = $this->buildCollection(array($t6, $t7));
  99. $this->assertEquals($coll, $objs, 'childrenOf() filters by children');
  100. $objs = Table9Query::create()
  101. ->childrenOf($t3)
  102. ->orderByBranch()
  103. ->find();
  104. $coll = $this->buildCollection(array($t4, $t5));
  105. $this->assertEquals($coll, $objs, 'childrenOf() filters by children and not by descendants');
  106. }
  107. public function testSiblingsOf()
  108. {
  109. list($t1, $t2, $t3, $t4, $t5, $t6, $t7) = $this->initTree();
  110. /* Tree used for tests
  111. t1
  112. | \
  113. t2 t3
  114. | \
  115. t4 t5
  116. | \
  117. t6 t7
  118. */
  119. $desc = Table9Query::create()
  120. ->siblingsOf($t1)
  121. ->orderByBranch()
  122. ->find();
  123. $coll = $this->buildCollection(array());
  124. $this->assertEquals($coll, $desc, 'siblingsOf() returns empty collection for the root node');
  125. $desc = Table9Query::create()
  126. ->siblingsOf($t3)
  127. ->orderByBranch()
  128. ->find();
  129. $coll = $this->buildCollection(array($t2));
  130. $this->assertEquals($coll, $desc, 'siblingsOf() filters by siblings');
  131. }
  132. public function testAncestorsOf()
  133. {
  134. list($t1, $t2, $t3, $t4, $t5, $t6, $t7) = $this->initTree();
  135. /* Tree used for tests
  136. t1
  137. | \
  138. t2 t3
  139. | \
  140. t4 t5
  141. | \
  142. t6 t7
  143. */
  144. $objs = Table9Query::create()
  145. ->ancestorsOf($t1)
  146. ->orderByBranch()
  147. ->find();
  148. $coll = $this->buildCollection(array());
  149. $this->assertEquals($coll, $objs, 'ancestorsOf() returns empty collection for root node');
  150. $objs = Table9Query::create()
  151. ->ancestorsOf($t3)
  152. ->orderByBranch()
  153. ->find();
  154. $coll = $this->buildCollection(array($t1));
  155. $this->assertEquals($coll, $objs, 'ancestorsOf() filters by ancestors');
  156. $objs = Table9Query::create()
  157. ->ancestorsOf($t7)
  158. ->orderByBranch()
  159. ->find();
  160. $coll = $this->buildCollection(array($t1, $t3, $t5));
  161. $this->assertEquals($coll, $objs, 'childrenOf() filters by ancestors');
  162. }
  163. public function testRootsOf()
  164. {
  165. list($t1, $t2, $t3, $t4, $t5, $t6, $t7) = $this->initTree();
  166. /* Tree used for tests
  167. t1
  168. | \
  169. t2 t3
  170. | \
  171. t4 t5
  172. | \
  173. t6 t7
  174. */
  175. $objs = Table9Query::create()
  176. ->rootsOf($t1)
  177. ->orderByBranch()
  178. ->find();
  179. $coll = $this->buildCollection(array($t1));
  180. $this->assertEquals($coll, $objs, 'rootsOf() returns the root node for root node');
  181. $objs = Table9Query::create()
  182. ->rootsOf($t3)
  183. ->orderByBranch()
  184. ->find();
  185. $coll = $this->buildCollection(array($t1, $t3));
  186. $this->assertEquals($coll, $objs, 'rootsOf() filters by ancestors and includes the node passed as parameter');
  187. $objs = Table9Query::create()
  188. ->rootsOf($t7)
  189. ->orderByBranch()
  190. ->find();
  191. $coll = $this->buildCollection(array($t1, $t3, $t5, $t7));
  192. $this->assertEquals($coll, $objs, 'rootsOf() filters by ancestors and includes the node passed as parameter');
  193. }
  194. public function testOrderByBranch()
  195. {
  196. list($t1, $t2, $t3, $t4, $t5, $t6, $t7) = $this->initTree();
  197. $t5->moveToPrevSiblingOf($t4);
  198. /* Results in
  199. t1
  200. | \
  201. t2 t3
  202. | \
  203. t5 t4
  204. | \
  205. t6 t7
  206. */
  207. $objs = Table9Query::create()
  208. ->orderByBranch()
  209. ->find();
  210. $coll = $this->buildCollection(array($t1, $t2, $t3, $t5, $t6, $t7, $t4), 'orderByBranch() orders by branch left to right');
  211. $objs = Table9Query::create()
  212. ->orderByBranch(true)
  213. ->find();
  214. $coll = $this->buildCollection(array($t4, $t7, $t6, $t5, $t3, $t2, $t1), 'orderByBranch(true) orders by branch right to left');
  215. }
  216. public function testOrderByLevel()
  217. {
  218. list($t1, $t2, $t3, $t4, $t5, $t6, $t7) = $this->initTree();
  219. $t5->moveToPrevSiblingOf($t4);
  220. /* Results in
  221. t1
  222. | \
  223. t2 t3
  224. | \
  225. t5 t4
  226. | \
  227. t6 t7
  228. */
  229. $objs = Table9Query::create()
  230. ->orderByLevel()
  231. ->find();
  232. $coll = $this->buildCollection(array($t1, $t2, $t5, $t4, $t6, $t7), 'orderByLevel() orders by level, from the root to the leaf');
  233. $objs = Table9Query::create()
  234. ->orderByLevel(true)
  235. ->find();
  236. $coll = $this->buildCollection(array($t7, $t6, $t4, $t5, $t2, $t1), 'orderByLevel(true) orders by level, from the leaf to the root');
  237. }
  238. public function testFindRoot()
  239. {
  240. $this->assertTrue(method_exists('Table9Query', 'findRoot'), 'nested_set adds a findRoot() method');
  241. Table9Query::create()->deleteAll();
  242. $this->assertNull(Table9Query::create()->findRoot(), 'findRoot() returns null as long as no root node is defined');
  243. $t1 = new Table9();
  244. $t1->setLeftValue(123);
  245. $t1->setRightValue(456);
  246. $t1->save();
  247. $this->assertNull(Table9Query::create()->findRoot(), 'findRoot() returns null as long as no root node is defined');
  248. $t2 = new Table9();
  249. $t2->setLeftValue(1);
  250. $t2->setRightValue(2);
  251. $t2->save();
  252. $this->assertEquals(Table9Query::create()->findRoot(), $t2, 'findRoot() retrieves the root node');
  253. }
  254. public function testfindTree()
  255. {
  256. list($t1, $t2, $t3, $t4, $t5, $t6, $t7) = $this->initTree();
  257. $tree = Table9Query::create()->findTree();
  258. $coll = $this->buildCollection(array($t1, $t2, $t3, $t4, $t5, $t6, $t7));
  259. $this->assertEquals($coll, $tree, 'findTree() retrieves the whole tree, ordered by branch');
  260. }
  261. protected function buildCollection($arr)
  262. {
  263. $coll = new PropelObjectCollection();
  264. $coll->setData($arr);
  265. $coll->setModel('Table9');
  266. return $coll;
  267. }
  268. }