1
0
mirror of synced 2024-12-14 23:26:04 +03:00
doctrine2/tests/RawSqlTestCase.php

225 lines
8.6 KiB
PHP
Raw Normal View History

2006-07-22 03:22:15 +04:00
<?php
class Doctrine_RawSql_TestCase extends Doctrine_UnitTestCase {
2006-08-22 23:34:40 +04:00
2006-08-20 23:21:52 +04:00
public function testQueryParser() {
$sql = "SELECT {p.*} FROM photos p";
2006-08-22 03:20:33 +04:00
$query = new Doctrine_RawSql($this->connection);
2006-08-20 23:21:52 +04:00
$query->parseQuery($sql);
$this->assertEqual($query->from, array('photos p'));
$sql = "SELECT {p.*} FROM (SELECT p.* FROM photos p LEFT JOIN photos_tags t ON t.photo_id = p.id WHERE t.tag_id = 65) p LEFT JOIN photos_tags t ON t.photo_id = p.id WHERE p.can_see = -1 AND t.tag_id = 62 LIMIT 200";
$query->parseQuery($sql);
$this->assertEqual($query->from, array("(SELECT p.* FROM photos p LEFT JOIN photos_tags t ON t.photo_id = p.id WHERE t.tag_id = 65) p LEFT JOIN photos_tags t ON t.photo_id = p.id"));
2006-08-20 23:25:04 +04:00
$this->assertEqual($query->where, array('p.can_see = -1 AND t.tag_id = 62'));
2006-08-20 23:21:52 +04:00
$this->assertEqual($query->limit, array(200));
}
2006-07-22 03:22:15 +04:00
public function testAsteriskOperator() {
// Selecting with *
2006-08-22 03:20:33 +04:00
$query = new Doctrine_RawSql($this->connection);
2006-07-22 03:22:15 +04:00
$query->parseQuery("SELECT {entity.*} FROM entity");
$fields = $query->getFields();
$this->assertEqual($fields, array("entity.*"));
$query->addComponent("entity", "Entity");
$coll = $query->execute();
$this->assertEqual($coll->count(), 11);
}
public function testLazyPropertyLoading() {
2006-08-22 03:20:33 +04:00
$query = new Doctrine_RawSql($this->connection);
$this->connection->clear();
2006-07-22 03:22:15 +04:00
// selecting proxy objects (lazy property loading)
$query->parseQuery("SELECT {entity.name}, {entity.id} FROM entity");
$fields = $query->getFields();
$this->assertEqual($fields, array("entity.name", "entity.id"));
$query->addComponent("entity", "Entity");
$coll = $query->execute();
$this->assertEqual($coll->count(), 11);
2007-01-21 21:31:51 +03:00
$this->assertEqual($coll[0]->state(), Doctrine_Record::STATE_PROXY);
$this->assertEqual($coll[3]->state(), Doctrine_Record::STATE_PROXY);
2006-07-22 03:22:15 +04:00
}
public function testSmartMapping() {
2006-08-22 03:20:33 +04:00
$query = new Doctrine_RawSql($this->connection);
2006-07-22 03:22:15 +04:00
// smart component mapping (no need for additional addComponent call
$query->parseQuery("SELECT {entity.name}, {entity.id} FROM entity");
$fields = $query->getFields();
$this->assertEqual($fields, array("entity.name", "entity.id"));
$coll = $query->execute();
$this->assertEqual($coll->count(), 11);
2007-01-21 21:31:51 +03:00
$this->assertEqual($coll[0]->state(), Doctrine_Record::STATE_PROXY);
$this->assertEqual($coll[3]->state(), Doctrine_Record::STATE_PROXY);
2006-07-22 03:22:15 +04:00
}
public function testMultipleComponents() {
2006-08-22 03:20:33 +04:00
$query = new Doctrine_RawSql($this->connection);
2006-07-22 03:22:15 +04:00
// multi component fetching
$query->parseQuery("SELECT {entity.name}, {entity.id}, {phonenumber.*} FROM entity LEFT JOIN phonenumber ON phonenumber.entity_id = entity.id");
$query->addComponent("entity", "Entity");
$query->addComponent("phonenumber", "Entity.Phonenumber");
2006-08-07 00:46:12 +04:00
2006-07-22 03:22:15 +04:00
$coll = $query->execute();
$this->assertEqual($coll->count(), 11);
2006-08-07 00:46:12 +04:00
2006-07-22 03:22:15 +04:00
$count = $this->dbh->count();
$coll[4]->Phonenumber[0]->phonenumber;
$this->assertEqual($count, $this->dbh->count());
$coll[5]->Phonenumber[0]->phonenumber;
$this->assertEqual($count, $this->dbh->count());
}
public function testPrimaryKeySelectForcing() {
// forcing the select of primary key fields
2006-08-22 03:20:33 +04:00
$query = new Doctrine_RawSql($this->connection);
2006-07-22 03:22:15 +04:00
$query->parseQuery("SELECT {entity.name} FROM entity");
$coll = $query->execute();
$this->assertEqual($coll->count(), 11);
$this->assertTrue(is_numeric($coll[0]->id));
$this->assertTrue(is_numeric($coll[3]->id));
$this->assertTrue(is_numeric($coll[7]->id));
}
public function testMethodOverloading() {
2006-08-22 03:20:33 +04:00
$query = new Doctrine_RawSql($this->connection);
2006-07-22 03:22:15 +04:00
$query->select('{entity.name}')->from('entity');
$query->addComponent("entity", "User");
$coll = $query->execute();
$this->assertEqual($coll->count(), 8);
$this->assertTrue(is_numeric($coll[0]->id));
$this->assertTrue(is_numeric($coll[3]->id));
$this->assertTrue(is_numeric($coll[7]->id));
}
2006-08-22 23:34:40 +04:00
2006-07-22 03:22:15 +04:00
public function testColumnAggregationInheritance() {
// forcing the select of primary key fields
2006-08-22 03:20:33 +04:00
$query = new Doctrine_RawSql($this->connection);
2006-07-22 03:22:15 +04:00
$query->parseQuery("SELECT {entity.name} FROM entity");
$query->addComponent("entity", "User");
$coll = $query->execute();
$this->assertEqual($coll->count(), 8);
$this->assertTrue(is_numeric($coll[0]->id));
$this->assertTrue(is_numeric($coll[3]->id));
$this->assertTrue(is_numeric($coll[7]->id));
}
2006-08-22 23:34:40 +04:00
public function testColumnAggregationInheritanceWithOrderBy() {
// forcing the select of primary key fields
$query = new Doctrine_RawSql($this->connection);
$query->parseQuery("SELECT {entity.name} FROM entity ORDER BY entity.name");
$query->addComponent("entity", "User");
$this->assertEqual($query->getQuery(), "SELECT entity.name AS entity__name, entity.id AS entity__id FROM entity WHERE entity.type = 0 ORDER BY entity.name");
$coll = $query->execute();
$this->assertEqual($coll->count(), 8);
$this->assertTrue(is_numeric($coll[0]->id));
$this->assertTrue(is_numeric($coll[3]->id));
$this->assertTrue(is_numeric($coll[7]->id));
}
public function testsqlExplode() {
$str = "word1 word2 word3";
$a = Doctrine_Query::sqlExplode($str);
$this->assertEqual($a, array("word1", "word2", "word3"));
$str = "word1 (word2 word3)";
$a = Doctrine_Query::sqlExplode($str);
$this->assertEqual($a, array("word1", "(word2 word3)"));
$str = "word1 'word2 word3'";
$a = Doctrine_Query::sqlExplode($str);
$this->assertEqual($a, array("word1", "'word2 word3'"));
$str = "word1 <20>word2 word3<64>";
$a = Doctrine_Query::sqlExplode($str);
$this->assertEqual($a, array("word1", "<EFBFBD>word2 word3<64>"));
$str = "word1 \"word2 word3\"";
$a = Doctrine_Query::sqlExplode($str);
$this->assertEqual($a, array("word1", "\"word2 word3\""));
$str = "word1 ((word2) word3)";
$a = Doctrine_Query::sqlExplode($str);
$this->assertEqual($a, array("word1", "((word2) word3)"));
$str = "word1 ( (word2) 'word3')";
$a = Doctrine_Query::sqlExplode($str);
$this->assertEqual($a, array("word1", "( (word2) 'word3')"));
$str = "word1 ( \"(word2) 'word3')";
$a = Doctrine_Query::sqlExplode($str);
$this->assertEqual($a, array("word1", "( \"(word2) 'word3')"));
$str = "word1 ( <20><>(word2) 'word3')";
$a = Doctrine_Query::sqlExplode($str);
$this->assertEqual($a, array("word1", "( <20><>(word2) 'word3')"));
$str = "word1 ( <20>()()<29>(word2) 'word3')";
$a = Doctrine_Query::sqlExplode($str);
$this->assertEqual($a, array("word1", "( <20>()()<29>(word2) 'word3')"));
$str = "word1 'word2)() word3'";
$a = Doctrine_Query::sqlExplode($str);
$this->assertEqual($a, array("word1", "'word2)() word3'"));
$str = "word1 <20>word2)() word3<64>";
$a = Doctrine_Query::sqlExplode($str);
$this->assertEqual($a, array("word1", "<EFBFBD>word2)() word3<64>"));
$str = "word1 \"word2)() word3\"";
$a = Doctrine_Query::sqlExplode($str);
$this->assertEqual($a, array("word1", "\"word2)() word3\""));
$str = "something (subquery '')";
$a = Doctrine_Query::sqlExplode($str);
$this->assertEqual($a, array("something", "(subquery '')"));
$str = "something (( ))";
$a = Doctrine_Query::sqlExplode($str);
$this->assertEqual($a, array("something", "(( ))"));
}
public function testQueryParser2() {
$query = new Doctrine_RawSql();
$query->parseQuery("SELECT {entity.name} FROM (SELECT entity.name FROM entity WHERE entity.name = 'something') WHERE entity.id = 2 ORDER BY entity.name");
$this->assertEqual($query->getQuery(),
"SELECT entity.name AS entity__name, entity.id AS entity__id FROM (SELECT entity.name FROM entity WHERE entity.name = 'something') WHERE entity.id = 2 ORDER BY entity.name");
}
2006-08-22 23:34:40 +04:00
2006-07-22 03:22:15 +04:00
}
?>