1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://www.yiiframework.com/ |
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
5
|
|
|
* @license https://www.yiiframework.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace yii\test; |
9
|
|
|
|
10
|
|
|
use Yii; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* InitDbFixture represents the initial state needed for DB-related tests. |
14
|
|
|
* |
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. |
18
|
|
|
* |
19
|
|
|
* Besides, DbFixture also attempts to load an [[initScript|initialization script]] if it exists. |
20
|
|
|
* |
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. |
23
|
|
|
* |
24
|
|
|
* For more details and usage information on InitDbFixture, see the [guide article on fixtures](guide:test-fixtures). |
25
|
|
|
* |
26
|
|
|
* @author Qiang Xue <[email protected]> |
27
|
|
|
* @since 2.0 |
28
|
|
|
*/ |
29
|
|
|
class InitDbFixture extends DbFixture |
30
|
|
|
{ |
31
|
|
|
/** |
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. |
35
|
|
|
*/ |
36
|
|
|
public $initScript = '@app/tests/fixtures/initdb.php'; |
37
|
|
|
/** |
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. |
42
|
|
|
*/ |
43
|
|
|
public $schemas = ['']; |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
|
|
public function beforeLoad() |
50
|
|
|
{ |
51
|
|
|
$this->checkIntegrity(false); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
|
|
public function afterLoad() |
58
|
|
|
{ |
59
|
|
|
$this->checkIntegrity(true); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
|
|
public function load() |
66
|
|
|
{ |
67
|
|
|
$file = Yii::getAlias($this->initScript); |
68
|
|
|
if (is_file($file)) { |
|
|
|
|
69
|
|
|
require $file; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
*/ |
76
|
|
|
public function beforeUnload() |
77
|
|
|
{ |
78
|
|
|
$this->checkIntegrity(false); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritdoc} |
83
|
|
|
*/ |
84
|
|
|
public function afterUnload() |
85
|
|
|
{ |
86
|
|
|
$this->checkIntegrity(true); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Toggles the DB integrity check. |
91
|
|
|
* @param bool $check whether to turn on or off the integrity check. |
92
|
|
|
*/ |
93
|
|
|
public function checkIntegrity($check) |
94
|
|
|
{ |
95
|
|
|
if (!$this->db instanceof \yii\db\Connection) { |
96
|
|
|
return; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if ($this->db->getDriverName() === 'oci') { |
100
|
|
|
return; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
foreach ($this->schemas as $schema) { |
104
|
|
|
$this->db->createCommand()->checkIntegrity($check, $schema)->execute(); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|