QueryTest.php 3.44 KB
Newer Older
Qiang Xue committed
1 2
<?php

Qiang Xue committed
3
namespace yiiunit\framework\db;
Qiang Xue committed
4

Qiang Xue committed
5 6 7 8
use yii\db\Connection;
use yii\db\Command;
use yii\db\Query;
use yii\db\DataReader;
Qiang Xue committed
9

10 11 12 13
/**
 * @group db
 * @group mysql
 */
Alexander Makarov committed
14
class QueryTest extends DatabaseTestCase
Qiang Xue committed
15
{
Alexander Makarov committed
16
	public function testSelect()
Qiang Xue committed
17 18 19
	{
		// default
		$query = new Query;
20
		$query->select('*');
Qiang Xue committed
21
		$this->assertEquals(array('*'), $query->select);
Qiang Xue committed
22
		$this->assertNull($query->distinct);
Qiang Xue committed
23 24 25
		$this->assertEquals(null, $query->selectOption);

		$query = new Query;
Qiang Xue committed
26
		$query->select('id, name', 'something')->distinct(true);
resurtm committed
27
		$this->assertEquals(array('id', 'name'), $query->select);
Qiang Xue committed
28 29 30 31
		$this->assertTrue($query->distinct);
		$this->assertEquals('something', $query->selectOption);
	}

Alexander Makarov committed
32
	public function testFrom()
Qiang Xue committed
33 34 35
	{
		$query = new Query;
		$query->from('tbl_user');
Qiang Xue committed
36
		$this->assertEquals(array('tbl_user'), $query->from);
Qiang Xue committed
37 38
	}

Alexander Makarov committed
39
	public function testWhere()
Qiang Xue committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
	{
		$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);
	}

Alexander Makarov committed
55
	public function testJoin()
Qiang Xue committed
56 57 58
	{
	}

Alexander Makarov committed
59
	public function testGroup()
Qiang Xue committed
60 61
	{
		$query = new Query;
Qiang Xue committed
62
		$query->groupBy('team');
Qiang Xue committed
63
		$this->assertEquals(array('team'), $query->groupBy);
Qiang Xue committed
64

Qiang Xue committed
65
		$query->addGroupBy('company');
Qiang Xue committed
66
		$this->assertEquals(array('team', 'company'), $query->groupBy);
Qiang Xue committed
67

Qiang Xue committed
68
		$query->addGroupBy('age');
Qiang Xue committed
69
		$this->assertEquals(array('team', 'company', 'age'), $query->groupBy);
Qiang Xue committed
70 71
	}

Alexander Makarov committed
72
	public function testHaving()
Qiang Xue committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
	{
		$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);
	}

Alexander Makarov committed
88
	public function testOrder()
Qiang Xue committed
89 90
	{
		$query = new Query;
Qiang Xue committed
91
		$query->orderBy('team');
Qiang Xue committed
92
		$this->assertEquals(array('team' => false), $query->orderBy);
Qiang Xue committed
93

Qiang Xue committed
94
		$query->addOrderBy('company');
Qiang Xue committed
95
		$this->assertEquals(array('team' => false, 'company' => false), $query->orderBy);
Qiang Xue committed
96

Qiang Xue committed
97
		$query->addOrderBy('age');
Qiang Xue committed
98 99 100 101 102 103 104
		$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);
Qiang Xue committed
105 106
	}

Alexander Makarov committed
107
	public function testLimitOffset()
Qiang Xue committed
108 109 110 111 112 113 114
	{
		$query = new Query;
		$query->limit(10)->offset(5);
		$this->assertEquals(10, $query->limit);
		$this->assertEquals(5, $query->offset);
	}

Alexander Makarov committed
115
	public function testUnion()
Qiang Xue committed
116 117
	{
	}
Zander Baldwin committed
118
}