Completed
Push — 2.1 ( 7c8525...0afc41 )
by Alexander
21:05 queued 16:02
created

ActiveFixture::load()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
ccs 7
cts 7
cp 1
rs 9.6666
cc 2
eloc 6
nc 2
nop 0
crap 2
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
 * For more details and usage information on ActiveFixture, see the [guide article on fixtures](guide:test-fixtures).
27
 *
28
 * @property TableSchema $tableSchema The schema information of the database table associated with this
29
 * fixture. This property is read-only.
30
 *
31
 * @author Qiang Xue <[email protected]>
32
 * @since 2.0
33
 */
34
class ActiveFixture extends BaseActiveFixture
35
{
36
    /**
37
     * @var string 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 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 8
    public function init()
60
    {
61 8
        parent::init();
62 8
        if ($this->modelClass === null && $this->tableName === null) {
63
            throw new InvalidConfigException('Either "modelClass" or "tableName" must be set.');
64
        }
65 8
    }
66
67
    /**
68
     * Loads the fixture.
69
     *
70
     * The default implementation will first clean up the table by calling [[resetTable()]].
71
     * It will then populate the table with the data returned by [[getData()]].
72
     *
73
     * If you override this method, you should consider calling the parent implementation
74
     * so that the data returned by [[getData()]] can be populated into the table.
75
     */
76 8
    public function load()
77
    {
78 8
        $this->data = [];
79 8
        $table = $this->getTableSchema();
80 8
        foreach ($this->getData() as $alias => $row) {
81 8
            $primaryKeys = $this->db->schema->insert($table->fullName, $row);
82 8
            $this->data[$alias] = array_merge($row, $primaryKeys);
83
        }
84 8
    }
85
86
    /**
87
     * Returns the fixture data.
88
     *
89
     * The default implementation will try to return the fixture data by including the external file specified by [[dataFile]].
90
     * The file should return an array of data rows (column name => column value), each corresponding to a row in the table.
91
     *
92
     * If the data file does not exist, an empty array will be returned.
93
     *
94
     * @return array the data rows to be inserted into the database table.
95
     */
96 8
    protected function getData()
97
    {
98 8
        if ($this->dataFile === null) {
99 8
            $class = new \ReflectionClass($this);
100 8
            $dataFile = dirname($class->getFileName()) . '/data/' . $this->getTableSchema()->fullName . '.php';
101
102 8
            return is_file($dataFile) ? require($dataFile) : [];
103
        } else {
104
            return parent::getData();
105
        }
106
    }
107
108
    /**
109
     * @inheritdoc
110
     */
111 8
    public function unload()
112
    {
113 8
        $this->resetTable();
114 8
        parent::unload();
115 8
    }
116
117
    /**
118
     * Removes all existing data from the specified table and resets sequence number to 1 (if any).
119
     * This method is called before populating fixture data into the table associated with this fixture.
120
     */
121 8
    protected function resetTable()
122
    {
123 8
        $table = $this->getTableSchema();
124 8
        $this->db->createCommand()->delete($table->fullName)->execute();
125 8
        if ($table->sequenceName !== null) {
126 8
            $this->db->createCommand()->resetSequence($table->fullName, 1)->execute();
127
        }
128 8
    }
129
130
    /**
131
     * @return TableSchema the schema information of the database table associated with this fixture.
132
     * @throws \yii\base\InvalidConfigException if the table does not exist
133
     */
134 8
    public function getTableSchema()
135
    {
136 8
        if ($this->_table !== null) {
137 8
            return $this->_table;
138
        }
139
140 8
        $db = $this->db;
141 8
        $tableName = $this->tableName;
142 8
        if ($tableName === null) {
143
            /* @var $modelClass \yii\db\ActiveRecord */
144 8
            $modelClass = $this->modelClass;
145 8
            $tableName = $modelClass::tableName();
146
        }
147
148 8
        $this->_table = $db->getSchema()->getTableSchema($tableName);
149 8
        if ($this->_table === null) {
150
            throw new InvalidConfigException("Table does not exist: {$tableName}");
151
        }
152
153 8
        return $this->_table;
154
    }
155
}
156