LimitTestCase.php 12.5 KB
Newer Older
zYne's avatar
zYne committed
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 35 36 37 38
/*
 *  $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_Limit_TestCase
 *
 * This test case is used for testing DQL LIMIT clause
 *
 * @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_Limit_TestCase extends Doctrine_UnitTestCase 
{
    public function prepareTables() 
    {
39 40 41
        $this->tables[] = "Photo";
        $this->tables[] = "Tag";
        $this->tables[] = "Phototag";
zYne's avatar
zYne committed
42

43 44
        parent::prepareTables();
    }
zYne's avatar
zYne committed
45 46
    /**
    public function testLimitWithOneToOneLeftJoin()
zYne's avatar
zYne committed
47
    {
zYne's avatar
zYne committed
48
        $q = new Doctrine_Query();
49
        $q->select('u.id, e.*')->from('User u, u.Email e')->limit(5);
zYne's avatar
zYne committed
50 51 52

        $users = $q->execute();
        $this->assertEqual($users->count(), 5);
53
        $this->assertEqual($q->getQuery(), "SELECT e.id AS e__id, e2.id AS e2__id, e2.address AS e2__address FROM entity e LEFT JOIN email e2 ON e.email_id = e2.id WHERE (e.type = 0) LIMIT 5");
zYne's avatar
zYne committed
54 55

    }
zYne's avatar
zYne committed
56
    public function testLimitWithOneToOneInnerJoin()
zYne's avatar
zYne committed
57
    {
zYne's avatar
zYne committed
58
        $q = new Doctrine_Query();
59
        $q->select('u.id, e.*')->from('User u, u:Email e')->limit(5);
zYne's avatar
zYne committed
60 61 62

        $users = $q->execute();
        $this->assertEqual($users->count(), 5);
63
        $this->assertEqual($q->getQuery(), "SELECT e.id AS e__id, e2.id AS e2__id, e2.address AS e2__address FROM entity e INNER JOIN email e2 ON e.email_id = e2.id WHERE (e.type = 0) LIMIT 5");
zYne's avatar
zYne committed
64
    }
zYne's avatar
zYne committed
65
    */
zYne's avatar
zYne committed
66 67
    public function testLimitWithOneToManyLeftJoin() 
    {
zYne's avatar
zYne committed
68
        $q = new Doctrine_Query();
zYne's avatar
zYne committed
69
        $q->select('u.id, p.*')->from('User u, u.Phonenumber p')->limit(5);
zYne's avatar
zYne committed
70

zYne's avatar
zYne committed
71
        $sql = $q->getQuery();
zYne's avatar
zYne committed
72

zYne's avatar
zYne committed
73
        $this->assertEqual($q->getQuery(), 
zYne's avatar
zYne committed
74
        'SELECT e.id AS e__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.id IN (SELECT DISTINCT e2.id FROM entity e2 LEFT JOIN phonenumber p2 ON e2.id = p2.entity_id WHERE (e2.type = 0) LIMIT 5) AND (e.type = 0)');
zYne's avatar
zYne committed
75

zYne's avatar
zYne committed
76
        $users = $q->execute();
77
        $count = $this->conn->count();
zYne's avatar
zYne committed
78 79
        $this->assertEqual($users->count(), 5);
        $users[0]->Phonenumber[0];
80
        $this->assertEqual($count, $this->conn->count());
zYne's avatar
zYne committed
81

zYne's avatar
zYne committed
82
        $q->offset(2);
zYne's avatar
zYne committed
83

zYne's avatar
zYne committed
84
        $users = $q->execute();
85
        $count = $this->conn->count();
zYne's avatar
zYne committed
86 87
        $this->assertEqual($users->count(), 5);
        $users[3]->Phonenumber[0];
88
        $this->assertEqual($count, $this->conn->count());
zYne's avatar
zYne committed
89
    }
90

