diff --git a/framework/yii/ar/ActiveRelationTrait.php b/framework/yii/ar/ActiveRelationTrait.php index 350718d..5d49641 100644 --- a/framework/yii/ar/ActiveRelationTrait.php +++ b/framework/yii/ar/ActiveRelationTrait.php @@ -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); diff --git a/tests/unit/framework/data/ActiveDataProviderTest.php b/tests/unit/framework/data/ActiveDataProviderTest.php index 3b445e0..4e4ca77 100644 --- a/tests/unit/framework/data/ActiveDataProviderTest.php +++ b/tests/unit/framework/data/ActiveDataProviderTest.php @@ -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; diff --git a/tests/unit/framework/db/ActiveRecordTest.php b/tests/unit/framework/db/ActiveRecordTest.php index d0d2f12..f96f2d3 100644 --- a/tests/unit/framework/db/ActiveRecordTest.php +++ b/tests/unit/framework/db/ActiveRecordTest.php @@ -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() diff --git a/tests/unit/framework/db/QueryTest.php b/tests/unit/framework/db/QueryTest.php index a48d719..77c1ac0 100644 --- a/tests/unit/framework/db/QueryTest.php +++ b/tests/unit/framework/db/QueryTest.php @@ -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() diff --git a/tests/unit/framework/db/cubrid/CubridActiveDataProviderTest.php b/tests/unit/framework/db/cubrid/CubridActiveDataProviderTest.php new file mode 100644 index 0000000..191442a --- /dev/null +++ b/tests/unit/framework/db/cubrid/CubridActiveDataProviderTest.php @@ -0,0 +1,14 @@ +