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