PageRenderTime 57ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/nextbigsound/propel-orm
PHP | 350 lines | 301 code | 14 blank | 35 comment | 0 complexity | ab2fe07ec640f5d36db78d54f1c44ed9 MD5 | raw file
  1. <?php
  2. /*
  3. * $Id$
  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 'tools/helpers/bookstore/behavior/BookstoreNestedSetTestBase.php';
  11. /**
  12. * Tests for NestedSetBehaviorPeerBuilderModifier class
  13. *
  14. * @author François Zaninotto
  15. * @version $Revision$
  16. * @package generator.behavior.nestedset
  17. */
  18. class NestedSetBehaviorPeerBuilderModifierTest extends BookstoreNestedSetTestBase
  19. {
  20. public function testConstants()
  21. {
  22. $this->assertEquals(Table9Peer::LEFT_COL, 'table9.TREE_LEFT', 'nested_set adds a LEFT_COL constant');
  23. $this->assertEquals(Table9Peer::RIGHT_COL, 'table9.TREE_RIGHT', 'nested_set adds a RIGHT_COL constant');
  24. $this->assertEquals(Table9Peer::LEVEL_COL, 'table9.TREE_LEVEL', 'nested_set adds a LEVEL_COL constant');
  25. }
  26. public function testRetrieveRoot()
  27. {
  28. $this->assertTrue(method_exists('Table9Peer', 'retrieveRoot'), 'nested_set adds a retrieveRoot() method');
  29. Table9Peer::doDeleteAll();
  30. $this->assertNull(Table9Peer::retrieveRoot(), 'retrieveRoot() returns null as long as no root node is defined');
  31. $t1 = new Table9();
  32. $t1->setLeftValue(123);
  33. $t1->setRightValue(456);
  34. $t1->save();
  35. $this->assertNull(Table9Peer::retrieveRoot(), 'retrieveRoot() returns null as long as no root node is defined');
  36. $t2 = new Table9();
  37. $t2->setLeftValue(1);
  38. $t2->setRightValue(2);
  39. $t2->save();
  40. $this->assertEquals(Table9Peer::retrieveRoot(), $t2, 'retrieveRoot() retrieves the root node');
  41. }
  42. public function testRetrieveTree()
  43. {
  44. list($t1, $t2, $t3, $t4, $t5, $t6, $t7) = $this->initTree();
  45. $tree = Table9Peer::retrieveTree();
  46. $this->assertEquals(array($t1, $t2, $t3, $t4, $t5, $t6, $t7), $tree, 'retrieveTree() retrieves the whole tree');
  47. $c = new Criteria();
  48. $c->add(Table9Peer::LEFT_COL, 4, Criteria::GREATER_EQUAL);
  49. $tree = Table9Peer::retrieveTree($c);
  50. $this->assertEquals(array($t3, $t4, $t5, $t6, $t7), $tree, 'retrieveTree() accepts a Criteria as first parameter');
  51. }
  52. public function testIsValid()
  53. {
  54. $this->assertTrue(method_exists('Table9Peer', 'isValid'), 'nested_set adds an isValid() method');
  55. $this->assertFalse(Table9Peer::isValid(null), 'isValid() returns false when passed null ');
  56. $t1 = new Table9();
  57. $this->assertFalse(Table9Peer::isValid($t1), 'isValid() returns false when passed an empty node object');
  58. $t2 = new Table9();
  59. $t2->setLeftValue(5)->setRightValue(2);
  60. $this->assertFalse(Table9Peer::isValid($t2), 'isValid() returns false when passed a node object with left > right');
  61. $t3 = new Table9();
  62. $t3->setLeftValue(5)->setRightValue(5);
  63. $this->assertFalse(Table9Peer::isValid($t3), 'isValid() returns false when passed a node object with left = right');
  64. $t4 = new Table9();
  65. $t4->setLeftValue(2)->setRightValue(5);
  66. $this->assertTrue(Table9Peer::isValid($t4), 'isValid() returns true when passed a node object with left < right');
  67. }
  68. public function testDeleteTree()
  69. {
  70. $this->initTree();
  71. Table9Peer::deleteTree();
  72. $this->assertEquals(array(), Table9Peer::doSelect(new Criteria()), 'deleteTree() deletes the whole tree');
  73. }
  74. public function testShiftRLValuesDelta()
  75. {
  76. $this->initTree();
  77. Table9Peer::shiftRLValues($delta = 1, $left = 1);
  78. Table9Peer::clearInstancePool();
  79. $expected = array(
  80. 't1' => array(2, 15, 0),
  81. 't2' => array(3, 4, 1),
  82. 't3' => array(5, 14, 1),
  83. 't4' => array(6, 7, 2),
  84. 't5' => array(8, 13, 2),
  85. 't6' => array(9, 10, 3),
  86. 't7' => array(11, 12, 3),
  87. );
  88. $this->assertEquals($this->dumpTree(), $expected, 'shiftRLValues shifts all nodes with a positive amount');
  89. $this->initTree();
  90. Table9Peer::shiftRLValues($delta = -1, $left = 1);
  91. Table9Peer::clearInstancePool();
  92. $expected = array(
  93. 't1' => array(0, 13, 0),
  94. 't2' => array(1, 2, 1),
  95. 't3' => array(3, 12, 1),
  96. 't4' => array(4, 5, 2),
  97. 't5' => array(6, 11, 2),
  98. 't6' => array(7, 8, 3),
  99. 't7' => array(9, 10, 3),
  100. );
  101. $this->assertEquals($this->dumpTree(), $expected, 'shiftRLValues can shift all nodes with a negative amount');
  102. $this->initTree();
  103. Table9Peer::shiftRLValues($delta = 3, $left = 1);
  104. Table9Peer::clearInstancePool();
  105. $expected = array(
  106. 't1'=> array(4, 17, 0),
  107. 't2' => array(5, 6, 1),
  108. 't3' => array(7, 16, 1),
  109. 't4' => array(8, 9, 2),
  110. 't5' => array(10, 15, 2),
  111. 't6' => array(11, 12, 3),
  112. 't7' => array(13, 14, 3),
  113. );
  114. $this->assertEquals($this->dumpTree(), $expected, 'shiftRLValues shifts all nodes several units to the right');
  115. Table9Peer::shiftRLValues($delta = -3, $left = 1);
  116. Table9Peer::clearInstancePool();
  117. $expected = array(
  118. 't1' => array(1, 14, 0),
  119. 't2' => array(2, 3, 1),
  120. 't3' => array(4, 13, 1),
  121. 't4' => array(5, 6, 2),
  122. 't5' => array(7, 12, 2),
  123. 't6' => array(8, 9, 3),
  124. 't7' => array(10, 11, 3),
  125. );
  126. $this->assertEquals($this->dumpTree(), $expected, 'shiftRLValues shifts all nodes several units to the left');
  127. }
  128. public function testShiftRLValuesLeftLimit()
  129. {
  130. $this->initTree();
  131. Table9Peer::shiftRLValues($delta = 1, $left = 15);
  132. Table9Peer::clearInstancePool();
  133. $expected = array(
  134. 't1' => array(1, 14, 0),
  135. 't2' => array(2, 3, 1),
  136. 't3' => array(4, 13, 1),
  137. 't4' => array(5, 6, 2),
  138. 't5' => array(7, 12, 2),
  139. 't6' => array(8, 9, 3),
  140. 't7' => array(10, 11, 3),
  141. );
  142. $this->assertEquals($this->dumpTree(), $expected, 'shiftRLValues does not shift anything when the left parameter is higher than the highest right value');
  143. $this->initTree();
  144. Table9Peer::shiftRLValues($delta = 1, $left = 5);
  145. Table9Peer::clearInstancePool();
  146. $expected = array(
  147. 't1' => array(1, 15, 0),
  148. 't2' => array(2, 3, 1),
  149. 't3' => array(4, 14, 1),
  150. 't4' => array(6, 7, 2),
  151. 't5' => array(8, 13, 2),
  152. 't6' => array(9, 10, 3),
  153. 't7' => array(11, 12, 3),
  154. );
  155. $this->assertEquals($this->dumpTree(), $expected, 'shiftRLValues shifts only the nodes having a LR value higher than the given left parameter');
  156. $this->initTree();
  157. Table9Peer::shiftRLValues($delta = 1, $left = 1);
  158. Table9Peer::clearInstancePool();
  159. $expected = array(
  160. 't1'=> array(2, 15, 0),
  161. 't2' => array(3, 4, 1),
  162. 't3' => array(5, 14, 1),
  163. 't4' => array(6, 7, 2),
  164. 't5' => array(8, 13, 2),
  165. 't6' => array(9, 10, 3),
  166. 't7' => array(11, 12, 3),
  167. );
  168. $this->assertEquals($this->dumpTree(), $expected, 'shiftRLValues shifts all nodes when the left parameter is 1');
  169. }
  170. public function testShiftRLValuesRightLimit()
  171. {
  172. $this->initTree();
  173. Table9Peer::shiftRLValues($delta = 1, $left = 1, $right = 0);
  174. Table9Peer::clearInstancePool();
  175. $expected = array(
  176. 't1' => array(1, 14, 0),
  177. 't2' => array(2, 3, 1),
  178. 't3' => array(4, 13, 1),
  179. 't4' => array(5, 6, 2),
  180. 't5' => array(7, 12, 2),
  181. 't6' => array(8, 9, 3),
  182. 't7' => array(10, 11, 3),
  183. );
  184. $this->assertEquals($this->dumpTree(), $expected, 'shiftRLValues does not shift anything when the right parameter is 0');
  185. $this->initTree();
  186. Table9Peer::shiftRLValues($delta = 1, $left = 1, $right = 5);
  187. Table9Peer::clearInstancePool();
  188. $expected = array(
  189. 't1' => array(2, 14, 0),
  190. 't2' => array(3, 4, 1),
  191. 't3' => array(5, 13, 1),
  192. 't4' => array(6, 6, 2),
  193. 't5' => array(7, 12, 2),
  194. 't6' => array(8, 9, 3),
  195. 't7' => array(10, 11, 3),
  196. );
  197. $this->assertEquals($this->dumpTree(), $expected, 'shiftRLValues shiftRLValues shifts only the nodes having a LR value lower than the given right parameter');
  198. $this->initTree();
  199. Table9Peer::shiftRLValues($delta = 1, $left = 1, $right = 15);
  200. Table9Peer::clearInstancePool();
  201. $expected = array(
  202. 't1'=> array(2, 15, 0),
  203. 't2' => array(3, 4, 1),
  204. 't3' => array(5, 14, 1),
  205. 't4' => array(6, 7, 2),
  206. 't5' => array(8, 13, 2),
  207. 't6' => array(9, 10, 3),
  208. 't7' => array(11, 12, 3),
  209. );
  210. $this->assertEquals($this->dumpTree(), $expected, 'shiftRLValues shifts all nodes when the right parameter is higher than the highest right value');
  211. }
  212. public function testShiftLevel()
  213. {
  214. /* Tree used for tests
  215. t1
  216. | \
  217. t2 t3
  218. | \
  219. t4 t5
  220. | \
  221. t6 t7
  222. */
  223. $this->initTree();
  224. Table9Peer::shiftLevel($delta = 1, $first = 7, $last = 12);
  225. Table9Peer::clearInstancePool();
  226. $expected = array(
  227. 't1' => array(1, 14, 0),
  228. 't2' => array(2, 3, 1),
  229. 't3' => array(4, 13, 1),
  230. 't4' => array(5, 6, 2),
  231. 't5' => array(7, 12, 3),
  232. 't6' => array(8, 9, 4),
  233. 't7' => array(10, 11, 4),
  234. );
  235. $this->assertEquals($this->dumpTree(), $expected, 'shiftLevel shifts all nodes with a left value between the first and last');
  236. $this->initTree();
  237. Table9Peer::shiftLevel($delta = -1, $first = 7, $last = 12);
  238. Table9Peer::clearInstancePool();
  239. $expected = array(
  240. 't1' => array(1, 14, 0),
  241. 't2' => array(2, 3, 1),
  242. 't3' => array(4, 13, 1),
  243. 't4' => array(5, 6, 2),
  244. 't5' => array(7, 12, 1),
  245. 't6' => array(8, 9, 2),
  246. 't7' => array(10, 11, 2),
  247. );
  248. $this->assertEquals($this->dumpTree(), $expected, 'shiftLevel shifts all nodes wit ha negative amount');
  249. }
  250. public function testUpdateLoadedNodes()
  251. {
  252. $this->assertTrue(method_exists('Table9Peer', 'updateLoadedNodes'), 'nested_set adds a updateLoadedNodes() method');
  253. $fixtures = $this->initTree();
  254. Table9Peer::shiftRLValues(1, 5);
  255. $expected = array(
  256. 't1' => array(1, 14),
  257. 't2' => array(2, 3),
  258. 't3' => array(4, 13),
  259. 't4' => array(5, 6),
  260. 't5' => array(7, 12),
  261. 't6' => array(8, 9),
  262. 't7' => array(10, 11),
  263. );
  264. $actual = array();
  265. foreach ($fixtures as $t) {
  266. $actual[$t->getTitle()] = array($t->getLeftValue(), $t->getRightValue());
  267. }
  268. $this->assertEquals($actual, $expected, 'Loaded nodes are not in sync before calling updateLoadedNodes()');
  269. Table9Peer::updateLoadedNodes();
  270. $expected = array(
  271. 't1' => array(1, 15),
  272. 't2' => array(2, 3),
  273. 't3' => array(4, 14),
  274. 't4' => array(6, 7),
  275. 't5' => array(8, 13),
  276. 't6' => array(9, 10),
  277. 't7' => array(11, 12),
  278. );
  279. $actual = array();
  280. foreach ($fixtures as $t) {
  281. $actual[$t->getTitle()] = array($t->getLeftValue(), $t->getRightValue());
  282. }
  283. $this->assertEquals($actual, $expected, 'Loaded nodes are in sync after calling updateLoadedNodes()');
  284. }
  285. public function testMakeRoomForLeaf()
  286. {
  287. $this->assertTrue(method_exists('Table9Peer', 'makeRoomForLeaf'), 'nested_set adds a makeRoomForLeaf() method');
  288. list($t1, $t2, $t3, $t4, $t5, $t6, $t7) = $this->initTree();
  289. /* Tree used for tests
  290. t1
  291. | \
  292. t2 t3
  293. | \
  294. t4 t5
  295. | \
  296. t6 t7
  297. */
  298. $t = Table9Peer::makeRoomForLeaf(5); // first child of t3
  299. $expected = array(
  300. 't1' => array(1, 16, 0),
  301. 't2' => array(2, 3, 1),
  302. 't3' => array(4, 15, 1),
  303. 't4' => array(7, 8, 2),
  304. 't5' => array(9, 14, 2),
  305. 't6' => array(10, 11, 3),
  306. 't7' => array(12, 13, 3),
  307. );
  308. $this->assertEquals($expected, $this->dumpTree(), 'makeRoomForLeaf() shifts the other nodes correctly');
  309. foreach ($expected as $key => $values)
  310. {
  311. $this->assertEquals($values, array($$key->getLeftValue(), $$key->getRightValue(), $$key->getLevel()), 'makeRoomForLeaf() updates nodes already in memory');
  312. }
  313. }
  314. public function testFixLevels()
  315. {
  316. $fixtures = $this->initTree();
  317. // reset the levels
  318. foreach ($fixtures as $node) {
  319. $node->setLevel(null)->save();
  320. }
  321. // fix the levels
  322. Table9Peer::fixLevels();
  323. $expected = array(
  324. 't1' => array(1, 14, 0),
  325. 't2' => array(2, 3, 1),
  326. 't3' => array(4, 13, 1),
  327. 't4' => array(5, 6, 2),
  328. 't5' => array(7, 12, 2),
  329. 't6' => array(8, 9, 3),
  330. 't7' => array(10, 11, 3),
  331. );
  332. $this->assertEquals($expected, $this->dumpTree(), 'fixLevels() fixes the levels correctly');
  333. Table9Peer::fixLevels();
  334. $this->assertEquals($expected, $this->dumpTree(), 'fixLevels() can be called several times');
  335. }
  336. }