Completed
Push — unique-validator-fix ( 963824...d331b7 )
by Alexander
29:51 queued 14:14
created

BaseActiveFixture::getModel()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 6.1308

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 11
cts 13
cp 0.8462
rs 8.6737
c 0
b 0
f 0
cc 6
eloc 13
nc 6
nop 1
crap 6.1308
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\ArrayAccessTrait;
12
use yii\base\InvalidConfigException;
13
14
/**
15
 * BaseActiveFixture is the base class for fixture classes that support accessing fixture data as ActiveRecord objects.
16
 *
17
 * For more details and usage information on BaseActiveFixture, see the [guide article on fixtures](guide:test-fixtures).
18
 *
19
 * @author Qiang Xue <[email protected]>
20
 * @since 2.0
21
 */
22
abstract class BaseActiveFixture extends DbFixture implements \IteratorAggregate, \ArrayAccess, \Countable
23
{
24
    use ArrayAccessTrait;
25
26
    /**
27
     * @var string the AR model class associated with this fixture.
28
     */
29
    public $modelClass;
30
    /**
31
     * @var array the data rows. Each array element represents one row of data (column name => column value).
32
     */
33
    public $data = [];
34
    /**
35
     * @var string|bool the file path or path alias of the data file that contains the fixture data
36
     * to be returned by [[getData()]]. You can set this property to be false to prevent loading any data.
37
     */
38
    public $dataFile;
39
40
    /**
41
     * @var \yii\db\ActiveRecord[] the loaded AR models
42
     */
43
    private $_models = [];
44
45
46
    /**
47
     * Returns the AR model by the specified model name.
48
     * A model name is the key of the corresponding data row in [[data]].
49
     * @param string $name the model name.
50
     * @return null|\yii\db\ActiveRecord the AR model, or null if the model cannot be found in the database
51
     * @throws \yii\base\InvalidConfigException if [[modelClass]] is not set.
52
     */
53 4
    public function getModel($name)
54
    {
55 4
        if (!isset($this->data[$name])) {
56
            return null;
57
        }
58 4
        if (array_key_exists($name, $this->_models)) {
59 4
            return $this->_models[$name];
60
        }
61
62 4
        if ($this->modelClass === null) {
63
            throw new InvalidConfigException('The "modelClass" property must be set.');
64
        }
65 4
        $row = $this->data[$name];
66
        /* @var $modelClass \yii\db\ActiveRecord */
67 4
        $modelClass = $this->modelClass;
68 4
        $keys = [];
69 4
        foreach ($modelClass::primaryKey() as $key) {
70 4
            $keys[$key] = isset($row[$key]) ? $row[$key] : null;
71
        }
72
73 4
        return $this->_models[$name] = $modelClass::findOne($keys);
74
    }
75
76
    /**
77
     * Loads the fixture.
78
     *
79
     * The default implementation simply stores the data returned by [[getData()]] in [[data]].
80
     * You should usually override this method by putting the data into the underlying database.
81
     */
82
    public function load()
83
    {
84
        $this->data = $this->getData();
85
    }
86
87
    /**
88
     * Returns the fixture data.
89
     *
90
     * The default implementation will try to return the fixture data by including the external file specified by [[dataFile]].
91
     * The file should return the data array that will be stored in [[data]] after inserting into the database.
92
     *
93
     * @return array the data to be put into the database
94
     * @throws InvalidConfigException if the specified data file does not exist.
95
     */
96
    protected function getData()
97
    {
98
        if ($this->dataFile === false || $this->dataFile === null) {
99
            return [];
100
        }
101
        $dataFile = Yii::getAlias($this->dataFile);
102
        if (is_file($dataFile)) {
103
            return require($dataFile);
104
        } else {
105
            throw new InvalidConfigException("Fixture data file does not exist: {$this->dataFile}");
106
        }
107
    }
108
109
    /**
110
     * @inheritdoc
111
     */
112 8
    public function unload()
113
    {
114 8
        parent::unload();
115 8
        $this->data = [];
116 8
        $this->_models = [];
117 8
    }
118
}
119