ActiveRelationTest.php 2.77 KB
Newer Older
1 2
<?php

3
namespace yiiunit\extensions\mongodb;
4

5 6 7
use yiiunit\data\ar\mongodb\ActiveRecord;
use yiiunit\data\ar\mongodb\Customer;
use yiiunit\data\ar\mongodb\CustomerOrder;
8 9

/**
10
 * @group mongodb
11
 */
12
class ActiveRelationTest extends MongoDbTestCase
13
{
14 15 16 17 18 19
    protected function setUp()
    {
        parent::setUp();
        ActiveRecord::$db = $this->getConnection();
        $this->setUpTestRows();
    }
20

21 22 23 24 25 26
    protected function tearDown()
    {
        $this->dropCollection(Customer::collectionName());
        $this->dropCollection(CustomerOrder::collectionName());
        parent::tearDown();
    }
27

28 29 30 31 32 33
    /**
     * Sets up test rows.
     */
    protected function setUpTestRows()
    {
        $customerCollection = $this->getConnection()->getCollection('customer');
34

35 36 37 38 39 40 41 42 43 44
        $customers = [];
        for ($i = 1; $i <= 5; $i++) {
            $customers[] = [
                'name' => 'name' . $i,
                'email' => 'email' . $i,
                'address' => 'address' . $i,
                'status' => $i,
            ];
        }
        $customerCollection->batchInsert($customers);
45

46 47 48 49 50 51 52 53 54 55 56 57 58 59
        $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'] + 100,
            ];
        }
        $customerOrderCollection->batchInsert($customerOrders);
    }
60

61
    // Tests :
62

63 64
    public function testFindLazy()
    {
65
        /* @var $order CustomerOrder */
Alexander Makarov committed
66
        $order = CustomerOrder::findOne(['number' => 2]);
67 68 69 70 71 72 73
        $this->assertFalse($order->isRelationPopulated('customer'));
        $customer = $order->customer;
        $this->assertTrue($order->isRelationPopulated('customer'));
        $this->assertTrue($customer instanceof Customer);
        $this->assertEquals((string) $customer->_id, (string) $order->customer_id);
        $this->assertEquals(1, count($order->relatedRecords));
    }
74

75 76 77 78 79 80 81 82 83 84 85
    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]->customer instanceof Customer);
        $this->assertEquals((string) $orders[0]->customer->_id, (string) $orders[0]->customer_id);
        $this->assertTrue($orders[1]->customer instanceof Customer);
        $this->assertEquals((string) $orders[1]->customer->_id, (string) $orders[1]->customer_id);
    }
86
}