Commit 8fc2028b authored by zYne's avatar zYne

added alias tests for rawSql component

parent 15088034
......@@ -33,33 +33,33 @@
*/
class Doctrine_RawSql_TestCase extends Doctrine_UnitTestCase
{
public function testQueryParser()
public function testQueryParser()
{
$sql = "SELECT {p.*} FROM photos p";
$sql = 'SELECT {p.*} FROM photos p';
$query = new Doctrine_RawSql($this->connection);
$query->parseQuery($sql);
$this->assertEqual($query->from, array('photos p'));
$this->assertEqual($query->getQueryPart('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";
$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"));
$this->assertEqual($query->where, array('p.can_see = -1 AND t.tag_id = 62'));
$this->assertEqual($query->limit, array(200));
$this->assertEqual($query->getQueryPart('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'));
$this->assertEqual($query->getQueryPart('where'), array('p.can_see = -1 AND t.tag_id = 62'));
$this->assertEqual($query->getQueryPart('limit'), array(200));
}
public function testAsteriskOperator()
{
// Selecting with *
$query = new Doctrine_RawSql($this->connection);
$query->parseQuery("SELECT {entity.*} FROM entity");
$query->parseQuery('SELECT {entity.*} FROM entity');
$fields = $query->getFields();
$this->assertEqual($fields, array("entity.*"));
$this->assertEqual($fields, array('entity.*'));
$query->addComponent("entity", "Entity");
$query->addComponent('entity', 'Entity');
$coll = $query->execute();
......@@ -72,11 +72,11 @@ class Doctrine_RawSql_TestCase extends Doctrine_UnitTestCase
// selecting proxy objects (lazy property loading)
$query->parseQuery("SELECT {entity.name}, {entity.id} FROM entity");
$query->parseQuery('SELECT {entity.name}, {entity.id} FROM entity');
$fields = $query->getFields();
$this->assertEqual($fields, array("entity.name", "entity.id"));
$query->addComponent("entity", "Entity");
$this->assertEqual($fields, array('entity.name', 'entity.id'));
$query->addComponent('entity', 'Entity');
$coll = $query->execute();
......@@ -90,10 +90,10 @@ class Doctrine_RawSql_TestCase extends Doctrine_UnitTestCase
$query = new Doctrine_RawSql($this->connection);
// smart component mapping (no need for additional addComponent call
$query->parseQuery("SELECT {entity.name}, {entity.id} FROM entity");
$query->parseQuery('SELECT {entity.name}, {entity.id} FROM entity');
$fields = $query->getFields();
$this->assertEqual($fields, array("entity.name", "entity.id"));
$this->assertEqual($fields, array('entity.name', 'entity.id'));
$coll = $query->execute();
......@@ -108,11 +108,33 @@ class Doctrine_RawSql_TestCase extends Doctrine_UnitTestCase
$query = new Doctrine_RawSql($this->connection);
// multi component fetching
$query->parseQuery("SELECT {entity.name}, {entity.id}, {phonenumber.*} FROM entity LEFT JOIN phonenumber ON phonenumber.entity_id = entity.id");
$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");
$query->addComponent('entity', 'Entity');
$query->addComponent('phonenumber', 'Entity.Phonenumber');
$coll = $query->execute();
$this->assertEqual($coll->count(), 11);
$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 testAliasesAreSupportedInAddComponent()
{
$query = new Doctrine_RawSql();
$query->parseQuery('SELECT {entity.name}, {entity.id}, {phonenumber.*} FROM entity LEFT JOIN phonenumber ON phonenumber.entity_id = entity.id');
$query->addComponent('entity', 'Entity e');
$query->addComponent('phonenumber', 'e.Phonenumber');
$this->assertEqual(array_keys($query->getAliasMap()), array('e', 'e.Phonenumber'));
$coll = $query->execute();
$this->assertEqual($coll->count(), 11);
......@@ -124,13 +146,13 @@ class Doctrine_RawSql_TestCase extends Doctrine_UnitTestCase
$coll[5]->Phonenumber[0]->phonenumber;
$this->assertEqual($count, $this->dbh->count());
}
public function testPrimaryKeySelectForcing()
public function testPrimaryKeySelectForcing()
{
// forcing the select of primary key fields
$query = new Doctrine_RawSql($this->connection);
$query->parseQuery("SELECT {entity.name} FROM entity");
$query->parseQuery('SELECT {entity.name} FROM entity');
$coll = $query->execute();
......@@ -143,7 +165,7 @@ class Doctrine_RawSql_TestCase extends Doctrine_UnitTestCase
{
$query = new Doctrine_RawSql($this->connection);
$query->select('{entity.name}')->from('entity');
$query->addComponent("entity", "User");
$query->addComponent('entity', 'User');
$coll = $query->execute();
$this->assertEqual($coll->count(), 8);
......@@ -155,11 +177,11 @@ class Doctrine_RawSql_TestCase extends Doctrine_UnitTestCase
public function testColumnAggregationInheritance()
{
// forcing the select of primary key fields
$query = new Doctrine_RawSql($this->connection);
$query->parseQuery("SELECT {entity.name} FROM entity");
$query->addComponent("entity", "User");
$query->parseQuery('SELECT {entity.name} FROM entity');
$query->addComponent('entity', 'User');
$coll = $query->execute();
$this->assertEqual($coll->count(), 8);
......@@ -174,8 +196,8 @@ class Doctrine_RawSql_TestCase extends Doctrine_UnitTestCase
$query = new Doctrine_RawSql($this->connection);
$query->parseQuery("SELECT {entity.name} FROM entity ORDER BY entity.name");
$query->addComponent("entity", "User");
$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");
......@@ -198,6 +220,5 @@ class Doctrine_RawSql_TestCase extends Doctrine_UnitTestCase
$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");
}
}
?>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment