/tests/Propel/Tests/Generator/Behavior/NestedSet/NestedSetBehaviorQueryBuilderModifierWithScopeTest.php

https://github.com/vworldat/Propel2 · PHP · 312 lines · 125 code · 19 blank · 168 comment · 0 complexity · 18f73407dc77a31aa00343df14d08b4e MD5 · raw file

  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. namespace Propel\Tests\Generator\Behavior\NestedSet;
  10. use Propel\Tests\Helpers\Bookstore\Behavior\BookstoreNestedSetTestBase;
  11. use Propel\Tests\Bookstore\Behavior\Table10;
  12. use Propel\Tests\Bookstore\Behavior\Table10Query;
  13. /**
  14. * Tests for NestedSetBehaviorQueryBuilderModifier class with scope enabled
  15. *
  16. * @author François Zaninotto
  17. * @version $Revision$
  18. * @package generator.behavior.nestedset
  19. */
  20. class NestedSetBehaviorQueryBuilderModifierWithScopeTest extends BookstoreNestedSetTestBase
  21. {
  22. public function testTreeRoots()
  23. {
  24. list($t1, $t2, $t3, $t4, $t5, $t6, $t7, $t8, $t9, $t10) = $this->initTreeWithScope();
  25. /* Tree used for tests
  26. Scope 1
  27. t1
  28. | \
  29. t2 t3
  30. | \
  31. t4 t5
  32. | \
  33. t6 t7
  34. Scope 2
  35. t8
  36. | \
  37. t9 t10
  38. */
  39. $objs = Table10Query::create()
  40. ->treeRoots()
  41. ->find();
  42. $coll = $this->buildCollection(array($t1, $t8));
  43. $this->assertEquals($coll, $objs, 'treeRoots() filters by roots');
  44. }
  45. public function testInTree()
  46. {
  47. list($t1, $t2, $t3, $t4, $t5, $t6, $t7, $t8, $t9, $t10) = $this->initTreeWithScope();
  48. /* Tree used for tests
  49. Scope 1
  50. t1
  51. | \
  52. t2 t3
  53. | \
  54. t4 t5
  55. | \
  56. t6 t7
  57. Scope 2
  58. t8
  59. | \
  60. t9 t10
  61. */
  62. $tree = Table10Query::create()
  63. ->inTree(1)
  64. ->orderByBranch()
  65. ->find();
  66. $coll = $this->buildCollection(array($t1, $t2, $t3, $t4, $t5, $t6, $t7));
  67. $this->assertEquals($coll, $tree, 'inTree() filters by node');
  68. $tree = Table10Query::create()
  69. ->inTree(2)
  70. ->orderByBranch()
  71. ->find();
  72. $coll = $this->buildCollection(array($t8, $t9, $t10));
  73. $this->assertEquals($coll, $tree, 'inTree() filters by node');
  74. }
  75. public function testDescendantsOf()
  76. {
  77. list($t1, $t2, $t3, $t4, $t5, $t6, $t7, $t8, $t9, $t10) = $this->initTreeWithScope();
  78. /* Tree used for tests
  79. Scope 1
  80. t1
  81. | \
  82. t2 t3
  83. | \
  84. t4 t5
  85. | \
  86. t6 t7
  87. Scope 2
  88. t8
  89. | \
  90. t9 t10
  91. */
  92. $objs = Table10Query::create()
  93. ->descendantsOf($t1)
  94. ->orderByBranch()
  95. ->find();
  96. $coll = $this->buildCollection(array($t2, $t3, $t4, $t5, $t6, $t7));
  97. $this->assertEquals($coll, $objs, 'decendantsOf() filters by descendants of the same scope');
  98. }
  99. public function testBranchOf()
  100. {
  101. list($t1, $t2, $t3, $t4, $t5, $t6, $t7, $t8, $t9, $t10) = $this->initTreeWithScope();
  102. /* Tree used for tests
  103. Scope 1
  104. t1
  105. | \
  106. t2 t3
  107. | \
  108. t4 t5
  109. | \
  110. t6 t7
  111. Scope 2
  112. t8
  113. | \
  114. t9 t10
  115. */
  116. $objs = Table10Query::create()
  117. ->branchOf($t1)
  118. ->orderByBranch()
  119. ->find();
  120. $coll = $this->buildCollection(array($t1, $t2, $t3, $t4, $t5, $t6, $t7));
  121. $this->assertEquals($coll, $objs, 'branchOf() filters by branch of the same scope');
  122. }
  123. public function testChildrenOf()
  124. {
  125. list($t1, $t2, $t3, $t4, $t5, $t6, $t7, $t8, $t9, $t10) = $this->initTreeWithScope();
  126. /* Tree used for tests
  127. Scope 1
  128. t1
  129. | \
  130. t2 t3
  131. | \
  132. t4 t5
  133. | \
  134. t6 t7
  135. Scope 2
  136. t8
  137. | \
  138. t9 t10
  139. */
  140. $objs = Table10Query::create()
  141. ->childrenOf($t1)
  142. ->orderByBranch()
  143. ->find();
  144. $coll = $this->buildCollection(array($t2, $t3));
  145. $this->assertEquals($coll, $objs, 'childrenOf() filters by children of the same scope');
  146. }
  147. public function testSiblingsOf()
  148. {
  149. list($t1, $t2, $t3, $t4, $t5, $t6, $t7, $t8, $t9, $t10) = $this->initTreeWithScope();
  150. /* Tree used for tests
  151. Scope 1
  152. t1
  153. | \
  154. t2 t3
  155. | \
  156. t4 t5
  157. | \
  158. t6 t7
  159. Scope 2
  160. t8
  161. | \
  162. t9 t10
  163. */
  164. $desc = Table10Query::create()
  165. ->siblingsOf($t3)
  166. ->orderByBranch()
  167. ->find();
  168. $coll = $this->buildCollection(array($t2));
  169. $this->assertEquals($coll, $desc, 'siblingsOf() returns filters by siblings of the same scope');
  170. }
  171. public function testAncestorsOf()
  172. {
  173. list($t1, $t2, $t3, $t4, $t5, $t6, $t7, $t8, $t9, $t10) = $this->initTreeWithScope();
  174. /* Tree used for tests
  175. Scope 1
  176. t1
  177. | \
  178. t2 t3
  179. | \
  180. t4 t5
  181. | \
  182. t6 t7
  183. Scope 2
  184. t8
  185. | \
  186. t9 t10
  187. */
  188. $objs = Table10Query::create()
  189. ->ancestorsOf($t5)
  190. ->orderByBranch()
  191. ->find();
  192. $coll = $this->buildCollection(array($t1, $t3), 'ancestorsOf() filters by ancestors of the same scope');
  193. }
  194. public function testRootsOf()
  195. {
  196. list($t1, $t2, $t3, $t4, $t5, $t6, $t7, $t8, $t9, $t10) = $this->initTreeWithScope();
  197. /* Tree used for tests
  198. Scope 1
  199. t1
  200. | \
  201. t2 t3
  202. | \
  203. t4 t5
  204. | \
  205. t6 t7
  206. Scope 2
  207. t8
  208. | \
  209. t9 t10
  210. */
  211. $objs = Table10Query::create()
  212. ->rootsOf($t5)
  213. ->orderByBranch()
  214. ->find();
  215. $coll = $this->buildCollection(array($t1, $t3, $t5), 'rootsOf() filters by ancestors of the same scope');
  216. }
  217. public function testFindRoot()
  218. {
  219. $this->assertTrue(method_exists('Table10Query', 'findRoot'), 'nested_set adds a findRoot() method');
  220. Table10Query::create()->deleteAll();
  221. $this->assertNull(Table10Query::create()->findRoot(1), 'findRoot() returns null as long as no root node is defined');
  222. list($t1, $t2, $t3, $t4, $t5, $t6, $t7, $t8, $t9, $t10) = $this->initTreeWithScope();
  223. /* Tree used for tests
  224. Scope 1
  225. t1
  226. | \
  227. t2 t3
  228. | \
  229. t4 t5
  230. | \
  231. t6 t7
  232. Scope 2
  233. t8
  234. | \
  235. t9 t10
  236. */
  237. $this->assertEquals($t1, Table10Query::create()->findRoot(1), 'findRoot() returns a tree root');
  238. $this->assertEquals($t8, Table10Query::create()->findRoot(2), 'findRoot() returns a tree root');
  239. }
  240. public function testFindRoots()
  241. {
  242. list($t1, $t2, $t3, $t4, $t5, $t6, $t7, $t8, $t9, $t10) = $this->initTreeWithScope();
  243. /* Tree used for tests
  244. Scope 1
  245. t1
  246. | \
  247. t2 t3
  248. | \
  249. t4 t5
  250. | \
  251. t6 t7
  252. Scope 2
  253. t8
  254. | \
  255. t9 t10
  256. */
  257. $objs = Table10Query::create()
  258. ->findRoots();
  259. $coll = $this->buildCollection(array($t1, $t8));
  260. $this->assertEquals($coll, $objs, 'findRoots() returns all root objects');
  261. }
  262. public function testFindTree()
  263. {
  264. list($t1, $t2, $t3, $t4, $t5, $t6, $t7, $t8, $t9, $t10) = $this->initTreeWithScope();
  265. /* Tree used for tests
  266. Scope 1
  267. t1
  268. | \
  269. t2 t3
  270. | \
  271. t4 t5
  272. | \
  273. t6 t7
  274. Scope 2
  275. t8
  276. | \
  277. t9 t10
  278. */
  279. $tree = Table10Query::create()->findTree(1);
  280. $coll = $this->buildCollection(array($t1, $t2, $t3, $t4, $t5, $t6, $t7));
  281. $this->assertEquals($coll, $tree, 'findTree() retrieves the tree of a scope, ordered by branch');
  282. $tree = Table10Query::create()->findTree(2);
  283. $coll = $this->buildCollection(array($t8, $t9, $t10));
  284. $this->assertEquals($coll, $tree, 'findTree() retrieves the tree of a scope, ordered by branch');
  285. }
  286. protected function buildCollection($arr)
  287. {
  288. $coll = new PropelObjectCollection();
  289. $coll->setData($arr);
  290. $coll->setModel('Table10');
  291. return $coll;
  292. }
  293. }