3
* @link https://www.yiiframework.com/
4
* @copyright Copyright (c) 2008 Yii Software LLC
5
* @license https://www.yiiframework.com/license/
13
* InitDbFixture represents the initial state needed for DB-related tests.
15
* Its main task is to toggle integrity check of the database during data loading.
16
* This is needed by other DB-related fixtures (e.g. [[ActiveFixture]]) so that they can populate
17
* data into the database without triggering integrity check errors.
19
* Besides, DbFixture also attempts to load an [[initScript|initialization script]] if it exists.
21
* You should normally use InitDbFixture to prepare a skeleton test database.
22
* Other DB fixtures will then add specific tables and data to this database.
24
* For more details and usage information on InitDbFixture, see the [guide article on fixtures](guide:test-fixtures).
26
* @author Qiang Xue <qiang.xue@gmail.com>
29
class InitDbFixture extends DbFixture
32
* @var string the init script file that should be executed when loading this fixture.
33
* This should be either a file path or [path alias](guide:concept-aliases). Note that if the file does not exist,
34
* no error will be raised.
36
public $initScript = '@app/tests/fixtures/initdb.php';
38
* @var array list of database schemas that the test tables may reside in. Defaults to
39
* `['']`, meaning using the default schema (an empty string refers to the
40
* default schema). This property is mainly used when turning on and off integrity checks
41
* so that fixture data can be populated into the database without causing problem.
43
public $schemas = [''];
49
public function beforeLoad()
51
$this->checkIntegrity(false);
57
public function afterLoad()
59
$this->checkIntegrity(true);
65
public function load()
67
$file = Yii::getAlias($this->initScript);
76
public function beforeUnload()
78
$this->checkIntegrity(false);
84
public function afterUnload()
86
$this->checkIntegrity(true);
90
* Toggles the DB integrity check.
91
* @param bool $check whether to turn on or off the integrity check.
93
public function checkIntegrity($check)
95
if (!$this->db instanceof \yii\db\Connection) {
98
foreach ($this->schemas as $schema) {
99
$this->db->createCommand()->checkIntegrity($check, $schema)->execute();