ActiveDataProviderTest.php 2.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
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
19 20
 *
 * @group data
21 22 23 24 25 26 27 28 29 30 31 32
 */
class ActiveDataProviderTest extends DatabaseTestCase
{
	protected function setUp()
	{
		parent::setUp();
		ActiveRecord::$db = $this->getConnection();
	}

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

		$provider = new ActiveDataProvider(array(
			'query' => Order::find(),
			'pagination' => array(
				'pageSize' => 2,
			)
		));
46
		$orders = $provider->getModels();
47 48 49 50 51
		$this->assertEquals(2, count($orders));
	}

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

62 63
		$query = new Query;
		$provider = new ActiveDataProvider(array(
Qiang Xue committed
64
			'db' => $this->getConnection(),
65 66 67 68 69
			'query' => $query->from('tbl_order'),
			'pagination' => array(
				'pageSize' => 2,
			)
		));
70
		$orders = $provider->getModels();
71
		$this->assertEquals(2, count($orders));
72
	}
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87

	public function testRefresh()
	{
		$query = new Query;
		$provider = new ActiveDataProvider(array(
			'db' => $this->getConnection(),
			'query' => $query->from('tbl_order')->orderBy('id'),
		));
		$this->assertEquals(3, count($provider->getModels()));

		$provider->getPagination()->pageSize = 2;
		$this->assertEquals(3, count($provider->getModels()));
		$provider->refresh();
		$this->assertEquals(2, count($provider->getModels()));
	}
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
	
	public function testPaginationBeforeModels() 
	{
		$query = new Query;
		$provider = new ActiveDataProvider(array(
			'db' => $this->getConnection(),
			'query' => $query->from('tbl_order')->orderBy('id'),
		));
		$pagination = $provider->getPagination();
		$this->assertEquals(1, $pagination->getPageCount());
		$this->assertCount(3, $provider->getModels());
		
		$provider->getPagination()->pageSize = 2;
		$this->assertEquals(3, count($provider->getModels()));
		$provider->refresh();
		$this->assertEquals(2, count($provider->getModels()));
	}
105
}