コード例 #1
0
ファイル: ActiveFixtureTest.php プロジェクト: howq/yii2
 public function testGetModel()
 {
     $test = new MyDbTestCase();
     $test->setUp();
     $fixture = $test->getFixture('customers');
     $this->assertEquals(Customer::className(), get_class($fixture->getModel('customer1')));
     $this->assertEquals(1, $fixture->getModel('customer1')->id);
     $this->assertEquals('*****@*****.**', $fixture->getModel('customer1')->email);
     $this->assertEquals(2, $fixture->getModel('customer2')->id);
     $this->assertEquals('*****@*****.**', $fixture->getModel('customer2')->email);
     $test->tearDown();
 }
コード例 #2
0
 public function testActiveRelation()
 {
     /** @var Customer $customer */
     $customer = Customer::findOne(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));
 }
コード例 #3
0
ファイル: ActiveRecordTest.php プロジェクト: howq/yii2
 public function testIssues()
 {
     // https://github.com/yiisoft/yii2/issues/4938
     $category = Category::findOne(2);
     $this->assertTrue($category instanceof Category);
     $this->assertEquals(3, $category->getItems()->count());
     $this->assertEquals(1, $category->getLimitedItems()->count());
     $this->assertEquals(1, $category->getLimitedItems()->distinct(true)->count());
     // https://github.com/yiisoft/yii2/issues/3197
     $orders = Order::find()->with('orderItems')->orderBy('id')->all();
     $this->assertEquals(3, count($orders));
     $this->assertEquals(2, count($orders[0]->orderItems));
     $this->assertEquals(3, count($orders[1]->orderItems));
     $this->assertEquals(1, count($orders[2]->orderItems));
     $orders = Order::find()->with(['orderItems' => function ($q) {
         $q->indexBy('item_id');
     }])->orderBy('id')->all();
     $this->assertEquals(3, count($orders));
     $this->assertEquals(2, count($orders[0]->orderItems));
     $this->assertEquals(3, count($orders[1]->orderItems));
     $this->assertEquals(1, count($orders[2]->orderItems));
     // https://github.com/yiisoft/yii2/issues/8149
     $model = new Customer();
     $model->name = 'test';
     $model->email = 'test';
     $model->save(false);
     $model->updateCounters(['status' => 1]);
     $this->assertEquals(1, $model->status);
 }
コード例 #4
0
 public function testPopulateWithoutPk()
 {
     // tests with single pk asArray
     $aggregation = Customer::find()->select(['{{customer}}.[[status]]', 'SUM({{order}}.[[total]]) AS [[sumtotal]]'])->joinWith('ordersPlain', false)->groupBy('{{customer}}.[[status]]')->orderBy('status')->asArray()->all();
     $expected = [['status' => 1, 'sumtotal' => 183], ['status' => 2, 'sumtotal' => 0]];
     $this->assertEquals($expected, $aggregation);
     // tests with single pk with Models
     $aggregation = Customer::find()->select(['{{customer}}.[[status]]', 'SUM({{order}}.[[total]]) AS [[sumTotal]]'])->joinWith('ordersPlain', false)->groupBy('{{customer}}.[[status]]')->orderBy('status')->all();
     $this->assertCount(2, $aggregation);
     $this->assertContainsOnlyInstancesOf(Customer::className(), $aggregation);
     foreach ($aggregation as $item) {
         if ($item->status == 1) {
             $this->assertEquals(183, $item->sumTotal);
         } elseif ($item->status == 2) {
             $this->assertEquals(0, $item->sumTotal);
         }
     }
     // tests with composite pk asArray
     $aggregation = OrderItem::find()->select(['[[order_id]]', 'SUM([[subtotal]]) AS [[subtotal]]'])->joinWith('order', false)->groupBy('[[order_id]]')->orderBy('[[order_id]]')->asArray()->all();
     $expected = [['order_id' => 1, 'subtotal' => 70], ['order_id' => 2, 'subtotal' => 33], ['order_id' => 3, 'subtotal' => 40]];
     $this->assertEquals($expected, $aggregation);
     // tests with composite pk with Models
     $aggregation = OrderItem::find()->select(['[[order_id]]', 'SUM([[subtotal]]) AS [[subtotal]]'])->joinWith('order', false)->groupBy('[[order_id]]')->orderBy('[[order_id]]')->all();
     $this->assertCount(3, $aggregation);
     $this->assertContainsOnlyInstancesOf(OrderItem::className(), $aggregation);
     foreach ($aggregation as $item) {
         if ($item->order_id == 1) {
             $this->assertEquals(70, $item->subtotal);
         } elseif ($item->order_id == 2) {
             $this->assertEquals(33, $item->subtotal);
         } elseif ($item->order_id == 3) {
             $this->assertEquals(40, $item->subtotal);
         }
     }
 }
