JoinCondition2TestCase.php 4.53 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
<?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
19
 * <http://www.phpdoctrine.org>.
20 21 22 23 24 25 26 27 28
 */

/**
 * Doctrine_Query_JoinCondition2_TestCase
 *
 * @package     Doctrine
 * @author      Guilherme Blanco <guilhermeblanco@hotmail.com>
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @category    Object Relational Mapping
29
 * @link        www.phpdoctrine.org
30 31 32 33 34 35 36
 * @since       1.0
 * @version     $Revision$
 */
class Doctrine_Query_JoinCondition2_TestCase extends Doctrine_UnitTestCase 
{
    public function prepareTables()
    {
37
        $this->tables = array('User', 'Groupuser');
38 39 40 41 42 43
        parent::prepareTables();
    }


    public function prepareData()
    {
44 45 46
        $this->conn->clear('User');
        $this->conn->clear('Group');
        $this->conn->clear('Groupuser');
47 48 49 50 51
        
        $zYne = new User();
        $zYne->name = 'zYne';
        $zYne->save();
        
52 53 54
        $groups = new Doctrine_Collection('Group');
        $groups[0]->name = 'PHP Users';
        $groups[1]->name = 'Developers';
55 56 57
        //$groups->save();
        
        $zYne->Group = $groups;
58
        $zYne->save();
59 60 61 62 63 64 65 66 67 68
        
        /*$q = new Doctrine_Query($this->connection);
        $q->select('g.*')->from('Groupuser g');
        //echo $q->getSql();
        var_dump($q->execute(array(), Doctrine::HYDRATE_ARRAY));
        echo "<br /><br />";*/
        
        //$q = new Doctrine_Query($this->connection);
        //$q->select('u.id, g.id')->from('User u')->leftJoin('u.Group g')->where('u.name = ?', 'zYne');
        //var_dump($q->execute(array(), Doctrine::HYDRATE_ARRAY));
69 70
    }

romanb's avatar
romanb committed
71
    public function testJoinConditionsArgumentsLeftJoins()
72 73 74
    {
        $q = new Doctrine_Query($this->connection);

75
        $q->select('u.id, g.id')->from('User u')->leftJoin('u.Group g WITH g.name = ?', 'Developers')->where('u.name = ?', 'zYne');
76

romanb's avatar
romanb committed
77 78
	    $this->assertEqual($q->getQuery(), "SELECT e.id AS e__id, e2.id AS e2__id FROM entity e"
	            . " LEFT JOIN groupuser g ON e.id = g.user_id LEFT JOIN entity e2 ON e2.id = g.group_id"
79
	            . " AND e2.name = ? WHERE e.name = ? AND (e.type = 0 AND (e2.type = 1 OR e2.type IS NULL))");
80 81 82 83 84

        $rs = $q->execute();

        // Should only find zYne
        $this->assertEqual($rs->count(), 1);
85
        
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
        // Grab the number of runned queries
        $queryCount = $this->connection->count();

        // Only one Group fetched for zYne
        $this->assertEqual($rs[0]->Group->count(), 1);

        // Check if it executed any other query
        $bug = ($this->connection->count() - $queryCount);

        // Should return 0 (no more queries executed)
        $this->assertEqual($bug, 0);
    }

    public function testJoinCondifitionsArgumentsInnerJoins()
    {
        $q = new Doctrine_Query($this->connection);

103
        $q->select('u.id, g.id')->from('User u')->innerJoin('u.Group g WITH g.name = ?', 'Developers')->where('u.name = ?', 'zYne');
104

romanb's avatar
romanb committed
105 106
	    $this->assertEqual($q->getQuery(), "SELECT e.id AS e__id, e2.id AS e2__id FROM entity e"
	            . " INNER JOIN groupuser g ON e.id = g.user_id INNER JOIN entity e2 ON e2.id = g.group_id"
107
	            . " AND e2.name = ? WHERE e.name = ? AND (e.type = 0 AND (e2.type = 1 OR e2.type IS NULL))");
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126

        $rs = $q->execute();

        // Should only find zYne
        $this->assertEqual($rs->count(), 1);

        // Grab the number of runned queries
        $queryCount = $this->connection->count();

        // Only one Group fetched for zYne
        $this->assertEqual($rs[0]->Group->count(), 1);

        // Check if it executed any other query
        $bug = ($this->connection->count() - $queryCount);

        // Should return 0 (no more queries executed)
        $this->assertEqual($bug, 0);
    }
}