Exemplo n.º 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 testActiveRelationViaTable()
 {
     /** @var Order $order */
     $order = Order::findOne(1);
     $provider = new ActiveDataProvider(['query' => $order->getBooks()]);
     $items = $provider->getModels();
     $this->assertEquals(2, count($items));
     $this->assertTrue($items[0] instanceof Item);
     $this->assertTrue($items[1] instanceof Item);
     $provider = new ActiveDataProvider(['query' => $order->getBooks(), 'pagination' => ['pageSize' => 1]]);
     $items = $provider->getModels();
     $this->assertEquals(1, count($items));
 }
Exemplo n.º 3
0
 public function testValidateCompositeKeys()
 {
     $val = new UniqueValidator(['targetClass' => OrderItem::className(), 'targetAttribute' => ['order_id', 'item_id']]);
     // validate old record
     $m = OrderItem::findOne(['order_id' => 1, 'item_id' => 2]);
     $val->validateAttribute($m, 'order_id');
     $this->assertFalse($m->hasErrors('order_id'));
     $m->item_id = 1;
     $val->validateAttribute($m, 'order_id');
     $this->assertTrue($m->hasErrors('order_id'));
     // validate new record
     $m = new OrderItem(['order_id' => 1, 'item_id' => 2]);
     $val->validateAttribute($m, 'order_id');
     $this->assertTrue($m->hasErrors('order_id'));
     $m = new OrderItem(['order_id' => 10, 'item_id' => 2]);
     $val->validateAttribute($m, 'order_id');
     $this->assertFalse($m->hasErrors('order_id'));
     $val = new UniqueValidator(['targetClass' => OrderItem::className(), 'targetAttribute' => ['id' => 'order_id']]);
     // validate old record
     $m = Order::findOne(1);
     $val->validateAttribute($m, 'id');
     $this->assertTrue($m->hasErrors('id'));
     $m = Order::findOne(1);
     $m->id = 2;
     $val->validateAttribute($m, 'id');
     $this->assertTrue($m->hasErrors('id'));
     $m = Order::findOne(1);
     $m->id = 10;
     $val->validateAttribute($m, 'id');
     $this->assertFalse($m->hasErrors('id'));
     $m = new Order(['id' => 1]);
     $val->validateAttribute($m, 'id');
     $this->assertTrue($m->hasErrors('id'));
     $m = new Order(['id' => 10]);
     $val->validateAttribute($m, 'id');
     $this->assertFalse($m->hasErrors('id'));
 }
Exemplo n.º 4
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);
 }
Exemplo n.º 5
0
 public function getOrders2()
 {
     return $this->hasMany(Order::className(), ['customer_id' => 'id'])->inverseOf('customer2')->orderBy('id');
 }
Exemplo n.º 6
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']);
 }
 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));
 }