ActiveDataProviderTest.php 1.71 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yiiunit\framework\data;

use yii\data\ActiveDataProvider;
11
use yii\db\Query;
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
use yiiunit\data\ar\ActiveRecord;
use yiiunit\framework\db\DatabaseTestCase;
use yiiunit\data\ar\Order;

/**
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class ActiveDataProviderTest extends DatabaseTestCase
{
	protected function setUp()
	{
		parent::setUp();
		ActiveRecord::$db = $this->getConnection();
	}

	public function testActiveQuery()
	{
		$provider = new ActiveDataProvider(array(
31
			'query' => Order::find()->orderBy('id'),
32 33 34
		));
		$orders = $provider->getItems();
		$this->assertEquals(3, count($orders));
35 36
		$this->assertTrue($orders[0] instanceof Order);
		$this->assertEquals(array(1, 2, 3), $provider->getKeys());
37 38 39 40 41 42 43 44 45 46 47 48 49

		$provider = new ActiveDataProvider(array(
			'query' => Order::find(),
			'pagination' => array(
				'pageSize' => 2,
			)
		));
		$orders = $provider->getItems();
		$this->assertEquals(2, count($orders));
	}

	public function testQuery()
	{
50 51
		$query = new Query;
		$provider = new ActiveDataProvider(array(
Qiang Xue committed
52
			'db' => $this->getConnection(),
53 54 55 56 57 58
			'query' => $query->from('tbl_order')->orderBy('id'),
		));
		$orders = $provider->getItems();
		$this->assertEquals(3, count($orders));
		$this->assertTrue(is_array($orders[0]));
		$this->assertEquals(array(0, 1, 2), $provider->getKeys());
59

60 61
		$query = new Query;
		$provider = new ActiveDataProvider(array(
Qiang Xue committed
62
			'db' => $this->getConnection(),
63 64 65 66 67 68 69
			'query' => $query->from('tbl_order'),
			'pagination' => array(
				'pageSize' => 2,
			)
		));
		$orders = $provider->getItems();
		$this->assertEquals(2, count($orders));
70 71
	}
}