Beispiel #1
0
 protected function setUp()
 {
     parent::setUp();
     ActiveRecord::$db = $this->getConnection();
     $this->mockWebApplication();
     $this->breadcrumbs = new Breadcrumbs();
 }
 public function testMasterSlave()
 {
     $counts = [[0, 2], [1, 2], [2, 2]];
     foreach ($counts as $count) {
         list($masterCount, $slaveCount) = $count;
         $db = $this->prepareMasterSlave($masterCount, $slaveCount);
         $this->assertTrue($db->getSlave() instanceof Connection);
         $this->assertTrue($db->getSlave()->isActive);
         $this->assertFalse($db->isActive);
         // test SELECT uses slave
         $this->assertEquals(2, $db->createCommand('SELECT COUNT(*) FROM profile')->queryScalar());
         $this->assertFalse($db->isActive);
         // test UPDATE uses master
         $db->createCommand("UPDATE profile SET description='test' WHERE id=1")->execute();
         $this->assertTrue($db->isActive);
         $this->assertNotEquals('test', $db->createCommand("SELECT description FROM profile WHERE id=1")->queryScalar());
         $result = $db->useMaster(function (Connection $db) {
             return $db->createCommand("SELECT description FROM profile WHERE id=1")->queryScalar();
         });
         $this->assertEquals('test', $result);
         // test ActiveRecord read/write split
         ActiveRecord::$db = $db = $this->prepareMasterSlave($masterCount, $slaveCount);
         $this->assertFalse($db->isActive);
         $customer = Customer::findOne(1);
         $this->assertTrue($customer instanceof Customer);
         $this->assertEquals('user1', $customer->name);
         $this->assertFalse($db->isActive);
         $customer->name = 'test';
         $customer->save();
         $this->assertTrue($db->isActive);
         $customer = Customer::findOne(1);
         $this->assertTrue($customer instanceof Customer);
         $this->assertEquals('user1', $customer->name);
         $result = $db->useMaster(function () {
             return Customer::findOne(1)->name;
         });
         $this->assertEquals('test', $result);
     }
 }
Beispiel #3
0
 public function setUp()
 {
     parent::setUp();
     $this->mockApplication();
     ActiveRecord::$db = $this->getConnection();
 }
Beispiel #4
0
 protected function setUp()
 {
     parent::setUp();
     ActiveRecord::$db = $this->getConnection();
 }
Beispiel #5
0
 public function afterSave($insert)
 {
     ActiveRecordTest::$afterSaveInsert = $insert;
     ActiveRecordTest::$afterSaveNewRecord = $this->isNewRecord;
     parent::afterSave($insert);
 }
Beispiel #6
0
 public function afterSave($insert, $changedAttributes)
 {
     ActiveRecordTest::$afterSaveInsert = $insert;
     ActiveRecordTest::$afterSaveNewRecord = $this->isNewRecord;
     parent::afterSave($insert, $changedAttributes);
 }
Beispiel #7
0
 public function setUp()
 {
     parent::setUp();
     \Yii::$app->set('db', $this->getConnection());
     ActiveRecord::$db = $this->getConnection();
 }