QueryLimitTestCase.php 10.6 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
    public function testLimitWithOneToOneLeftJoin() {
zYne's avatar
zYne committed
11
        $q = new Doctrine_Query($this->connection);
zYne's avatar
zYne committed
12 13 14 15
        $q->from('User(id).Email')->limit(5);

        $users = $q->execute();
        $this->assertEqual($users->count(), 5);
16
        $this->assertEqual($q->getQuery(), "SELECT entity.id AS entity__id, email.id AS email__id, email.address AS email__address FROM entity LEFT JOIN email ON entity.email_id = email.id WHERE (entity.type = 0) LIMIT 5");
zYne's avatar
zYne committed
17 18 19

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

        $users = $q->execute();
        $this->assertEqual($users->count(), 5);
25
        $this->assertEqual($q->getQuery(), "SELECT entity.id AS entity__id, email.id AS email__id, email.address AS email__address FROM entity INNER JOIN email ON entity.email_id = email.id WHERE (entity.type = 0) LIMIT 5");
zYne's avatar
zYne committed
26 27 28
    }
    public function testLimitWithOneToManyLeftJoin() {
        $this->query->from("User(id).Phonenumber");
zYne's avatar
zYne committed
29 30 31 32
        $this->query->limit(5);

        $sql = $this->query->getQuery();

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

zYne's avatar
zYne committed
40 41 42 43 44 45 46 47 48 49 50 51
        $this->assertEqual($this->query->getQuery(), 
        'SELECT entity.id AS entity__id, phonenumber.id AS phonenumber__id, phonenumber.phonenumber AS phonenumber__phonenumber, phonenumber.entity_id AS phonenumber__entity_id FROM entity LEFT JOIN phonenumber ON entity.id = phonenumber.entity_id WHERE entity.id IN (SELECT DISTINCT entity.id FROM entity WHERE (entity.type = 0) LIMIT 5) AND (entity.type = 0)');


        $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 56
        $q->from("User(name)")->where("User.Phonenumber.phonenumber LIKE '%123%'")->limit(5);
        $users = $q->execute();
zYne's avatar
zYne committed
57
        
zYne's avatar
zYne committed
58 59 60 61 62 63 64
        $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($users->count(), 5);
65

zYne's avatar
zYne committed
66 67
        $this->assertEqual($q->getQuery(),
        "SELECT entity.id AS entity__id, entity.name AS entity__name FROM entity LEFT JOIN phonenumber ON entity.id = phonenumber.entity_id WHERE entity.id IN (SELECT DISTINCT entity.id FROM entity LEFT JOIN phonenumber ON entity.id = phonenumber.entity_id WHERE phonenumber.phonenumber LIKE '%123%' AND (entity.type = 0) LIMIT 5) AND phonenumber.phonenumber LIKE '%123%' AND (entity.type = 0)");
68 69
    }

zYne's avatar
zYne committed
70
    public function testLimitWithOneToManyLeftJoinAndOrderBy() {
zYne's avatar
zYne committed
71
        $q = new Doctrine_Query($this->connection);
zYne's avatar
zYne committed
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
        $q->from("User(name)")->where("User.Phonenumber.phonenumber LIKE '%123%'")->orderby("User.Email.address")->limit(5);
        $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);
    }
    

    public function testLimitWithOneToManyInnerJoin() {
        $this->query->from("User(id):Phonenumber");
        $this->query->limit(5);

89

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

        $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
106
        
107
        $this->assertEqual($this->query->getQuery(),
zYne's avatar
zYne committed
108
        'SELECT entity.id AS entity__id, phonenumber.id AS phonenumber__id, phonenumber.phonenumber AS phonenumber__phonenumber, phonenumber.entity_id AS phonenumber__entity_id FROM entity INNER JOIN phonenumber ON entity.id = phonenumber.entity_id WHERE entity.id IN (SELECT DISTINCT entity.id FROM entity INNER JOIN phonenumber ON entity.id = phonenumber.entity_id WHERE (entity.type = 0) LIMIT 5 OFFSET 2) AND (entity.type = 0)');
zYne's avatar
zYne committed
109
    }
110
    public function testLimitWithPreparedQueries() {
111 112 113 114 115 116 117 118 119 120
        $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);
        $count = $this->dbh->count();
        $users[0]->Phonenumber[0];
        $this->assertEqual($count, $this->dbh->count());
121

