Exemplo n.º 1
0
 public function testIsPrimaryKey()
 {
     $this->assertFalse(Customer::isPrimaryKey([]));
     $this->assertTrue(Customer::isPrimaryKey(['id']));
     $this->assertFalse(Customer::isPrimaryKey(['id', 'name']));
     $this->assertFalse(Customer::isPrimaryKey(['name']));
     $this->assertFalse(Customer::isPrimaryKey(['name', 'email']));
     $this->assertFalse(OrderItem::isPrimaryKey([]));
     $this->assertFalse(OrderItem::isPrimaryKey(['order_id']));
     $this->assertFalse(OrderItem::isPrimaryKey(['item_id']));
     $this->assertFalse(OrderItem::isPrimaryKey(['quantity']));
     $this->assertFalse(OrderItem::isPrimaryKey(['quantity', 'subtotal']));
     $this->assertTrue(OrderItem::isPrimaryKey(['order_id', 'item_id']));
     $this->assertFalse(OrderItem::isPrimaryKey(['order_id', 'item_id', 'quantity']));
 }
Exemplo n.º 2
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.º 3
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);
         }
     }
 }