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\base\InvalidConfigException; |
||
11 | use yii\db\ActiveRecord; |
||
12 | use yii\db\TableSchema; |
||
13 | |||
14 | /** |
||
15 | * ActiveFixture represents a fixture backed up by a [[modelClass|ActiveRecord class]] or a [[tableName|database table]]. |
||
16 | * |
||
17 | * Either [[modelClass]] or [[tableName]] must be set. You should also provide fixture data in the file |
||
18 | * specified by [[dataFile]] or overriding [[getData()]] if you want to use code to generate the fixture data. |
||
19 | * |
||
20 | * When the fixture is being loaded, it will first call [[resetTable()]] to remove any existing data in the table. |
||
21 | * It will then populate the table with the data returned by [[getData()]]. |
||
22 | * |
||
23 | * After the fixture is loaded, you can access the loaded data via the [[data]] property. If you set [[modelClass]], |
||
24 | * you will also be able to retrieve an instance of [[modelClass]] with the populated data via [[getModel()]]. |
||
25 | * |
||
26 | * For more details and usage information on ActiveFixture, see the [guide article on fixtures](guide:test-fixtures). |
||
27 | * |
||
28 | * @property-read TableSchema $tableSchema The schema information of the database table associated with this |
||
29 | * fixture. |
||
30 | * |
||
31 | * @author Qiang Xue <[email protected]> |
||
32 | * @since 2.0 |
||
33 | */ |
||
34 | class ActiveFixture extends BaseActiveFixture |
||
35 | { |
||
36 | /** |
||
37 | * @var string|null the name of the database table that this fixture is about. If this property is not set, |
||
38 | * the table name will be determined via [[modelClass]]. |
||
39 | * @see modelClass |
||
40 | */ |
||
41 | public $tableName; |
||
42 | /** |
||
43 | * @var string|bool|null the file path or [path alias](guide:concept-aliases) of the data file that contains the fixture data |
||
44 | * to be returned by [[getData()]]. If this is not set, it will default to `FixturePath/data/TableName.php`, |
||
45 | * where `FixturePath` stands for the directory containing this fixture class, and `TableName` stands for the |
||
46 | * name of the table associated with this fixture. You can set this property to be false to prevent loading any data. |
||
47 | */ |
||
48 | public $dataFile; |
||
49 | |||
50 | /** |
||
51 | * @var TableSchema the table schema for the table associated with this fixture |
||
52 | */ |
||
53 | private $_table; |
||
54 | |||
55 | |||
56 | /** |
||
57 | * {@inheritdoc} |
||
58 | */ |
||
59 | 29 | public function init() |
|
60 | { |
||
61 | 29 | parent::init(); |
|
62 | 29 | if ($this->tableName === null) { |
|
63 | 29 | if ($this->modelClass === null) { |
|
64 | throw new InvalidConfigException('Either "modelClass" or "tableName" must be set.'); |
||
65 | } |
||
66 | /** @var ActiveRecord $modelClass */ |
||
67 | 29 | $modelClass = $this->modelClass; |
|
68 | 29 | $this->db = $modelClass::getDb(); |
|
69 | } |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * Loads the fixture. |
||
74 | * |
||
75 | * It populate the table with the data returned by [[getData()]]. |
||
76 | * |
||
77 | * If you override this method, you should consider calling the parent implementation |
||
78 | * so that the data returned by [[getData()]] can be populated into the table. |
||
79 | */ |
||
80 | 23 | public function load() |
|
81 | { |
||
82 | 23 | $this->data = []; |
|
83 | 23 | $table = $this->getTableSchema(); |
|
84 | 23 | foreach ($this->getData() as $alias => $row) { |
|
85 | 16 | $primaryKeys = $this->db->schema->insert($table->fullName, $row); |
|
86 | 16 | $this->data[$alias] = array_merge($row, $primaryKeys); |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
87 | } |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * Returns the fixture data. |
||
92 | * |
||
93 | * The default implementation will try to return the fixture data by including the external file specified by [[dataFile]]. |
||
94 | * The file should return an array of data rows (column name => column value), each corresponding to a row in the table. |
||
95 | * |
||
96 | * If the data file does not exist, an empty array will be returned. |
||
97 | * |
||
98 | * @return array the data rows to be inserted into the database table. |
||
99 | */ |
||
100 | 23 | protected function getData() |
|
101 | { |
||
102 | 23 | if ($this->dataFile === null) { |
|
103 | 19 | if ($this->dataDirectory !== null) { |
|
104 | 4 | $dataFile = $this->getTableSchema()->fullName . '.php'; |
|
105 | } else { |
||
106 | 15 | $class = new \ReflectionClass($this); |
|
107 | 15 | $dataFile = dirname($class->getFileName()) . '/data/' . $this->getTableSchema()->fullName . '.php'; |
|
108 | } |
||
109 | |||
110 | 19 | return $this->loadData($dataFile, false); |
|
111 | } |
||
112 | 4 | return parent::getData(); |
|
113 | } |
||
114 | |||
115 | /** |
||
116 | * {@inheritdoc} |
||
117 | */ |
||
118 | 25 | public function unload() |
|
119 | { |
||
120 | 25 | $this->resetTable(); |
|
121 | 25 | parent::unload(); |
|
122 | } |
||
123 | |||
124 | /** |
||
125 | * Removes all existing data from the specified table and resets sequence number to 1 (if any). |
||
126 | * This method is called before populating fixture data into the table associated with this fixture. |
||
127 | */ |
||
128 | 25 | protected function resetTable() |
|
129 | { |
||
130 | 25 | $table = $this->getTableSchema(); |
|
131 | 25 | $this->db->createCommand()->delete($table->fullName)->execute(); |
|
132 | 25 | if ($table->sequenceName !== null) { |
|
133 | 25 | $this->db->createCommand()->executeResetSequence($table->fullName, 1); |
|
134 | } |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * @return TableSchema the schema information of the database table associated with this fixture. |
||
139 | * @throws \yii\base\InvalidConfigException if the table does not exist |
||
140 | */ |
||
141 | 25 | public function getTableSchema() |
|
142 | { |
||
143 | 25 | if ($this->_table !== null) { |
|
144 | 23 | return $this->_table; |
|
145 | } |
||
146 | |||
147 | 25 | $db = $this->db; |
|
148 | 25 | $tableName = $this->tableName; |
|
149 | 25 | if ($tableName === null) { |
|
150 | /* @var $modelClass \yii\db\ActiveRecord */ |
||
151 | 25 | $modelClass = $this->modelClass; |
|
152 | 25 | $tableName = $modelClass::tableName(); |
|
153 | } |
||
154 | |||
155 | 25 | $this->_table = $db->getSchema()->getTableSchema($tableName); |
|
156 | 25 | if ($this->_table === null) { |
|
157 | throw new InvalidConfigException("Table does not exist: {$tableName}"); |
||
158 | } |
||
159 | |||
160 | 25 | return $this->_table; |
|
161 | } |
||
162 | } |
||
163 |