1
0
mirror of synced 2024-12-14 07:06:04 +03:00
doctrine2/tests/QueryLimitTestCase.php

230 lines
11 KiB
PHP
Raw Normal View History

2006-08-16 01:45:00 +04:00
<?php
class Doctrine_Query_Limit_TestCase extends Doctrine_UnitTestCase {
public function prepareTables() {
$this->tables[] = "Photo";
$this->tables[] = "Tag";
$this->tables[] = "Phototag";
2006-08-16 13:17:43 +04:00
parent::prepareTables();
}
2006-08-16 13:17:43 +04:00
public function testLimitWithOneToOneLeftJoin() {
2006-08-22 03:20:33 +04:00
$q = new Doctrine_Query($this->connection);
2006-08-16 13:17:43 +04:00
$q->from('User(id).Email')->limit(5);
$users = $q->execute();
$this->assertEqual($users->count(), 5);
2006-08-17 13:42:18 +04:00
$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");
2006-08-16 13:17:43 +04:00
}
public function testLimitWithOneToOneInnerJoin() {
2006-08-22 03:20:33 +04:00
$q = new Doctrine_Query($this->connection);
2006-08-16 13:17:43 +04:00
$q->from('User(id):Email')->limit(5);
$users = $q->execute();
$this->assertEqual($users->count(), 5);
2006-08-17 13:42:18 +04:00
$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");
2006-08-16 13:17:43 +04:00
}
public function testLimitWithOneToManyLeftJoin() {
$this->query->from("User(id).Phonenumber");
2006-08-16 03:25:53 +04:00
$this->query->limit(5);
$sql = $this->query->getQuery();
2006-08-16 13:17:43 +04:00
$users = $this->query->execute();
$count = $this->dbh->count();
$this->assertEqual($users->count(), 5);
$users[0]->Phonenumber[0];
$this->assertEqual($count, $this->dbh->count());
2006-08-16 13:17:43 +04:00
$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());
}
2006-08-16 13:17:43 +04:00
public function testLimitWithOneToManyLeftJoinAndCondition() {
2006-08-22 03:20:33 +04:00
$q = new Doctrine_Query($this->connection);
2006-08-16 13:17:43 +04:00
$q->from("User(name)")->where("User.Phonenumber.phonenumber LIKE '%123%'")->limit(5);
$users = $q->execute();
2006-08-16 03:25:53 +04:00
2006-08-16 13:17:43 +04:00
$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);
2006-08-16 13:17:43 +04:00
$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)");
}
2006-08-16 13:17:43 +04:00
public function testLimitWithOneToManyLeftJoinAndOrderBy() {
2006-08-22 03:20:33 +04:00
$q = new Doctrine_Query($this->connection);
2006-08-16 13:17:43 +04:00
$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->select('u.id')->from("User u INNER JOIN u.Phonenumber");
2006-08-16 13:17:43 +04:00
$this->query->limit(5);
2006-08-16 13:17:43 +04:00
$sql = $this->query->getQuery();
2006-08-16 03:25:53 +04:00
$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());
2006-08-16 13:17:43 +04:00
$this->assertEqual($this->query->getQuery(),
2006-08-16 13:17:43 +04:00
'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)');
2006-08-16 01:45:00 +04:00
}
public function testLimitWithPreparedQueries() {
$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());
$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);
$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)");
}
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);
2006-08-22 03:20:33 +04:00
$this->connection->flush();
}
public function testLimitWithManyToManyColumnAggInheritanceLeftJoin() {
2006-08-22 03:20:33 +04:00
$q = new Doctrine_Query($this->connection);
2006-08-16 13:17:43 +04:00
$q->from("User.Group")->limit(5);
2006-08-17 13:42:18 +04:00
$users = $q->execute();
$this->assertEqual($users->count(), 5);
$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");
2006-08-22 03:20:33 +04:00
$this->connection->flush();
$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 ASC")->limit(5);
$users = $q->execute(array($user->Group[1]->id));
$this->assertEqual($users->count(), 3);
2006-08-22 03:20:33 +04:00
$this->connection->clear();
$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);
2006-08-16 13:17:43 +04:00
}
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);
}
public function testLimitWithNormalManyToMany() {
2006-08-22 03:20:33 +04:00
$coll = new Doctrine_Collection($this->connection->getTable("Photo"));
$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";
2006-08-22 03:20:33 +04:00
$this->connection->flush();
2006-08-16 13:17:43 +04:00
$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 = ? ORDER BY photo.id DESC LIMIT 100) AND tag.id = ? ORDER BY photo.id DESC");
}
2006-08-16 01:45:00 +04:00
}
?>