Another option: When you create db migration , you should apply it to production db and test db, in addition, you should fill the test tables with test data.
The advantages of this approach:
- You run sql sql only once (not like fixture - it calls a test every time).
- Your test will run quickly because db will be prepared.
- When you make a new function that needs a new test with new data in db, you create a db migration and it will be executed only once, in an explicit way.
For instance:
<?php class m150608_110143_init extends CDbMigration { public function safeUp() { $sql1 = " CREATE TABLE brand ( id INT AUTO_INCREMENT, name VARCHAR(100) NOT NULL DEFAULT '', country VARCHAR(50) NOT NULL DEFAULT '', PRIMARY KEY (id) ); "; $sql2 = " INSERT INTO brand VALUES (null, 'aston martin', 'UK'), (null, 'audi', 'Germany'), (null, 'bmw', 'Germany'), (null, 'citroen', 'France'), (null, 'peugeot', 'France'), (null, 'porsche', 'Germany'), (null, 'toyota', 'Japan'), (null, 'ferrari', 'Italy') ; "; // Production db. $this->setDbConnection(Yii::app()->db); $this->execute($sql1); // Test db. $this->setDbConnection(Yii::app()->dbUnitTest); $this->execute($sql1); // Populate test db with fixtures. $this->execute($sql2); return true; } public function down() { $sql = 'DROP TABLE brand;'; // Test db. $this->setDbConnection(Yii::app()->dbUnitTest); $this->execute($sql); // Production db. $this->setDbConnection(Yii::app()->db); $this->execute($sql); return true; } }
And in the test, you do not need to think about lights.
source share