PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/propelorm/Propel2
PHP | 895 lines | 517 code | 81 blank | 297 comment | 0 complexity | d8bfb74ba42a6f12308457314468f148 MD5 | raw 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 Map\NestedSetTable10TableMap;
  9. use NestedSetTable10;
  10. use NestedSetTable10Query;
  11. use Propel\Runtime\Exception\PropelException;
  12. use Propel\Tests\Generator\Behavior\NestedSet\Fixtures\PublicTable10;
  13. /**
  14. * Tests for NestedSetBehaviorObjectBuilderModifier class
  15. *
  16. * @author François Zaninotto
  17. */
  18. class NestedSetBehaviorObjectBuilderModifierWithScopeTest extends TestCase
  19. {
  20. protected function getByTitle($title)
  21. {
  22. return NestedSetTable10Query::create()->filterByTitle($title)->findOne();
  23. }
  24. /**
  25. * @return void
  26. */
  27. public function testSaveRootInTreeWithExistingRootWithSameScope()
  28. {
  29. $this->expectException(PropelException::class);
  30. NestedSetTable10TableMap::doDeleteAll();
  31. $t1 = new NestedSetTable10();
  32. $t1->setScopeValue(1);
  33. $t1->makeRoot();
  34. $t1->save();
  35. $t2 = new NestedSetTable10();
  36. $t2->setScopeValue(1);
  37. $t2->makeRoot();
  38. $t2->save();
  39. }
  40. /**
  41. * @return void
  42. */
  43. public function testSaveRootInTreeWithExistingRootWithDifferentScope()
  44. {
  45. NestedSetTable10TableMap::doDeleteAll();
  46. $t1 = new NestedSetTable10();
  47. $t1->setScopeValue(1);
  48. $t1->makeRoot();
  49. $t1->save();
  50. $t2 = new NestedSetTable10();
  51. $t2->setScopeValue(2);
  52. $t2->makeRoot();
  53. $t2->save();
  54. $this->assertTrue(!$t2->isNew());
  55. }
  56. /**
  57. * @return void
  58. */
  59. public function testDelete()
  60. {
  61. [$t1, $t2, $t3, $t4, $t5, $t6, $t7, $t8, $t9, $t10] = $this->initTreeWithScope();
  62. /* Tree used for tests
  63. Scope 1
  64. t1
  65. | \
  66. t2 t3
  67. | \
  68. t4 t5
  69. | \
  70. t6 t7
  71. Scope 2
  72. t8
  73. | \
  74. t9 t10
  75. */
  76. $t5->delete();
  77. $expected = [
  78. 't1' => [1, 8, 0],
  79. 't2' => [2, 3, 1],
  80. 't3' => [4, 7, 1],
  81. 't4' => [5, 6, 2],
  82. ];
  83. $this->assertEquals($expected, $this->dumpTreeWithScope(1), 'delete() deletes all descendants and shifts the entire subtree correctly');
  84. $expected = [
  85. 't8' => [1, 6, 0],
  86. 't9' => [2, 3, 1],
  87. 't10' => [4, 5, 1],
  88. ];
  89. $this->assertEquals($expected, $this->dumpTreeWithScope(2), 'delete() does not delete anything out of the scope');
  90. }
  91. /**
  92. * @return void
  93. */
  94. public function testIsDescendantOf()
  95. {
  96. [$t1, $t2, $t3, $t4, $t5, $t6, $t7, $t8, $t9, $t10] = $this->initTreeWithScope();
  97. /* Tree used for tests
  98. Scope 1
  99. t1
  100. | \
  101. t2 t3
  102. | \
  103. t4 t5
  104. | \
  105. t6 t7
  106. Scope 2
  107. t8
  108. | \
  109. t9 t10
  110. */
  111. $this->assertFalse($t8->isDescendantOf($t9), 'root is not seen as a child of root');
  112. $this->assertTrue($t9->isDescendantOf($t8), 'direct child is seen as a child of root');
  113. $this->assertFalse($t2->isDescendantOf($t8), 'is false, since both are in different scopes');
  114. }
  115. /**
  116. * @return void
  117. */
  118. public function testGetParent()
  119. {
  120. $this->initTreeWithScope();
  121. $t1 = $this->getByTitle('t1');
  122. $this->assertNull($t1->getParent($this->con), 'getParent() return null for root nodes');
  123. $t2 = $this->getByTitle('t2');
  124. $this->assertEquals($t2->getParent($this->con), $t1, 'getParent() correctly retrieves parent for leafs');
  125. $t3 = $this->getByTitle('t3');
  126. $this->assertEquals($t3->getParent($this->con), $t1, 'getParent() correctly retrieves parent for nodes');
  127. $t4 = $this->getByTitle('t4');
  128. $this->assertEquals($t4->getParent($this->con), $t3, 'getParent() retrieves the same parent for nodes');
  129. }
  130. /**
  131. * @return void
  132. */
  133. public function testGetPrevSibling()
  134. {
  135. [$t1, $t2, $t3, $t4, $t5, $t6, $t7, $t8, $t9, $t10] = $this->initTreeWithScope();
  136. /* Tree used for tests
  137. Scope 1
  138. t1
  139. | \
  140. t2 t3
  141. | \
  142. t4 t5
  143. | \
  144. t6 t7
  145. Scope 2
  146. t8
  147. | \
  148. t9 t10
  149. */
  150. $this->assertNull($t1->getPrevSibling($this->con), 'getPrevSibling() returns null for root nodes');
  151. $this->assertNull($t2->getPrevSibling($this->con), 'getPrevSibling() returns null for first siblings');
  152. $this->assertEquals($t3->getPrevSibling($this->con), $t2, 'getPrevSibling() correctly retrieves prev sibling');
  153. $this->assertNull($t6->getPrevSibling($this->con), 'getPrevSibling() returns null for first siblings');
  154. $this->assertEquals($t7->getPrevSibling($this->con), $t6, 'getPrevSibling() correctly retrieves prev sibling');
  155. }
  156. /**
  157. * @return void
  158. */
  159. public function testGetNextSibling()
  160. {
  161. [$t1, $t2, $t3, $t4, $t5, $t6, $t7, $t8, $t9, $t10] = $this->initTreeWithScope();
  162. /* Tree used for tests
  163. Scope 1
  164. t1
  165. | \
  166. t2 t3
  167. | \
  168. t4 t5
  169. | \
  170. t6 t7
  171. Scope 2
  172. t8
  173. | \
  174. t9 t10
  175. */
  176. $this->assertNull($t1->getNextSibling($this->con), 'getNextSibling() returns null for root nodes');
  177. $this->assertEquals($t2->getNextSibling($this->con), $t3, 'getNextSibling() correctly retrieves next sibling');
  178. $this->assertNull($t3->getNextSibling($this->con), 'getNextSibling() returns null for last siblings');
  179. $this->assertEquals($t6->getNextSibling($this->con), $t7, 'getNextSibling() correctly retrieves next sibling');
  180. $this->assertNull($t7->getNextSibling($this->con), 'getNextSibling() returns null for last siblings');
  181. }
  182. /**
  183. * @return void
  184. */
  185. public function testGetDescendants()
  186. {
  187. [$t1, $t2, $t3, $t4, $t5, $t6, $t7, $t8, $t9, $t10] = $this->initTreeWithScope();
  188. /* Tree used for tests
  189. Scope 1
  190. t1
  191. | \
  192. t2 t3
  193. | \
  194. t4 t5
  195. | \
  196. t6 t7
  197. Scope 2
  198. t8
  199. | \
  200. t9 t10
  201. */
  202. $descendants = $t3->getDescendants();
  203. $expected = [
  204. 't4' => [5, 6, 2],
  205. 't5' => [7, 12, 2],
  206. 't6' => [8, 9, 3],
  207. 't7' => [10, 11, 3],
  208. ];
  209. $this->assertEquals($expected, $this->dumpNodes($descendants), 'getDescendants() returns descendants from the current scope only');
  210. }
  211. /**
  212. * @return void
  213. */
  214. public function testGetAncestors()
  215. {
  216. [$t1, $t2, $t3, $t4, $t5, $t6, $t7, $t8, $t9, $t10] = $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. $this->assertEquals([], $t1->getAncestors(), 'getAncestors() returns an empty array for roots');
  232. $ancestors = $t5->getAncestors();
  233. $expected = [
  234. 't1' => [1, 14, 0],
  235. 't3' => [4, 13, 1],
  236. ];
  237. $this->assertEquals($expected, $this->dumpNodes($ancestors), 'getAncestors() returns ancestors from the current scope only');
  238. }
  239. /**
  240. * @return void
  241. */
  242. public function testInsertAsFirstChildOf()
  243. {
  244. $this->assertTrue(
  245. method_exists('NestedSetTable10', 'insertAsFirstChildOf'),
  246. 'nested_set adds a insertAsFirstChildOf() method'
  247. );
  248. $fixtures = $this->initTreeWithScope();
  249. /* Tree used for tests
  250. Scope 1
  251. t1
  252. | \
  253. t2 t3
  254. | \
  255. t4 t5
  256. | \
  257. t6 t7
  258. Scope 2
  259. t8
  260. | \
  261. t9 t10
  262. */
  263. $t11 = new PublicTable10();
  264. $t11->setTitle('t11');
  265. $t11->insertAsFirstChildOf($fixtures[2]); // first child of t3
  266. $this->assertEquals(1, $t11->getScopeValue(), 'insertAsFirstChildOf() sets the scope value correctly');
  267. $t11->save();
  268. $expected = [
  269. 't1' => [1, 16, 0],
  270. 't2' => [2, 3, 1],
  271. 't3' => [4, 15, 1],
  272. 't4' => [7, 8, 2],
  273. 't5' => [9, 14, 2],
  274. 't6' => [10, 11, 3],
  275. 't7' => [12, 13, 3],
  276. 't11' => [5, 6, 2],
  277. ];
  278. $this->assertEquals($expected, $this->dumpTreeWithScope(1), 'insertAsFirstChildOf() shifts the other nodes correctly');
  279. $expected = [
  280. 't8' => [1, 6, 0],
  281. 't9' => [2, 3, 1],
  282. 't10' => [4, 5, 1],
  283. ];
  284. $this->assertEquals($expected, $this->dumpTreeWithScope(2), 'insertAsFirstChildOf() does not shift anything out of the scope');
  285. }
  286. /**
  287. * @return void
  288. */
  289. public function testInsertAsFirstChildOfExistingObject()
  290. {
  291. NestedSetTable10Query::create()->deleteAll();
  292. $t = new NestedSetTable10();
  293. $t->setScopeValue(34);
  294. $t->makeRoot();
  295. $t->save();
  296. $this->assertEquals(1, $t->getLeftValue());
  297. $this->assertEquals(2, $t->getRightValue());
  298. $this->assertEquals(0, $t->getLevel());
  299. $t1 = new NestedSetTable10();
  300. $t1->save();
  301. $t1->insertAsFirstChildOf($t);
  302. $this->assertEquals(2, $t1->getLeftValue());
  303. $this->assertEquals(3, $t1->getRightValue());
  304. $this->assertEquals(34, $t1->getScopeValue());
  305. $this->assertEquals(1, $t1->getLevel());
  306. $t1->save();
  307. $this->assertEquals(1, $t->getLeftValue());
  308. $this->assertEquals(4, $t->getRightValue());
  309. $this->assertEquals(0, $t->getLevel());
  310. $this->assertEquals(2, $t1->getLeftValue());
  311. $this->assertEquals(3, $t1->getRightValue());
  312. $this->assertEquals(34, $t1->getScopeValue());
  313. $this->assertEquals(1, $t1->getLevel());
  314. }
  315. /**
  316. * @return void
  317. */
  318. public function testInsertAsLastChildOf()
  319. {
  320. $this->assertTrue(
  321. method_exists('NestedSetTable10', 'insertAsLastChildOf'),
  322. 'nested_set adds a insertAsLastChildOf() method'
  323. );
  324. $fixtures = $this->initTreeWithScope();
  325. /* Tree used for tests
  326. Scope 1
  327. t1
  328. | \
  329. t2 t3
  330. | \
  331. t4 t5
  332. | \
  333. t6 t7
  334. Scope 2
  335. t8
  336. | \
  337. t9 t10
  338. */
  339. $t11 = new PublicTable10();
  340. $t11->setTitle('t11');
  341. $t11->insertAsLastChildOf($fixtures[2]); // last child of t3
  342. $this->assertEquals(1, $t11->getScopeValue(), 'insertAsLastChildOf() sets the scope value correctly');
  343. $t11->save();
  344. $expected = [
  345. 't1' => [1, 16, 0],
  346. 't2' => [2, 3, 1],
  347. 't3' => [4, 15, 1],
  348. 't4' => [5, 6, 2],
  349. 't5' => [7, 12, 2],
  350. 't6' => [8, 9, 3],
  351. 't7' => [10, 11, 3],
  352. 't11' => [13, 14, 2],
  353. ];
  354. $this->assertEquals($expected, $this->dumpTreeWithScope(1), 'insertAsLastChildOf() shifts the other nodes correctly');
  355. $expected = [
  356. 't8' => [1, 6, 0],
  357. 't9' => [2, 3, 1],
  358. 't10' => [4, 5, 1],
  359. ];
  360. $this->assertEquals($expected, $this->dumpTreeWithScope(2), 'insertAsLastChildOf() does not shift anything out of the scope');
  361. }
  362. /**
  363. * @return void
  364. */
  365. public function testInsertAsLastChildOfExistingObject()
  366. {
  367. NestedSetTable10Query::create()->deleteAll();
  368. $t = new NestedSetTable10();
  369. $t->setScopeValue(34);
  370. $t->makeRoot();
  371. $t->save();
  372. $this->assertEquals(1, $t->getLeftValue());
  373. $this->assertEquals(2, $t->getRightValue());
  374. $this->assertEquals(0, $t->getLevel());
  375. $t1 = new NestedSetTable10();
  376. $t1->save();
  377. $t1->insertAsLastChildOf($t);
  378. $this->assertEquals(2, $t1->getLeftValue());
  379. $this->assertEquals(3, $t1->getRightValue());
  380. $this->assertEquals(34, $t1->getScopeValue());
  381. $this->assertEquals(1, $t1->getLevel());
  382. $t1->save();
  383. $this->assertEquals(1, $t->getLeftValue());
  384. $this->assertEquals(4, $t->getRightValue());
  385. $this->assertEquals(0, $t->getLevel());
  386. $this->assertEquals(2, $t1->getLeftValue());
  387. $this->assertEquals(3, $t1->getRightValue());
  388. $this->assertEquals(34, $t1->getScopeValue());
  389. $this->assertEquals(1, $t1->getLevel());
  390. }
  391. /**
  392. * @return void
  393. */
  394. public function testInsertAsPrevSiblingOf()
  395. {
  396. $this->assertTrue(
  397. method_exists('NestedSetTable10', 'insertAsPrevSiblingOf'),
  398. 'nested_set adds a insertAsPrevSiblingOf() method'
  399. );
  400. $fixtures = $this->initTreeWithScope();
  401. /* Tree used for tests
  402. Scope 1
  403. t1
  404. | \
  405. t2 t3
  406. | \
  407. t4 t5
  408. | \
  409. t6 t7
  410. Scope 2
  411. t8
  412. | \
  413. t9 t10
  414. */
  415. $t11 = new PublicTable10();
  416. $t11->setTitle('t11');
  417. $t11->insertAsPrevSiblingOf($fixtures[2]); // prev sibling of t3
  418. $this->assertEquals(1, $t11->getScopeValue(), 'insertAsPrevSiblingOf() sets the scope value correctly');
  419. $t11->save();
  420. $expected = [
  421. 't1' => [1, 16, 0],
  422. 't2' => [2, 3, 1],
  423. 't3' => [6, 15, 1],
  424. 't4' => [7, 8, 2],
  425. 't5' => [9, 14, 2],
  426. 't6' => [10, 11, 3],
  427. 't7' => [12, 13, 3],
  428. 't11' => [4, 5, 1],
  429. ];
  430. $this->assertEquals($expected, $this->dumpTreeWithScope(1), 'insertAsPrevSiblingOf() shifts the other nodes correctly');
  431. $expected = [
  432. 't8' => [1, 6, 0],
  433. 't9' => [2, 3, 1],
  434. 't10' => [4, 5, 1],
  435. ];
  436. $this->assertEquals($expected, $this->dumpTreeWithScope(2), 'insertAsPrevSiblingOf() does not shift anything out of the scope');
  437. }
  438. /**
  439. * @return void
  440. */
  441. public function testInsertAsPrevSiblingOfExistingObject()
  442. {
  443. NestedSetTable10Query::create()->deleteAll();
  444. $t = new NestedSetTable10();
  445. $t->setScopeValue(34);
  446. $t->makeRoot();
  447. $t->save();
  448. $t1 = new NestedSetTable10();
  449. $t1->insertAsFirstChildOf($t);
  450. $t1->save();
  451. $this->assertEquals(1, $t->getLeftValue());
  452. $this->assertEquals(4, $t->getRightValue());
  453. $this->assertEquals(0, $t->getLevel());
  454. $this->assertEquals(2, $t1->getLeftValue());
  455. $this->assertEquals(3, $t1->getRightValue());
  456. $this->assertEquals(34, $t1->getScopeValue());
  457. $this->assertEquals(1, $t1->getLevel());
  458. $t2 = new NestedSetTable10();
  459. $t2->save();
  460. $t2->insertAsPrevSiblingOf($t1);
  461. $this->assertEquals(2, $t2->getLeftValue());
  462. $this->assertEquals(3, $t2->getRightValue());
  463. $this->assertEquals(34, $t2->getScopeValue());
  464. $this->assertEquals(1, $t2->getLevel());
  465. $t2->save();
  466. $this->assertEquals(1, $t->getLeftValue());
  467. $this->assertEquals(6, $t->getRightValue());
  468. $this->assertEquals(0, $t->getLevel());
  469. $this->assertEquals(4, $t1->getLeftValue());
  470. $this->assertEquals(5, $t1->getRightValue());
  471. $this->assertEquals(34, $t1->getScopeValue());
  472. $this->assertEquals(1, $t1->getLevel());
  473. $this->assertEquals(2, $t2->getLeftValue());
  474. $this->assertEquals(3, $t2->getRightValue());
  475. $this->assertEquals(34, $t2->getScopeValue());
  476. $this->assertEquals(1, $t2->getLevel());
  477. }
  478. /**
  479. * @return void
  480. */
  481. public function testInsertAsNextSiblingOf()
  482. {
  483. $this->assertTrue(
  484. method_exists('NestedSetTable10', 'insertAsNextSiblingOf'),
  485. 'nested_set adds a insertAsNextSiblingOf() method'
  486. );
  487. $fixtures = $this->initTreeWithScope();
  488. /* Tree used for tests
  489. Scope 1
  490. t1
  491. | \
  492. t2 t3
  493. | \
  494. t4 t5
  495. | \
  496. t6 t7
  497. Scope 2
  498. t8
  499. | \
  500. t9 t10
  501. */
  502. $t11 = new PublicTable10();
  503. $t11->setTitle('t11');
  504. $t11->insertAsNextSiblingOf($fixtures[2]); // next sibling of t3
  505. $this->assertEquals(1, $t11->getScopeValue(), 'insertAsNextSiblingOf() sets the scope value correctly');
  506. $t11->save();
  507. $expected = [
  508. 't1' => [1, 16, 0],
  509. 't2' => [2, 3, 1],
  510. 't3' => [4, 13, 1],
  511. 't4' => [5, 6, 2],
  512. 't5' => [7, 12, 2],
  513. 't6' => [8, 9, 3],
  514. 't7' => [10, 11, 3],
  515. 't11' => [14, 15, 1],
  516. ];
  517. $this->assertEquals($expected, $this->dumpTreeWithScope(1), 'insertAsNextSiblingOf() shifts the other nodes correctly');
  518. $expected = [
  519. 't8' => [1, 6, 0],
  520. 't9' => [2, 3, 1],
  521. 't10' => [4, 5, 1],
  522. ];
  523. $this->assertEquals($expected, $this->dumpTreeWithScope(2), 'insertAsNextSiblingOf() does not shift anything out of the scope');
  524. }
  525. /**
  526. * @return void
  527. */
  528. public function testInsertAsNextSiblingOfExistingObject()
  529. {
  530. NestedSetTable10Query::create()->deleteAll();
  531. $t = new NestedSetTable10();
  532. $t->setScopeValue(34);
  533. $t->makeRoot();
  534. $t->save();
  535. $t1 = new NestedSetTable10();
  536. $t1->insertAsFirstChildOf($t);
  537. $t1->save();
  538. $this->assertEquals(1, $t->getLeftValue());
  539. $this->assertEquals(4, $t->getRightValue());
  540. $this->assertEquals(0, $t->getLevel());
  541. $this->assertEquals(2, $t1->getLeftValue());
  542. $this->assertEquals(3, $t1->getRightValue());
  543. $this->assertEquals(34, $t1->getScopeValue());
  544. $this->assertEquals(1, $t1->getLevel());
  545. $t2 = new NestedSetTable10();
  546. $t2->save();
  547. $t2->insertAsNextSiblingOf($t1);
  548. $this->assertEquals(4, $t2->getLeftValue());
  549. $this->assertEquals(5, $t2->getRightValue());
  550. $this->assertEquals(34, $t2->getScopeValue());
  551. $this->assertEquals(1, $t2->getLevel());
  552. $t2->save();
  553. $this->assertEquals(1, $t->getLeftValue());
  554. $this->assertEquals(6, $t->getRightValue());
  555. $this->assertEquals(0, $t->getLevel());
  556. $this->assertEquals(2, $t1->getLeftValue());
  557. $this->assertEquals(3, $t1->getRightValue());
  558. $this->assertEquals(34, $t1->getScopeValue());
  559. $this->assertEquals(1, $t1->getLevel());
  560. $this->assertEquals(4, $t2->getLeftValue());
  561. $this->assertEquals(5, $t2->getRightValue());
  562. $this->assertEquals(34, $t2->getScopeValue());
  563. $this->assertEquals(1, $t2->getLevel());
  564. }
  565. /**
  566. * @return void
  567. */
  568. public function testMoveToFirstChildOf()
  569. {
  570. [$t1, $t2, $t3, $t4, $t5, $t6, $t7, $t8, $t9, $t10] = $this->initTreeWithScope();
  571. /* Tree used for tests
  572. Scope 1
  573. t1
  574. | \
  575. t2 t3
  576. | \
  577. t4 t5
  578. | \
  579. t6 t7
  580. Scope 2
  581. t8
  582. | \
  583. t9 t10
  584. */
  585. $this->assertEquals(13, $t3->getRightValue(), 't3 left has 13 per init');
  586. $this->assertEquals(1, $t10->getLevel(), 'Init level is 1');
  587. $t10->moveToFirstChildOf($t2);
  588. $this->assertEquals(2, $t2->getLeftValue(), 'As before');
  589. $this->assertEquals(5, $t2->getRightValue(), 'Extended by 2');
  590. $this->assertEquals(3, $t10->getLeftValue(), 'Moved into t2');
  591. $this->assertEquals(4, $t10->getRightValue(), 'Moved into t2');
  592. $this->assertEquals(2, $t10->getLevel(), 'New level is 2');
  593. $this->assertEquals($t2->getScopeValue(), $t10->getScopeValue(), 'Should have now the same scope');
  594. $expected = [
  595. 't8' => [1, 4, 0],
  596. 't9' => [2, 3, 1],
  597. ];
  598. $this->assertEquals($expected, $this->dumpTreeWithScope(2), 't10 removed from scope 2, therefore t8 `right` has been changed');
  599. $this->assertEquals(15, $t3->getRightValue(), 't3 has shifted by one item, so from 13 to 15');
  600. //move t7 into t9, from scope 1 to scope 2
  601. $t7->moveToFirstChildOf($t9);
  602. $this->assertEquals(13, $t3->getRightValue(), 't3 `right` has now 15-2 => 13');
  603. $this->assertEquals(2, $t7->getScopeValue(), 't7 is now in scope 2');
  604. $this->assertEquals(6, $t8->getRightValue(), 't8 extended by 1 item, 4+2 => 6');
  605. $this->assertEquals(2, $t7->getLevel(), 'New level is 2');
  606. //dispose scope 2
  607. $oldt4Left = $t4->getLeftValue();
  608. $t8->moveToFirstChildOf($t3);
  609. $this->assertEquals($t3->getLeftValue() + 1, $t8->getLeftValue(), 't8 has been moved to first children of t3');
  610. $this->assertEquals(19, $t3->getRightValue(), 't3 was extended for 3 more children, from 13+(3*2) to 19');
  611. $this->assertEquals($oldt4Left + (2 * 3), $t4->getLeftValue(), 't4 was moved by 3 items before it');
  612. $this->assertEquals(3, $t9->getLevel(), 'New level is 3');
  613. $expected = [];
  614. $this->assertEquals($expected, $this->dumpTreeWithScope(2), 'root of scope 2 to scope 1, therefore scope 2 is empty');
  615. }
  616. /**
  617. * @return void
  618. */
  619. public function testMoveToLastChildOf()
  620. {
  621. [$t1, $t2, $t3, $t4, $t5, $t6, $t7, $t8, $t9, $t10] = $this->initTreeWithScope();
  622. /* Tree used for tests
  623. Scope 1
  624. t1
  625. | \
  626. t2 t3
  627. | \
  628. t4 t5
  629. | \
  630. t6 t7
  631. Scope 2
  632. t8
  633. | \
  634. t9 t10
  635. */
  636. $this->assertEquals(13, $t3->getRightValue(), 't3 left has 13 per init');
  637. $t10->moveToLastChildOf($t2);
  638. $this->assertEquals(2, $t2->getLeftValue(), 'As before');
  639. $this->assertEquals(5, $t2->getRightValue(), 'Extended by 2');
  640. $this->assertEquals(3, $t10->getLeftValue(), 'Moved into t2');
  641. $this->assertEquals(4, $t10->getRightValue(), 'Moved into t2');
  642. $this->assertEquals(2, $t10->getLevel(), 'New level is 2');
  643. $this->assertEquals($t2->getScopeValue(), $t10->getScopeValue(), 'Should have now the same scope');
  644. $expected = [
  645. 't8' => [1, 4, 0],
  646. 't9' => [2, 3, 1],
  647. ];
  648. $this->assertEquals($expected, $this->dumpTreeWithScope(2), 't10 removed from scope 2, therefore t8 `right` has been changed');
  649. $this->assertEquals(15, $t3->getRightValue(), 't3 has shifted by one item, so from 13 to 15');
  650. //move t7 into t9, from scope 1 to scope 2
  651. $t7->moveToLastChildOf($t9);
  652. $this->assertEquals(13, $t3->getRightValue(), 't3 `right` has now 15-2 => 13');
  653. $this->assertEquals(2, $t7->getScopeValue(), 't7 is now in scope 2');
  654. $this->assertEquals(6, $t8->getRightValue(), 't8 extended by 1 item, 4+2 => 6');
  655. $this->assertEquals(2, $t7->getLevel(), 'New level is 2');
  656. //dispose scope 2
  657. $t8->moveToLastChildOf($t3);
  658. $this->assertEquals(13, $t8->getLeftValue(), 't8 has been moved to last children of t3');
  659. $this->assertEquals(19, $t3->getRightValue(), 't3 was extended for 3 more children, from 13+(3*2) to 19');
  660. $this->assertEquals(3, $t9->getLevel(), 'New level is 3');
  661. $expected = [];
  662. $this->assertEquals($expected, $this->dumpTreeWithScope(2), 'root of scope 2 to scope 1, therefore scope 2 is empty');
  663. }
  664. /**
  665. * @return void
  666. */
  667. public function testMoveToPrevSiblingOf()
  668. {
  669. [$t1, $t2, $t3, $t4, $t5, $t6, $t7, $t8, $t9, $t10] = $this->initTreeWithScope();
  670. /* Tree used for tests
  671. Scope 1
  672. t1
  673. | \
  674. t2 t3
  675. | \
  676. t4 t5
  677. | \
  678. t6 t7
  679. Scope 2
  680. t8
  681. | \
  682. t9 t10
  683. */
  684. $this->assertEquals(13, $t3->getRightValue(), 't3 left has 13 per init');
  685. $this->assertEquals(2, $t2->getLeftValue(), 'Init');
  686. $this->assertEquals(3, $t2->getRightValue(), 'Init');
  687. $t10->moveToPrevSiblingOf($t2);
  688. $this->assertEquals(4, $t2->getLeftValue(), 'Move by one item, +2');
  689. $this->assertEquals(5, $t2->getRightValue(), 'Move by one item, +2');
  690. $this->assertEquals(1, $t10->getLevel(), 'Level is 1 as old');
  691. $this->assertEquals(2, $t10->getLeftValue(), 'Moved before t2');
  692. $this->assertEquals(3, $t10->getRightValue(), 'Moved before t2');
  693. $this->assertEquals($t2->getScopeValue(), $t10->getScopeValue(), 'Should have now the same scope');
  694. $expected = [
  695. 't8' => [1, 4, 0],
  696. 't9' => [2, 3, 1],
  697. ];
  698. $this->assertEquals($expected, $this->dumpTreeWithScope(2), 't10 removed from scope 2, therefore t8 `right` has been changed');
  699. $this->assertEquals(15, $t3->getRightValue(), 't3 has shifted by one item, so from 13 to 15');
  700. //move t7 before t9, from scope 1 to scope 2
  701. $t7->moveToPrevSiblingOf($t9);
  702. $this->assertEquals(13, $t3->getRightValue(), 't3 `right` has now 15-2 => 13');
  703. $this->assertEquals(2, $t7->getScopeValue(), 't7 is now in scope 2');
  704. $this->assertEquals(6, $t8->getRightValue(), 't8 extended by 1 item, 4+2 => 6');
  705. $this->assertEquals(1, $t7->getLevel(), 'New level is 1');
  706. //dispose scope 2
  707. $t8->moveToPrevSiblingOf($t3);
  708. $this->assertEquals(6, $t8->getLeftValue(), 't8 has been moved to last children of t3');
  709. $this->assertEquals(19, $t3->getRightValue(), 't3 was moved for 3 item before it, so from 13+(3*2) to 19');
  710. $this->assertEquals(2, $t9->getLevel(), 'New level is 2');
  711. $this->assertEquals(1, $t8->getLevel(), 'New level is 1');
  712. $expected = [];
  713. $this->assertEquals($expected, $this->dumpTreeWithScope(2), 'root of scope 2 to scope 1, therefore scope 2 is empty');
  714. }
  715. /**
  716. * @return void
  717. */
  718. public function testMoveToNextSiblingOf()
  719. {
  720. [$t1, $t2, $t3, $t4, $t5, $t6, $t7, $t8, $t9, $t10] = $this->initTreeWithScope();
  721. /* Tree used for tests
  722. Scope 1
  723. t1
  724. | \
  725. t2 t3
  726. | \
  727. t4 t5
  728. | \
  729. t6 t7
  730. Scope 2
  731. t8
  732. | \
  733. t9 t10
  734. */
  735. $this->assertEquals(13, $t3->getRightValue(), 't3 left has 13 per init');
  736. $this->assertEquals(2, $t2->getLeftValue(), 'Init');
  737. $this->assertEquals(3, $t2->getRightValue(), 'Init');
  738. $t10->moveToNextSiblingOf($t2);
  739. $this->assertEquals(2, $t2->getLeftValue(), 'Same as before');
  740. $this->assertEquals(3, $t2->getRightValue(), 'Same as before');
  741. $this->assertEquals(1, $t10->getLevel(), 'Level is 1 as before');
  742. $this->assertEquals(6, $t3->getLeftValue(), 'Move by one item, +2');
  743. $this->assertEquals(15, $t3->getRightValue(), 'Move by one item, +2');
  744. $this->assertEquals(4, $t10->getLeftValue(), 'Moved after t2');
  745. $this->assertEquals(5, $t10->getRightValue(), 'Moved after t2');
  746. $this->assertEquals($t2->getScopeValue(), $t10->getScopeValue(), 'Should have now the same scope');
  747. $expected = [
  748. 't8' => [1, 4, 0],
  749. 't9' => [2, 3, 1],
  750. ];
  751. $this->assertEquals($expected, $this->dumpTreeWithScope(2), 't10 removed from scope 2, therefore t8 `right` has been changed');
  752. $this->assertEquals(15, $t3->getRightValue(), 't3 has shifted by one item, so from 13 to 15');
  753. //move t7 after t9, from scope 1 to scope 2
  754. $t7->moveToNextSiblingOf($t9);
  755. $this->assertEquals(13, $t3->getRightValue(), 't3 `right` has now 15-2 => 13');
  756. $this->assertEquals(2, $t7->getScopeValue(), 't7 is now in scope 2');
  757. $this->assertEquals(6, $t8->getRightValue(), 't8 extended by 1 item, 4+2 => 6');
  758. $this->assertEquals(1, $t7->getLevel(), 'New level is 1');
  759. $this->assertEquals($t9->getRightValue() + 1, $t7->getLeftValue(), 'Moved after t9, so we have t9.right+1 as left');
  760. //dispose scope 2
  761. $oldT1Right = $t1->getRightValue();
  762. $t8->moveToNextSiblingOf($t3);
  763. $this->assertEquals($oldT1Right + (2 * 3), $t1->getRightValue(), 't1 has been extended by 3 items');
  764. $this->assertEquals(13, $t3->getRightValue(), 't3 has no change.');
  765. $this->assertEquals(1, $t8->getLevel(), 'New level is 1');
  766. $this->assertEquals(2, $t9->getLevel(), 'New level is 2');
  767. $expected = [];
  768. $this->assertEquals($expected, $this->dumpTreeWithScope(2), 'root of scope 2 to scope 1, therefore scope 2 is empty');
  769. }
  770. /**
  771. * @return void
  772. */
  773. public function testDeleteDescendants()
  774. {
  775. [$t1, $t2, $t3, $t4, $t5, $t6, $t7, $t8, $t9, $t10] = $this->initTreeWithScope();
  776. /* Tree used for tests
  777. Scope 1
  778. t1
  779. | \
  780. t2 t3
  781. | \
  782. t4 t5
  783. | \
  784. t6 t7
  785. Scope 2
  786. t8
  787. | \
  788. t9 t10
  789. */
  790. $this->assertEquals(4, $t3->deleteDescendants(), 'deleteDescendants() returns the number of deleted nodes');
  791. $expected = [
  792. 't1' => [1, 6, 0],
  793. 't2' => [2, 3, 1],
  794. 't3' => [4, 5, 1],
  795. ];
  796. $this->assertEquals($expected, $this->dumpTreeWithScope(1), 'deleteDescendants() shifts the entire subtree correctly');
  797. $expected = [
  798. 't8' => [1, 6, 0],
  799. 't9' => [2, 3, 1],
  800. 't10' => [4, 5, 1],
  801. ];
  802. $this->assertEquals($expected, $this->dumpTreeWithScope(2), 'deleteDescendants() does not delete anything out of the scope');
  803. }
  804. /**
  805. * @return void
  806. */
  807. public function testConstants()
  808. {
  809. $this->assertEquals(NestedSetTable10::LEFT_COL, 'nested_set_table10.my_left_column');
  810. $this->assertEquals(NestedSetTable10::RIGHT_COL, 'nested_set_table10.my_right_column');
  811. $this->assertEquals(NestedSetTable10::LEVEL_COL, 'nested_set_table10.my_level_column');
  812. $this->assertEquals(NestedSetTable10::SCOPE_COL, 'nested_set_table10.my_scope_column');
  813. }
  814. }