PageRenderTime 70ms CodeModel.GetById 3ms app.highlight 58ms RepoModel.GetById 1ms app.codeStats 0ms

/propel_15/vendor/propel/test/testsuite/generator/behavior/nestedset/NestedSetBehaviorObjectBuilderModifierTest.php

http://github.com/eventhorizonpl/forked-php-orm-benchmark
PHP | 1521 lines | 1058 code | 59 blank | 404 comment | 0 complexity | 04231140b21dbb86fe87b05add302c01 MD5 | raw file

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

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

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