zYne's avatar
zYne committed
91 92
    public function testLimitWithOneToManyLeftJoinAndCondition() 
    {
zYne's avatar
zYne committed
93
        $q = new Doctrine_Query();
zYne's avatar
zYne committed
94
        $q->select('User.name')->from('User')->where("User.Phonenumber.phonenumber LIKE '%123%'")->limit(5);
95

zYne's avatar
zYne committed
96
        $users = $q->execute();
97 98 99

        $this->assertEqual($users->count(), 5);

zYne's avatar
zYne committed
100 101 102 103 104 105 106
        $this->assertEqual($users[0]->name, 'zYne');
        $this->assertEqual($users[1]->name, 'Arnold Schwarzenegger');
        $this->assertEqual($users[2]->name, 'Michael Caine');
        $this->assertEqual($users[3]->name, 'Sylvester Stallone');
        $this->assertEqual($users[4]->name, 'Jean Reno');

        $this->assertEqual($q->getQuery(),
107
        "SELECT e.id AS e__id, e.name AS e__name FROM entity e LEFT JOIN phonenumber p ON e.id = p.entity_id WHERE e.id IN (SELECT DISTINCT e2.id FROM entity e2 LEFT JOIN phonenumber p2 ON e2.id = p2.entity_id WHERE p2.phonenumber LIKE '%123%' AND (e2.type = 0) LIMIT 5) AND p.phonenumber LIKE '%123%' AND (e.type = 0)");
108 109
    }

110

zYne's avatar
zYne committed
111 112
    public function testLimitWithOneToManyLeftJoinAndOrderBy() 
    {
zYne's avatar
zYne committed
113
        $q = new Doctrine_Query();
zYne's avatar
zYne committed
114
        $q->select('User.name')->from('User')->where("User.Phonenumber.phonenumber LIKE '%123%'")->orderby('User.Email.address')->limit(5);
zYne's avatar
zYne committed
115 116
        

zYne's avatar
zYne committed
117 118 119 120 121 122 123 124 125 126
        $users = $q->execute();

        $this->assertEqual($users[0]->name, 'Arnold Schwarzenegger');
        $this->assertEqual($users[1]->name, 'Michael Caine');
        $this->assertEqual($users[2]->name, 'Jean Reno');
        $this->assertEqual($users[3]->name, 'Sylvester Stallone');
        $this->assertEqual($users[4]->name, 'zYne');

        $this->assertEqual($users->count(), 5);
    }
127

zYne's avatar
zYne committed
128 129
    public function testLimitWithOneToManyInnerJoin() 
    {
zYne's avatar
zYne committed
130
        $q = new Doctrine_Query();
zYne's avatar
zYne committed
131 132
        $q->select('u.id, p.*')->from('User u INNER JOIN u.Phonenumber p');
        $q->limit(5);
zYne's avatar
zYne committed
133

134

zYne's avatar
zYne committed
135
        $sql = $q->getQuery();
zYne's avatar
zYne committed
136

zYne's avatar
zYne committed
137
        $users = $q->execute();
138
        $count = $this->conn->count();
zYne's avatar
zYne committed
139 140
        $this->assertEqual($users->count(), 5);
        $users[0]->Phonenumber[0];
141
        $this->assertEqual($count, $this->conn->count());
zYne's avatar
zYne committed
142 143


zYne's avatar
zYne committed
144
        $q->offset(2);
zYne's avatar
zYne committed
145

zYne's avatar
zYne committed
146
        $users = $q->execute();
147
        $count = $this->conn->count();
zYne's avatar
zYne committed
148 149
        $this->assertEqual($users->count(), 5);
        $users[3]->Phonenumber[0];
150
        $this->assertEqual($count, $this->conn->count());
zYne's avatar
zYne committed
151
        
zYne's avatar
zYne committed
152
        $this->assertEqual($q->getQuery(),
153
        'SELECT e.id AS e__id, p.id AS p__id, p.phonenumber AS p__phonenumber, p.entity_id AS p__entity_id FROM entity e INNER JOIN phonenumber p ON e.id = p.entity_id WHERE e.id IN (SELECT DISTINCT e2.id FROM entity e2 INNER JOIN phonenumber p2 ON e2.id = p2.entity_id WHERE (e2.type = 0) LIMIT 5 OFFSET 2) AND (e.type = 0)');
zYne's avatar
zYne committed
154
    }
155

