QueryTest.php 3.58 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
use yii\db\Query;
Qiang Xue committed
6

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

		$query = new Query;
Qiang Xue committed
23
		$query->select('id, name', 'something')->distinct(true);
Alexander Makarov committed
24
		$this->assertEquals(['id', 'name'], $query->select);
Qiang Xue committed
25 26 27 28
		$this->assertTrue($query->distinct);
		$this->assertEquals('something', $query->selectOption);
	}

Alexander Makarov committed
29
	public function testFrom()
Qiang Xue committed
30 31 32
	{
		$query = new Query;
		$query->from('tbl_user');
Alexander Makarov committed
33
		$this->assertEquals(['tbl_user'], $query->from);
Qiang Xue committed
34 35
	}

Alexander Makarov committed
36
	public function testWhere()
Qiang Xue committed
37 38
	{
		$query = new Query;
Alexander Makarov committed
39
		$query->where('id = :id', [':id' => 1]);
Qiang Xue committed
40
		$this->assertEquals('id = :id', $query->where);
Alexander Makarov committed
41
		$this->assertEquals([':id' => 1], $query->params);
Qiang Xue committed
42

Alexander Makarov committed
43 44 45
		$query->andWhere('name = :name', [':name' => 'something']);
		$this->assertEquals(['and', 'id = :id', 'name = :name'], $query->where);
		$this->assertEquals([':id' => 1, ':name' => 'something'], $query->params);
Qiang Xue committed
46

Alexander Makarov committed
47 48 49
		$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);
Qiang Xue committed
50 51
	}

Alexander Makarov committed
52
	public function testJoin()
Qiang Xue committed
53 54 55
	{
	}

Alexander Makarov committed
56
	public function testGroup()
Qiang Xue committed
57 58
	{
		$query = new Query;
Qiang Xue committed
59
		$query->groupBy('team');
Alexander Makarov committed
60
		$this->assertEquals(['team'], $query->groupBy);
Qiang Xue committed
61

Qiang Xue committed
62
		$query->addGroupBy('company');
Alexander Makarov committed
63
		$this->assertEquals(['team', 'company'], $query->groupBy);
Qiang Xue committed
64

Qiang Xue committed
65
		$query->addGroupBy('age');
Alexander Makarov committed
66
		$this->assertEquals(['team', 'company', 'age'], $query->groupBy);
Qiang Xue committed
67 68
	}

Alexander Makarov committed
69
	public function testHaving()
Qiang Xue committed
70 71
	{
		$query = new Query;
Alexander Makarov committed
72
		$query->having('id = :id', [':id' => 1]);
Qiang Xue committed
73
		$this->assertEquals('id = :id', $query->having);
Alexander Makarov committed
74
		$this->assertEquals([':id' => 1], $query->params);
Qiang Xue committed
75

Alexander Makarov committed
76 77 78
		$query->andHaving('name = :name', [':name' => 'something']);
		$this->assertEquals(['and', 'id = :id', 'name = :name'], $query->having);
		$this->assertEquals([':id' => 1, ':name' => 'something'], $query->params);
Qiang Xue committed
79

Alexander Makarov committed
80 81 82
		$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);
Qiang Xue committed
83 84
	}

Alexander Makarov committed
85
	public function testOrder()
Qiang Xue committed
86 87
	{
		$query = new Query;
Qiang Xue committed
88
		$query->orderBy('team');
89
		$this->assertEquals(['team' => SORT_ASC], $query->orderBy);
Qiang Xue committed
90

Qiang Xue committed
91
		$query->addOrderBy('company');
92
		$this->assertEquals(['team' => SORT_ASC, 'company' => SORT_ASC], $query->orderBy);
Qiang Xue committed
93

Qiang Xue committed
94
		$query->addOrderBy('age');
95
		$this->assertEquals(['team' => SORT_ASC, 'company' => SORT_ASC, 'age' => SORT_ASC], $query->orderBy);
Qiang Xue committed
96

97 98
		$query->addOrderBy(['age' => SORT_DESC]);
		$this->assertEquals(['team' => SORT_ASC, 'company' => SORT_ASC, 'age' => SORT_DESC], $query->orderBy);
Qiang Xue committed
99 100

		$query->addOrderBy('age ASC, company DESC');
101
		$this->assertEquals(['team' => SORT_ASC, 'company' => SORT_DESC, 'age' => SORT_ASC], $query->orderBy);
Qiang Xue committed
102 103
	}

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

Alexander Makarov committed
112
	public function testUnion()
Qiang Xue committed
113 114
	{
	}
115 116 117 118 119 120 121 122 123 124 125

	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);
	}
Zander Baldwin committed
126
}