ActiveDataProviderTest.php 4.68 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
use yiiunit\data\ar\ActiveRecord;
13 14
use yiiunit\data\ar\Customer;
use yiiunit\data\ar\Item;
15 16 17 18 19 20
use yiiunit\framework\db\DatabaseTestCase;
use yiiunit\data\ar\Order;

/**
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
21 22
 *
 * @group data
23
 * @group db
24 25 26 27 28 29 30 31 32 33 34
 */
class ActiveDataProviderTest extends DatabaseTestCase
{
	protected function setUp()
	{
		parent::setUp();
		ActiveRecord::$db = $this->getConnection();
	}

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

Alexander Makarov committed
45
		$provider = new ActiveDataProvider([
46
			'query' => Order::find(),
Alexander Makarov committed
47
			'pagination' => [
48
				'pageSize' => 2,
Alexander Makarov committed
49 50
			]
		]);
51
		$orders = $provider->getModels();
52 53 54
		$this->assertEquals(2, count($orders));
	}

55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
	public function testActiveRelation()
	{
		/** @var Customer $customer */
		$customer = Customer::find(2);
		$provider = new ActiveDataProvider([
			'query' => $customer->getOrders(),
		]);
		$orders = $provider->getModels();
		$this->assertEquals(2, count($orders));
		$this->assertTrue($orders[0] instanceof Order);
		$this->assertTrue($orders[1] instanceof Order);
		$this->assertEquals([2, 3], $provider->getKeys());

		$provider = new ActiveDataProvider([
			'query' => $customer->getOrders(),
			'pagination' => [
				'pageSize' => 1,
			]
		]);
		$orders = $provider->getModels();
		$this->assertEquals(1, count($orders));
	}

	public function testActiveRelationVia()
	{
		/** @var Order $order */
		$order = Order::find(2);
		$provider = new ActiveDataProvider([
			'query' => $order->getItems(),
		]);
		$items = $provider->getModels();
		$this->assertEquals(3, count($items));
		$this->assertTrue($items[0] instanceof Item);
		$this->assertTrue($items[1] instanceof Item);
		$this->assertTrue($items[2] instanceof Item);
		$this->assertEquals([3, 4, 5], $provider->getKeys());

		$provider = new ActiveDataProvider([
			'query' => $order->getItems(),
			'pagination' => [
				'pageSize' => 2,
			]
		]);
		$items = $provider->getModels();
		$this->assertEquals(2, count($items));
	}

	public function testActiveRelationViaTable()
	{
		/** @var Order $order */
		$order = Order::find(1);
		$provider = new ActiveDataProvider([
			'query' => $order->getBooks(),
		]);
		$items = $provider->getModels();
		$this->assertEquals(2, count($items));
		$this->assertTrue($items[0] instanceof Item);
		$this->assertTrue($items[1] instanceof Item);

		$provider = new ActiveDataProvider([
			'query' => $order->getBooks(),
			'pagination' => [
				'pageSize' => 1,
			]
		]);
		$items = $provider->getModels();
		$this->assertEquals(1, count($items));
	}

124 125
	public function testQuery()
	{
126
		$query = new Query;
Alexander Makarov committed
127
		$provider = new ActiveDataProvider([
Qiang Xue committed
128
			'db' => $this->getConnection(),
129
			'query' => $query->from('tbl_order')->orderBy('id'),
Alexander Makarov committed
130
		]);
131
		$orders = $provider->getModels();
132 133
		$this->assertEquals(3, count($orders));
		$this->assertTrue(is_array($orders[0]));
Alexander Makarov committed
134
		$this->assertEquals([0, 1, 2], $provider->getKeys());
135

136
		$query = new Query;
Alexander Makarov committed
137
		$provider = new ActiveDataProvider([
Qiang Xue committed
138
			'db' => $this->getConnection(),
139
			'query' => $query->from('tbl_order'),
Alexander Makarov committed
140
			'pagination' => [
141
				'pageSize' => 2,
Alexander Makarov committed
142 143
			]
		]);
144
		$orders = $provider->getModels();
145
		$this->assertEquals(2, count($orders));
146
	}
147 148 149 150

	public function testRefresh()
	{
		$query = new Query;
Alexander Makarov committed
151
		$provider = new ActiveDataProvider([
152 153
			'db' => $this->getConnection(),
			'query' => $query->from('tbl_order')->orderBy('id'),
Alexander Makarov committed
154
		]);
155 156 157 158 159 160 161
		$this->assertEquals(3, count($provider->getModels()));

		$provider->getPagination()->pageSize = 2;
		$this->assertEquals(3, count($provider->getModels()));
		$provider->refresh();
		$this->assertEquals(2, count($provider->getModels()));
	}
162
	
163
	public function testPaginationBeforeModels()
164 165
	{
		$query = new Query;
Alexander Makarov committed
166
		$provider = new ActiveDataProvider([
167 168
			'db' => $this->getConnection(),
			'query' => $query->from('tbl_order')->orderBy('id'),
Alexander Makarov committed
169
		]);
170
		$pagination = $provider->getPagination();
Qiang Xue committed
171
		$this->assertEquals(0, $pagination->getPageCount());
172
		$this->assertCount(3, $provider->getModels());
Qiang Xue committed
173 174
		$this->assertEquals(1, $pagination->getPageCount());

175 176 177 178 179
		$provider->getPagination()->pageSize = 2;
		$this->assertEquals(3, count($provider->getModels()));
		$provider->refresh();
		$this->assertEquals(2, count($provider->getModels()));
	}
180
}