zYne's avatar
zYne committed
156 157
    public function testLimitWithPreparedQueries() 
    {
zYne's avatar
zYne committed
158
        $q = new Doctrine_Query();
159
        $q->select('u.id, p.id')->from('User u LEFT JOIN u.Phonenumber p');
zYne's avatar
zYne committed
160
        $q->where('u.name = ?');
161 162 163 164
        $q->limit(5);
        $users = $q->execute(array('zYne'));
        
        $this->assertEqual($users->count(), 1);
165
        $count = $this->conn->count();
166
        $users[0]->Phonenumber[0];
167
        $this->assertEqual($count, $this->conn->count());
168

169
        $this->assertEqual($q->getQuery(),
zYne's avatar
zYne committed
170
        'SELECT e.id AS e__id, p.id AS p__id FROM entity e LEFT JOIN phonenumber p ON e.id = p.entity_id WHERE e.id IN (SELECT DISTINCT e2.id FROM entity e2 LEFT JOIN phonenumber p2 ON e2.id = p2.entity_id WHERE e2.name = ? AND (e2.type = 0) LIMIT 5) AND e.name = ? AND (e.type = 0)');
171

zYne's avatar
zYne committed
172
        $q = new Doctrine_Query();
173
        $q->select('u.id, p.id')->from('User u LEFT JOIN u.Phonenumber p');
zYne's avatar
zYne committed
174
        $q->where("u.name LIKE ? || u.name LIKE ?");
175
        $q->limit(5);
zYne's avatar
zYne committed
176

177 178
        $users = $q->execute(array('%zYne%', '%Arnold%'));
        $this->assertEqual($users->count(), 2);
179 180


181
        $count = $this->conn->count();
182
        $users[0]->Phonenumber[0];
183
        $this->assertEqual($count, $this->conn->count());
184 185

        $this->assertEqual($q->getQuery(),
zYne's avatar
zYne committed
186 187 188 189
        "SELECT e.id AS e__id, p.id AS p__id FROM entity e LEFT JOIN phonenumber p ON"
        . " e.id = p.entity_id WHERE e.id IN (SELECT DISTINCT e2.id FROM entity e2 LEFT JOIN phonenumber p2"
        . " ON e2.id = p2.entity_id WHERE (e2.name LIKE ? OR e2.name LIKE ?) AND (e2.type = 0) LIMIT 5) AND "
        . "(e.name LIKE ? OR e.name LIKE ?) AND (e.type = 0)");
zYne's avatar
zYne committed
190
    }
zYne's avatar
zYne committed
191

zYne's avatar
zYne committed
192 193
    public function testConnectionFlushing() 
    {
zYne's avatar
zYne committed
194
        $q = new Doctrine_Query();
zYne's avatar
zYne committed
195 196
        $q->from('User.Phonenumber');
        $q->where('User.name = ?');
197
        $q->limit(5);
zYne's avatar
zYne committed
198

199 200 201
        $users = $q->execute(array('zYne'));
        
        $this->assertEqual($users->count(), 1);
zYne's avatar
zYne committed
202
        //$this->connection->flush();
203
    }
zYne's avatar
zYne committed
204 205
    public function testLimitWithManyToManyColumnAggInheritanceLeftJoin() 
    {
zYne's avatar
zYne committed
206
        $q = new Doctrine_Query();
zYne's avatar
zYne committed
207
        $q->from('User.Group')->limit(5);
208

209 210 211
        $users = $q->execute();

        $this->assertEqual($users->count(), 5);
212 213 214 215 216 217 218 219 220 221 222 223
        
        $user = $this->objTable->find(5);
        $user->Group[1]->name = "Tough guys inc.";
        $user->Group[2]->name = "Terminators";
        
        $user2 = $this->objTable->find(4);
        $user2->Group = $user->Group;
        
        $user3 = $this->objTable->find(6);
        $user3->Group = $user->Group;

        $this->assertEqual($user->Group[0]->name, "Action Actors");
zYne's avatar
zYne committed
224 225
        $this->assertEqual(count($user->Group), 3);

zYne's avatar
zYne committed
226
        $this->connection->flush();
227 228 229 230 231 232

        $this->assertEqual($user->Group[0]->name, "Action Actors");
        $this->assertEqual(count($user->Group), 3);



zYne's avatar
zYne committed
233
        $q = new Doctrine_Query();
234 235
        $q->from("User")->where("User.Group.id = ?")->orderby("User.id ASC")->limit(5);

236

237 238 239 240
        $users = $q->execute(array($user->Group[1]->id));

        $this->assertEqual($users->count(), 3);

zYne's avatar
zYne committed
241
        $this->connection->clear();
zYne's avatar
zYne committed
242
        $q = new Doctrine_Query();
zYne's avatar
zYne committed
243
        $q->from('User')->where('User.Group.id = ?')->orderby('User.id DESC');
244 245 246
        $users = $q->execute(array($user->Group[1]->id));

        $this->assertEqual($users->count(), 3);
zYne's avatar
zYne committed
247
    }
