1
0
mirror of synced 2024-12-13 14:56:01 +03:00
doctrine2/tests/QueryLimitTestCase.php

190 lines
8.2 KiB
PHP
Raw Normal View History

2006-08-16 01:45:00 +04:00
<?php
class Doctrine_Query_Limit_TestCase extends Doctrine_UnitTestCase {
2006-08-16 13:17:43 +04:00
public function testLimitWithOneToOneLeftJoin() {
$q = new Doctrine_Query($this->session);
$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() {
$q = new Doctrine_Query($this->session);
$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() {
$q = new Doctrine_Query($this->session);
$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() {
$q = new Doctrine_Query($this->session);
$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);
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);
$this->session->flush();
}
2006-08-16 13:17:43 +04:00
public function testLimitWithManyToManyLeftJoin() {
$q = new Doctrine_Query($this->session);
$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");
$this->session->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 DESC")->limit(5);
$users = $q->execute(array($user->Group[1]->id));
$this->assertEqual($users->count(), 3);
$this->session->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
}
2006-08-16 01:45:00 +04:00
}
?>