You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
3.4 KiB

13 years ago
<?php
12 years ago
namespace yiiunit\framework\db;
13 years ago
use yii\db\Connection;
use yii\db\Command;
use yii\db\Query;
use yii\db\DataReader;
13 years ago
/**
* @group db
* @group mysql
*/
class QueryTest extends DatabaseTestCase
13 years ago
{
12 years ago
public function testSelect()
13 years ago
{
// default
$query = new Query;
$query->select('*');
$this->assertEquals(array('*'), $query->select);
13 years ago
$this->assertNull($query->distinct);
13 years ago
$this->assertEquals(null, $query->selectOption);
$query = new Query;
13 years ago
$query->select('id, name', 'something')->distinct(true);
12 years ago
$this->assertEquals(array('id', 'name'), $query->select);
13 years ago
$this->assertTrue($query->distinct);
$this->assertEquals('something', $query->selectOption);
}
12 years ago
public function testFrom()
13 years ago
{
$query = new Query;
$query->from('tbl_user');
$this->assertEquals(array('tbl_user'), $query->from);
13 years ago
}
12 years ago
public function testWhere()
13 years ago
{
$query = new Query;
$query->where('id = :id', array(':id' => 1));
$this->assertEquals('id = :id', $query->where);
$this->assertEquals(array(':id' => 1), $query->params);
$query->andWhere('name = :name', array(':name' => 'something'));
$this->assertEquals(array('and', 'id = :id', 'name = :name'), $query->where);
$this->assertEquals(array(':id' => 1, ':name' => 'something'), $query->params);
$query->orWhere('age = :age', array(':age' => '30'));
$this->assertEquals(array('or', array('and', 'id = :id', 'name = :name'), 'age = :age'), $query->where);
$this->assertEquals(array(':id' => 1, ':name' => 'something', ':age' => '30'), $query->params);
}
12 years ago
public function testJoin()
13 years ago
{
}
12 years ago
public function testGroup()
13 years ago
{
$query = new Query;
12 years ago
$query->groupBy('team');
$this->assertEquals(array('team'), $query->groupBy);
13 years ago
$query->addGroupBy('company');
12 years ago
$this->assertEquals(array('team', 'company'), $query->groupBy);
13 years ago
$query->addGroupBy('age');
12 years ago
$this->assertEquals(array('team', 'company', 'age'), $query->groupBy);
13 years ago
}
12 years ago
public function testHaving()
13 years ago
{
$query = new Query;
$query->having('id = :id', array(':id' => 1));
$this->assertEquals('id = :id', $query->having);
$this->assertEquals(array(':id' => 1), $query->params);
$query->andHaving('name = :name', array(':name' => 'something'));
$this->assertEquals(array('and', 'id = :id', 'name = :name'), $query->having);
$this->assertEquals(array(':id' => 1, ':name' => 'something'), $query->params);
$query->orHaving('age = :age', array(':age' => '30'));
$this->assertEquals(array('or', array('and', 'id = :id', 'name = :name'), 'age = :age'), $query->having);
$this->assertEquals(array(':id' => 1, ':name' => 'something', ':age' => '30'), $query->params);
}
12 years ago
public function testOrder()
13 years ago
{
$query = new Query;
12 years ago
$query->orderBy('team');
$this->assertEquals(array('team' => false), $query->orderBy);
13 years ago
12 years ago
$query->addOrderBy('company');
$this->assertEquals(array('team' => false, 'company' => false), $query->orderBy);
13 years ago
12 years ago
$query->addOrderBy('age');
$this->assertEquals(array('team' => false, 'company' => false, 'age' => false), $query->orderBy);
$query->addOrderBy(array('age' => true));
$this->assertEquals(array('team' => false, 'company' => false, 'age' => true), $query->orderBy);
$query->addOrderBy('age ASC, company DESC');
$this->assertEquals(array('team' => false, 'company' => true, 'age' => false), $query->orderBy);
13 years ago
}
12 years ago
public function testLimitOffset()
13 years ago
{
$query = new Query;
$query->limit(10)->offset(5);
$this->assertEquals(10, $query->limit);
$this->assertEquals(5, $query->offset);
}
12 years ago
public function testUnion()
13 years ago
{
}
}