Commit c393e108 authored by Benjamin Eberlei's avatar Benjamin Eberlei

DBAL-12 - Add tests and fix some bugs, still some failing tests and more work needs to be done

parent 1862f645
......@@ -1093,4 +1093,14 @@ class Connection implements DriverConnection
}
}
}
/**
* Create a new instance of a SQL query builder.
*
* @return Query\QueryBuilder
*/
public function createQueryBuilder()
{
return new Query\QueryBuilder($this);
}
}
\ No newline at end of file
......@@ -117,4 +117,14 @@ class CompositeExpression implements \Countable
return '(' . implode(') ' . $this->type . ' (', $this->parts) . ')';
}
/**
* Return type of this composite expression (AND/OR)
*
* @return string
*/
public function getType()
{
return $this->type;
}
}
\ No newline at end of file
This diff is collapsed.
......@@ -6,6 +6,9 @@ use Doctrine\DBAL\Query\Expression\CompositeExpression;
require_once __DIR__ . '/../../../TestInit.php';
/**
* @group DBAL-12
*/
class CompositeExpressionTest extends \Doctrine\Tests\DbalTestCase
{
public function testCount()
......
......@@ -7,6 +7,9 @@ use Doctrine\DBAL\Query\Expression\ExpressionBuilder,
require_once __DIR__ . '/../../../TestInit.php';
/**
* @group DBAL-12
*/
class ExpressionBuilderTest extends \Doctrine\Tests\DbalTestCase
{
protected $expr;
......
......@@ -7,6 +7,9 @@ use Doctrine\DBAL\Query\Expression\ExpressionBuilder,
require_once __DIR__ . '/../../TestInit.php';
/**
* @group DBAL-12
*/
class QueryBuilderTest extends \Doctrine\Tests\DbalTestCase
{
protected $conn;
......@@ -44,7 +47,7 @@ class QueryBuilderTest extends \Doctrine\Tests\DbalTestCase
$this->assertEquals("SELECT u.id FROM users u WHERE u.nickname = ?", (string) $qb);
}
public function testSelectWithJoin()
public function testSelectWithLeftJoin()
{
$qb = new QueryBuilder($this->conn);
$expr = $qb->expr();
......@@ -55,4 +58,277 @@ class QueryBuilderTest extends \Doctrine\Tests\DbalTestCase
$this->assertEquals('SELECT u.*, p.* FROM users u LEFT JOIN phones p ON p.user_id = u.id', (string) $qb);
}
public function testSelectWithJoin()
{
$qb = new QueryBuilder($this->conn);
$expr = $qb->expr();
$qb->select('u.*', 'p.*')
->from('users', 'u')
->Join('u', 'phones', 'p', $expr->eq('p.user_id', 'u.id'));
$this->assertEquals('SELECT u.*, p.* FROM users u INNER JOIN phones p ON p.user_id = u.id', (string) $qb);
}
public function testSelectWithInnerJoin()
{
$qb = new QueryBuilder($this->conn);
$expr = $qb->expr();
$qb->select('u.*', 'p.*')
->from('users', 'u')
->innerJoin('u', 'phones', 'p', $expr->eq('p.user_id', 'u.id'));
$this->assertEquals('SELECT u.*, p.* FROM users u INNER JOIN phones p ON p.user_id = u.id', (string) $qb);
}
public function testSelectWithRightJoin()
{
$qb = new QueryBuilder($this->conn);
$expr = $qb->expr();
$qb->select('u.*', 'p.*')
->from('users', 'u')
->rightJoin('u', 'phones', 'p', $expr->eq('p.user_id', 'u.id'));
$this->assertEquals('SELECT u.*, p.* FROM users u RIGHT JOIN phones p ON p.user_id = u.id', (string) $qb);
}
public function testSelectWithAndWhereConditions()
{
$qb = new QueryBuilder($this->conn);
$expr = $qb->expr();
$qb->select('u.*', 'p.*')
->from('users', 'u')
->where('u.username = ?')
->andWhere('u.name = ?');
$this->assertEquals('SELECT u.*, p.* FROM users u WHERE (u.username = ?) AND (u.name = ?)', (string) $qb);
}
public function testSelectWithOrWhereConditions()
{
$qb = new QueryBuilder($this->conn);
$expr = $qb->expr();
$qb->select('u.*', 'p.*')
->from('users', 'u')
->where('u.username = ?')
->orWhere('u.name = ?');
$this->assertEquals('SELECT u.*, p.* FROM users u WHERE (u.username = ?) OR (u.name = ?)', (string) $qb);
}
public function testSelectWithOrOrWhereConditions()
{
$qb = new QueryBuilder($this->conn);
$expr = $qb->expr();
$qb->select('u.*', 'p.*')
->from('users', 'u')
->orWhere('u.username = ?')
->orWhere('u.name = ?');
$this->assertEquals('SELECT u.*, p.* FROM users u WHERE (u.username = ?) OR (u.name = ?)', (string) $qb);
}
public function testSelectWithAndOrWhereConditions()
{
$qb = new QueryBuilder($this->conn);
$expr = $qb->expr();
$qb->select('u.*', 'p.*')
->from('users', 'u')
->where('u.username = ?')
->andWhere('u.username = ?')
->orWhere('u.name = ?')
->andWhere('u.name = ?');
$this->assertEquals('SELECT u.*, p.* FROM users u WHERE (((u.username = ?) AND (u.username = ?)) OR (u.name = ?)) AND (u.name = ?)', (string) $qb);
}
public function testSelectGroupBy()
{
$qb = new QueryBuilder($this->conn);
$expr = $qb->expr();
$qb->select('u.*', 'p.*')
->from('users', 'u')
->groupBy('u.id');
$this->assertEquals('SELECT u.*, p.* FROM users u GROUP BY u.id', (string) $qb);
}
public function testSelectEmptyGroupBy()
{
$qb = new QueryBuilder($this->conn);
$expr = $qb->expr();
$qb->select('u.*', 'p.*')
->groupBy(array())
->from('users', 'u');
$this->assertEquals('SELECT u.*, p.* FROM users u', (string) $qb);
}
public function testSelectEmptyAddGroupBy()
{
$qb = new QueryBuilder($this->conn);
$expr = $qb->expr();
$qb->select('u.*', 'p.*')
->addGroupBy(array())
->from('users', 'u');
$this->assertEquals('SELECT u.*, p.* FROM users u', (string) $qb);
}
public function testSelectAddGroupBy()
{
$qb = new QueryBuilder($this->conn);
$expr = $qb->expr();
$qb->select('u.*', 'p.*')
->from('users', 'u')
->groupBy('u.id')
->addGroupBy('u.foo');
$this->assertEquals('SELECT u.*, p.* FROM users u GROUP BY u.id, u.foo', (string) $qb);
}
public function testSelectAddGroupBys()
{
$qb = new QueryBuilder($this->conn);
$expr = $qb->expr();
$qb->select('u.*', 'p.*')
->from('users', 'u')
->groupBy('u.id')
->addGroupBy('u.foo', 'u.bar');
$this->assertEquals('SELECT u.*, p.* FROM users u GROUP BY u.id, u.foo, u.bar', (string) $qb);
}
public function testSelectHaving()
{
$qb = new QueryBuilder($this->conn);
$expr = $qb->expr();
$qb->select('u.*', 'p.*')
->from('users', 'u')
->groupBy('u.id')
->having('u.name = ?');
$this->assertEquals('SELECT u.*, p.* FROM users u GROUP BY u.id HAVING u.name = ?', (string) $qb);
}
public function testSelectAndHaving()
{
$qb = new QueryBuilder($this->conn);
$expr = $qb->expr();
$qb->select('u.*', 'p.*')
->from('users', 'u')
->groupBy('u.id')
->andHaving('u.name = ?');
$this->assertEquals('SELECT u.*, p.* FROM users u GROUP BY u.id HAVING u.name = ?', (string) $qb);
}
public function testSelectHavingAndHaving()
{
$qb = new QueryBuilder($this->conn);
$expr = $qb->expr();
$qb->select('u.*', 'p.*')
->from('users', 'u')
->groupBy('u.id')
->having('u.name = ?')
->andHaving('u.username = ?');
$this->assertEquals('SELECT u.*, p.* FROM users u GROUP BY u.id HAVING (u.name = ?) AND (u.username = ?)', (string) $qb);
}
public function testSelectHavingOrHaving()
{
$qb = new QueryBuilder($this->conn);
$expr = $qb->expr();
$qb->select('u.*', 'p.*')
->from('users', 'u')
->groupBy('u.id')
->having('u.name = ?')
->orHaving('u.username = ?');
$this->assertEquals('SELECT u.*, p.* FROM users u GROUP BY u.id HAVING (u.name = ?) OR (u.username = ?)', (string) $qb);
}
public function testSelectOrHavingOrHaving()
{
$qb = new QueryBuilder($this->conn);
$expr = $qb->expr();
$qb->select('u.*', 'p.*')
->from('users', 'u')
->groupBy('u.id')
->orHaving('u.name = ?')
->orHaving('u.username = ?');
$this->assertEquals('SELECT u.*, p.* FROM users u GROUP BY u.id HAVING (u.name = ?) OR (u.username = ?)', (string) $qb);
}
public function testSelectHavingAndOrHaving()
{
$qb = new QueryBuilder($this->conn);
$expr = $qb->expr();
$qb->select('u.*', 'p.*')
->from('users', 'u')
->groupBy('u.id')
->having('u.name = ?')
->orHaving('u.username = ?')
->andHaving('u.username = ?');
$this->assertEquals('SELECT u.*, p.* FROM users u GROUP BY u.id HAVING ((u.name = ?) OR (u.username = ?)) AND (u.username = ?)', (string) $qb);
}
public function testSelectOrderBy()
{
$qb = new QueryBuilder($this->conn);
$expr = $qb->expr();
$qb->select('u.*', 'p.*')
->from('users', 'u')
->orderBy('u.name');
$this->assertEquals('SELECT u.*, p.* FROM users u ORDER BY u.name ASC', (string) $qb);
}
public function testSelectAddOrderBy()
{
$qb = new QueryBuilder($this->conn);
$expr = $qb->expr();
$qb->select('u.*', 'p.*')
->from('users', 'u')
->orderBy('u.name')
->addOrderBy('u.username', 'DESC');
$this->assertEquals('SELECT u.*, p.* FROM users u ORDER BY u.name ASC, u.username DESC', (string) $qb);
}
public function testSelectAddAddOrderBy()
{
$qb = new QueryBuilder($this->conn);
$expr = $qb->expr();
$qb->select('u.*', 'p.*')
->from('users', 'u')
->addOrderBy('u.name')
->addOrderBy('u.username', 'DESC');
$this->assertEquals('SELECT u.*, p.* FROM users u ORDER BY u.name ASC, u.username DESC', (string) $qb);
}
}
\ No newline at end of file
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