QueryTest.php 2.07 KB
Newer Older
1 2
<?php

3
namespace yiiunit\extensions\mongodb;
4

5
use yii\mongodb\Query;
6 7

/**
8
 * @group mongodb
9
 */
10
class QueryTest extends MongoDbTestCase
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
{
	public function testSelect()
	{
		// default
		$query = new Query;
		$select = [];
		$query->select($select);
		$this->assertEquals($select, $query->select);

		$query = new Query;
		$select = ['name', 'something'];
		$query->select($select);
		$this->assertEquals($select, $query->select);
	}

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

		$query = new Query;
		$from = ['', 'customer'];
		$query->from($from);
		$this->assertEquals($from, $query->from);
	}

39 40 41 42 43 44 45 46 47
	public function testWhere()
	{
		$query = new Query;
		$query->where(['name' => 'name1']);
		$this->assertEquals(['name' => 'name1'], $query->where);

		$query->andWhere(['address' => 'address1']);
		$this->assertEquals(
			[
48 49 50
				'and',
				['name' => 'name1'],
				['address' => 'address1']
51 52 53 54 55 56 57
			],
			$query->where
		);

		$query->orWhere(['name' => 'name2']);
		$this->assertEquals(
			[
58 59 60 61 62
				'or',
				[
					'and',
					['name' => 'name1'],
					['address' => 'address1']
63
				],
64
				['name' => 'name2']
65 66 67 68 69 70

			],
			$query->where
		);
	}

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
	public function testOrder()
	{
		$query = new Query;
		$query->orderBy('team');
		$this->assertEquals(['team' => SORT_ASC], $query->orderBy);

		$query->addOrderBy('company');
		$this->assertEquals(['team' => SORT_ASC, 'company' => SORT_ASC], $query->orderBy);

		$query->addOrderBy('age');
		$this->assertEquals(['team' => SORT_ASC, 'company' => SORT_ASC, 'age' => SORT_ASC], $query->orderBy);

		$query->addOrderBy(['age' => SORT_DESC]);
		$this->assertEquals(['team' => SORT_ASC, 'company' => SORT_ASC, 'age' => SORT_DESC], $query->orderBy);

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

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