PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/forum/library/propel/tmp/test/testsuite/generator/behavior/nestedset/NestedSetBehaviorObjectBuilderModifierWithScopeTest.php

https://github.com/albrzykowski/zforum
PHP | 551 lines | 307 code | 19 blank | 225 comment | 0 complexity | 424cc2133b2c1dbf269059aa26d6feaf MD5 | raw file
  1. <?php
  2. /*
  3. * $Id: NestedSetBehaviorObjectBuilderModifierWithScopeTest.php 1612 2010-03-16 22:56:21Z francois $
  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. require_once 'tools/helpers/bookstore/behavior/BookstoreNestedSetTestBase.php';
  11. /**
  12. * Tests for NestedSetBehaviorObjectBuilderModifier class
  13. *
  14. * @author Franรงois Zaninotto
  15. * @version $Revision: 1612 $
  16. * @package generator.behavior.nestedset
  17. */
  18. class NestedSetBehaviorObjectBuilderModifierWithScopeTest extends BookstoreNestedSetTestBase
  19. {
  20. protected function getByTitle($title)
  21. {
  22. $c = new Criteria();
  23. $c->add(Table10Peer::TITLE, $title);
  24. return Table10Peer::doSelectOne($c);
  25. }
  26. public function testDelete()
  27. {
  28. list($t1, $t2, $t3, $t4, $t5, $t6, $t7, $t8, $t9, $t10) = $this->initTreeWithScope();
  29. /* Tree used for tests
  30. Scope 1
  31. t1
  32. | \
  33. t2 t3
  34. | \
  35. t4 t5
  36. | \
  37. t6 t7
  38. Scope 2
  39. t8
  40. | \
  41. t9 t10
  42. */
  43. $t5->delete();
  44. $expected = array(
  45. 't1' => array(1, 8, 0),
  46. 't2' => array(2, 3, 1),
  47. 't3' => array(4, 7, 1),
  48. 't4' => array(5, 6, 2),
  49. );
  50. $this->assertEquals($expected, $this->dumpTreeWithScope(1), 'delete() deletes all descendants and shifts the entire subtree correctly');
  51. $expected = array(
  52. 't8' => array(1, 6, 0),
  53. 't9' => array(2, 3, 1),
  54. 't10' => array(4, 5, 1),
  55. );
  56. $this->assertEquals($expected, $this->dumpTreeWithScope(2), 'delete() does not delete anything out of the scope');
  57. }
  58. public function testIsDescendantOf()
  59. {
  60. list($t1, $t2, $t3, $t4, $t5, $t6, $t7, $t8, $t9, $t10) = $this->initTreeWithScope();
  61. /* Tree used for tests
  62. Scope 1
  63. t1
  64. | \
  65. t2 t3
  66. | \
  67. t4 t5
  68. | \
  69. t6 t7
  70. Scope 2
  71. t8
  72. | \
  73. t9 t10
  74. */
  75. $this->assertFalse($t8->isDescendantOf($t9), 'root is not seen as a child of root');
  76. $this->assertTrue($t9->isDescendantOf($t8), 'direct child is seen as a child of root');
  77. try {
  78. $t2->isDescendantOf($t8);
  79. $this->fail('isDescendantOf() throws an exception when comparing two nodes of different trees');
  80. } catch (PropelException $e) {
  81. $this->assertTrue(true, 'isDescendantOf() throws an exception when comparing two nodes of different trees');
  82. }
  83. }
  84. public function testGetParent()
  85. {
  86. $this->initTreeWithScope();
  87. $t1 = $this->getByTitle('t1');
  88. $this->assertNull($t1->getParent($this->con), 'getParent() return null for root nodes');
  89. $t2 = $this->getByTitle('t2');
  90. $this->assertEquals($t2->getParent($this->con), $t1, 'getParent() correctly retrieves parent for leafs');
  91. $t3 = $this->getByTitle('t3');
  92. $this->assertEquals($t3->getParent($this->con), $t1, 'getParent() correctly retrieves parent for nodes');
  93. $t4 = $this->getByTitle('t4');
  94. $this->assertEquals($t4->getParent($this->con), $t3, 'getParent() retrieves the same parent for nodes');
  95. }
  96. public function testGetPrevSibling()
  97. {
  98. list($t1, $t2, $t3, $t4, $t5, $t6, $t7, $t8, $t9, $t10) = $this->initTreeWithScope();
  99. /* Tree used for tests
  100. Scope 1
  101. t1
  102. | \
  103. t2 t3
  104. | \
  105. t4 t5
  106. | \
  107. t6 t7
  108. Scope 2
  109. t8
  110. | \
  111. t9 t10
  112. */
  113. $this->assertNull($t1->getPrevSibling($this->con), 'getPrevSibling() returns null for root nodes');
  114. $this->assertNull($t2->getPrevSibling($this->con), 'getPrevSibling() returns null for first siblings');
  115. $this->assertEquals($t3->getPrevSibling($this->con), $t2, 'getPrevSibling() correctly retrieves prev sibling');
  116. $this->assertNull($t6->getPrevSibling($this->con), 'getPrevSibling() returns null for first siblings');
  117. $this->assertEquals($t7->getPrevSibling($this->con), $t6, 'getPrevSibling() correctly retrieves prev sibling');
  118. }
  119. public function testGetNextSibling()
  120. {
  121. list($t1, $t2, $t3, $t4, $t5, $t6, $t7, $t8, $t9, $t10) = $this->initTreeWithScope();
  122. /* Tree used for tests
  123. Scope 1
  124. t1
  125. | \
  126. t2 t3
  127. | \
  128. t4 t5
  129. | \
  130. t6 t7
  131. Scope 2
  132. t8
  133. | \
  134. t9 t10
  135. */
  136. $this->assertNull($t1->getNextSibling($this->con), 'getNextSibling() returns null for root nodes');
  137. $this->assertEquals($t2->getNextSibling($this->con), $t3, 'getNextSibling() correctly retrieves next sibling');
  138. $this->assertNull($t3->getNextSibling($this->con), 'getNextSibling() returns null for last siblings');
  139. $this->assertEquals($t6->getNextSibling($this->con), $t7, 'getNextSibling() correctly retrieves next sibling');
  140. $this->assertNull($t7->getNextSibling($this->con), 'getNextSibling() returns null for last siblings');
  141. }
  142. public function testGetDescendants()
  143. {
  144. list($t1, $t2, $t3, $t4, $t5, $t6, $t7, $t8, $t9, $t10) = $this->initTreeWithScope();
  145. /* Tree used for tests
  146. Scope 1
  147. t1
  148. | \
  149. t2 t3
  150. | \
  151. t4 t5
  152. | \
  153. t6 t7
  154. Scope 2
  155. t8
  156. | \
  157. t9 t10
  158. */
  159. $descendants = $t3->getDescendants();
  160. $expected = array(
  161. 't4' => array(5, 6, 2),
  162. 't5' => array(7, 12, 2),
  163. 't6' => array(8, 9, 3),
  164. 't7' => array(10, 11, 3),
  165. );
  166. $this->assertEquals($expected, $this->dumpNodes($descendants), 'getDescendants() returns descendants from the current scope only');
  167. }
  168. public function testGetAncestors()
  169. {
  170. list($t1, $t2, $t3, $t4, $t5, $t6, $t7, $t8, $t9, $t10) = $this->initTreeWithScope();
  171. /* Tree used for tests
  172. Scope 1
  173. t1
  174. | \
  175. t2 t3
  176. | \
  177. t4 t5
  178. | \
  179. t6 t7
  180. Scope 2
  181. t8
  182. | \
  183. t9 t10
  184. */
  185. $this->assertEquals(array(), $t1->getAncestors(), 'getAncestors() returns an empty array for roots');
  186. $ancestors = $t5->getAncestors();
  187. $expected = array(
  188. 't1' => array(1, 14, 0),
  189. 't3' => array(4, 13, 1),
  190. );
  191. $this->assertEquals($expected, $this->dumpNodes($ancestors), 'getAncestors() returns ancestors from the current scope only');
  192. }
  193. public function testInsertAsFirstChildOf()
  194. {
  195. $this->assertTrue(method_exists('Table10', 'insertAsFirstChildOf'), 'nested_set adds a insertAsFirstChildOf() method');
  196. $fixtures = $this->initTreeWithScope();
  197. /* Tree used for tests
  198. Scope 1
  199. t1
  200. | \
  201. t2 t3
  202. | \
  203. t4 t5
  204. | \
  205. t6 t7
  206. Scope 2
  207. t8
  208. | \
  209. t9 t10
  210. */
  211. $t11 = new PublicTable10();
  212. $t11->setTitle('t11');
  213. $t11->insertAsFirstChildOf($fixtures[2]); // first child of t3
  214. $this->assertEquals(1, $t11->getScopeValue(), 'insertAsFirstChildOf() sets the scope value correctly');
  215. $t11->save();
  216. $expected = array(
  217. 't1' => array(1, 16, 0),
  218. 't2' => array(2, 3, 1),
  219. 't3' => array(4, 15, 1),
  220. 't4' => array(7, 8, 2),
  221. 't5' => array(9, 14, 2),
  222. 't6' => array(10, 11, 3),
  223. 't7' => array(12, 13, 3),
  224. 't11' => array(5, 6, 2)
  225. );
  226. $this->assertEquals($expected, $this->dumpTreeWithScope(1), 'insertAsFirstChildOf() shifts the other nodes correctly');
  227. $expected = array(
  228. 't8' => array(1, 6, 0),
  229. 't9' => array(2, 3, 1),
  230. 't10' => array(4, 5, 1),
  231. );
  232. $this->assertEquals($expected, $this->dumpTreeWithScope(2), 'insertAsFirstChildOf() does not shift anything out of the scope');
  233. }
  234. public function testInsertAsLastChildOf()
  235. {
  236. $this->assertTrue(method_exists('Table10', 'insertAsLastChildOf'), 'nested_set adds a insertAsLastChildOf() method');
  237. $fixtures = $this->initTreeWithScope();
  238. /* Tree used for tests
  239. Scope 1
  240. t1
  241. | \
  242. t2 t3
  243. | \
  244. t4 t5
  245. | \
  246. t6 t7
  247. Scope 2
  248. t8
  249. | \
  250. t9 t10
  251. */
  252. $t11 = new PublicTable10();
  253. $t11->setTitle('t11');
  254. $t11->insertAsLastChildOf($fixtures[2]); // last child of t3
  255. $this->assertEquals(1, $t11->getScopeValue(), 'insertAsLastChildOf() sets the scope value correctly');
  256. $t11->save();
  257. $expected = array(
  258. 't1' => array(1, 16, 0),
  259. 't2' => array(2, 3, 1),
  260. 't3' => array(4, 15, 1),
  261. 't4' => array(5, 6, 2),
  262. 't5' => array(7, 12, 2),
  263. 't6' => array(8, 9, 3),
  264. 't7' => array(10, 11, 3),
  265. 't11' => array(13, 14, 2)
  266. );
  267. $this->assertEquals($expected, $this->dumpTreeWithScope(1), 'insertAsLastChildOf() shifts the other nodes correctly');
  268. $expected = array(
  269. 't8' => array(1, 6, 0),
  270. 't9' => array(2, 3, 1),
  271. 't10' => array(4, 5, 1),
  272. );
  273. $this->assertEquals($expected, $this->dumpTreeWithScope(2), 'insertAsLastChildOf() does not shift anything out of the scope');
  274. }
  275. public function testInsertAsPrevSiblingOf()
  276. {
  277. $this->assertTrue(method_exists('Table10', 'insertAsPrevSiblingOf'), 'nested_set adds a insertAsPrevSiblingOf() method');
  278. $fixtures = $this->initTreeWithScope();
  279. /* Tree used for tests
  280. Scope 1
  281. t1
  282. | \
  283. t2 t3
  284. | \
  285. t4 t5
  286. | \
  287. t6 t7
  288. Scope 2
  289. t8
  290. | \
  291. t9 t10
  292. */
  293. $t11 = new PublicTable10();
  294. $t11->setTitle('t11');
  295. $t11->insertAsPrevSiblingOf($fixtures[2]); // prev sibling of t3
  296. $this->assertEquals(1, $t11->getScopeValue(), 'insertAsPrevSiblingOf() sets the scope value correctly');
  297. $t11->save();
  298. $expected = array(
  299. 't1' => array(1, 16, 0),
  300. 't2' => array(2, 3, 1),
  301. 't3' => array(6, 15, 1),
  302. 't4' => array(7, 8, 2),
  303. 't5' => array(9, 14, 2),
  304. 't6' => array(10, 11, 3),
  305. 't7' => array(12, 13, 3),
  306. 't11' => array(4, 5, 1)
  307. );
  308. $this->assertEquals($expected, $this->dumpTreeWithScope(1), 'insertAsPrevSiblingOf() shifts the other nodes correctly');
  309. $expected = array(
  310. 't8' => array(1, 6, 0),
  311. 't9' => array(2, 3, 1),
  312. 't10' => array(4, 5, 1),
  313. );
  314. $this->assertEquals($expected, $this->dumpTreeWithScope(2), 'insertAsPrevSiblingOf() does not shift anything out of the scope');
  315. }
  316. public function testInsertAsNextSiblingOf()
  317. {
  318. $this->assertTrue(method_exists('Table10', 'insertAsNextSiblingOf'), 'nested_set adds a insertAsNextSiblingOf() method');
  319. $fixtures = $this->initTreeWithScope();
  320. /* Tree used for tests
  321. Scope 1
  322. t1
  323. | \
  324. t2 t3
  325. | \
  326. t4 t5
  327. | \
  328. t6 t7
  329. Scope 2
  330. t8
  331. | \
  332. t9 t10
  333. */
  334. $t11 = new PublicTable10();
  335. $t11->setTitle('t11');
  336. $t11->insertAsNextSiblingOf($fixtures[2]); // next sibling of t3
  337. $this->assertEquals(1, $t11->getScopeValue(), 'insertAsNextSiblingOf() sets the scope value correctly');
  338. $t11->save();
  339. $expected = array(
  340. 't1' => array(1, 16, 0),
  341. 't2' => array(2, 3, 1),
  342. 't3' => array(4, 13, 1),
  343. 't4' => array(5, 6, 2),
  344. 't5' => array(7, 12, 2),
  345. 't6' => array(8, 9, 3),
  346. 't7' => array(10, 11, 3),
  347. 't11' => array(14, 15, 1)
  348. );
  349. $this->assertEquals($expected, $this->dumpTreeWithScope(1), 'insertAsNextSiblingOf() shifts the other nodes correctly');
  350. $expected = array(
  351. 't8' => array(1, 6, 0),
  352. 't9' => array(2, 3, 1),
  353. 't10' => array(4, 5, 1),
  354. );
  355. $this->assertEquals($expected, $this->dumpTreeWithScope(2), 'insertAsNextSiblingOf() does not shift anything out of the scope');
  356. }
  357. public function testMoveToFirstChildOf()
  358. {
  359. list($t1, $t2, $t3, $t4, $t5, $t6, $t7, $t8, $t9, $t10) = $this->initTreeWithScope();
  360. /* Tree used for tests
  361. Scope 1
  362. t1
  363. | \
  364. t2 t3
  365. | \
  366. t4 t5
  367. | \
  368. t6 t7
  369. Scope 2
  370. t8
  371. | \
  372. t9 t10
  373. */
  374. try {
  375. $t8->moveToFirstChildOf($t3);
  376. $this->fail('moveToFirstChildOf() throws an exception when the target is in a different tree');
  377. } catch (PropelException $e) {
  378. $this->assertTrue(true, 'moveToFirstChildOf() throws an exception when the target is in a different tree');
  379. }
  380. try {
  381. $t5->moveToLastChildOf($t2);
  382. $this->assertTrue(true, 'moveToFirstChildOf() does not throw an exception when the target is in the same tree');
  383. } catch (PropelException $e) {
  384. $this->fail('moveToFirstChildOf() does not throw an exception when the target is in the same tree');
  385. }
  386. $expected = array(
  387. 't8' => array(1, 6, 0),
  388. 't9' => array(2, 3, 1),
  389. 't10' => array(4, 5, 1),
  390. );
  391. $this->assertEquals($expected, $this->dumpTreeWithScope(2), 'moveToFirstChildOf() does not shift anything out of the scope');
  392. }
  393. public function testMoveToLastChildOf()
  394. {
  395. list($t1, $t2, $t3, $t4, $t5, $t6, $t7, $t8, $t9, $t10) = $this->initTreeWithScope();
  396. /* Tree used for tests
  397. Scope 1
  398. t1
  399. | \
  400. t2 t3
  401. | \
  402. t4 t5
  403. | \
  404. t6 t7
  405. Scope 2
  406. t8
  407. | \
  408. t9 t10
  409. */
  410. try {
  411. $t8->moveToLastChildOf($t3);
  412. $this->fail('moveToLastChildOf() throws an exception when the target is in a different tree');
  413. } catch (PropelException $e) {
  414. $this->assertTrue(true, 'moveToLastChildOf() throws an exception when the target is in a different tree');
  415. }
  416. try {
  417. $t5->moveToLastChildOf($t2);
  418. $this->assertTrue(true, 'moveToLastChildOf() does not throw an exception when the target is in the same tree');
  419. } catch (PropelException $e) {
  420. $this->fail('moveToLastChildOf() does not throw an exception when the target is in the same tree');
  421. }
  422. $expected = array(
  423. 't8' => array(1, 6, 0),
  424. 't9' => array(2, 3, 1),
  425. 't10' => array(4, 5, 1),
  426. );
  427. $this->assertEquals($expected, $this->dumpTreeWithScope(2), 'moveToLastChildOf() does not shift anything out of the scope');
  428. }
  429. public function testMoveToPrevSiblingOf()
  430. {
  431. list($t1, $t2, $t3, $t4, $t5, $t6, $t7, $t8, $t9, $t10) = $this->initTreeWithScope();
  432. /* Tree used for tests
  433. Scope 1
  434. t1
  435. | \
  436. t2 t3
  437. | \
  438. t4 t5
  439. | \
  440. t6 t7
  441. Scope 2
  442. t8
  443. | \
  444. t9 t10
  445. */
  446. try {
  447. $t8->moveToPrevSiblingOf($t3);
  448. $this->fail('moveToPrevSiblingOf() throws an exception when the target is in a different tree');
  449. } catch (PropelException $e) {
  450. $this->assertTrue(true, 'moveToPrevSiblingOf() throws an exception when the target is in a different tree');
  451. }
  452. try {
  453. $t5->moveToPrevSiblingOf($t2);
  454. $this->assertTrue(true, 'moveToPrevSiblingOf() does not throw an exception when the target is in the same tree');
  455. } catch (PropelException $e) {
  456. $this->fail('moveToPrevSiblingOf() does not throw an exception when the target is in the same tree');
  457. }
  458. $expected = array(
  459. 't8' => array(1, 6, 0),
  460. 't9' => array(2, 3, 1),
  461. 't10' => array(4, 5, 1),
  462. );
  463. $this->assertEquals($expected, $this->dumpTreeWithScope(2), 'moveToPrevSiblingOf() does not shift anything out of the scope');
  464. }
  465. public function testMoveToNextSiblingOf()
  466. {
  467. list($t1, $t2, $t3, $t4, $t5, $t6, $t7, $t8, $t9, $t10) = $this->initTreeWithScope();
  468. /* Tree used for tests
  469. Scope 1
  470. t1
  471. | \
  472. t2 t3
  473. | \
  474. t4 t5
  475. | \
  476. t6 t7
  477. Scope 2
  478. t8
  479. | \
  480. t9 t10
  481. */
  482. try {
  483. $t8->moveToNextSiblingOf($t3);
  484. $this->fail('moveToNextSiblingOf() throws an exception when the target is in a different tree');
  485. } catch (PropelException $e) {
  486. $this->assertTrue(true, 'moveToNextSiblingOf() throws an exception when the target is in a different tree');
  487. }
  488. try {
  489. $t5->moveToNextSiblingOf($t2);
  490. $this->assertTrue(true, 'moveToNextSiblingOf() does not throw an exception when the target is in the same tree');
  491. } catch (PropelException $e) {
  492. $this->fail('moveToNextSiblingOf() does not throw an exception when the target is in the same tree');
  493. }
  494. $expected = array(
  495. 't8' => array(1, 6, 0),
  496. 't9' => array(2, 3, 1),
  497. 't10' => array(4, 5, 1),
  498. );
  499. $this->assertEquals($expected, $this->dumpTreeWithScope(2), 'moveToNextSiblingOf() does not shift anything out of the scope');
  500. }
  501. public function testDeleteDescendants()
  502. {
  503. list($t1, $t2, $t3, $t4, $t5, $t6, $t7, $t8, $t9, $t10) = $this->initTreeWithScope();
  504. /* Tree used for tests
  505. Scope 1
  506. t1
  507. | \
  508. t2 t3
  509. | \
  510. t4 t5
  511. | \
  512. t6 t7
  513. Scope 2
  514. t8
  515. | \
  516. t9 t10
  517. */
  518. $this->assertEquals(4, $t3->deleteDescendants(), 'deleteDescendants() returns the number of deleted nodes');
  519. $expected = array(
  520. 't1' => array(1, 6, 0),
  521. 't2' => array(2, 3, 1),
  522. 't3' => array(4, 5, 1),
  523. );
  524. $this->assertEquals($expected, $this->dumpTreeWithScope(1), 'deleteDescendants() shifts the entire subtree correctly');
  525. $expected = array(
  526. 't8' => array(1, 6, 0),
  527. 't9' => array(2, 3, 1),
  528. 't10' => array(4, 5, 1),
  529. );
  530. $this->assertEquals($expected, $this->dumpTreeWithScope(2), 'deleteDescendants() does not delete anything out of the scope');
  531. }
  532. }