248

zYne's avatar
zYne committed
249 250
    public function testLimitAttribute() 
    {
zYne's avatar
zYne committed
251
        $this->manager->setAttribute(Doctrine::ATTR_QUERY_LIMIT, Doctrine::LIMIT_ROWS);
252 253
        
        $this->connection->clear();
zYne's avatar
zYne committed
254
        $q = new Doctrine_Query();
255
        $q->from('User')->where('User.Group.id = ?')->orderby('User.id DESC')->limit(5);
zYne's avatar
zYne committed
256
        $users = $q->execute(array(12));
zYne's avatar
zYne committed
257

258
        $this->assertEqual($users->count(), 3);
zYne's avatar
zYne committed
259

260
        $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 LEFT JOIN groupuser g ON e.id = g.user_id LEFT JOIN entity e2 ON e2.id = g.group_id WHERE e2.id = ? AND (e.type = 0 AND (e2.type = 1 OR e2.type IS NULL)) ORDER BY e.id DESC LIMIT 5");
261 262
        $this->manager->setAttribute(Doctrine::ATTR_QUERY_LIMIT, Doctrine::LIMIT_RECORDS);
    }
zYne's avatar
zYne committed
263

zYne's avatar
zYne committed
264 265
    public function testLimitWithManyToManyAndColumnAggregationInheritance() 
    {
zYne's avatar
zYne committed
266
        $q = new Doctrine_Query();
267 268 269
        $q->from('User u, u.Group g')->where('g.id > 1')->orderby('u.name DESC')->limit(10); 

    }
zYne's avatar
zYne committed
270 271
    public function testLimitWithNormalManyToMany() 
    {
zYne's avatar
zYne committed
272
        $coll = new Doctrine_Collection($this->connection->getTable("Photo"));
273 274 275 276 277 278 279 280 281 282
        $tag = new Tag();
        $tag->tag = "Some tag";
        $coll[0]->Tag[0] = $tag;
        $coll[0]->name = "photo 1";
        $coll[1]->Tag[0] = $tag;
        $coll[1]->name = "photo 2";
        $coll[2]->Tag[0] = $tag;
        $coll[2]->name = "photo 3";
        $coll[3]->Tag[0]->tag = "Other tag";
        $coll[3]->name = "photo 4";
zYne's avatar
zYne committed
283
        $this->connection->flush();
zYne's avatar
zYne committed
284

zYne's avatar
zYne committed
285
        $q = new Doctrine_Query();
zYne's avatar
zYne committed
286
        $q->from('Photo')->where('Photo.Tag.id = ?')->orderby('Photo.id DESC')->limit(100);
287

288
        $photos = $q->execute(array(1));
289
        $this->assertEqual($photos->count(), 3);            
290
        $this->assertEqual($q->getQuery(), 
291
        "SELECT p.id AS p__id, p.name AS p__name FROM photo p LEFT JOIN phototag p2 ON p.id = p2.photo_id LEFT JOIN tag t ON t.id = p2.tag_id WHERE p.id IN (SELECT DISTINCT p3.id FROM photo p3 LEFT JOIN phototag p4 ON p3.id = p4.photo_id LEFT JOIN tag t2 ON t2.id = p4.tag_id WHERE t2.id = ? ORDER BY p3.id DESC LIMIT 100) AND t.id = ? ORDER BY p.id DESC");
292
    }
zYne's avatar
zYne committed
293

zYne's avatar
zYne committed
294 295 296 297 298 299 300 301 302 303
    public function testLimitNoticesOrderbyJoins()
    {
        $q = new Doctrine_Query();
        
        $q->from('Photo p')
          ->leftJoin('p.Tag t')
          ->orderby('t.id DESC')->limit(10);

        $this->assertEqual($q->getSql(), "SELECT p.id AS p__id, p.name AS p__name, t.id AS t__id, t.tag AS t__tag FROM photo p LEFT JOIN phototag p2 ON p.id = p2.photo_id LEFT JOIN tag t ON t.id = p2.tag_id WHERE p.id IN (SELECT DISTINCT p3.id FROM photo p3 LEFT JOIN phototag p4 ON p3.id = p4.photo_id LEFT JOIN tag t2 ON t2.id = p4.tag_id ORDER BY t2.id DESC LIMIT 10) ORDER BY t.id DESC");
    }
zYne's avatar
zYne committed
304 305
}
?>