PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

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

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