SelectTestCase.php 9.54 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 34
/*
 *  $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_Select_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_Query_Select_TestCase extends Doctrine_UnitTestCase 
{
zYne's avatar
zYne committed
35 36 37 38 39 40 41 42 43 44 45
    public function testAggregateFunctionParsingSupportsMultipleComponentReferences()
    {
        $q = new Doctrine_Query();
        $q->select("CONCAT(u.name, ' ', e.address) value")
          ->from('User u')->innerJoin('u.Email e');

        $this->assertEqual($q->getQuery(), "SELECT CONCAT(e.name, ' ', e2.address) AS e__0 FROM entity e INNER JOIN email e2 ON e.email_id = e2.id WHERE (e.type = 0)");

        $users = $q->execute();
        $this->assertEqual($users[0]->value, 'zYne zYne@example.com');
    }
zYne's avatar
zYne committed
46

zYne's avatar
zYne committed
47 48 49 50 51
    public function testSelectDistinctIsSupported()
    {
        $q = new Doctrine_Query();
        
        $q->distinct()->select('u.name')->from('User u');
zYne's avatar
zYne committed
52

zYne's avatar
zYne committed
53 54
        $this->assertEqual($q->getSql(), "SELECT DISTINCT e.id AS e__id, e.name AS e__name FROM entity e WHERE (e.type = 0)");
    }
zYne's avatar
zYne committed
55

zYne's avatar
zYne committed
56 57 58 59 60 61 62 63
    public function testSelectDistinctIsSupported2()
    {
        $q = new Doctrine_Query();
        
        $q->select('DISTINCT u.name')->from('User u');

        $this->assertEqual($q->getSql(), "SELECT DISTINCT e.id AS e__id, e.name AS e__name FROM entity e WHERE (e.type = 0)");
    }
zYne's avatar
zYne committed
64

zYne's avatar
zYne committed
65 66
    public function testAggregateFunctionWithDistinctKeyword() 
    {
zYne's avatar
zYne committed
67
        $q = new Doctrine_Query();
68 69 70 71 72

        $q->parseQuery('SELECT COUNT(DISTINCT u.name) FROM User u');

        $this->assertEqual($q->getQuery(), 'SELECT COUNT(DISTINCT e.name) AS e__0 FROM entity e WHERE (e.type = 0)');
    }
zYne's avatar
zYne committed
73
    
zYne's avatar
zYne committed
74 75
    public function testAggregateFunction() 
    {
zYne's avatar
zYne committed
76
        $q = new Doctrine_Query();
zYne's avatar
zYne committed
77 78 79

        $q->parseQuery('SELECT COUNT(u.id) FROM User u');

80
        $this->assertEqual($q->getQuery(), 'SELECT COUNT(e.id) AS e__0 FROM entity e WHERE (e.type = 0)');
zYne's avatar
zYne committed
81
    }
82

zYne's avatar
zYne committed
83 84
    public function testSelectPartSupportsMultipleAggregateFunctions() 
    {
zYne's avatar
zYne committed
85
        $q = new Doctrine_Query();
zYne's avatar
zYne committed
86 87 88

        $q->parseQuery('SELECT MAX(u.id), MIN(u.name) FROM User u');

89
        $this->assertEqual($q->getQuery(), 'SELECT MAX(e.id) AS e__0, MIN(e.name) AS e__1 FROM entity e WHERE (e.type = 0)');
zYne's avatar
zYne committed
90
    }
zYne's avatar
zYne committed
91 92
    public function testMultipleAggregateFunctionsWithMultipleComponents() 
    {
zYne's avatar
zYne committed
93
        $q = new Doctrine_Query();
zYne's avatar
zYne committed
94 95 96

        $q->parseQuery('SELECT MAX(u.id), MIN(u.name), COUNT(p.id) FROM User u, u.Phonenumber p');

97
        $this->assertEqual($q->getQuery(), 'SELECT MAX(e.id) AS e__0, MIN(e.name) AS e__1, COUNT(p.id) AS p__2 FROM entity e LEFT JOIN phonenumber p ON e.id = p.entity_id WHERE (e.type = 0)');
zYne's avatar
zYne committed
98
    }
zYne's avatar
zYne committed
99 100
    public function testEmptySelectPart() 
    {
zYne's avatar
zYne committed
101
        $q = new Doctrine_Query();
zYne's avatar
zYne committed
102 103
        
        try {
zYne's avatar
zYne committed
104
            $q->select(null);
zYne's avatar
zYne committed
105 106 107 108 109 110
            
            $this->fail();
        } catch(Doctrine_Query_Exception $e) {
            $this->pass();
        }
    }
zYne's avatar
zYne committed
111 112
    public function testUnknownAggregateFunction() 
    {
zYne's avatar
zYne committed
113
        $q = new Doctrine_Query();
zYne's avatar
zYne committed
114 115 116
        
        try {
            $q->parseQuery('SELECT UNKNOWN(u.id) FROM User');
zYne's avatar
zYne committed
117 118
            
            $q->getQuery();
zYne's avatar
zYne committed
119 120 121 122
            $this->fail();
        } catch(Doctrine_Query_Exception $e) {
            $this->pass();
        }
zYne's avatar
zYne committed
123

zYne's avatar
zYne committed
124
    }
zYne's avatar
zYne committed
125
    public function testAggregateFunctionValueHydration()
zYne's avatar
zYne committed
126
    {
zYne's avatar
zYne committed
127
        $q = new Doctrine_Query();
zYne's avatar
zYne committed
128

zYne's avatar
zYne committed
129
        $q->parseQuery('SELECT u.id, COUNT(p.id) FROM User u LEFT JOIN u.Phonenumber p GROUP BY u.id');
zYne's avatar
zYne committed
130 131

        $users = $q->execute();
zYne's avatar
zYne committed
132 133

        $this->assertEqual($users[0]->Phonenumber[0]->COUNT, 1);
zYne's avatar
zYne committed
134

zYne's avatar
zYne committed
135 136 137 138
        $this->assertEqual($users[1]->Phonenumber[0]->COUNT, 3);
        $this->assertEqual($users[2]->Phonenumber[0]->COUNT, 1);
        $this->assertEqual($users[3]->Phonenumber[0]->COUNT, 1);
        $this->assertEqual($users[4]->Phonenumber[0]->COUNT, 3);
zYne's avatar
zYne committed
139 140
    }

zYne's avatar
zYne committed
141 142
    public function testSingleComponentWithAsterisk()
    {
zYne's avatar
zYne committed
143
        $q = new Doctrine_Query();
zYne's avatar
zYne committed
144 145 146 147 148 149 150

        $q->parseQuery('SELECT u.* FROM User u');

        $this->assertEqual($q->getQuery(), 'SELECT e.id AS e__id, e.name AS e__name, e.loginname AS e__loginname, e.password AS e__password, e.type AS e__type, e.created AS e__created, e.updated AS e__updated, e.email_id AS e__email_id FROM entity e WHERE (e.type = 0)');
    }
    public function testSingleComponentWithMultipleColumns()
    {
zYne's avatar
zYne committed
151
        $q = new Doctrine_Query();
zYne's avatar
zYne committed
152 153 154 155 156 157 158

        $q->parseQuery('SELECT u.name, u.type FROM User u'); 
        
        $this->assertEqual($q->getQuery(), 'SELECT e.id AS e__id, e.name AS e__name, e.type AS e__type FROM entity e WHERE (e.type = 0)');
    }
    public function testMultipleComponentsWithAsterisk()
    {
zYne's avatar
zYne committed
159
        $q = new Doctrine_Query();
zYne's avatar
zYne committed
160 161 162 163 164 165 166

        $q->parseQuery('SELECT u.*, p.* FROM User u, u.Phonenumber p');

        $this->assertEqual($q->getQuery(),'SELECT e.id AS e__id, e.name AS e__name, e.loginname AS e__loginname, e.password AS e__password, e.type AS e__type, e.created AS e__created, e.updated AS e__updated, e.email_id AS e__email_id, p.id AS p__id, p.phonenumber AS p__phonenumber, p.entity_id AS p__entity_id FROM entity e LEFT JOIN phonenumber p ON e.id = p.entity_id WHERE (e.type = 0)');
    }
    public function testMultipleComponentsWithMultipleColumns()
    {
zYne's avatar
zYne committed
167
        $q = new Doctrine_Query();
zYne's avatar
zYne committed
168 169

        $q->parseQuery('SELECT u.id, u.name, p.id FROM User u, u.Phonenumber p');
zYne's avatar
zYne committed
170

zYne's avatar
zYne committed
171 172 173 174 175
        $this->assertEqual($q->getQuery(),'SELECT e.id AS e__id, e.name AS e__name, p.id AS p__id FROM entity e LEFT JOIN phonenumber p ON e.id = p.entity_id WHERE (e.type = 0)');
    }
    public function testAggregateFunctionValueHydrationWithAliases()
    {

zYne's avatar
zYne committed
176
        $q = new Doctrine_Query();
zYne's avatar
zYne committed
177 178

        $q->parseQuery('SELECT u.id, COUNT(p.id) count FROM User u, u.Phonenumber p GROUP BY u.id');
zYne's avatar
zYne committed
179

zYne's avatar
zYne committed
180
        $users = $q->execute();
zYne's avatar
zYne committed
181 182 183 184 185 186

        $this->assertEqual($users[0]->Phonenumber[0]->count, 1);
        $this->assertEqual($users[1]->Phonenumber[0]->count, 3);
        $this->assertEqual($users[2]->Phonenumber[0]->count, 1);
        $this->assertEqual($users[3]->Phonenumber[0]->count, 1);
        $this->assertEqual($users[4]->Phonenumber[0]->count, 3);
zYne's avatar
zYne committed
187
    }
zYne's avatar
zYne committed
188 189
    public function testMultipleAggregateFunctionValueHydrationWithAliases()
    {
zYne's avatar
zYne committed
190
        $q = new Doctrine_Query();
zYne's avatar
zYne committed
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206

        $q->parseQuery('SELECT u.id, COUNT(p.id) count, MAX(p.phonenumber) max FROM User u, u.Phonenumber p GROUP BY u.id');

        $users = $q->execute();
        $this->assertEqual($users[0]->Phonenumber[0]->count, 1);
        $this->assertEqual($users[1]->Phonenumber[0]->count, 3);
        $this->assertEqual($users[2]->Phonenumber[0]->count, 1);
        $this->assertEqual($users[3]->Phonenumber[0]->count, 1);
        $this->assertEqual($users[4]->Phonenumber[0]->count, 3);

        $this->assertEqual($users[0]->Phonenumber[0]->max, '123 123');
        $this->assertEqual($users[1]->Phonenumber[0]->max, '789 789');
        $this->assertEqual($users[2]->Phonenumber[0]->max, '123 123');
        $this->assertEqual($users[3]->Phonenumber[0]->max, '111 222 333');
        $this->assertEqual($users[4]->Phonenumber[0]->max, '444 555');
    }
zYne's avatar
zYne committed
207 208
    public function testMultipleAggregateFunctionValueHydrationWithAliasesAndCleanRecords()
    {
zYne's avatar
zYne committed
209
        $this->connection->clear();
zYne's avatar
zYne committed
210

zYne's avatar
zYne committed
211
        $q = new Doctrine_Query();
zYne's avatar
zYne committed
212

zYne's avatar
zYne committed
213
        $q->parseQuery('SELECT u.id, COUNT(p.id) count, MAX(p.phonenumber) max FROM User u, u.Phonenumber p GROUP BY u.id');
zYne's avatar
zYne committed
214 215
        
        $this->assertEqual($q->getQuery(), 'SELECT e.id AS e__id, COUNT(p.id) AS p__0, MAX(p.phonenumber) AS p__1 FROM entity e LEFT JOIN phonenumber p ON e.id = p.entity_id WHERE (e.type = 0) GROUP BY e.id');
zYne's avatar
zYne committed
216 217 218

        $users = $q->execute();

zYne's avatar
zYne committed
219 220 221 222 223 224 225 226 227 228 229 230 231
        $this->assertEqual($users[0]->Phonenumber[0]->state(), Doctrine_Record::STATE_TDIRTY);

        $this->assertEqual($users[0]->Phonenumber[0]->count, 1);
        $this->assertEqual($users[1]->Phonenumber[0]->count, 3);
        $this->assertEqual($users[2]->Phonenumber[0]->count, 1);
        $this->assertEqual($users[3]->Phonenumber[0]->count, 1);
        $this->assertEqual($users[4]->Phonenumber[0]->count, 3);

        $this->assertEqual($users[0]->Phonenumber[0]->max, '123 123');
        $this->assertEqual($users[1]->Phonenumber[0]->max, '789 789');
        $this->assertEqual($users[2]->Phonenumber[0]->max, '123 123');
        $this->assertEqual($users[3]->Phonenumber[0]->max, '111 222 333');
        $this->assertEqual($users[4]->Phonenumber[0]->max, '444 555');
zYne's avatar
zYne committed
232
    }
233 234
}
?>