1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link http://www.yiiframework.com/ |
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
5
|
|
|
* @license http://www.yiiframework.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace yii\test; |
9
|
|
|
|
10
|
|
|
use Yii; |
11
|
|
|
use yii\base\InvalidConfigException; |
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
|
|
|
* @property TableSchema $tableSchema The schema information of the database table associated with this |
27
|
|
|
* fixture. This property is read-only. |
28
|
|
|
* |
29
|
|
|
* @author Qiang Xue <[email protected]> |
30
|
|
|
* @since 2.0 |
31
|
|
|
*/ |
32
|
|
|
class ActiveFixture extends BaseActiveFixture |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var string the name of the database table that this fixture is about. If this property is not set, |
36
|
|
|
* the table name will be determined via [[modelClass]]. |
37
|
|
|
* @see modelClass |
38
|
|
|
*/ |
39
|
|
|
public $tableName; |
40
|
|
|
/** |
41
|
|
|
* @var string|boolean the file path or path alias of the data file that contains the fixture data |
42
|
|
|
* to be returned by [[getData()]]. If this is not set, it will default to `FixturePath/data/TableName.php`, |
43
|
|
|
* where `FixturePath` stands for the directory containing this fixture class, and `TableName` stands for the |
44
|
|
|
* name of the table associated with this fixture. You can set this property to be false to prevent loading any data. |
45
|
|
|
*/ |
46
|
|
|
public $dataFile; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var TableSchema the table schema for the table associated with this fixture |
50
|
|
|
*/ |
51
|
|
|
private $_table; |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @inheritdoc |
56
|
|
|
*/ |
57
|
6 |
|
public function init() |
58
|
|
|
{ |
59
|
6 |
|
parent::init(); |
60
|
6 |
|
if ($this->modelClass === null && $this->tableName === null) { |
61
|
|
|
throw new InvalidConfigException('Either "modelClass" or "tableName" must be set.'); |
62
|
|
|
} |
63
|
6 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Loads the fixture. |
67
|
|
|
* |
68
|
|
|
* The default implementation will first clean up the table by calling [[resetTable()]]. |
69
|
|
|
* It will then populate the table with the data returned by [[getData()]]. |
70
|
|
|
* |
71
|
|
|
* If you override this method, you should consider calling the parent implementation |
72
|
|
|
* so that the data returned by [[getData()]] can be populated into the table. |
73
|
|
|
*/ |
74
|
6 |
|
public function load() |
75
|
|
|
{ |
76
|
6 |
|
$this->resetTable(); |
77
|
6 |
|
$this->data = []; |
78
|
6 |
|
$table = $this->getTableSchema(); |
79
|
6 |
|
foreach ($this->getData() as $alias => $row) { |
80
|
6 |
|
$primaryKeys = $this->db->schema->insert($table->fullName, $row); |
81
|
6 |
|
$this->data[$alias] = array_merge($row, $primaryKeys); |
82
|
6 |
|
} |
83
|
6 |
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Returns the fixture data. |
87
|
|
|
* |
88
|
|
|
* The default implementation will try to return the fixture data by including the external file specified by [[dataFile]]. |
89
|
|
|
* The file should return an array of data rows (column name => column value), each corresponding to a row in the table. |
90
|
|
|
* |
91
|
|
|
* If the data file does not exist, an empty array will be returned. |
92
|
|
|
* |
93
|
|
|
* @return array the data rows to be inserted into the database table. |
94
|
|
|
*/ |
95
|
6 |
|
protected function getData() |
96
|
|
|
{ |
97
|
6 |
|
if ($this->dataFile === null) { |
98
|
6 |
|
$class = new \ReflectionClass($this); |
99
|
6 |
|
$dataFile = dirname($class->getFileName()) . '/data/' . $this->getTableSchema()->fullName . '.php'; |
100
|
|
|
|
101
|
6 |
|
return is_file($dataFile) ? require($dataFile) : []; |
102
|
|
|
} else { |
103
|
|
|
return parent::getData(); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Removes all existing data from the specified table and resets sequence number to 1 (if any). |
109
|
|
|
* This method is called before populating fixture data into the table associated with this fixture. |
110
|
|
|
*/ |
111
|
6 |
|
protected function resetTable() |
112
|
|
|
{ |
113
|
6 |
|
$table = $this->getTableSchema(); |
114
|
6 |
|
$this->db->createCommand()->delete($table->fullName)->execute(); |
115
|
6 |
|
if ($table->sequenceName !== null) { |
116
|
6 |
|
$this->db->createCommand()->resetSequence($table->fullName, 1)->execute(); |
117
|
6 |
|
} |
118
|
6 |
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return TableSchema the schema information of the database table associated with this fixture. |
122
|
|
|
* @throws \yii\base\InvalidConfigException if the table does not exist |
123
|
|
|
*/ |
124
|
6 |
|
public function getTableSchema() |
125
|
|
|
{ |
126
|
6 |
|
if ($this->_table !== null) { |
127
|
6 |
|
return $this->_table; |
128
|
|
|
} |
129
|
|
|
|
130
|
6 |
|
$db = $this->db; |
131
|
6 |
|
$tableName = $this->tableName; |
132
|
6 |
|
if ($tableName === null) { |
133
|
|
|
/* @var $modelClass \yii\db\ActiveRecord */ |
134
|
6 |
|
$modelClass = $this->modelClass; |
135
|
6 |
|
$tableName = $modelClass::tableName(); |
136
|
6 |
|
} |
137
|
|
|
|
138
|
6 |
|
$this->_table = $db->getSchema()->getTableSchema($tableName); |
139
|
6 |
|
if ($this->_table === null) { |
140
|
|
|
throw new InvalidConfigException("Table does not exist: {$tableName}"); |
141
|
|
|
} |
142
|
|
|
|
143
|
6 |
|
return $this->_table; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|