Exemple #1
0
 public function testLabelsExplicit()
 {
     $dataProvider = new ActiveDataProvider(['query' => Order::find(), 'models' => [new Order()], 'totalCount' => 1, 'sort' => ['attributes' => ['total'], 'route' => 'site/index']]);
     ob_start();
     echo ListView::widget(['dataProvider' => $dataProvider, 'layout' => "{sorter}"]);
     $actualHtml = ob_get_clean();
     $this->assertFalse(strpos($actualHtml, '<a href="/index.php?r=site%2Findex&amp;sort=customer_id" data-sort="customer_id">Customer</a>') !== false);
     $this->assertTrue(strpos($actualHtml, '<a href="/index.php?r=site%2Findex&amp;sort=total" data-sort="total">Invoice Total</a>') !== false);
 }
 public function testActiveQuery()
 {
     $provider = new ActiveDataProvider(['query' => Order::find()->orderBy('id')]);
     $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(['query' => Order::find(), 'pagination' => ['pageSize' => 2]]);
     $orders = $provider->getModels();
     $this->assertEquals(2, count($orders));
 }
Exemple #3
0
 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);
 }
 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']);
 }
 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));
 }