ActiveRecordTest.php 8.06 KB
Newer Older
1 2
<?php

3
namespace yiiunit\extensions\mongodb;
4

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

/**
10
 * @group mongodb
11
 */
12
class ActiveRecordTest extends MongoDbTestCase
13
{
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
    /**
     * @var array[] list of test rows.
     */
    protected $testRows = [];

    protected function setUp()
    {
        parent::setUp();
        ActiveRecord::$db = $this->getConnection();
        $this->setUpTestRows();
    }

    protected function tearDown()
    {
        $this->dropCollection(Customer::collectionName());
        parent::tearDown();
    }

    /**
     * Sets up test rows.
     */
    protected function setUpTestRows()
    {
        $collection = $this->getConnection()->getCollection('customer');
        $rows = [];
        for ($i = 1; $i <= 10; $i++) {
            $rows[] = [
                'name' => 'name' . $i,
                'email' => 'email' . $i,
                'address' => 'address' . $i,
                'status' => $i,
            ];
        }
        $collection->batchInsert($rows);
        $this->testRows = $rows;
    }

    // Tests :

    public function testFind()
    {
        // find one
        $result = Customer::find();
        $this->assertTrue($result instanceof ActiveQuery);
        $customer = $result->one();
        $this->assertTrue($customer instanceof Customer);

        // find all
        $customers = Customer::find()->all();
        $this->assertEquals(10, count($customers));
        $this->assertTrue($customers[0] instanceof Customer);
        $this->assertTrue($customers[1] instanceof Customer);

        // find by _id
        $testId = $this->testRows[0]['_id'];
Alexander Makarov committed
69
        $customer = Customer::findOne($testId);
70 71 72 73
        $this->assertTrue($customer instanceof Customer);
        $this->assertEquals($testId, $customer->_id);

        // find by column values
Alexander Makarov committed
74
        $customer = Customer::findOne(['name' => 'name5']);
75 76 77
        $this->assertTrue($customer instanceof Customer);
        $this->assertEquals($this->testRows[4]['_id'], $customer->_id);
        $this->assertEquals('name5', $customer->name);
Alexander Makarov committed
78
        $customer = Customer::findOne(['name' => 'unexisting name']);
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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
        $this->assertNull($customer);

        // find by attributes
        $customer = Customer::find()->where(['status' => 4])->one();
        $this->assertTrue($customer instanceof Customer);
        $this->assertEquals(4, $customer->status);

        // find count, sum, average, min, max, distinct
        $this->assertEquals(10, Customer::find()->count());
        $this->assertEquals(1, Customer::find()->where(['status' => 2])->count());
        $this->assertEquals((1+10)/2*10, Customer::find()->sum('status'));
        $this->assertEquals((1+10)/2, Customer::find()->average('status'));
        $this->assertEquals(1, Customer::find()->min('status'));
        $this->assertEquals(10, Customer::find()->max('status'));
        $this->assertEquals(range(1, 10), Customer::find()->distinct('status'));

        // scope
        $this->assertEquals(1, Customer::find()->activeOnly()->count());

        // asArray
        $testRow = $this->testRows[2];
        $customer = Customer::find()->where(['_id' => $testRow['_id']])->asArray()->one();
        $this->assertEquals($testRow, $customer);

        // indexBy
        $customers = Customer::find()->indexBy('name')->all();
        $this->assertTrue($customers['name1'] instanceof Customer);
        $this->assertTrue($customers['name2'] instanceof Customer);

        // indexBy callable
        $customers = Customer::find()->indexBy(function ($customer) {
            return $customer->status . '-' . $customer->status;
        })->all();
        $this->assertTrue($customers['1-1'] instanceof Customer);
        $this->assertTrue($customers['2-2'] instanceof Customer);
    }

    public function testInsert()
    {
        $record = new Customer;
        $record->name = 'new name';
        $record->email = 'new email';
        $record->address = 'new address';
        $record->status = 7;

        $this->assertTrue($record->isNewRecord);

        $record->save();

        $this->assertTrue($record->_id instanceof \MongoId);
        $this->assertFalse($record->isNewRecord);
    }

    /**
     * @depends testInsert
     */
    public function testUpdate()
    {
        $record = new Customer;
        $record->name = 'new name';
        $record->email = 'new email';
        $record->address = 'new address';
        $record->status = 7;
        $record->save();

        // save
Alexander Makarov committed
145
        $record = Customer::findOne($record->_id);
146 147 148 149 150 151 152 153
        $this->assertTrue($record instanceof Customer);
        $this->assertEquals(7, $record->status);
        $this->assertFalse($record->isNewRecord);

        $record->status = 9;
        $record->save();
        $this->assertEquals(9, $record->status);
        $this->assertFalse($record->isNewRecord);
Alexander Makarov committed
154
        $record2 = Customer::findOne($record->_id);
155 156 157 158 159 160
        $this->assertEquals(9, $record2->status);

        // updateAll
        $pk = ['_id' => $record->_id];
        $ret = Customer::updateAll(['status' => 55], $pk);
        $this->assertEquals(1, $ret);
Alexander Makarov committed
161
        $record = Customer::findOne($pk);
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
        $this->assertEquals(55, $record->status);
    }

    /**
     * @depends testInsert
     */
    public function testDelete()
    {
        // delete
        $record = new Customer;
        $record->name = 'new name';
        $record->email = 'new email';
        $record->address = 'new address';
        $record->status = 7;
        $record->save();

Alexander Makarov committed
178
        $record = Customer::findOne($record->_id);
179
        $record->delete();
Alexander Makarov committed
180
        $record = Customer::findOne($record->_id);
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
        $this->assertNull($record);

        // deleteAll
        $record = new Customer;
        $record->name = 'new name';
        $record->email = 'new email';
        $record->address = 'new address';
        $record->status = 7;
        $record->save();

        $ret = Customer::deleteAll(['name' => 'new name']);
        $this->assertEquals(1, $ret);
        $records = Customer::find()->where(['name' => 'new name'])->all();
        $this->assertEquals(0, count($records));
    }

    public function testUpdateAllCounters()
    {
        $this->assertEquals(1, Customer::updateAllCounters(['status' => 10], ['status' => 10]));

Alexander Makarov committed
201
        $record = Customer::findOne(['status' => 10]);
202 203 204 205 206 207 208 209
        $this->assertNull($record);
    }

    /**
     * @depends testUpdateAllCounters
     */
    public function testUpdateCounters()
    {
Alexander Makarov committed
210
        $record = Customer::findOne($this->testRows[9]);
211 212 213 214 215 216

        $originalCounter = $record->status;
        $counterIncrement = 20;
        $record->updateCounters(['status' => $counterIncrement]);
        $this->assertEquals($originalCounter + $counterIncrement, $record->status);

Alexander Makarov committed
217
        $refreshedRecord = Customer::findOne($record->_id);
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
        $this->assertEquals($originalCounter + $counterIncrement, $refreshedRecord->status);
    }

    /**
     * @depends testUpdate
     */
    public function testUpdateNestedAttribute()
    {
        $record = new Customer;
        $record->name = 'new name';
        $record->email = 'new email';
        $record->address = [
            'city' => 'SomeCity',
            'street' => 'SomeStreet',
        ];
        $record->status = 7;
        $record->save();

        // save
Alexander Makarov committed
237
        $record = Customer::findOne($record->_id);
238 239 240 241 242
        $newAddress = [
            'city' => 'AnotherCity'
        ];
        $record->address = $newAddress;
        $record->save();
Alexander Makarov committed
243
        $record2 = Customer::findOne($record->_id);
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
        $this->assertEquals($newAddress, $record2->address);
    }

    /**
     * @depends testFind
     * @depends testInsert
     */
    public function testQueryByIntegerField()
    {
        $record = new Customer;
        $record->name = 'new name';
        $record->status = 7;
        $record->save();

        $row = Customer::find()->where(['status' => 7])->one();
        $this->assertNotEmpty($row);
        $this->assertEquals(7, $row->status);

        $rowRefreshed = Customer::find()->where(['status' => $row->status])->one();
        $this->assertNotEmpty($rowRefreshed);
        $this->assertEquals(7, $rowRefreshed->status);
    }
AlexGx committed
266
}