122 123 124 125 126 127 128 129 130
        $this->assertEqual($q->getQuery(),
        'SELECT entity.id AS entity__id, phonenumber.id AS phonenumber__id FROM entity LEFT JOIN phonenumber ON entity.id = phonenumber.entity_id WHERE entity.id IN (SELECT DISTINCT entity.id FROM entity WHERE entity.name = ? AND (entity.type = 0) LIMIT 5) AND entity.name = ? AND (entity.type = 0)');

        $q = new Doctrine_Query();
        $q->from("User(id).Phonenumber(id)");
        $q->where("User.name LIKE ? || User.name LIKE ?");
        $q->limit(5);
        $users = $q->execute(array('%zYne%', '%Arnold%'));
        $this->assertEqual($users->count(), 2);
131 132


133 134 135 136 137 138
        $count = $this->dbh->count();
        $users[0]->Phonenumber[0];
        $this->assertEqual($count, $this->dbh->count());

        $this->assertEqual($q->getQuery(),
        "SELECT entity.id AS entity__id, phonenumber.id AS phonenumber__id FROM entity LEFT JOIN phonenumber ON entity.id = phonenumber.entity_id WHERE entity.id IN (SELECT DISTINCT entity.id FROM entity WHERE (entity.name LIKE ? OR entity.name LIKE ?) AND (entity.type = 0) LIMIT 5) AND (entity.name LIKE ? OR entity.name LIKE ?) AND (entity.type = 0)");
139 140 141 142 143 144 145 146 147 148 149

    }    

    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
150
        $this->connection->flush();
151
    }
152

153
    public function testLimitWithManyToManyColumnAggInheritanceLeftJoin() {
zYne's avatar
zYne committed
154
        $q = new Doctrine_Query($this->connection);
zYne's avatar
zYne committed
155
        $q->from("User.Group")->limit(5);
156 157 158
        $users = $q->execute();

        $this->assertEqual($users->count(), 5);
159 160 161 162 163 164 165 166 167 168 169 170 171
        
        $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
172
        $this->connection->flush();
173 174 175 176 177 178 179 180 181 182 183 184

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



        $q = new Doctrine_Query();
        $q->from("User")->where("User.Group.id = ?")->orderby("User.id DESC")->limit(5);
        $users = $q->execute(array($user->Group[1]->id));

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

zYne's avatar
zYne committed
185
        $this->connection->clear();
186 187 188 189 190
        $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
191
    }
192 193 194 195 196 197 198 199 200 201 202 203 204 205
    public function testLimitAttribute() {
        $this->manager->setAttribute(Doctrine::ATTR_QUERY_LIMIT, Doctrine::LIMIT_ROWS);
        
        $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));

        $this->assertEqual($users->count(), 3);
        
        $this->assertEqual($q->getQuery(), "SELECT entity.id AS entity__id, entity.name AS entity__name, entity.loginname AS entity__loginname, entity.password AS entity__password, entity.type AS entity__type, entity.created AS entity__created, entity.updated AS entity__updated, entity.email_id AS entity__email_id FROM entity LEFT JOIN groupuser ON entity.id = groupuser.user_id LEFT JOIN entity AS entity2 ON entity2.id = groupuser.group_id WHERE entity2.id = ? AND (entity.type = 0 AND (entity2.type = 1 OR entity2.type IS NULL)) ORDER BY entity.id DESC LIMIT 5");

        $this->manager->setAttribute(Doctrine::ATTR_QUERY_LIMIT, Doctrine::LIMIT_RECORDS);
    }
206
    public function testLimitWithNormalManyToMany() {
zYne's avatar
zYne committed
207
        $coll = new Doctrine_Collection($this->connection->getTable("Photo"));
208 209 210 211 212 213 214 215 216 217
        $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
218
        $this->connection->flush();
zYne's avatar
zYne committed
219

220 221 222 223 224 225 226
        $q = new Doctrine_Query();
        $q->from("Photo")->where("Photo.Tag.id = ?")->orderby("Photo.id DESC")->limit(100);
        $photos = $q->execute(array(1));
        $this->assertEqual($photos->count(), 3);
        $this->assertEqual($q->getQuery(), 
        "SELECT photo.id AS photo__id, photo.name AS photo__name FROM photo LEFT JOIN phototag ON photo.id = phototag.photo_id LEFT JOIN tag ON tag.id = phototag.tag_id WHERE photo.id IN (SELECT DISTINCT photo.id FROM photo LEFT JOIN phototag ON photo.id = phototag.photo_id LEFT JOIN tag ON tag.id = phototag.tag_id WHERE tag.id = ? LIMIT 100) AND tag.id = ? ORDER BY photo.id DESC");
    }
zYne's avatar
zYne committed
227 228
}
?>