Commit 9448c3d4 by Carsten Brandt

added unit tests for active dataprovider and fixed query tests

parent 65338972
......@@ -97,13 +97,13 @@ trait ActiveRelationTrait
if ($this->via instanceof self) {
// via pivot table
/** @var ActiveRelation $viaQuery */
/** @var ActiveRelationTrait $viaQuery */
$viaQuery = $this->via;
$viaModels = $viaQuery->findPivotRows($primaryModels);
$this->filterByModels($viaModels);
} elseif (is_array($this->via)) {
// via relation
/** @var ActiveRelation $viaQuery */
/** @var ActiveRelationTrait $viaQuery */
list($viaName, $viaQuery) = $this->via;
$viaQuery->primaryModel = null;
$viaModels = $viaQuery->findWith($viaName, $primaryModels);
......
......@@ -10,6 +10,8 @@ namespace yiiunit\framework\data;
use yii\data\ActiveDataProvider;
use yii\db\Query;
use yiiunit\data\ar\ActiveRecord;
use yiiunit\data\ar\Customer;
use yiiunit\data\ar\Item;
use yiiunit\framework\db\DatabaseTestCase;
use yiiunit\data\ar\Order;
......@@ -18,6 +20,7 @@ use yiiunit\data\ar\Order;
* @since 2.0
*
* @group data
* @group db
*/
class ActiveDataProviderTest extends DatabaseTestCase
{
......@@ -35,6 +38,8 @@ class ActiveDataProviderTest extends DatabaseTestCase
$orders = $provider->getModels();
$this->assertEquals(3, count($orders));
$this->assertTrue($orders[0] instanceof Order);
$this->assertTrue($orders[1] instanceof Order);
$this->assertTrue($orders[2] instanceof Order);
$this->assertEquals([1, 2, 3], $provider->getKeys());
$provider = new ActiveDataProvider([
......@@ -47,6 +52,75 @@ class ActiveDataProviderTest extends DatabaseTestCase
$this->assertEquals(2, count($orders));
}
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));
}
public function testQuery()
{
$query = new Query;
......
......@@ -119,10 +119,19 @@ class ActiveRecordTest extends DatabaseTestCase
{
/** @var Customer $customer */
$customer = Customer::find(2);
$this->assertFalse($customer->isRelationPopulated('orders'));
$orders = $customer->orders;
$this->assertTrue($customer->isRelationPopulated('orders'));
$this->assertEquals(2, count($orders));
$this->assertEquals(1, count($customer->populatedRelations));
/** @var Customer $customer */
$customer = Customer::find(2);
$this->assertFalse($customer->isRelationPopulated('orders'));
$orders = $customer->getOrders()->where('id=3')->all();
$this->assertFalse($customer->isRelationPopulated('orders'));
$this->assertEquals(0, count($customer->populatedRelations));
$this->assertEquals(1, count($orders));
$this->assertEquals(3, $orders[0]->id);
}
......@@ -131,8 +140,15 @@ class ActiveRecordTest extends DatabaseTestCase
{
$customers = Customer::find()->with('orders')->all();
$this->assertEquals(3, count($customers));
$this->assertTrue($customers[0]->isRelationPopulated('orders'));
$this->assertTrue($customers[1]->isRelationPopulated('orders'));
$this->assertEquals(1, count($customers[0]->orders));
$this->assertEquals(2, count($customers[1]->orders));
$customer = Customer::find()->with('orders')->one();
$this->assertTrue($customer->isRelationPopulated('orders'));
$this->assertEquals(1, count($customer->orders));
$this->assertEquals(1, count($customer->populatedRelations));
}
public function testFindLazyVia()
......
......@@ -86,19 +86,19 @@ class QueryTest extends DatabaseTestCase
{
$query = new Query;
$query->orderBy('team');
$this->assertEquals(['team' => false], $query->orderBy);
$this->assertEquals(['team' => SORT_ASC], $query->orderBy);
$query->addOrderBy('company');
$this->assertEquals(['team' => false, 'company' => false], $query->orderBy);
$this->assertEquals(['team' => SORT_ASC, 'company' => SORT_ASC], $query->orderBy);
$query->addOrderBy('age');
$this->assertEquals(['team' => false, 'company' => false, 'age' => false], $query->orderBy);
$this->assertEquals(['team' => SORT_ASC, 'company' => SORT_ASC, 'age' => SORT_ASC], $query->orderBy);
$query->addOrderBy(['age' => true]);
$this->assertEquals(['team' => false, 'company' => false, 'age' => true], $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' => false, 'company' => true, 'age' => false], $query->orderBy);
$this->assertEquals(['team' => SORT_ASC, 'company' => SORT_DESC, 'age' => SORT_ASC], $query->orderBy);
}
public function testLimitOffset()
......
<?php
namespace yiiunit\framework\db\cubrid;
use yiiunit\framework\data\ActiveDataProviderTest;
/**
* @group db
* @group cubrid
* @group data
*/
class CubridActiveDataProviderTest extends ActiveDataProviderTest
{
public $driverName = 'cubrid';
}
<?php
namespace yiiunit\framework\db\mssql;
use yiiunit\framework\data\ActiveDataProviderTest;
/**
* @group db
* @group mssql
* @group data
*/
class MssqlActiveDataProviderTest extends ActiveDataProviderTest
{
public $driverName = 'sqlsrv';
}
<?php
namespace yiiunit\framework\db\pgsql;
use yiiunit\framework\data\ActiveDataProviderTest;
/**
* @group db
* @group pgsql
* @group data
*/
class PostgreSQLActiveDataProviderTest extends ActiveDataProviderTest
{
public $driverName = 'pgsql';
}
<?php
namespace yiiunit\framework\db\sqlite;
use yiiunit\framework\data\ActiveDataProviderTest;
/**
* @group db
* @group sqlite
* @group data
*/
class SqliteActiveDataProviderTest extends ActiveDataProviderTest
{
public $driverName = 'sqlite';
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment