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.

127 lines
3.6 KiB

13 years ago
<?php
12 years ago
namespace yiiunit\framework\db;
13 years ago
use yii\db\Query;
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(['*'], $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);
$this->assertEquals(['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(['tbl_user'], $query->from);
13 years ago
}
12 years ago
public function testWhere()
13 years ago
{
$query = new Query;
$query->where('id = :id', [':id' => 1]);
13 years ago
$this->assertEquals('id = :id', $query->where);
$this->assertEquals([':id' => 1], $query->params);
13 years ago
$query->andWhere('name = :name', [':name' => 'something']);
$this->assertEquals(['and', 'id = :id', 'name = :name'], $query->where);
$this->assertEquals([':id' => 1, ':name' => 'something'], $query->params);
13 years ago
$query->orWhere('age = :age', [':age' => '30']);
$this->assertEquals(['or', ['and', 'id = :id', 'name = :name'], 'age = :age'], $query->where);
$this->assertEquals([':id' => 1, ':name' => 'something', ':age' => '30'], $query->params);
13 years ago
}
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(['team'], $query->groupBy);
13 years ago
$query->addGroupBy('company');
$this->assertEquals(['team', 'company'], $query->groupBy);
13 years ago
$query->addGroupBy('age');
$this->assertEquals(['team', 'company', 'age'], $query->groupBy);
13 years ago
}
12 years ago
public function testHaving()
13 years ago
{
$query = new Query;
$query->having('id = :id', [':id' => 1]);
13 years ago
$this->assertEquals('id = :id', $query->having);
$this->assertEquals([':id' => 1], $query->params);
13 years ago
$query->andHaving('name = :name', [':name' => 'something']);
$this->assertEquals(['and', 'id = :id', 'name = :name'], $query->having);
$this->assertEquals([':id' => 1, ':name' => 'something'], $query->params);
13 years ago
$query->orHaving('age = :age', [':age' => '30']);
$this->assertEquals(['or', ['and', 'id = :id', 'name = :name'], 'age = :age'], $query->having);
$this->assertEquals([':id' => 1, ':name' => 'something', ':age' => '30'], $query->params);
13 years ago
}
12 years ago
public function testOrder()
13 years ago
{
$query = new Query;
12 years ago
$query->orderBy('team');
$this->assertEquals(['team' => SORT_ASC], $query->orderBy);
13 years ago
12 years ago
$query->addOrderBy('company');
$this->assertEquals(['team' => SORT_ASC, 'company' => SORT_ASC], $query->orderBy);
13 years ago
12 years ago
$query->addOrderBy('age');
$this->assertEquals(['team' => SORT_ASC, 'company' => SORT_ASC, 'age' => SORT_ASC], $query->orderBy);
$query->addOrderBy(['age' => SORT_DESC]);
$this->assertEquals(['team' => SORT_ASC, 'company' => SORT_ASC, 'age' => SORT_DESC], $query->orderBy);
$query->addOrderBy('age ASC, company DESC');
$this->assertEquals(['team' => SORT_ASC, 'company' => SORT_DESC, 'age' => SORT_ASC], $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
{
}
public function testOne()
{
$db = $this->getConnection();
$result = (new Query)->from('tbl_customer')->where(['status' => 2])->one($db);
$this->assertEquals('user3', $result['name']);
$result = (new Query)->from('tbl_customer')->where(['status' => 3])->one($db);
$this->assertFalse($result);
}
}