1<?php
2/*
3 *  $Id$
4 *
5 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
6 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
10 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
11 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
12 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
13 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
14 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
15 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16 *
17 * This software consists of voluntary contributions made by many individuals
18 * and is licensed under the LGPL. For more information, see
19 * <http://www.doctrine-project.org>.
20 */
21
22/**
23 * Doctrine_Record_State_TestCase
24 *
25 * @package     Doctrine
26 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
27 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
28 * @category    Object Relational Mapping
29 * @link        www.doctrine-project.org
30 * @since       1.0
31 * @version     $Revision$
32 */
33class Doctrine_NestedSet_MultiRoot_TestCase extends Doctrine_UnitTestCase
34{
35    public function prepareTables()
36    {
37        $this->tables[] = 'NestedSet_MultiRootNode';
38        parent::prepareTables();
39    }
40
41    public function prepareData()
42    {}
43
44    public function testSavingNewRecordAsRootWithoutRootIdThrowsException() {
45        $node = new NestedSet_MultiRootNode();
46        $node->name = 'root';
47        try {
48            $treeMngr = $this->conn->getTable('NestedSet_MultiRootNode')->getTree();
49            $treeMngr->createRoot($node);
50            $this->fail();
51        } catch (Doctrine_Tree_Exception $e) {
52            $this->pass();
53        }
54    }
55
56    public function testSavingNewRecordWithRootIdWorks() {
57        $node = new NestedSet_MultiRootNode();
58        $node->name = 'root';
59        $node->root_id = 42;
60        try {
61            $treeMngr = $this->conn->getTable('NestedSet_MultiRootNode')->getTree();
62            $treeMngr->createRoot($node);
63            $this->assertEqual(1, $node['lft']);
64            $this->assertEqual(2, $node['rgt']);
65            $node->getNode()->delete();
66        } catch (Doctrine_Tree_Exception $e) {
67            $this->fail();
68        }
69    }
70
71    public function testSavingPersistentRecordAsRootAssignsIdToRootId() {
72        $node = new NestedSet_MultiRootNode();
73        $node->name = 'root';
74        $node->save();
75        try {
76            $treeMngr = $this->conn->getTable('NestedSet_MultiRootNode')->getTree();
77            $treeMngr->createRoot($node);
78            $this->assertEqual(1, $node['lft']);
79            $this->assertEqual(2, $node['rgt']);
80            $this->assertEqual($node->id, $node->root_id);
81            $node->getNode()->delete();
82        } catch (Doctrine_Tree_Exception $e) {
83            $this->fail();
84        }
85    }
86
87    public function testSaveMultipleRootsWithChildren() {
88        $root1 = new NestedSet_MultiRootNode();
89        $root1->name = 'root1';
90        $root1->save();
91        try {
92            $treeMngr = $this->conn->getTable('NestedSet_MultiRootNode')->getTree();
93            $treeMngr->createRoot($root1);
94            $this->assertEqual(1, $root1['lft']);
95            $this->assertEqual(2, $root1['rgt']);
96            $this->assertEqual($root1->id, $root1->root_id);
97        } catch (Doctrine_Tree_Exception $e) {
98            $this->fail();
99        }
100
101        $root2 = new NestedSet_MultiRootNode();
102        $root2->name = 'root';
103        $root2->save();
104        try {
105            $treeMngr = $this->conn->getTable('NestedSet_MultiRootNode')->getTree();
106            $treeMngr->createRoot($root2);
107            $this->assertEqual(1, $root2['lft']);
108            $this->assertEqual(2, $root2['rgt']);
109            $this->assertEqual($root2->id, $root2->root_id);
110        } catch (Doctrine_Tree_Exception $e) {
111            $this->fail();
112        }
113
114        // now a child for root1
115        $child1 = new NestedSet_MultiRootNode();
116        $child1->name = "child1";
117        $child1->getNode()->insertAsLastChildOf($root1);
118
119        $root1->refresh(); // ! updates lft/rgt
120        // test insertion
121        $this->assertEqual(2, $child1->lft);
122        $this->assertEqual(3, $child1->rgt);
123        $this->assertEqual(1, $child1->level);
124        // test root1 has been shifted
125        $this->assertEqual(1, $root1->lft);
126        $this->assertEqual(4, $root1->rgt);
127        $this->assertEqual(0, $root1->level);
128
129        // now a child for root2
130        $child2 = new NestedSet_MultiRootNode();
131        $child2->name = "child2";
132        $child2->getNode()->insertAsLastChildOf($root2);
133
134        $root2->refresh(); // ! updates lft/rgt
135        // test insertion
136        $this->assertEqual(2, $child2->lft);
137        $this->assertEqual(3, $child2->rgt);
138        $this->assertEqual(1, $child2->level);
139        // test root2 has been shifted
140        $this->assertEqual(1, $root2->lft);
141        $this->assertEqual(4, $root2->rgt);
142        $this->assertEqual(0, $root2->level);
143
144        // query some
145        $root1Id = $root1->id;
146        $root2Id = $root2->id;
147        $this->conn->getTable('NestedSet_MultiRootNode')->clear();
148        // check the root1 child
149        $treeMngr = $this->conn->getTable('NestedSet_MultiRootNode')->getTree();
150        $root1 = $treeMngr->fetchRoot($root1Id);
151        $desc = $root1->getNode()->getDescendants();
152        $this->assertTrue($desc !== false);
153        $this->assertEqual(1, count($desc));
154        $this->assertEqual('child1', $desc[0]['name']);
155        $this->assertEqual(1, $desc[0]['level']);
156        // check the root2 child
157        $root2 = $treeMngr->fetchRoot($root2Id);
158        $desc = $root2->getNode()->getDescendants();
159        $this->assertTrue($desc !== false);
160        $this->assertEqual(1, count($desc));
161        $this->assertEqual('child2', $desc[0]['name']);
162        $this->assertEqual(1, $desc[0]['level']);
163    }
164
165}
166