Passed
Pull Request — master (#20227)
by Moiseenko
09:56
created

ActiveFixture   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Test Coverage

Coverage 95.35%

Importance

Changes 0
Metric Value
eloc 43
dl 0
loc 131
ccs 41
cts 43
cp 0.9535
rs 10
c 0
b 0
f 0
wmc 16

6 Methods

Rating   Name   Duplication   Size   Complexity  
A unload() 0 4 1
A init() 0 10 3
A load() 0 7 2
A getData() 0 13 3
A resetTable() 0 9 3
A getTableSchema() 0 20 4
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 33
    public function init()
60
    {
61 33
        parent::init();
62 33
        if ($this->tableName === null) {
63 33
            if ($this->modelClass === null) {
64
                throw new InvalidConfigException('Either "modelClass" or "tableName" must be set.');
65
            }
66
            /** @var ActiveRecord $modelClass */
67 33
            $modelClass = $this->modelClass;
68 33
            $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 27
    public function load()
81
    {
82 27
        $this->data = [];
83 27
        $table = $this->getTableSchema();
84 27
        foreach ($this->getData() as $alias => $row) {
85 20
            $primaryKeys = $this->db->schema->insert($table->fullName, $row);
86 20
            $this->data[$alias] = array_merge($row, $primaryKeys);
0 ignored issues
show
Bug introduced by
It seems like $primaryKeys can also be of type false; however, parameter $arrays of array_merge() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

86
            $this->data[$alias] = array_merge($row, /** @scrutinizer ignore-type */ $primaryKeys);
Loading history...
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 27
    protected function getData()
101
    {
102 27
        if ($this->dataFile === null) {
103 23
            if ($this->dataDirectory !== null) {
104 4
                $dataFile = $this->getTableSchema()->fullName . '.php';
105
            } else {
106 19
                $class = new \ReflectionClass($this);
107 19
                $dataFile = dirname($class->getFileName()) . '/data/' . $this->getTableSchema()->fullName . '.php';
108
            }
109
110 23
            return $this->loadData($dataFile, false);
111
        }
112 8
        return parent::getData();
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118 29
    public function unload()
119
    {
120 29
        $this->resetTable();
121 29
        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 29
    protected function resetTable()
129
    {
130 29
        $table = $this->getTableSchema();
131 29
        $this->db->createCommand()->delete($table->fullName)->execute();
132 29
        if ($table->sequenceName !== null) {
133 29
            if ('pgsql' === $this->db->driverName) {
134 6
                $this->db->createCommand("ALTER SEQUENCE {$table->sequenceName} RESTART;")->execute();
135
            } else {
136 23
                $this->db->createCommand()->executeResetSequence($table->fullName, 1);
137
            }
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 29
    public function getTableSchema()
146
    {
147 29
        if ($this->_table !== null) {
148 27
            return $this->_table;
149
        }
150
151 29
        $db = $this->db;
152 29
        $tableName = $this->tableName;
153 29
        if ($tableName === null) {
154
            /* @var $modelClass \yii\db\ActiveRecord */
155 29
            $modelClass = $this->modelClass;
156 29
            $tableName = $modelClass::tableName();
157
        }
158
159 29
        $this->_table = $db->getSchema()->getTableSchema($tableName);
160 29
        if ($this->_table === null) {
161
            throw new InvalidConfigException("Table does not exist: {$tableName}");
162
        }
163
164 29
        return $this->_table;
165
    }
166
}
167