コード例 #5
0
 public function testInverseOf()
 {
     // eager loading: find one and all
     $customer = Customer::find()->with('orders2')->where(['id' => 1])->one();
     $this->assertTrue($customer->orders2[0]->customer2 === $customer);
     $customers = Customer::find()->with('orders2')->where(['id' => [1, 3]])->all();
     $this->assertTrue($customers[0]->orders2[0]->customer2 === $customers[0]);
     $this->assertTrue(empty($customers[1]->orders2));
     // lazy loading
     $customer = Customer::findOne(2);
     $orders = $customer->orders2;
     $this->assertTrue(count($orders) === 2);
     $this->assertTrue($customer->orders2[0]->customer2 === $customer);
     $this->assertTrue($customer->orders2[1]->customer2 === $customer);
     // ad-hoc lazy loading
     $customer = Customer::findOne(2);
     $orders = $customer->getOrders2()->all();
     $this->assertTrue(count($orders) === 2);
     $this->assertTrue($customer->orders2[0]->customer2 === $customer);
     $this->assertTrue($customer->orders2[1]->customer2 === $customer);
     // the other way around
     $customer = Customer::find()->with('orders2')->where(['id' => 1])->asArray()->one();
     $this->assertTrue($customer['orders2'][0]['customer2']['id'] === $customer['id']);
     $customers = Customer::find()->with('orders2')->where(['id' => [1, 3]])->asArray()->all();
     $this->assertTrue($customer['orders2'][0]['customer2']['id'] === $customers[0]['id']);
     $this->assertTrue(empty($customers[1]['orders2']));
     $orders = Order::find()->with('customer2')->where(['id' => 1])->all();
     $this->assertTrue($orders[0]->customer2->orders2 === [$orders[0]]);
     $order = Order::find()->with('customer2')->where(['id' => 1])->one();
     $this->assertTrue($order->customer2->orders2 === [$order]);
     $orders = Order::find()->with('customer2')->where(['id' => 1])->asArray()->all();
     $this->assertTrue($orders[0]['customer2']['orders2'][0]['id'] === $orders[0]['id']);
     $order = Order::find()->with('customer2')->where(['id' => 1])->asArray()->one();
     $this->assertTrue($order['customer2']['orders2'][0]['id'] === $orders[0]['id']);
     $orders = Order::find()->with('customer2')->where(['id' => [1, 3]])->all();
     $this->assertTrue($orders[0]->customer2->orders2 === [$orders[0]]);
     $this->assertTrue($orders[1]->customer2->orders2 === [$orders[1]]);
     $orders = Order::find()->with('customer2')->where(['id' => [2, 3]])->orderBy('id')->all();
     $this->assertTrue($orders[0]->customer2->orders2 === $orders);
     $this->assertTrue($orders[1]->customer2->orders2 === $orders);
     $orders = Order::find()->with('customer2')->where(['id' => [2, 3]])->orderBy('id')->asArray()->all();
     $this->assertTrue($orders[0]['customer2']['orders2'][0]['id'] === $orders[0]['id']);
     $this->assertTrue($orders[0]['customer2']['orders2'][1]['id'] === $orders[1]['id']);
     $this->assertTrue($orders[1]['customer2']['orders2'][0]['id'] === $orders[0]['id']);
     $this->assertTrue($orders[1]['customer2']['orders2'][1]['id'] === $orders[1]['id']);
 }
コード例 #6
0
 public function testActiveQuery()
 {
     $db = $this->getConnection();
     $query = Customer::find()->orderBy('id');
     $customers = [];
     foreach ($query->batch(2, $db) as $models) {
         $customers = array_merge($customers, $models);
     }
     $this->assertEquals(3, count($customers));
     $this->assertEquals('user1', $customers[0]->name);
     $this->assertEquals('user2', $customers[1]->name);
     $this->assertEquals('user3', $customers[2]->name);
     // batch with eager loading
     $query = Customer::find()->with('orders')->orderBy('id');
     $customers = [];
     foreach ($query->batch(2, $db) as $models) {
         $customers = array_merge($customers, $models);
         foreach ($models as $model) {
             $this->assertTrue($model->isRelationPopulated('orders'));
         }
     }
     $this->assertEquals(3, count($customers));
     $this->assertEquals(1, count($customers[0]->orders));
     $this->assertEquals(2, count($customers[1]->orders));
     $this->assertEquals(0, count($customers[2]->orders));
 }
コード例 #7
0
 public function testMasterSlave()
 {
     $counts = [[0, 2], [1, 2], [2, 2]];
     foreach ($counts as $count) {
         list($masterCount, $slaveCount) = $count;
         $db = $this->prepareMasterSlave($masterCount, $slaveCount);
         $this->assertTrue($db->getSlave() instanceof Connection);
         $this->assertTrue($db->getSlave()->isActive);
         $this->assertFalse($db->isActive);
         // test SELECT uses slave
         $this->assertEquals(2, $db->createCommand('SELECT COUNT(*) FROM profile')->queryScalar());
         $this->assertFalse($db->isActive);
         // test UPDATE uses master
         $db->createCommand("UPDATE profile SET description='test' WHERE id=1")->execute();
         $this->assertTrue($db->isActive);
         $this->assertNotEquals('test', $db->createCommand("SELECT description FROM profile WHERE id=1")->queryScalar());
         $result = $db->useMaster(function (Connection $db) {
             return $db->createCommand("SELECT description FROM profile WHERE id=1")->queryScalar();
         });
         $this->assertEquals('test', $result);
         // test ActiveRecord read/write split
         ActiveRecord::$db = $db = $this->prepareMasterSlave($masterCount, $slaveCount);
         $this->assertFalse($db->isActive);
         $customer = Customer::findOne(1);
         $this->assertTrue($customer instanceof Customer);
         $this->assertEquals('user1', $customer->name);
         $this->assertFalse($db->isActive);
         $customer->name = 'test';
         $customer->save();
         $this->assertTrue($db->isActive);
         $customer = Customer::findOne(1);
         $this->assertTrue($customer instanceof Customer);
         $this->assertEquals('user1', $customer->name);
         $result = $db->useMaster(function () {
             return Customer::findOne(1)->name;
         });
         $this->assertEquals('test', $result);
     }
 }