From 3fd6d95aff244b8fbbbac31a6966c280bd93dba8 Mon Sep 17 00:00:00 2001 From: Paul Klimov Date: Fri, 29 Nov 2013 17:25:17 +0200 Subject: [PATCH] Unit test for Mongo Active Relation added. --- tests/unit/data/ar/mongo/Customer.php | 5 ++ tests/unit/data/ar/mongo/CustomerOrder.php | 27 +++++++ tests/unit/extensions/mongo/ActiveRelationTest.php | 83 ++++++++++++++++++++++ 3 files changed, 115 insertions(+) create mode 100644 tests/unit/data/ar/mongo/CustomerOrder.php create mode 100644 tests/unit/extensions/mongo/ActiveRelationTest.php diff --git a/tests/unit/data/ar/mongo/Customer.php b/tests/unit/data/ar/mongo/Customer.php index 16819ca..83c4255 100644 --- a/tests/unit/data/ar/mongo/Customer.php +++ b/tests/unit/data/ar/mongo/Customer.php @@ -24,4 +24,9 @@ class Customer extends ActiveRecord { $query->andWhere(['status' => 2]); } + + public function getOrders() + { + return $this->hasMany(CustomerOrder::className(), ['customer_id' => 'id']); + } } \ No newline at end of file diff --git a/tests/unit/data/ar/mongo/CustomerOrder.php b/tests/unit/data/ar/mongo/CustomerOrder.php new file mode 100644 index 0000000..2192be3 --- /dev/null +++ b/tests/unit/data/ar/mongo/CustomerOrder.php @@ -0,0 +1,27 @@ +hasOne(Customer::className(), ['id' => 'customer_id']); + } +} \ No newline at end of file diff --git a/tests/unit/extensions/mongo/ActiveRelationTest.php b/tests/unit/extensions/mongo/ActiveRelationTest.php new file mode 100644 index 0000000..ae12d72 --- /dev/null +++ b/tests/unit/extensions/mongo/ActiveRelationTest.php @@ -0,0 +1,83 @@ +getConnection(); + $this->setUpTestRows(); + } + + protected function tearDown() + { + $this->dropCollection(Customer::collectionName()); + $this->dropCollection(CustomerOrder::collectionName()); + parent::tearDown(); + } + + /** + * Sets up test rows. + */ + protected function setUpTestRows() + { + $customerCollection = $this->getConnection()->getCollection('customer'); + + $customers = []; + for ($i = 1; $i <= 5; $i++) { + $customers[] = [ + 'name' => 'name' . $i, + 'email' => 'email' . $i, + 'address' => 'address' . $i, + 'status' => $i, + ]; + } + $customerCollection->batchInsert($customers); + + $customerOrderCollection = $this->getConnection()->getCollection('customer_order'); + $customerOrders = []; + foreach ($customers as $customer) { + $customerOrders[] = [ + 'customer_id' => $customer['_id'], + 'number' => $customer['status'], + ]; + $customerOrders[] = [ + 'customer_id' => $customer['_id'], + 'number' => $customer['status'] + 1, + ]; + } + $customerOrderCollection->batchInsert($customerOrders); + } + + // Tests : + + public function testFindLazy() + { + /** @var CustomerOrder $order */ + $order = CustomerOrder::find(['number' => 2]); + $this->assertFalse($order->isRelationPopulated('customer')); + $index = $order->customer; + $this->assertTrue($order->isRelationPopulated('customer')); + $this->assertTrue($index instanceof Customer); + $this->assertEquals(1, count($order->populatedRelations)); + } + + public function testFindEager() + { + $orders = CustomerOrder::find()->with('customer')->all(); + $this->assertEquals(10, count($orders)); + $this->assertTrue($orders[0]->isRelationPopulated('customer')); + $this->assertTrue($orders[1]->isRelationPopulated('customer')); + $this->assertTrue($orders[0]->index instanceof ArticleIndex); + $this->assertTrue($orders[1]->index instanceof ArticleIndex); + } +} \ No newline at end of file