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(); }
public function getCustomerClass() { return Customer::className(); }
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); } } }