TreeStructureTestCase.php 3.73 KB
Newer Older
1
<?php
zYne's avatar
zYne committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
/*
 *  $Id$
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information, see
 * <http://www.phpdoctrine.com>.
 */

/**
 * Doctrine_TreeStructure_TestCase
 *
 * @package     Doctrine
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @category    Object Relational Mapping
 * @link        www.phpdoctrine.com
 * @since       1.0
 * @version     $Revision$
 */
class Doctrine_TreeStructure_TestCase extends Doctrine_UnitTestCase
34
{
zYne's avatar
zYne committed
35 36 37 38 39
    public function prepareTables() 
    { 
        // we don't need the standard tables here
        $this->tables = array('TreeLeaf');
        parent::prepareTables();
zYne's avatar
zYne committed
40
    }
zYne's avatar
zYne committed
41 42
    
    public function prepareData()
zYne's avatar
zYne committed
43
    {
44

zYne's avatar
zYne committed
45
    }
46 47 48 49 50 51 52 53 54 55 56 57 58 59

    public function testSelfReferentialRelationship() 
    {
        $component = new TreeLeaf();

        try {
            $rel = $component->getTable()->getRelation('Parent');
            $rel = $component->getTable()->getRelation('Children');
            $this->pass();
        } catch(Doctrine_Exception $e) {
            $this->fail();
        }
    }

zYne's avatar
zYne committed
60 61 62 63 64 65 66 67 68 69 70 71
    public function testLocalAndForeignKeysAreSetCorrectly() {
        $component = new TreeLeaf();

        $rel = $component->getTable()->getRelation('Parent');
        $this->assertEqual($rel->getLocal(), 'parent_id');
        $this->assertEqual($rel->getForeign(), 'id');

        $rel = $component->getTable()->getRelation('Children');
        $this->assertEqual($rel->getLocal(), 'id');
        $this->assertEqual($rel->getForeign(), 'parent_id');
    }

72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
    public function testTreeLeafRelationships() 
    {
        /* structure:
         *
         * o1
         *  -> o2
         *  -> o3
         *
         * o4
         *
         * thus it would be expected that o1 has 2 children and o4 is a
         * leaf with no parents or children.
         */

        $o1 = new TreeLeaf();
zYne's avatar
zYne committed
87
        $o1->name = 'o1';
88 89 90
        $o1->save();

        $o2 = new TreeLeaf();
zYne's avatar
zYne committed
91
        $o2->name   = 'o2';
92 93 94 95
        $o2->Parent = $o1;
        $o2->save();

        $o3 = new TreeLeaf();
zYne's avatar
zYne committed
96
        $o3->name   = 'o3';
97 98 99
        $o3->Parent = $o1;
        $o3->save();

zYne's avatar
zYne committed
100
        //$o1->refresh();
101 102

        $o4 = new TreeLeaf();
zYne's avatar
zYne committed
103
        $o4->name = 'o4';
104 105
        $o4->save();

zYne's avatar
zYne committed
106
        $o1->Children;
107 108 109 110 111
        $this->assertFalse(isset($o1->Parent));
        $this->assertTrue(count($o1->Children) == 2);
        $this->assertTrue(count($o1->get('Children')) == 2);

        $this->assertTrue(isset($o2->Parent));
112
        $this->assertTrue($o2->Parent === $o1);
113 114 115 116

        $this->assertTrue(count($o4->Children) == 0);
        $this->assertFalse(isset($o4->Parent));
    }
zYne's avatar
zYne committed
117 118 119 120 121 122 123 124 125
    public function testTreeStructureFetchingWorksWithDql()
    {
        $q = new Doctrine_Query();
        $q->select('l.*, c.*')
          ->from('TreeLeaf l, l.Children c')
          ->where('l.parent_id IS NULL')
          ->groupby('l.id, c.id');

        $coll = $q->execute();
126

zYne's avatar
zYne committed
127 128
    }
}
129