QueryTest.php 3.7 KB
Newer Older
Qiang Xue committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<?php

namespace yiiunit\framework\db\dao;

use yii\db\dao\Connection;
use yii\db\dao\Command;
use yii\db\dao\Query;
use yii\db\dao\DataReader;

class QueryTest extends \yiiunit\MysqlTestCase
{
	function testSelect()
	{
		// default
		$query = new Query;
16
		$query->select('*');
Qiang Xue committed
17
		$this->assertEquals('*', $query->select);
Qiang Xue committed
18
		$this->assertNull($query->distinct);
Qiang Xue committed
19 20 21
		$this->assertEquals(null, $query->selectOption);

		$query = new Query;
Qiang Xue committed
22
		$query->select('id, name', 'something')->distinct(true);
Qiang Xue committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
		$this->assertEquals('id, name', $query->select);
		$this->assertTrue($query->distinct);
		$this->assertEquals('something', $query->selectOption);
	}

	function testFrom()
	{
		$query = new Query;
		$query->from('tbl_user');
		$this->assertEquals('tbl_user', $query->from);
	}

	function testWhere()
	{
		$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);
	}

	function testJoin()
	{

	}

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

62
		$query->addGroup('company');
Qiang Xue committed
63
		$this->assertEquals(array('team', 'company'), $query->groupBy);
Qiang Xue committed
64

65
		$query->addGroup('age');
Qiang Xue committed
66
		$this->assertEquals(array('team', 'company', 'age'), $query->groupBy);
Qiang Xue committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
	}

	function testHaving()
	{
		$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);
	}

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

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

Qiang Xue committed
94 95
		$query->addOrderBy('age');
		$this->assertEquals(array('team', 'company', 'age'), $query->orderBy);
Qiang Xue committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
	}

	function testLimitOffset()
	{
		$query = new Query;
		$query->limit(10)->offset(5);
		$this->assertEquals(10, $query->limit);
		$this->assertEquals(5, $query->offset);
	}

	function testUnion()
	{

	}

	function testInsert()
	{

	}

	function testUpdate()
	{

	}

	function testDelete()
	{

	}

	function testCreateTable()
	{

	}

	function testRenameTable()
	{

	}

	function testDropTable()
	{

	}

	function testTruncateTable()
	{

	}

	function testAddColumn()
	{

	}

	function testDropColumn()
	{

	}

	function testRenameColumn()
	{

	}

	function testAlterColumn()
	{

	}

	function testAddForeignKey()
	{

	}

	function testDropForeignKey()
	{

	}

	function testCreateIndex()
	{

	}

	function testDropIndex()
	{

	}

	function testParams()
	{

	}

	function testGetSql()
	{

	}

	function testCreateCommand()
	{

	}

	function testReset()
	{

	}

	function testToArray()
	{

	}

	function testMergeWith()
	{

	}
}