PageRenderTime 70ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/propelorm/Propel2
PHP | 1683 lines | 1061 code | 63 blank | 559 comment | 0 complexity | db832840933ad27b3557ab3561c6f21d MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * MIT License. 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. namespace Propel\Tests\Generator\Behavior\NestedSet;
  8. use Exception;
  9. use Map\NestedSetTable9TableMap;
  10. use NestedSetTable10;
  11. use NestedSetTable9;
  12. use NestedSetTable9Query;
  13. use Propel\Runtime\ActiveQuery\Criteria;
  14. use Propel\Runtime\ActiveRecord\NestedSetRecursiveIterator;
  15. use Propel\Runtime\Collection\ObjectCollection;
  16. use Propel\Runtime\Exception\PropelException;
  17. use Propel\Runtime\Propel;
  18. use Propel\Tests\Generator\Behavior\NestedSet\Fixtures\PublicTable9;
  19. /**
  20. * Tests for NestedSetBehaviorObjectBuilderModifier class
  21. */
  22. class NestedSetBehaviorObjectBuilderModifierTest extends TestCase
  23. {
  24. /**
  25. * @return void
  26. */
  27. public function testDefault()
  28. {
  29. $t = new NestedSetTable9();
  30. $t->setTreeLeft('123');
  31. $this->assertEquals($t->getLeftValue(), '123', 'nested_set adds a getLeftValue() method');
  32. $t->setTreeRight('456');
  33. $this->assertEquals($t->getRightValue(), '456', 'nested_set adds a getRightValue() method');
  34. $t->setLevel('789');
  35. $this->assertEquals($t->getLevel(), '789', 'nested_set adds a getLevel() method');
  36. }
  37. /**
  38. * @return void
  39. */
  40. public function testParameters()
  41. {
  42. $t = new NestedSetTable10();
  43. $t->setMyLeftColumn('123');
  44. $this->assertEquals($t->getLeftValue(), '123', 'nested_set adds a getLeftValue() method');
  45. $t->setMyRightColumn('456');
  46. $this->assertEquals($t->getRightValue(), '456', 'nested_set adds a getRightValue() method');
  47. $t->setMyLevelColumn('789');
  48. $this->assertEquals($t->getLevel(), '789', 'nested_set adds a getLevel() method');
  49. $t->setMyScopeColumn('012');
  50. $this->assertEquals($t->getScopeValue(), '012', 'nested_set adds a getScopeValue() method');
  51. }
  52. /**
  53. * @return void
  54. */
  55. public function testObjectAttributes()
  56. {
  57. $expectedAttributes = ['nestedSetQueries'];
  58. foreach ($expectedAttributes as $attribute) {
  59. $this->assertClassHasAttribute($attribute, 'NestedSetTable9');
  60. }
  61. }
  62. /**
  63. * @return void
  64. */
  65. public function testSaveOutOfTree()
  66. {
  67. NestedSetTable9TableMap::doDeleteAll();
  68. $t1 = new NestedSetTable9();
  69. $t1->setTitle('t1');
  70. try {
  71. $t1->save();
  72. $this->assertTrue(true, 'A node can be saved without valid tree information');
  73. } catch (Exception $e) {
  74. $this->fail('A node can be saved without valid tree information');
  75. }
  76. try {
  77. $t1->makeRoot();
  78. $this->assertTrue(true, 'A saved node can be turned into root');
  79. } catch (Exception $e) {
  80. $this->fail('A saved node can be turned into root');
  81. }
  82. $t1->save();
  83. $t2 = new NestedSetTable9();
  84. $t2->setTitle('t1');
  85. $t2->save();
  86. try {
  87. $t2->insertAsFirstChildOf($t1);
  88. $this->assertTrue(true, 'A saved node can be inserted into the tree');
  89. } catch (Exception $e) {
  90. $this->fail('A saved node can be inserted into the tree');
  91. }
  92. try {
  93. $t2->save();
  94. $this->assertTrue(true, 'A saved node can be inserted into the tree');
  95. } catch (Exception $e) {
  96. $this->fail('A saved node can be inserted into the tree');
  97. }
  98. }
  99. /**
  100. * @return void
  101. */
  102. public function testSaveRootInTreeWithExistingRoot()
  103. {
  104. $this->expectException(PropelException::class);
  105. NestedSetTable9TableMap::doDeleteAll();
  106. $t1 = new NestedSetTable9();
  107. $t1->makeRoot();
  108. $t1->save();
  109. $t2 = new NestedSetTable9();
  110. $t2->makeRoot();
  111. $t2->save();
  112. }
  113. /**
  114. * @return void
  115. */
  116. public function testPreUpdate()
  117. {
  118. [$t1, $t2, $t3, $t4, $t5, $t6, $t7] = $this->initTree();
  119. $t3->setLeftValue(null);
  120. try {
  121. $t3->save();
  122. $this->fail('Trying to save a node incorrectly updated throws an exception');
  123. } catch (Exception $e) {
  124. $this->assertTrue(true, 'Trying to save a node incorrectly updated throws an exception');
  125. }
  126. }
  127. /**
  128. * @return void
  129. */
  130. public function testDelete()
  131. {
  132. [$t1, $t2, $t3, $t4, $t5, $t6, $t7] = $this->initTree();
  133. /* Tree used for tests
  134. t1
  135. | \
  136. t2 t3
  137. | \
  138. t4 t5
  139. | \
  140. t6 t7
  141. */
  142. $t5->delete();
  143. $this->assertEquals(13, $t3->getRightValue(), 'delete() does not update existing nodes (because delete() clears the instance cache)');
  144. $expected = [
  145. 't1' => [1, 8, 0],
  146. 't2' => [2, 3, 1],
  147. 't3' => [4, 7, 1],
  148. 't4' => [5, 6, 2],
  149. ];
  150. $this->assertEquals($expected, $this->dumpTree(), 'delete() deletes all descendants and shifts the entire subtree correctly');
  151. [$t1, $t2, $t3, $t4, $t5, $t6, $t7] = $this->initTree();
  152. try {
  153. $t1->delete();
  154. $this->fail('delete() throws an exception when called on a root node');
  155. } catch (PropelException $e) {
  156. $this->assertTrue(true, 'delete() throws an exception when called on a root node');
  157. }
  158. $this->assertNotEquals([], NestedSetTable9Query::create()->find(), 'delete() called on the root node does not delete the whole tree');
  159. }
  160. /**
  161. * @return void
  162. */
  163. public function testDeleteNotInTree()
  164. {
  165. $t1 = new NestedSetTable9();
  166. $t1->save();
  167. $t1->delete();
  168. $this->assertTrue($t1->isDeleted());
  169. }
  170. /**
  171. * @return void
  172. */
  173. public function testMakeRoot()
  174. {
  175. $t = new NestedSetTable9();
  176. $t->makeRoot();
  177. $this->assertEquals($t->getLeftValue(), 1, 'makeRoot() initializes left_column to 1');
  178. $this->assertEquals($t->getRightValue(), 2, 'makeRoot() initializes right_column to 2');
  179. $this->assertEquals($t->getLevel(), 0, 'makeRoot() initializes right_column to 0');
  180. $t = new NestedSetTable9();
  181. $t->setLeftValue(12);
  182. try {
  183. $t->makeRoot();
  184. $this->fail('makeRoot() throws an exception when called on an object with a left_column value');
  185. } catch (PropelException $e) {
  186. $this->assertTrue(true, 'makeRoot() throws an exception when called on an object with a left_column value');
  187. }
  188. }
  189. /**
  190. * @return void
  191. */
  192. public function testIsInTree()
  193. {
  194. $t1 = new NestedSetTable9();
  195. $this->assertFalse($t1->isInTree(), 'inInTree() returns false for nodes with no left and right value');
  196. $t1->save();
  197. $this->assertFalse($t1->isInTree(), 'inInTree() returns false for saved nodes with no left and right value');
  198. $t1->setLeftValue(1)->setRightValue(0);
  199. $this->assertFalse($t1->isInTree(), 'inInTree() returns false for nodes with zero left value');
  200. $t1->setLeftValue(0)->setRightValue(1);
  201. $this->assertFalse($t1->isInTree(), 'inInTree() returns false for nodes with zero right value');
  202. $t1->setLeftValue(1)->setRightValue(1);
  203. $this->assertFalse($t1->isInTree(), 'inInTree() returns false for nodes with equal left and right value');
  204. $t1->setLeftValue(1)->setRightValue(2);
  205. $this->assertTrue($t1->isInTree(), 'inInTree() returns true for nodes with left < right value');
  206. $t1->setLeftValue(2)->setRightValue(1);
  207. $this->assertFalse($t1->isInTree(), 'inInTree() returns false for nodes with left > right value');
  208. }
  209. /**
  210. * @return void
  211. */
  212. public function testIsRoot()
  213. {
  214. [$t1, $t2, $t3, $t4, $t5, $t6, $t7] = $this->initTree();
  215. /* Tree used for tests
  216. t1
  217. | \
  218. t2 t3
  219. | \
  220. t4 t5
  221. | \
  222. t6 t7
  223. */
  224. $this->assertTrue($t1->isRoot(), 'root is seen as root');
  225. $this->assertFalse($t2->isRoot(), 'leaf is not seen as root');
  226. $this->assertFalse($t3->isRoot(), 'node is not seen as root');
  227. }
  228. /**
  229. * @return void
  230. */
  231. public function testIsLeaf()
  232. {
  233. [$t1, $t2, $t3, $t4, $t5, $t6, $t7] = $this->initTree();
  234. /* Tree used for tests
  235. t1
  236. | \
  237. t2 t3
  238. | \
  239. t4 t5
  240. | \
  241. t6 t7
  242. */
  243. $this->assertFalse($t1->isLeaf(), 'root is not seen as leaf');
  244. $this->assertTrue($t2->isLeaf(), 'leaf is seen as leaf');
  245. $this->assertFalse($t3->isLeaf(), 'node is not seen as leaf');
  246. }
  247. /**
  248. * @return void
  249. */
  250. public function testIsDescendantOf()
  251. {
  252. [$t1, $t2, $t3, $t4, $t5, $t6, $t7] = $this->initTree();
  253. /* Tree used for tests
  254. t1
  255. | \
  256. t2 t3
  257. | \
  258. t4 t5
  259. | \
  260. t6 t7
  261. */
  262. $this->assertFalse($t1->isDescendantOf($t1), 'root is not seen as a descendant of root');
  263. $this->assertTrue($t2->isDescendantOf($t1), 'direct child is seen as a descendant of root');
  264. $this->assertFalse($t1->isDescendantOf($t2), 'root is not seen as a descendant of leaf');
  265. $this->assertTrue($t5->isDescendantOf($t1), 'grandchild is seen as a descendant of root');
  266. $this->assertTrue($t5->isDescendantOf($t3), 'direct child is seen as a descendant of node');
  267. $this->assertFalse($t3->isDescendantOf($t5), 'node is not seen as a descendant of its parent');
  268. }
  269. /**
  270. * @return void
  271. */
  272. public function testIsAncestorOf()
  273. {
  274. [$t1, $t2, $t3, $t4, $t5, $t6, $t7] = $this->initTree();
  275. /* Tree used for tests
  276. t1
  277. | \
  278. t2 t3
  279. | \
  280. t4 t5
  281. | \
  282. t6 t7
  283. */
  284. $this->assertFalse($t1->isAncestorOf($t1), 'root is not seen as an ancestor of root');
  285. $this->assertTrue($t1->isAncestorOf($t2), 'root is seen as an ancestor of direct child');
  286. $this->assertFalse($t2->isAncestorOf($t1), 'direct child is not seen as an ancestor of root');
  287. $this->assertTrue($t1->isAncestorOf($t5), 'root is seen as an ancestor of grandchild');
  288. $this->assertTrue($t3->isAncestorOf($t5), 'parent is seen as an ancestor of node');
  289. $this->assertFalse($t5->isAncestorOf($t3), 'child is not seen as an ancestor of its parent');
  290. }
  291. /**
  292. * @return void
  293. */
  294. public function testHasParent()
  295. {
  296. NestedSetTable9TableMap::doDeleteAll();
  297. $t0 = new NestedSetTable9();
  298. $t1 = new NestedSetTable9();
  299. $t1->setTitle('t1')->setLeftValue(1)->setRightValue(6)->setLevel(0)->save();
  300. $t2 = new NestedSetTable9();
  301. $t2->setTitle('t2')->setLeftValue(2)->setRightValue(5)->setLevel(1)->save();
  302. $t3 = new NestedSetTable9();
  303. $t3->setTitle('t3')->setLeftValue(3)->setRightValue(4)->setLevel(2)->save();
  304. $this->assertFalse($t0->hasParent(), 'empty node has no parent');
  305. $this->assertFalse($t1->hasParent(), 'root node has no parent');
  306. $this->assertTrue($t2->hasParent(), 'not root node has a parent');
  307. $this->assertTrue($t3->hasParent(), 'leaf node has a parent');
  308. }
  309. /**
  310. * @return void
  311. */
  312. public function testGetParent()
  313. {
  314. NestedSetTable9TableMap::doDeleteAll();
  315. $t0 = new NestedSetTable9();
  316. $this->assertFalse($t0->hasParent(), 'empty node has no parent');
  317. $t1 = new NestedSetTable9();
  318. $t1->setTitle('t1')->setLeftValue(1)->setRightValue(8)->setLevel(0)->save();
  319. $t2 = new NestedSetTable9();
  320. $t2->setTitle('t2')->setLeftValue(2)->setRightValue(7)->setLevel(1)->save();
  321. $t3 = new NestedSetTable9();
  322. $t3->setTitle('t3')->setLeftValue(3)->setRightValue(4)->setLevel(2)->save();
  323. $t4 = new NestedSetTable9();
  324. $t4->setTitle('t4')->setLeftValue(5)->setRightValue(6)->setLevel(2)->save();
  325. $this->assertNull($t1->getParent($this->con), 'getParent() return null for root nodes');
  326. $this->assertEquals($t2->getParent($this->con), $t1, 'getParent() correctly retrieves parent for nodes');
  327. $this->assertEquals($t3->getParent($this->con), $t2, 'getParent() correctly retrieves parent for leafs');
  328. $this->assertEquals($t4->getParent($this->con), $t2, 'getParent() retrieves the same parent for two siblings');
  329. }
  330. /**
  331. * @return void
  332. */
  333. public function testGetParentCache()
  334. {
  335. [$t1, $t2, $t3, $t4, $t5, $t6, $t7] = $this->initTree();
  336. /* Tree used for tests
  337. t1
  338. | \
  339. t2 t3
  340. | \
  341. t4 t5
  342. | \
  343. t6 t7
  344. */
  345. $con = Propel::getServiceContainer()->getReadConnection(NestedSetTable9TableMap::DATABASE_NAME);
  346. $con->useDebug();
  347. $count = $con->getQueryCount();
  348. $parent = $t5->getParent($con);
  349. $parent = $t5->getParent($con);
  350. $this->assertEquals($count + 1, $con->getQueryCount(), 'getParent() only issues a query once');
  351. $this->assertEquals('t3', $parent->getTitle(), 'getParent() returns the parent Node');
  352. }
  353. /**
  354. * @return void
  355. */
  356. public function testHasPrevSibling()
  357. {
  358. NestedSetTable9TableMap::doDeleteAll();
  359. $t0 = new NestedSetTable9();
  360. $t1 = new NestedSetTable9();
  361. $t1->setTitle('t1')->setLeftValue(1)->setRightValue(6)->save();
  362. $t2 = new NestedSetTable9();
  363. $t2->setTitle('t2')->setLeftValue(2)->setRightValue(3)->save();
  364. $t3 = new NestedSetTable9();
  365. $t3->setTitle('t3')->setLeftValue(4)->setRightValue(5)->save();
  366. $this->assertFalse($t0->hasPrevSibling(), 'empty node has no previous sibling');
  367. $this->assertFalse($t1->hasPrevSibling(), 'root node has no previous sibling');
  368. $this->assertFalse($t2->hasPrevSibling(), 'first sibling has no previous sibling');
  369. $this->assertTrue($t3->hasPrevSibling(), 'not first sibling has a previous sibling');
  370. }
  371. /**
  372. * @return void
  373. */
  374. public function testGetPrevSibling()
  375. {
  376. [$t1, $t2, $t3, $t4, $t5, $t6, $t7] = $this->initTree();
  377. /* Tree used for tests
  378. t1
  379. | \
  380. t2 t3
  381. | \
  382. t4 t5
  383. | \
  384. t6 t7
  385. */
  386. $this->assertNull($t1->getPrevSibling($this->con), 'getPrevSibling() returns null for root nodes');
  387. $this->assertNull($t2->getPrevSibling($this->con), 'getPrevSibling() returns null for first siblings');
  388. $this->assertEquals($t3->getPrevSibling($this->con), $t2, 'getPrevSibling() correctly retrieves prev sibling');
  389. $this->assertNull($t6->getPrevSibling($this->con), 'getPrevSibling() returns null for first siblings');
  390. $this->assertEquals($t7->getPrevSibling($this->con), $t6, 'getPrevSibling() correctly retrieves prev sibling');
  391. }
  392. /**
  393. * @return void
  394. */
  395. public function testHasNextSibling()
  396. {
  397. NestedSetTable9TableMap::doDeleteAll();
  398. $t0 = new NestedSetTable9();
  399. $t1 = new NestedSetTable9();
  400. $t1->setTitle('t1')->setLeftValue(1)->setRightValue(6)->save();
  401. $t2 = new NestedSetTable9();
  402. $t2->setTitle('t2')->setLeftValue(2)->setRightValue(3)->save();
  403. $t3 = new NestedSetTable9();
  404. $t3->setTitle('t3')->setLeftValue(4)->setRightValue(5)->save();
  405. $this->assertFalse($t0->hasNextSibling(), 'empty node has no next sibling');
  406. $this->assertFalse($t1->hasNextSibling(), 'root node has no next sibling');
  407. $this->assertTrue($t2->hasNextSibling(), 'not last sibling has a next sibling');
  408. $this->assertFalse($t3->hasNextSibling(), 'last sibling has no next sibling');
  409. }
  410. /**
  411. * @return void
  412. */
  413. public function testGetNextSibling()
  414. {
  415. [$t1, $t2, $t3, $t4, $t5, $t6, $t7] = $this->initTree();
  416. /* Tree used for tests
  417. t1
  418. | \
  419. t2 t3
  420. | \
  421. t4 t5
  422. | \
  423. t6 t7
  424. */
  425. $this->assertNull($t1->getNextSibling($this->con), 'getNextSibling() returns null for root nodes');
  426. $this->assertEquals($t2->getNextSibling($this->con), $t3, 'getNextSibling() correctly retrieves next sibling');
  427. $this->assertNull($t3->getNextSibling($this->con), 'getNextSibling() returns null for last siblings');
  428. $this->assertEquals($t6->getNextSibling($this->con), $t7, 'getNextSibling() correctly retrieves next sibling');
  429. $this->assertNull($t7->getNextSibling($this->con), 'getNextSibling() returns null for last siblings');
  430. }
  431. /**
  432. * @return void
  433. */
  434. public function testAddNestedSetChildren()
  435. {
  436. $t0 = new NestedSetTable9();
  437. $t1 = new NestedSetTable9();
  438. $t2 = new NestedSetTable9();
  439. $t0->addNestedSetChild($t1);
  440. $t0->addNestedSetChild($t2);
  441. $this->assertEquals(2, $t0->countChildren(), 'addNestedSetChild() adds the object to the internal children collection');
  442. $this->assertEquals($t0, $t1->getParent(), 'addNestedSetChild() sets the object as th parent of the parameter');
  443. $this->assertEquals($t0, $t2->getParent(), 'addNestedSetChild() sets the object as th parent of the parameter');
  444. }
  445. /**
  446. * @return void
  447. */
  448. public function testHasChildren()
  449. {
  450. [$t1, $t2, $t3, $t4, $t5, $t6, $t7] = $this->initTree();
  451. /* Tree used for tests
  452. t1
  453. | \
  454. t2 t3
  455. | \
  456. t4 t5
  457. | \
  458. t6 t7
  459. */
  460. $this->assertTrue($t1->hasChildren(), 'root has children');
  461. $this->assertFalse($t2->hasChildren(), 'leaf has no children');
  462. $this->assertTrue($t3->hasChildren(), 'node has children');
  463. }
  464. /**
  465. * @return void
  466. */
  467. public function testGetChildren()
  468. {
  469. [$t1, $t2, $t3, $t4, $t5, $t6, $t7] = $this->initTree();
  470. /* Tree used for tests
  471. t1
  472. | \
  473. t2 t3
  474. | \
  475. t4 t5
  476. | \
  477. t6 t7
  478. */
  479. $this->assertTrue($t2->getChildren() instanceof ObjectCollection, 'getChildren() returns a collection');
  480. $this->assertEquals(0, count($t2->getChildren()), 'getChildren() returns an empty collection for leafs');
  481. $children = $t3->getChildren();
  482. $expected = [
  483. 't4' => [5, 6, 2],
  484. 't5' => [7, 12, 2],
  485. ];
  486. $this->assertEquals($expected, $this->dumpNodes($children, true), 'getChildren() returns a collection of children');
  487. $c = new Criteria();
  488. $c->add(NestedSetTable9TableMap::COL_TITLE, 't5');
  489. $children = $t3->getChildren($c);
  490. $expected = [
  491. 't5' => [7, 12, 2],
  492. ];
  493. $this->assertEquals($expected, $this->dumpNodes($children, true), 'getChildren() accepts a criteria as parameter');
  494. }
  495. /**
  496. * @return void
  497. */
  498. public function testGetChildrenCache()
  499. {
  500. [$t1, $t2, $t3, $t4, $t5, $t6, $t7] = $this->initTree();
  501. $con = Propel::getServiceContainer()->getReadConnection(NestedSetTable9TableMap::DATABASE_NAME);
  502. $count = $con->getQueryCount();
  503. $children = $t3->getChildren(null, $con);
  504. $children = $t3->getChildren(null, $con);
  505. $this->assertEquals($count + 1, $con->getQueryCount(), 'getChildren() only issues a query once');
  506. $expected = [
  507. 't4' => [5, 6, 2],
  508. 't5' => [7, 12, 2],
  509. ];
  510. $this->assertEquals($expected, $this->dumpNodes($children, true), 'getChildren() returns a collection of children');
  511. // when using criteria, cache is not used
  512. $c = new Criteria();
  513. $c->add(NestedSetTable9TableMap::COL_TITLE, 't5');
  514. $children = $t3->getChildren($c, $con);
  515. $this->assertEquals($count + 2, $con->getQueryCount(), 'getChildren() issues a new query when âssed a non-null Criteria');
  516. $expected = [
  517. 't5' => [7, 12, 2],
  518. ];
  519. $this->assertEquals($expected, $this->dumpNodes($children, true), 'getChildren() accepts a criteria as parameter');
  520. // but not erased either
  521. $children = $t3->getChildren(null, $con);
  522. $this->assertEquals($count + 2, $con->getQueryCount(), 'getChildren() keeps its internal cache after being called with a Criteria');
  523. $expected = [
  524. 't4' => [5, 6, 2],
  525. 't5' => [7, 12, 2],
  526. ];
  527. $this->assertEquals($expected, $this->dumpNodes($children, true), 'getChildren() returns a collection of children');
  528. }
  529. /**
  530. * @return void
  531. */
  532. public function testCountChildren()
  533. {
  534. [$t1, $t2, $t3, $t4, $t5, $t6, $t7] = $this->initTree();
  535. /* Tree used for tests
  536. t1
  537. | \
  538. t2 t3
  539. | \
  540. t4 t5
  541. | \
  542. t6 t7
  543. */
  544. $this->assertEquals(0, $t2->countChildren(), 'countChildren() returns 0 for leafs');
  545. $this->assertEquals(2, $t3->countChildren(), 'countChildren() returns the number of children');
  546. $c = new Criteria();
  547. $c->add(NestedSetTable9TableMap::COL_TITLE, 't5');
  548. $this->assertEquals(1, $t3->countChildren($c), 'countChildren() accepts a criteria as parameter');
  549. }
  550. /**
  551. * @return void
  552. */
  553. public function testCountChildrenCache()
  554. {
  555. [$t1, $t2, $t3, $t4, $t5, $t6, $t7] = $this->initTree();
  556. /* Tree used for tests
  557. t1
  558. | \
  559. t2 t3
  560. | \
  561. t4 t5
  562. | \
  563. t6 t7
  564. */
  565. $con = Propel::getServiceContainer()->getReadConnection(NestedSetTable9TableMap::DATABASE_NAME);
  566. $count = $con->getQueryCount();
  567. $children = $t3->getChildren(null, $con);
  568. $nbChildren = $t3->countChildren(null, $con);
  569. $this->assertEquals($count + 1, $con->getQueryCount(), 'countChildren() uses the internal collection when passed no Criteria');
  570. $nbChildren = $t3->countChildren(new Criteria(), $con);
  571. $this->assertEquals($count + 2, $con->getQueryCount(), 'countChildren() issues a new query when passed a Criteria');
  572. }
  573. /**
  574. * @return void
  575. */
  576. public function testGetFirstChild()
  577. {
  578. [$t1, $t2, $t3, $t4, $t5, $t6, $t7] = $this->initTree();
  579. $t5->moveToNextSiblingOf($t3);
  580. /* Results in
  581. t1
  582. | \ \
  583. t2 t3 t5
  584. | | \
  585. t4 t6 t7
  586. */
  587. $this->assertEquals($t2, $t1->getFirstChild(), 'getFirstChild() returns the first child');
  588. $this->assertNull($t2->getFirstChild(), 'getFirstChild() returns null for leaf');
  589. }
  590. /**
  591. * @return void
  592. */
  593. public function testGetLastChild()
  594. {
  595. [$t1, $t2, $t3, $t4, $t5, $t6, $t7] = $this->initTree();
  596. $t5->moveToNextSiblingOf($t3);
  597. /* Results in
  598. t1
  599. | \ \
  600. t2 t3 t5
  601. | | \
  602. t4 t6 t7
  603. */
  604. $this->assertEquals($t5, $t1->getLastChild(), 'getLastChild() returns the last child');
  605. $this->assertNull($t2->getLastChild(), 'getLastChild() returns null for leaf');
  606. }
  607. /**
  608. * @return void
  609. */
  610. public function testGetSiblings()
  611. {
  612. [$t1, $t2, $t3, $t4, $t5, $t6, $t7] = $this->initTree();
  613. /* Tree used for tests
  614. t1
  615. | \
  616. t2 t3
  617. | \
  618. t4 t5
  619. | \
  620. t6 t7
  621. */
  622. $this->assertEquals([], $t1->getSiblings(), 'getSiblings() returns an empty array for root');
  623. $siblings = $t5->getSiblings();
  624. $expected = [
  625. 't4' => [5, 6, 2],
  626. ];
  627. $this->assertEquals($expected, $this->dumpNodes($siblings), 'getSiblings() returns an array of siblings');
  628. $siblings = $t5->getSiblings(true);
  629. $expected = [
  630. 't4' => [5, 6, 2],
  631. 't5' => [7, 12, 2],
  632. ];
  633. $this->assertEquals($expected, $this->dumpNodes($siblings), 'getSiblings(true) includes the current node');
  634. $t5->moveToNextSiblingOf($t3);
  635. /* Results in
  636. t1
  637. | \ \
  638. t2 t3 t5
  639. | | \
  640. t4 t6 t7
  641. */
  642. $this->assertEquals(0, count($t4->getSiblings()), 'getSiblings() returns an empty colleciton for lone children');
  643. $siblings = $t3->getSiblings();
  644. $expected = [
  645. 't2' => [2, 3, 1],
  646. 't5' => [8, 13, 1],
  647. ];
  648. $this->assertEquals($expected, $this->dumpNodes($siblings), 'getSiblings() returns all siblings');
  649. $this->assertEquals('t2', $siblings[0]->getTitle(), 'getSiblings() returns siblings in natural order');
  650. $this->assertEquals('t5', $siblings[1]->getTitle(), 'getSiblings() returns siblings in natural order');
  651. }
  652. /**
  653. * @return void
  654. */
  655. public function testGetDescendants()
  656. {
  657. [$t1, $t2, $t3, $t4, $t5, $t6, $t7] = $this->initTree();
  658. /* Tree used for tests
  659. t1
  660. | \
  661. t2 t3
  662. | \
  663. t4 t5
  664. | \
  665. t6 t7
  666. */
  667. $this->assertEquals([], $t2->getDescendants(), 'getDescendants() returns an empty array for leafs');
  668. $descendants = $t3->getDescendants();
  669. $expected = [
  670. 't4' => [5, 6, 2],
  671. 't5' => [7, 12, 2],
  672. 't6' => [8, 9, 3],
  673. 't7' => [10, 11, 3],
  674. ];
  675. $this->assertEquals($expected, $this->dumpNodes($descendants), 'getDescendants() returns an array of descendants');
  676. $c = new Criteria();
  677. $c->add(NestedSetTable9TableMap::COL_TITLE, 't5');
  678. $descendants = $t3->getDescendants($c);
  679. $expected = [
  680. 't5' => [7, 12, 2],
  681. ];
  682. $this->assertEquals($expected, $this->dumpNodes($descendants), 'getDescendants() accepts a criteria as parameter');
  683. }
  684. /**
  685. * @return void
  686. */
  687. public function testCountDescendants()
  688. {
  689. [$t1, $t2, $t3, $t4, $t5, $t6, $t7] = $this->initTree();
  690. /* Tree used for tests
  691. t1
  692. | \
  693. t2 t3
  694. | \
  695. t4 t5
  696. | \
  697. t6 t7
  698. */
  699. $this->assertEquals(0, $t2->countDescendants(), 'countDescendants() returns 0 for leafs');
  700. $this->assertEquals(4, $t3->countDescendants(), 'countDescendants() returns the number of descendants');
  701. $c = new Criteria();
  702. $c->add(NestedSetTable9TableMap::COL_TITLE, 't5');
  703. $this->assertEquals(1, $t3->countDescendants($c), 'countDescendants() accepts a criteria as parameter');
  704. }
  705. /**
  706. * @return void
  707. */
  708. public function testGetBranch()
  709. {
  710. [$t1, $t2, $t3, $t4, $t5, $t6, $t7] = $this->initTree();
  711. /* Tree used for tests
  712. t1
  713. | \
  714. t2 t3
  715. | \
  716. t4 t5
  717. | \
  718. t6 t7
  719. */
  720. $this->assertEquals([$t2], $t2->getBranch()->getArrayCopy(), 'getBranch() returns the current node for leafs');
  721. $descendants = $t3->getBranch();
  722. $expected = [
  723. 't3' => [4, 13, 1],
  724. 't4' => [5, 6, 2],
  725. 't5' => [7, 12, 2],
  726. 't6' => [8, 9, 3],
  727. 't7' => [10, 11, 3],
  728. ];
  729. $this->assertEquals($expected, $this->dumpNodes($descendants), 'getBranch() returns an array of descendants, including the current node');
  730. $c = new Criteria();
  731. $c->add(NestedSetTable9TableMap::COL_TITLE, 't3', Criteria::NOT_EQUAL);
  732. $descendants = $t3->getBranch($c);
  733. unset($expected['t3']);
  734. $this->assertEquals($expected, $this->dumpNodes($descendants), 'getBranch() accepts a criteria as first parameter');
  735. }
  736. /**
  737. * @return void
  738. */
  739. public function testGetAncestors()
  740. {
  741. [$t1, $t2, $t3, $t4, $t5, $t6, $t7] = $this->initTree();
  742. /* Tree used for tests
  743. t1
  744. | \
  745. t2 t3
  746. | \
  747. t4 t5
  748. | \
  749. t6 t7
  750. */
  751. $this->assertEquals([], $t1->getAncestors(), 'getAncestors() returns an empty array for roots');
  752. $ancestors = $t5->getAncestors();
  753. $expected = [
  754. 't1' => [1, 14, 0],
  755. 't3' => [4, 13, 1],
  756. ];
  757. $this->assertEquals($expected, $this->dumpNodes($ancestors), 'getAncestors() returns an array of ancestors');
  758. $c = new Criteria();
  759. $c->add(NestedSetTable9TableMap::COL_TITLE, 't3');
  760. $ancestors = $t5->getAncestors($c);
  761. $expected = [
  762. 't3' => [4, 13, 1],
  763. ];
  764. $this->assertEquals($expected, $this->dumpNodes($ancestors), 'getAncestors() accepts a criteria as parameter');
  765. }
  766. /**
  767. * @return void
  768. */
  769. public function testAddChild()
  770. {
  771. NestedSetTable9TableMap::doDeleteAll();
  772. $t1 = new NestedSetTable9();
  773. $t1->setTitle('t1');
  774. $t1->makeRoot();
  775. $t1->save();
  776. $t2 = new NestedSetTable9();
  777. $t2->setTitle('t2');
  778. $t1->addChild($t2);
  779. $t2->save();
  780. $t3 = new NestedSetTable9();
  781. $t3->setTitle('t3');
  782. $t1->addChild($t3);
  783. $t3->save();
  784. $t4 = new NestedSetTable9();
  785. $t4->setTitle('t4');
  786. $t2->addChild($t4);
  787. $t4->save();
  788. $expected = [
  789. 't1' => [1, 8, 0],
  790. 't2' => [4, 7, 1],
  791. 't3' => [2, 3, 1],
  792. 't4' => [5, 6, 2],
  793. ];
  794. $this->assertEquals($expected, $this->dumpTree(), 'addChild() adds the child and saves it');
  795. }
  796. /**
  797. * @return void
  798. */
  799. public function testInsertAsFirstChildOf()
  800. {
  801. $this->assertTrue(method_exists('NestedSetTable9', 'insertAsFirstChildOf'), 'nested_set adds a insertAsFirstChildOf() method');
  802. [$t1, $t2, $t3, $t4, $t5, $t6, $t7] = $this->initTree();
  803. /* Tree used for tests
  804. t1
  805. | \
  806. t2 t3
  807. | \
  808. t4 t5
  809. | \
  810. t6 t7
  811. */
  812. $t8 = new PublicTable9();
  813. $t8->setTitle('t8');
  814. $t = $t8->insertAsFirstChildOf($t3);
  815. $this->assertEquals($t8, $t, 'insertAsFirstChildOf() returns the object it was called on');
  816. $this->assertEquals(5, $t4->getLeftValue(), 'insertAsFirstChildOf() does not modify the tree until the object is saved');
  817. $t8->save();
  818. $this->assertEquals(5, $t8->getLeftValue(), 'insertAsFirstChildOf() sets the left value correctly');
  819. $this->assertEquals(6, $t8->getRightValue(), 'insertAsFirstChildOf() sets the right value correctly');
  820. $this->assertEquals(2, $t8->getLevel(), 'insertAsFirstChildOf() sets the level correctly');
  821. $expected = [
  822. 't1' => [1, 16, 0],
  823. 't2' => [2, 3, 1],
  824. 't3' => [4, 15, 1],
  825. 't4' => [7, 8, 2],
  826. 't5' => [9, 14, 2],
  827. 't6' => [10, 11, 3],
  828. 't7' => [12, 13, 3],
  829. 't8' => [5, 6, 2],
  830. ];
  831. $this->assertEquals($expected, $this->dumpTree(), 'insertAsFirstChildOf() shifts the other nodes correctly');
  832. try {
  833. $t8->insertAsFirstChildOf($t4);
  834. $this->fail('insertAsFirstChildOf() throws an exception when called on a saved object');
  835. } catch (PropelException $e) {
  836. $this->assertTrue(true, 'insertAsFirstChildOf() throws an exception when called on a saved object');
  837. }
  838. }
  839. /**
  840. * @return void
  841. */
  842. public function testInsertAsFirstChildOfExistingObject()
  843. {
  844. NestedSetTable9Query::create()->deleteAll();
  845. $t = new NestedSetTable9();
  846. $t->makeRoot();
  847. $t->save();
  848. $this->assertEquals(1, $t->getLeftValue());
  849. $this->assertEquals(2, $t->getRightValue());
  850. $this->assertEquals(0, $t->getLevel());
  851. $t1 = new NestedSetTable9();
  852. $t1->save();
  853. $t1->insertAsFirstChildOf($t);
  854. $this->assertEquals(2, $t1->getLeftValue());
  855. $this->assertEquals(3, $t1->getRightValue());
  856. $this->assertEquals(1, $t1->getLevel());
  857. $t1->save();
  858. $this->assertEquals(1, $t->getLeftValue());
  859. $this->assertEquals(4, $t->getRightValue());
  860. $this->assertEquals(0, $t->getLevel());
  861. $this->assertEquals(2, $t1->getLeftValue());
  862. $this->assertEquals(3, $t1->getRightValue());
  863. $this->assertEquals(1, $t1->getLevel());
  864. }
  865. /**
  866. * @return void
  867. */
  868. public function testInsertAsLastChildOf()
  869. {
  870. $this->assertTrue(method_exists('NestedSetTable9', 'insertAsLastChildOf'), 'nested_set adds a insertAsLastChildOf() method');
  871. [$t1, $t2, $t3, $t4, $t5, $t6, $t7] = $this->initTree();
  872. /* Tree used for tests
  873. t1
  874. | \
  875. t2 t3
  876. | \
  877. t4 t5
  878. | \
  879. t6 t7
  880. */
  881. $t8 = new PublicTable9();
  882. $t8->setTitle('t8');
  883. $t = $t8->insertAsLastChildOf($t3);
  884. $this->assertEquals($t8, $t, 'insertAsLastChildOf() returns the object it was called on');
  885. $this->assertEquals(13, $t3->getRightValue(), 'insertAsLastChildOf() does not modify the tree until the object is saved');
  886. $t8->save();
  887. $this->assertEquals(13, $t8->getLeftValue(), 'insertAsLastChildOf() sets the left value correctly');
  888. $this->assertEquals(14, $t8->getRightValue(), 'insertAsLastChildOf() sets the right value correctly');
  889. $this->assertEquals(2, $t8->getLevel(), 'insertAsLastChildOf() sets the level correctly');
  890. $expected = [
  891. 't1' => [1, 16, 0],
  892. 't2' => [2, 3, 1],
  893. 't3' => [4, 15, 1],
  894. 't4' => [5, 6, 2],
  895. 't5' => [7, 12, 2],
  896. 't6' => [8, 9, 3],
  897. 't7' => [10, 11, 3],
  898. 't8' => [13, 14, 2],
  899. ];
  900. $this->assertEquals($expected, $this->dumpTree(), 'insertAsLastChildOf() shifts the other nodes correctly');
  901. try {
  902. $t8->insertAsLastChildOf($t4);
  903. $this->fail('insertAsLastChildOf() throws an exception when called on a saved object');
  904. } catch (PropelException $e) {
  905. $this->assertTrue(true, 'insertAsLastChildOf() throws an exception when called on a saved object');
  906. }
  907. }
  908. /**
  909. * @return void
  910. */
  911. public function testInsertAsLastChildOfExistingObject()
  912. {
  913. NestedSetTable9Query::create()->deleteAll();
  914. $t = new NestedSetTable9();
  915. $t->makeRoot();
  916. $t->save();
  917. $this->assertEquals(1, $t->getLeftValue());
  918. $this->assertEquals(2, $t->getRightValue());
  919. $this->assertEquals(0, $t->getLevel());
  920. $t1 = new NestedSetTable9();
  921. $t1->save();
  922. $t1->insertAsLastChildOf($t);
  923. $this->assertEquals(2, $t1->getLeftValue());
  924. $this->assertEquals(3, $t1->getRightValue());
  925. $this->assertEquals(1, $t1->getLevel());
  926. $t1->save();
  927. $this->assertEquals(1, $t->getLeftValue());
  928. $this->assertEquals(4, $t->getRightValue());
  929. $this->assertEquals(0, $t->getLevel());
  930. $this->assertEquals(2, $t1->getLeftValue());
  931. $this->assertEquals(3, $t1->getRightValue());
  932. $this->assertEquals(1, $t1->getLevel());
  933. }
  934. /**
  935. * @return void
  936. */
  937. public function testInsertAsPrevSiblingOf()
  938. {
  939. $this->assertTrue(method_exists('NestedSetTable9', 'insertAsPrevSiblingOf'), 'nested_set adds a insertAsPrevSiblingOf() method');
  940. [$t1, $t2, $t3, $t4, $t5, $t6, $t7] = $this->initTree();
  941. /* Tree used for tests
  942. t1
  943. | \
  944. t2 t3
  945. | \
  946. t4 t5
  947. | \
  948. t6 t7
  949. */
  950. $t8 = new PublicTable9();
  951. $t8->setTitle('t8');
  952. $t = $t8->insertAsPrevSiblingOf($t3);
  953. $this->assertEquals($t8, $t, 'insertAsPrevSiblingOf() returns the object it was called on');
  954. $this->assertEquals(4, $t3->getLeftValue(), 'insertAsPrevSiblingOf() does not modify the tree until the object is saved');
  955. $t8->save();
  956. $this->assertEquals(4, $t8->getLeftValue(), 'insertAsPrevSiblingOf() sets the left value correctly');
  957. $this->assertEquals(5, $t8->getRightValue(), 'insertAsPrevSiblingOf() sets the right value correctly');
  958. $this->assertEquals(1, $t8->getLevel(), 'insertAsPrevSiblingOf() sets the level correctly');
  959. $expected = [
  960. 't1' => [1, 16, 0],
  961. 't2' => [2, 3, 1],
  962. 't3' => [6, 15, 1],
  963. 't4' => [7, 8, 2],
  964. 't5' => [9, 14, 2],
  965. 't6' => [10, 11, 3],
  966. 't7' => [12, 13, 3],
  967. 't8' => [4, 5, 1],
  968. ];
  969. $this->assertEquals($expected, $this->dumpTree(), 'insertAsPrevSiblingOf() shifts the other nodes correctly');
  970. try {
  971. $t8->insertAsPrevSiblingOf($t4);
  972. $this->fail('insertAsPrevSiblingOf() throws an exception when called on a saved object');
  973. } catch (PropelException $e) {
  974. $this->assertTrue(true, 'insertAsPrevSiblingOf() throws an exception when called on a saved object');
  975. }
  976. }
  977. /**
  978. * @return void
  979. */
  980. public function testInsertAsPrevSiblingOfExistingObject()
  981. {
  982. NestedSetTable9Query::create()->deleteAll();
  983. $t = new NestedSetTable9();
  984. $t->makeRoot();
  985. $t->save();
  986. $t1 = new NestedSetTable9();
  987. $t1->insertAsFirstChildOf($t);
  988. $t1->save();
  989. $this->assertEquals(1, $t->getLeftValue());
  990. $this->assertEquals(4, $t->getRightValue());
  991. $this->assertEquals(0, $t->getLevel());
  992. $this->assertEquals(2, $t1->getLeftValue());
  993. $this->assertEquals(3, $t1->getRightValue());
  994. $this->assertEquals(1, $t1->getLevel());
  995. $t2 = new NestedSetTable9();
  996. $t2->save();
  997. $t2->insertAsPrevSiblingOf($t1);
  998. $this->assertEquals(2, $t2->getLeftValue());
  999. $this->assertEquals(3, $t2->getRightValue());
  1000. $this->assertEquals(1, $t2->getLevel());
  1001. $t2->save();
  1002. $this->assertEquals(1, $t->getLeftValue());
  1003. $this->assertEquals(6, $t->getRightValue());
  1004. $this->assertEquals(0, $t->getLevel());
  1005. $this->assertEquals(4, $t1->getLeftValue());
  1006. $this->assertEquals(5, $t1->getRightValue());
  1007. $this->assertEquals(1, $t1->getLevel());
  1008. $this->assertEquals(2, $t2->getLeftValue());
  1009. $this->assertEquals(3, $t2->getRightValue());
  1010. $this->assertEquals(1, $t2->getLevel());
  1011. }
  1012. /**
  1013. * @return void
  1014. */
  1015. public function testInsertAsNextSiblingOf()
  1016. {
  1017. $this->assertTrue(method_exists('NestedSetTable9', 'insertAsNextSiblingOf'), 'nested_set adds a insertAsNextSiblingOf() method');
  1018. [$t1, $t2, $t3, $t4, $t5, $t6, $t7] = $this->initTree();
  1019. /* Tree used for tests
  1020. t1
  1021. | \
  1022. t2 t3
  1023. | \
  1024. t4 t5
  1025. | \
  1026. t6 t7
  1027. */
  1028. $t8 = new PublicTable9();
  1029. $t8->setTitle('t8');
  1030. $t = $t8->insertAsNextSiblingOf($t3);
  1031. $this->assertEquals($t8, $t, 'insertAsNextSiblingOf() returns the object it was called on');
  1032. $this->assertEquals(14, $t1->getRightValue(), 'insertAsNextSiblingOf() does not modify the tree until the object is saved');
  1033. $t8->save();
  1034. $this->assertEquals(14, $t8->getLeftValue(), 'insertAsNextSiblingOf() sets the left value correctly');
  1035. $this->assertEquals(15, $t8->getRightValue(), 'insertAsNextSiblingOf() sets the right value correctly');
  1036. $this->assertEquals(1, $t8->getLevel(), 'insertAsNextSiblingOf() sets the level correctly');
  1037. $expected = [
  1038. 't1' => [1, 16, 0],
  1039. 't2' => [2, 3, 1],
  1040. 't3' => [4, 13, 1],
  1041. 't4' => [5, 6, 2],
  1042. 't5' => [7, 12, 2],
  1043. 't6' => [8, 9, 3],
  1044. 't7' => [10, 11, 3],
  1045. 't8' => [14, 15, 1],
  1046. ];
  1047. $this->assertEquals($expected, $this->dumpTree(), 'insertAsNextSiblingOf() shifts the other nodes correctly');
  1048. try {
  1049. $t8->insertAsNextSiblingOf($t4);
  1050. $this->fail('insertAsNextSiblingOf() throws an exception when called on a saved object');
  1051. } catch (PropelException $e) {
  1052. $this->assertTrue(true, 'insertAsNextSiblingOf() throws an exception when called on a saved object');
  1053. }
  1054. }
  1055. /**
  1056. * @return void
  1057. */
  1058. public function testInsertAsNextSiblingOfExistingObject()
  1059. {
  1060. NestedSetTable9Query::create()->deleteAll();
  1061. $t = new NestedSetTable9();
  1062. $t->makeRoot();
  1063. $t->save();
  1064. $t1 = new NestedSetTable9();
  1065. $t1->insertAsFirstChildOf($t);
  1066. $t1->save();
  1067. $this->assertEquals(1, $t->getLeftValue());
  1068. $this->assertEquals(4, $t->getRightValue());
  1069. $this->assertEquals(0, $t->getLevel());
  1070. $this->assertEquals(2, $t1->getLeftValue());
  1071. $this->assertEquals(3, $t1->getRightValue());
  1072. $this->assertEquals(1, $t1->getLevel());
  1073. $t2 = new NestedSetTable9();
  1074. $t2->save();
  1075. $t2->insertAsNextSiblingOf($t1);
  1076. $this->assertEquals(4, $t2->getLeftValue());
  1077. $this->assertEquals(5, $t2->getRightValue());
  1078. $this->assertEquals(1, $t2->getLevel());
  1079. $t2->save();
  1080. $this->assertEquals(1, $t->getLeftValue());
  1081. $this->assertEquals(6, $t->getRightValue());
  1082. $this->assertEquals(0, $t->getLevel());
  1083. $this->assertEquals(2, $t1->getLeftValue());
  1084. $this->assertEquals(3, $t1->getRightValue());
  1085. $this->assertEquals(1, $t1->getLevel());
  1086. $this->assertEquals(4, $t2->getLeftValue());
  1087. $this->assertEquals(5, $t2->getRightValue());
  1088. $this->assertEquals(1, $t2->getLevel());
  1089. }
  1090. /**
  1091. * @return void
  1092. */
  1093. public function testMoveToFirstChildOf()
  1094. {
  1095. $this->assertTrue(method_exists('NestedSetTable9', 'moveToFirstChildOf'), 'nested_set adds a moveToFirstChildOf() method');
  1096. [$t1, $t2, $t3, $t4, $t5, $t6, $t7] = $this->initTree();
  1097. /* Tree used for tests
  1098. t1
  1099. | \
  1100. t2 t3
  1101. | \
  1102. t4 t5
  1103. | \
  1104. t6 t7
  1105. */
  1106. try {
  1107. $t3->moveToFirstChildOf($t5);
  1108. $this->fail('moveToFirstChildOf() throws an exception when the target is a child node');
  1109. } catch (PropelException $e) {
  1110. $this->assertTrue(true, 'moveToFirstChildOf() throws an exception when the target is a child node');
  1111. }
  1112. // moving down
  1113. $t = $t3->moveToFirstChildOf($t2);
  1114. $this->assertEquals($t3, $t, 'moveToFirstChildOf() returns the object it was called on');
  1115. $expected = [
  1116. 't1' => [1, 14, 0],
  1117. 't2' => [2, 13, 1],
  1118. 't3' => [3, 12, 2],
  1119. 't4' => [4, 5, 3],
  1120. 't5' => [6, 11, 3],
  1121. 't6' => [7, 8, 4],
  1122. 't7' => [9, 10, 4],
  1123. ];
  1124. $this->assertEquals($expected, $this->dumpTree(), 'moveToFirstChildOf() moves the entire subtree down correctly');
  1125. // moving up
  1126. [$t1, $t2, $t3, $t4, $t5, $t6, $t7] = $this->initTree();
  1127. $t5->moveToFirstChildOf($t1);
  1128. $expected = [
  1129. 't1' => [1, 14, 0],
  1130. 't2' => [8, 9, 1],
  1131. 't3' => [10, 13, 1],
  1132. 't4' => [11, 12, 2],
  1133. 't5' => [2, 7, 1],
  1134. 't6' => [3, 4, 2],
  1135. 't7' => [5, 6, 2],
  1136. ];
  1137. $this->assertEquals($expected, $this->dumpTree(), 'moveToFirstChildOf() moves the entire subtree up correctly');
  1138. // moving to the same level
  1139. [$t1, $t2, $t3, $t4, $t5, $t6, $t7] = $this->initTree();
  1140. $t5->moveToFirstChildOf($t3);
  1141. $expected = [
  1142. 't1' => [1, 14, 0],
  1143. 't2' => [2, 3, 1],
  1144. 't3' => [4, 13, 1],
  1145. 't4' => [11, 12, 2],
  1146. 't5' => [5, 10, 2],
  1147. 't6' => [6, 7, 3],
  1148. 't7' => [8, 9, 3],
  1149. ];
  1150. $this->assertEquals($expected, $this->dumpTree(), 'moveToFirstChildOf() moves the entire subtree to the same level correctly');
  1151. }
  1152. /**
  1153. * @return void
  1154. */
  1155. public function testMoveToFirstChildOfAndChildrenCache()
  1156. {
  1157. [$t1, $t2, $t3, $t4, $t5, $t6, $t7] = $this->initTree();
  1158. /* Tree used for tests
  1159. t1
  1160. | \
  1161. t2 t3
  1162. | \
  1163. t4 t5
  1164. | \
  1165. t6 t7
  1166. */
  1167. // fill children cache
  1168. $t3->getChildren();
  1169. $t1->getChildren();
  1170. // move
  1171. $t5->moveToFirstChildOf($t1);
  1172. $children = $t3->getChildren();
  1173. $expected = [
  1174. 't4' => [11, 12, 2],
  1175. ];
  1176. $this->assertEquals($expected, $this->dumpNodes($children, true), 'moveToFirstChildOf() reinitializes the child collection of all concerned nodes');
  1177. $children = $t1->getChildren();
  1178. $expected = [
  1179. 't5' => [2, 7, 1],
  1180. 't2' => [8, 9, 1],
  1181. 't3' => [10, 13, 1],
  1182. ];
  1183. $this->assertEquals($expected, $this->dumpNodes($children, true), 'moveToFirstChildOf() reinitializes the child collection of all concerned nodes');
  1184. }
  1185. /**
  1186. * @return void
  1187. */
  1188. public function testMoveToLastChildOf()
  1189. {
  1190. $this->assertTrue(method_exists('NestedSetTable9', 'moveToLastChildOf'), 'nested_set adds a moveToLastChildOf() method');
  1191. [$t1, $t2, $t3, $t4, $t5, $t6, $t7] = $this->initTree();
  1192. /* Tree used for tests
  1193. t1
  1194. | \
  1195. t2 t3
  1196. | \
  1197. t4 t5
  1198. | \
  1199. t6 t7
  1200. */
  1201. try {
  1202. $t3->moveToLastChildOf($t5);
  1203. $this->fail('moveToLastChildOf() throws an exception when the target is a child node');
  1204. } catch (PropelException $e) {
  1205. $this->assertTrue(true, 'moveToLastChildOf() throws an exception when the target is a child node');
  1206. }
  1207. // moving up
  1208. $t = $t5->moveToLastChildOf($t1);
  1209. $this->assertEquals($t5, $t, 'moveToLastChildOf() returns the object it was called on');
  1210. $expected = [
  1211. 't1' => [1, 14, 0],
  1212. 't2' => [2, 3, 1],
  1213. 't3' => [4, 7, 1],
  1214. 't4' => [5, 6, 2],
  1215. 't5' => [8, 13, 1],
  1216. 't6' => [9, 10, 2],
  1217. 't7' => [11, 12, 2],
  1218. ];
  1219. $this->assertEquals($expected, $this->dumpTree(), 'moveToLastChildOf() moves the entire subtree up correctly');
  1220. // moving down
  1221. [$t1, $t2, $t3, $t4, $t5, $t6, $t7] = $this->initTree();
  1222. $t3->moveToLastChildOf($t2);
  1223. $expected = [
  1224. 't1' => [1, 14, 0],
  1225. 't2' => [2, 13, 1],
  1226. 't3' => [3, 12, 2],
  1227. 't4' => [4, 5, 3],
  1228. 't5' => [6, 11, 3],
  1229. 't6' => [7, 8, 4],
  1230. 't7' => [9, 10, 4],
  1231. ];
  1232. $this->assertEquals($expected, $this->dumpTree(), 'moveToLastChildOf() moves the entire subtree down correctly');
  1233. // moving to the same level
  1234. [$t1, $t2, $t3, $t4, $t5, $t6, $t7] = $this->initTree();
  1235. $t4->moveToLastChildOf($t3);
  1236. $expected = [
  1237. 't1' => [1, 14, 0],
  1238. 't2' => [2, 3, 1],
  1239. 't3' => [4, 13, 1],
  1240. 't4' => [11, 12, 2],
  1241. 't5' => [5, 10, 2],
  1242. 't6' => [6, 7, 3],
  1243. 't7' => [8, 9, 3],
  1244. ];
  1245. $this->assertEquals($expected, $this->dumpTree(), 'moveToLastChildOf() moves the entire subtree to the same level correctly');
  1246. }
  1247. /**
  1248. * @return void
  1249. */
  1250. public function testMoveToLastChildOfAndChildrenCache()
  1251. {
  1252. [$t1, $t2, $t3, $t4, $t5, $t6, $t7] = $this->initTree();
  1253. /* Tree used for tests
  1254. t1
  1255. | \
  1256. t2 t3
  1257. | \
  1258. t4 t5
  1259. | \
  1260. t6 t7
  1261. */
  1262. // fill children cache
  1263. $t3->getChildren();
  1264. $t1->getChildren();
  1265. // move
  1266. $t5->moveToLastChildOf($t1);
  1267. $children = $t3->getChildren();
  1268. $expected = [
  1269. 't4' => [5, 6, 2],
  1270. ];
  1271. $this->assertEquals($expected, $this->dumpNodes($children, true), 'moveToLastChildOf() reinitializes the child collection of all concerned nodes');
  1272. $children = $t1->getChildren();
  1273. $expected = [
  1274. 't2' => [2, 3, 1],
  1275. 't3' => [4, 7, 1],
  1276. 't5' => [8, 13, 1],
  1277. ];
  1278. $this->assertEquals($expected, $this->dumpNodes($children, true), 'moveToLastChildOf() reinitializes the child collection of all concerned nodes');
  1279. }
  1280. /**
  1281. * @return void
  1282. */
  1283. public function testMoveToPrevSiblingOf()
  1284. {
  1285. $this->assertTrue(method_exists('NestedSetTable9', 'moveToPrevSiblingOf'), 'nested_set adds a moveToPrevSiblingOf() method');
  1286. [$t1, $t2, $t3, $t4, $t5, $t6, $t7] = $this->initTree();
  1287. /* Tree used for tests
  1288. t1
  1289. | \
  1290. t2 t3
  1291. | \
  1292. t4 t5
  1293. | \
  1294. t6 t7
  1295. */
  1296. try {
  1297. $t5->moveToPrevSiblingOf($t1);
  1298. $this->fail('moveToPrevSiblingOf() throws an exception when the target is a root node');
  1299. } catch (PropelException $e) {
  1300. $this->assertTrue(true, 'moveToPrevSiblingOf() throws an exception when the target is a root node');
  1301. }
  1302. try {
  1303. $t5->moveToPrevSiblingOf($t6);
  1304. $this->fail('moveToPrevSiblingOf() throws an exception when the target is a child node');
  1305. } catch (PropelException $e) {
  1306. $this->assertTrue(true, 'moveToPrevSiblingOf() throws an exception when the target is a child node');
  1307. }
  1308. // moving up
  1309. $t = $t5->moveToPrevSiblingOf($t3)

Large files files are truncated, but you can click here to view the full file