PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

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

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