Exemplo n.º 1
0
 public function testFind()
 {
     ActiveRecord::$db = $this->getConnection();
     // find one
     $result = Customer::find();
     $this->assertTrue($result instanceof ActiveQuery);
     $customer = $result->one();
     $this->assertTrue($customer instanceof Customer);
     // find all
     $customers = Customer::find()->all();
     $this->assertEquals(10, count($customers));
     $this->assertTrue($customers[0] instanceof Customer);
     $this->assertTrue($customers[1] instanceof Customer);
     // find by _id
     $testId = $this->testRows[0]['id'];
     $customer = Customer::findOne($testId);
     $this->assertTrue($customer instanceof Customer);
     $this->assertEquals($testId, $customer->id);
     // find by column values
     $customer = Customer::findOne(['name' => 'name5']);
     $this->assertTrue($customer instanceof Customer);
     $this->assertEquals($this->testRows[4]['id'], $customer->id);
     $this->assertEquals('name5', $customer->name);
     $customer = Customer::findOne(['name' => 'unexisting name']);
     $this->assertNull($customer);
     // find by attributes
     $customer = Customer::find()->where(['status' => 4])->one();
     $this->assertTrue($customer instanceof Customer);
     $this->assertEquals(4, $customer->status);
     // find count, sum, average, min, max, distinct
     $this->assertEquals(10, Customer::find()->count());
     $this->assertEquals(1, Customer::find()->where(['status' => 2])->count());
     $this->assertEquals((1 + 10) / 2 * 10, Customer::find()->sum('status'));
     $this->assertEquals((1 + 10) / 2, Customer::find()->average('status'));
     $this->assertEquals(1, Customer::find()->min('status'));
     $this->assertEquals(10, Customer::find()->max('status'));
     // $this->assertEquals(range(1, 10), Customer::find()->distinct('status')); this fails
     // scope
     $this->assertEquals(1, Customer::find()->activeOnly()->count());
     // asArray fails because column type
     // $testRow = $this->testRows[2];
     // $customer = Customer::find()->where(['id' => $testRow['id']])->asArray()->one();
     // $this->assertEquals($testRow, $customer);
     // indexBy
     $customers = Customer::find()->indexBy('name')->all();
     $this->assertTrue($customers['name1'] instanceof Customer);
     $this->assertTrue($customers['name2'] instanceof Customer);
     // indexBy callable
     $customers = Customer::find()->indexBy(function ($customer) {
         return $customer->status . '-' . $customer->status;
     })->all();
     $this->assertTrue($customers['1-1'] instanceof Customer);
     $this->assertTrue($customers['2-2'] instanceof Customer);
 }
Exemplo n.º 2
0
 protected function setUp()
 {
     parent::setUp();
     ActiveRecord::$db = $this->getConnection();
     $this->setUpTestRows();
 }