QueryLimitTestCase.php 10.4 KB
Newer Older
zYne's avatar
zYne committed
1 2
<?php
class Doctrine_Query_Limit_TestCase extends Doctrine_UnitTestCase {
3 4 5 6
    public function prepareTables() {
        $this->tables[] = "Photo";
        $this->tables[] = "Tag";
        $this->tables[] = "Phototag";
zYne's avatar
zYne committed
7

8 9
        parent::prepareTables();
    }
zYne's avatar
zYne committed
10

zYne's avatar
zYne committed
11
    public function testLimitWithOneToOneLeftJoin() {
zYne's avatar
zYne committed
12
        $q = new Doctrine_Query($this->connection);
13
        $q->select('u.id, e.*')->from('User u, u.Email e')->limit(5);
zYne's avatar
zYne committed
14 15 16

        $users = $q->execute();
        $this->assertEqual($users->count(), 5);
17
        $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
18 19 20

    }
    public function testLimitWithOneToOneInnerJoin() {
zYne's avatar
zYne committed
21
        $q = new Doctrine_Query($this->connection);
22
        $q->select('u.id, e.*')->from('User u, u:Email e')->limit(5);
zYne's avatar
zYne committed
23 24 25

        $users = $q->execute();
        $this->assertEqual($users->count(), 5);
26
        $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
27 28
    }
    public function testLimitWithOneToManyLeftJoin() {
29
        $this->query->select('u.id, p.*')->from('User u, u.Phonenumber p')->limit(5);
zYne's avatar
zYne committed
30 31

        $sql = $this->query->getQuery();
32 33
        $this->assertEqual($this->query->getQuery(), 
        '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 WHERE (e2.type = 0) LIMIT 5) AND (e.type = 0)');
zYne's avatar
zYne committed
34

zYne's avatar
zYne committed
35 36 37 38 39
        $users = $this->query->execute();
        $count = $this->dbh->count();
        $this->assertEqual($users->count(), 5);
        $users[0]->Phonenumber[0];
        $this->assertEqual($count, $this->dbh->count());
40 41
        

zYne's avatar
zYne committed
42 43 44 45 46 47 48 49 50 51


        $this->query->offset(2);

        $users = $this->query->execute();
        $count = $this->dbh->count();
        $this->assertEqual($users->count(), 5);
        $users[3]->Phonenumber[0];
        $this->assertEqual($count, $this->dbh->count());
    }
52

zYne's avatar
zYne committed
53
    public function testLimitWithOneToManyLeftJoinAndCondition() {
zYne's avatar
zYne committed
54
        $q = new Doctrine_Query($this->connection);
zYne's avatar
zYne committed
55
        $q->from("User(name)")->where("User.Phonenumber.phonenumber LIKE '%123%'")->limit(5);
56

zYne's avatar
zYne committed
57
        $users = $q->execute();
58 59 60

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

zYne's avatar
zYne committed
61 62 63 64 65 66 67
        $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(),
68
        "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)");
69 70
    }

71

zYne's avatar
zYne committed
72
    public function testLimitWithOneToManyLeftJoinAndOrderBy() {
zYne's avatar
zYne committed
73
        $q = new Doctrine_Query($this->connection);
zYne's avatar
zYne committed
74
        $q->from("User(name)")->where("User.Phonenumber.phonenumber LIKE '%123%'")->orderby("User.Email.address")->limit(5);
zYne's avatar
zYne committed
75 76
        

zYne's avatar
zYne committed
77 78 79 80 81 82 83 84 85 86
        $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);
    }
87

