JoinTestCase.php 4.08 KB
Newer Older
zYne's avatar
zYne committed
1 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
<?php
/*
 *  $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_Query_JoinCondition_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$
 */
zYne's avatar
zYne committed
33
class Doctrine_Query_Join_TestCase extends Doctrine_UnitTestCase
zYne's avatar
zYne committed
34
{
zYne's avatar
zYne committed
35 36 37 38 39
    public function prepareTables()
    {
    }
    public function prepareData()
    {
zYne's avatar
zYne committed
40
    }
zYne's avatar
zYne committed
41 42

    public function testInitData()
zYne's avatar
zYne committed
43 44
    {
        $c = new Record_Country();
zYne's avatar
zYne committed
45

zYne's avatar
zYne committed
46 47 48 49 50
        $c->name = 'Some country';

        $c->City[0]->name = 'City 1';
        $c->City[1]->name = 'City 2';
        $c->City[2]->name = 'City 3';
zYne's avatar
zYne committed
51

zYne's avatar
zYne committed
52 53
        $c->City[0]->District->name = 'District 1';
        $c->City[2]->District->name = 'District 2';
zYne's avatar
zYne committed
54

zYne's avatar
zYne committed
55
        $c->save();
zYne's avatar
zYne committed
56

zYne's avatar
zYne committed
57 58
        $this->connection->clear();
    }
zYne's avatar
zYne committed
59
    public function testRecordHydrationWorksWithDeeplyNestedStructures()
zYne's avatar
zYne committed
60 61
    {
        $q = new Doctrine_Query();
zYne's avatar
zYne committed
62

zYne's avatar
zYne committed
63 64 65
        $q->select('c.*, c2.*, d.*')
          ->from('Record_Country c')->leftJoin('c.City c2')->leftJoin('c2.District d')
          ->where('c.id = ?', array(1));
zYne's avatar
zYne committed
66

zYne's avatar
zYne committed
67
        $this->assertEqual($q->getQuery(), "SELECT r.id AS r__id, r.name AS r__name, r2.id AS r2__id, r2.name AS r2__name, r2.country_id AS r2__country_id, r2.district_id AS r2__district_id, r3.id AS r3__id, r3.name AS r3__name FROM record__country r LEFT JOIN record__city r2 ON r.id = r2.country_id LEFT JOIN record__district r3 ON r2.district_id = r3.id WHERE r.id = ?");
zYne's avatar
zYne committed
68

zYne's avatar
zYne committed
69 70 71 72 73 74 75 76 77
        $countries = $q->execute();
        $c = $countries[0];
        $this->assertEqual($c->City[0]->name, 'City 1');
        $this->assertEqual($c->City[1]->name, 'City 2');
        $this->assertEqual($c->City[2]->name, 'City 3');

        $this->assertEqual($c->City[0]->District->name, 'District 1');
        $this->assertEqual($c->City[2]->District->name, 'District 2');
    }
zYne's avatar
zYne committed
78
    public function testManyToManyJoinUsesProperTableAliases()
79 80 81 82 83 84 85
    {
        $q = new Doctrine_Query();

        $q->select('u.name')->from('User u INNER JOIN u.Group g');

        $this->assertEqual($q->getQuery(), 'SELECT e.id AS e__id, e.name AS e__name FROM entity e INNER JOIN groupuser g ON e.id = g.user_id INNER JOIN entity e2 ON e2.id = g.group_id WHERE (e.type = 0 AND (e2.type = 1 OR e2.type IS NULL))');
    }
zYne's avatar
zYne committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104

    public function testSelfReferentialAssociationJoinsAreSupported()
    {
        $q = new Doctrine_Query();

        $q->select('e.name')->from('Entity e INNER JOIN e.Entity e2');
        
        $this->assertEqual($q->getQuery(), 'SELECT e.id AS e__id, e.name AS e__name FROM entity e INNER JOIN entity_reference e3 ON e.id = e3.entity1 OR e.id = e3.entity2 INNER JOIN entity e2 ON e2.id = e3.entity2 OR e2.id = e3.entity1');
    }
    /**
    public function testSelfReferentialNestJoin()
    {
        $nest = new NestTest();
        $rel = $nest->getTable()->getRelation('Parents');

        $this->assertEqual($rel->getLocal(), 'parent_id');
        $this->assertTrue($rel instanceof Doctrine_Association);
    }
    */
zYne's avatar
zYne committed
105
}