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

https://github.com/apinstein/Propel2 · PHP · 262 lines · 192 code · 13 blank · 57 comment · 0 complexity · d9843c29db79f04700c0db7716b1387f 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. namespace Propel\Tests\Generator\Behavior\NestedSet;
  11. use Propel\Tests\Helpers\Bookstore\Behavior\BookstoreNestedSetTestBase;
  12. use Propel\Tests\Bookstore\Behavior\Table9;
  13. use Propel\Tests\Bookstore\Behavior\Table9Peer;
  14. use Propel\Tests\Bookstore\Behavior\Table10;
  15. use Propel\Tests\Bookstore\Behavior\Table10Peer;
  16. use Propel\Runtime\Query\Criteria;
  17. /**
  18. * Tests for NestedSetBehaviorPeerBuilderModifier class
  19. *
  20. * @author François Zaninotto
  21. * @version $Revision$
  22. * @package generator.behavior.nestedset
  23. */
  24. class NestedSetBehaviorPeerBuilderModifierWithScopeTest extends BookstoreNestedSetTestBase
  25. {
  26. public function testConstants()
  27. {
  28. $this->assertEquals(Table10Peer::LEFT_COL, 'table10.MY_LEFT_COLUMN', 'nested_set adds a LEFT_COL constant using the custom left_column parameter');
  29. $this->assertEquals(Table10Peer::RIGHT_COL, 'table10.MY_RIGHT_COLUMN', 'nested_set adds a RIGHT_COL constant using the custom right_column parameter');
  30. $this->assertEquals(Table10Peer::LEVEL_COL, 'table10.MY_LEVEL_COLUMN', 'nested_set adds a LEVEL_COL constant using the custom level_column parameter');
  31. $this->assertEquals(Table10Peer::SCOPE_COL, 'table10.MY_SCOPE_COLUMN', 'nested_set adds a SCOPE_COL constant when the use_scope parameter is true');
  32. }
  33. public function testRetrieveRoots()
  34. {
  35. $this->assertTrue(method_exists('\Propel\Tests\Bookstore\Behavior\Table10Peer', 'retrieveRoots'), 'nested_set adds a retrieveRoots() method for trees that use scope');
  36. $this->assertFalse(method_exists('\Propel\Tests\Bookstore\Behavior\Table9Peer', 'retrieveRoots'), 'nested_set does not add a retrieveRoots() method for trees that don\'t use scope');
  37. list($t1, $t2, $t3, $t4, $t5, $t6, $t7, $t8, $t9, $t10) = $this->initTreeWithScope();
  38. /* Tree used for tests
  39. Scope 1
  40. t1
  41. | \
  42. t2 t3
  43. | \
  44. t4 t5
  45. | \
  46. t6 t7
  47. Scope 2
  48. t8
  49. | \
  50. t9 t10
  51. */
  52. $this->assertEquals(array($t1, $t8), Table10Peer::retrieveRoots(), 'retrieveRoots() returns the tree roots');
  53. $c = new Criteria();
  54. $c->add(Table10Peer::TITLE, 't1');
  55. $this->assertEquals(array($t1), Table10Peer::retrieveRoots($c), 'retrieveRoots() accepts a Criteria as first parameter');
  56. }
  57. public function testRetrieveRoot()
  58. {
  59. $this->assertTrue(method_exists('\Propel\Tests\Bookstore\Behavior\Table10Peer', 'retrieveRoot'), 'nested_set adds a retrieveRoot() method');
  60. Table10Peer::doDeleteAll();
  61. $t1 = new Table10();
  62. $t1->setLeftValue(1);
  63. $t1->setRightValue(2);
  64. $t1->setScopeValue(2);
  65. $t1->save();
  66. $this->assertNull(Table10Peer::retrieveRoot(1), 'retrieveRoot() returns null as long as no root node is defined in the required scope');
  67. $t2 = new Table10();
  68. $t2->setLeftValue(1);
  69. $t2->setRightValue(2);
  70. $t2->setScopeValue(1);
  71. $t2->save();
  72. $this->assertEquals(Table10Peer::retrieveRoot(1), $t2, 'retrieveRoot() retrieves the root node in the required scope');
  73. }
  74. public function testRetrieveTree()
  75. {
  76. list($t1, $t2, $t3, $t4, $t5, $t6, $t7, $t8, $t9, $t10) = $this->initTreeWithScope();
  77. /* Tree used for tests
  78. Scope 1
  79. t1
  80. | \
  81. t2 t3
  82. | \
  83. t4 t5
  84. | \
  85. t6 t7
  86. Scope 2
  87. t8
  88. | \
  89. t9 t10
  90. */
  91. $tree = Table10Peer::retrieveTree(1);
  92. $this->assertEquals(array($t1, $t2, $t3, $t4, $t5, $t6, $t7), $tree, 'retrieveTree() retrieves the scoped tree');
  93. $tree = Table10Peer::retrieveTree(2);
  94. $this->assertEquals(array($t8, $t9, $t10), $tree, 'retrieveTree() retrieves the scoped tree');
  95. $c = new Criteria();
  96. $c->add(Table10Peer::LEFT_COL, 4, Criteria::GREATER_EQUAL);
  97. $tree = Table10Peer::retrieveTree(1, $c);
  98. $this->assertEquals(array($t3, $t4, $t5, $t6, $t7), $tree, 'retrieveTree() accepts a Criteria as first parameter');
  99. }
  100. public function testDeleteTree()
  101. {
  102. $this->initTreeWithScope();
  103. Table10Peer::deleteTree(1);
  104. $expected = array(
  105. 't8' => array(1, 6, 0),
  106. 't9' => array(2, 3, 1),
  107. 't10' => array(4, 5, 1),
  108. );
  109. $this->assertEquals($this->dumpTreeWithScope(2), $expected, 'deleteTree() does not delete anything out of the scope');
  110. }
  111. public function testShiftRLValues()
  112. {
  113. $this->assertTrue(method_exists('\Propel\Tests\Bookstore\Behavior\Table10Peer', 'shiftRLValues'), 'nested_set adds a shiftRLValues() method');
  114. $this->initTreeWithScope();
  115. Table10Peer::shiftRLValues(1, 100, null, 1);
  116. Table10Peer::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->dumpTreeWithScope(1), $expected, 'shiftRLValues does not shift anything when the first parameter is higher than the highest right value');
  127. $expected = array(
  128. 't8' => array(1, 6, 0),
  129. 't9' => array(2, 3, 1),
  130. 't10' => array(4, 5, 1),
  131. );
  132. $this->assertEquals($this->dumpTreeWithScope(2), $expected, 'shiftRLValues does not shift anything out of the scope');
  133. $this->initTreeWithScope();
  134. Table10Peer::shiftRLValues(1, 1, null, 1);
  135. Table10Peer::clearInstancePool();
  136. $expected = array(
  137. 't1' => array(2, 15, 0),
  138. 't2' => array(3, 4, 1),
  139. 't3' => array(5, 14, 1),
  140. 't4' => array(6, 7, 2),
  141. 't5' => array(8, 13, 2),
  142. 't6' => array(9, 10, 3),
  143. 't7' => array(11, 12, 3),
  144. );
  145. $this->assertEquals($this->dumpTreeWithScope(1), $expected, 'shiftRLValues can shift all nodes to the right');
  146. $expected = array(
  147. 't8' => array(1, 6, 0),
  148. 't9' => array(2, 3, 1),
  149. 't10' => array(4, 5, 1),
  150. );
  151. $this->assertEquals($this->dumpTreeWithScope(2), $expected, 'shiftRLValues does not shift anything out of the scope');
  152. $this->initTreeWithScope();
  153. Table10Peer::shiftRLValues(-1, 1, null, 1);
  154. Table10Peer::clearInstancePool();
  155. $expected = array(
  156. 't1' => array(0, 13, 0),
  157. 't2' => array(1, 2, 1),
  158. 't3' => array(3, 12, 1),
  159. 't4' => array(4, 5, 2),
  160. 't5' => array(6, 11, 2),
  161. 't6' => array(7, 8, 3),
  162. 't7' => array(9, 10, 3),
  163. );
  164. $this->assertEquals($this->dumpTreeWithScope(1), $expected, 'shiftRLValues can shift all nodes to the left');
  165. $expected = array(
  166. 't8' => array(1, 6, 0),
  167. 't9' => array(2, 3, 1),
  168. 't10' => array(4, 5, 1),
  169. );
  170. $this->assertEquals($this->dumpTreeWithScope(2), $expected, 'shiftRLValues does not shift anything out of the scope');
  171. $this->initTreeWithScope();
  172. Table10Peer::shiftRLValues(1, 5, null, 1);
  173. Table10Peer::clearInstancePool();
  174. $expected = array(
  175. 't1' => array(1, 15, 0),
  176. 't2' => array(2, 3, 1),
  177. 't3' => array(4, 14, 1),
  178. 't4' => array(6, 7, 2),
  179. 't5' => array(8, 13, 2),
  180. 't6' => array(9, 10, 3),
  181. 't7' => array(11, 12, 3),
  182. );
  183. $this->assertEquals($this->dumpTreeWithScope(1), $expected, 'shiftRLValues can shift some nodes to the right');
  184. $expected = array(
  185. 't8' => array(1, 6, 0),
  186. 't9' => array(2, 3, 1),
  187. 't10' => array(4, 5, 1),
  188. );
  189. $this->assertEquals($this->dumpTreeWithScope(2), $expected, 'shiftRLValues does not shift anything out of the scope');
  190. }
  191. public function testShiftLevel()
  192. {
  193. $this->initTreeWithScope();
  194. Table10Peer::shiftLevel($delta = 1, $first = 7, $last = 12, $scope = 1);
  195. Table10Peer::clearInstancePool();
  196. $expected = array(
  197. 't1' => array(1, 14, 0),
  198. 't2' => array(2, 3, 1),
  199. 't3' => array(4, 13, 1),
  200. 't4' => array(5, 6, 2),
  201. 't5' => array(7, 12, 3),
  202. 't6' => array(8, 9, 4),
  203. 't7' => array(10, 11, 4),
  204. );
  205. $this->assertEquals($this->dumpTreeWithScope(1), $expected, 'shiftLevel can shift level whith a scope');
  206. $expected = array(
  207. 't8' => array(1, 6, 0),
  208. 't9' => array(2, 3, 1),
  209. 't10' => array(4, 5, 1),
  210. );
  211. $this->assertEquals($this->dumpTreeWithScope(2), $expected, 'shiftLevel does not shift anything out of the scope');
  212. }
  213. public function testMakeRoomForLeaf()
  214. {
  215. $this->assertTrue(method_exists('\Propel\Tests\Bookstore\Behavior\Table10Peer', 'makeRoomForLeaf'), 'nested_set adds a makeRoomForLeaf() method');
  216. $fixtures = $this->initTreeWithScope();
  217. /* Tree used for tests
  218. Scope 1
  219. t1
  220. | \
  221. t2 t3
  222. | \
  223. t4 t5
  224. | \
  225. t6 t7
  226. Scope 2
  227. t8
  228. | \
  229. t9 t10
  230. */
  231. $t = Table10Peer::makeRoomForLeaf(5, 1); // first child of t3
  232. $expected = array(
  233. 't1' => array(1, 16, 0),
  234. 't2' => array(2, 3, 1),
  235. 't3' => array(4, 15, 1),
  236. 't4' => array(7, 8, 2),
  237. 't5' => array(9, 14, 2),
  238. 't6' => array(10, 11, 3),
  239. 't7' => array(12, 13, 3),
  240. );
  241. $this->assertEquals($expected, $this->dumpTreeWithScope(1), 'makeRoomForLeaf() shifts the other nodes correctly');
  242. $expected = array(
  243. 't8' => array(1, 6, 0),
  244. 't9' => array(2, 3, 1),
  245. 't10' => array(4, 5, 1),
  246. );
  247. $this->assertEquals($expected, $this->dumpTreeWithScope(2), 'makeRoomForLeaf() does not shift anything out of the scope');
  248. }
  249. }