QueryTest.php 2.97 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 14 15

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
	}

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

	function testUnion()
	{

	}
}