source: branches/1.5/test/testsuite/generator/behavior/nestedset/NestedSetBehaviorObjectBuilderModifierTest.php @ 1571

Revision 1571, 43.7 KB checked in by francois, 7 months ago (diff)

[1.5][nested set] Slight getChildren() refactoring:

  • Added internal cache for children
  • Renamed getNumberOfChildren() to countChildren()
  • Added addNestedSetChild() to allow population of internal cache

(refs #854)

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