QueryTest.php 3.42 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

Alexander Makarov committed
10
class QueryTest extends DatabaseTestCase
Qiang Xue committed
11
{
Qiang Xue committed
12 13 14 15 16 17
	protected function setUp()
	{
		parent::setUp();
		$this->mockApplication();
	}

Qiang Xue committed
18 19 20 21
	function testSelect()
	{
		// default
		$query = new Query;
22
		$query->select('*');
Qiang Xue committed
23
		$this->assertEquals(array('*'), $query->select);
Qiang Xue committed
24
		$this->assertNull($query->distinct);
Qiang Xue committed
25 26 27
		$this->assertEquals(null, $query->selectOption);

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

	function testFrom()
	{
		$query = new Query;
		$query->from('tbl_user');
Qiang Xue committed
38
		$this->assertEquals(array('tbl_user'), $query->from);
Qiang Xue committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
	}

	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()
	{

	}

62
	function testGroup()
Qiang Xue committed
63 64
	{
		$query = new Query;
Qiang Xue committed
65
		$query->groupBy('team');
Qiang Xue committed
66
		$this->assertEquals(array('team'), $query->groupBy);
Qiang Xue committed
67

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

Qiang Xue committed
71
		$query->addGroupBy('age');
Qiang Xue committed
72
		$this->assertEquals(array('team', 'company', 'age'), $query->groupBy);
Qiang Xue committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
	}

	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);
	}

91
	function testOrder()
Qiang Xue committed
92 93
	{
		$query = new Query;
Qiang Xue committed
94
		$query->orderBy('team');
Qiang Xue committed
95
		$this->assertEquals(array('team' => false), $query->orderBy);
Qiang Xue committed
96

Qiang Xue committed
97
		$query->addOrderBy('company');
Qiang Xue committed
98
		$this->assertEquals(array('team' => false, 'company' => false), $query->orderBy);
Qiang Xue committed
99

Qiang Xue committed
100
		$query->addOrderBy('age');
Qiang Xue committed
101 102 103 104 105 106 107
		$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
108 109 110 111 112 113 114 115 116 117 118 119 120 121
	}

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

	function testUnion()
	{

	}
Zander Baldwin committed
122
}