zYne's avatar
zYne committed
88
    public function testLimitWithOneToManyInnerJoin() {
89
        $this->query->select('u.id, p.*')->from('User u INNER JOIN u.Phonenumber p');
zYne's avatar
zYne committed
90 91
        $this->query->limit(5);

92

zYne's avatar
zYne committed
93
        $sql = $this->query->getQuery();
zYne's avatar
zYne committed
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108

        $users = $this->query->execute();
        $count = $this->dbh->count();
        $this->assertEqual($users->count(), 5);
        $users[0]->Phonenumber[0];
        $this->assertEqual($count, $this->dbh->count());


        $this->query->offset(2);

        $users = $this->query->execute();
        $count = $this->dbh->count();
        $this->assertEqual($users->count(), 5);
        $users[3]->Phonenumber[0];
        $this->assertEqual($count, $this->dbh->count());
zYne's avatar
zYne committed
109
        
110
        $this->assertEqual($this->query->getQuery(),
111
        '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
112
    }
113

114
    public function testLimitWithPreparedQueries() {
115
        $q = new Doctrine_Query();
116 117
        $q->select('u.id, p.id')->from('User u LEFT JOIN u.Phonenumber p');
        $q->where("u.name = ?");
118 119 120 121 122 123 124
        $q->limit(5);
        $users = $q->execute(array('zYne'));
        
        $this->assertEqual($users->count(), 1);
        $count = $this->dbh->count();
        $users[0]->Phonenumber[0];
        $this->assertEqual($count, $this->dbh->count());
125

126
        $this->assertEqual($q->getQuery(),
127
        '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 WHERE e2.name = ? AND (e2.type = 0) LIMIT 5) AND e.name = ? AND (e.type = 0)');
128 129

        $q = new Doctrine_Query();
130
        $q->select('u.id, p.id')->from('User u LEFT JOIN u.Phonenumber p');
131 132 133 134
        $q->where("User.name LIKE ? || User.name LIKE ?");
        $q->limit(5);
        $users = $q->execute(array('%zYne%', '%Arnold%'));
        $this->assertEqual($users->count(), 2);
135 136


137 138 139 140 141
        $count = $this->dbh->count();
        $users[0]->Phonenumber[0];
        $this->assertEqual($count, $this->dbh->count());

        $this->assertEqual($q->getQuery(),
142 143 144 145
        "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 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)");

    } 

146 147 148 149 150 151 152 153 154 155


    public function testConnectionFlushing() {
        $q = new Doctrine_Query();
        $q->from("User(id).Phonenumber(id)");
        $q->where("User.name = ?");
        $q->limit(5);
        $users = $q->execute(array('zYne'));
        
        $this->assertEqual($users->count(), 1);
zYne's avatar
zYne committed
156
        $this->connection->flush();
157
    }
158

159

160
    public function testLimitWithManyToManyColumnAggInheritanceLeftJoin() {
zYne's avatar
zYne committed
161
        $q = new Doctrine_Query($this->connection);
zYne's avatar
zYne committed
162
        $q->from("User.Group")->limit(5);
163

164
        $users = $q->execute();
165
 
166 167

        $this->assertEqual($users->count(), 5);
168 169 170 171 172 173 174 175 176 177 178 179 180
        
        $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
181
        $this->connection->flush();
182 183 184 185 186 187 188

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



        $q = new Doctrine_Query();
189 190
        $q->from("User")->where("User.Group.id = ?")->orderby("User.id ASC")->limit(5);

191

192 193 194 195
        $users = $q->execute(array($user->Group[1]->id));

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

zYne's avatar
zYne committed
196
        $this->connection->clear();
197 198 199 200 201
        $q = new Doctrine_Query();
        $q->from("User")->where("User.Group.id = ?")->orderby("User.id DESC");
        $users = $q->execute(array($user->Group[1]->id));

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

204
    public function testLimitAttribute() {
zYne's avatar
zYne committed
205
        $this->manager->setAttribute(Doctrine::ATTR_QUERY_LIMIT, Doctrine::LIMIT_ROWS);
206 207 208 209 210
        
        $this->connection->clear();
        $q = new Doctrine_Query();
        $q->from("User")->where("User.Group.id = ?")->orderby("User.id DESC")->limit(5);
        $users = $q->execute(array(3));
zYne's avatar
zYne committed
211

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

214
        $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 ON e.id = groupuser.user_id LEFT JOIN entity e2 ON e2.id = groupuser.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");
215 216 217

        $this->manager->setAttribute(Doctrine::ATTR_QUERY_LIMIT, Doctrine::LIMIT_RECORDS);
    }
zYne's avatar
zYne committed
218

219 220 221 222 223
    public function testLimitWithManyToManyAndColumnAggregationInheritance() {
        $q = new Doctrine_Query();
        $q->from('User u, u.Group g')->where('g.id > 1')->orderby('u.name DESC')->limit(10); 

    }
224
    public function testLimitWithNormalManyToMany() {
zYne's avatar
zYne committed
225
        $coll = new Doctrine_Collection($this->connection->getTable("Photo"));
226 227 228 229 230 231 232 233 234 235
        $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
236
        $this->connection->flush();
zYne's avatar
zYne committed
237

238 239
        $q = new Doctrine_Query();
        $q->from("Photo")->where("Photo.Tag.id = ?")->orderby("Photo.id DESC")->limit(100);
240

241 242 243
        $photos = $q->execute(array(1));
        $this->assertEqual($photos->count(), 3);
        $this->assertEqual($q->getQuery(), 
244 245
        "SELECT p.id AS p__id, p.name AS p__name FROM photo p LEFT JOIN phototag ON p.id = phototag.photo_id LEFT JOIN tag t ON t.id = phototag.tag_id WHERE p.id IN (SELECT DISTINCT p2.id FROM photo p2 LEFT JOIN phototag ON p2.id = phototag.photo_id LEFT JOIN tag t2 ON t2.id = phototag.tag_id WHERE t2.id = ? ORDER BY p2.id DESC LIMIT 100) AND t.id = ? ORDER BY p.id DESC");

246
    }
247

zYne's avatar
zYne committed
248 249
}
?>