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

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