ActiveFixture.php 4.97 KB
Newer Older
Qiang Xue committed
1 2 3 4 5 6 7 8 9 10
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\test;

use Yii;
Qiang Xue committed
11
use yii\base\ArrayAccessTrait;
Qiang Xue committed
12 13 14 15 16 17
use yii\base\InvalidConfigException;
use yii\db\TableSchema;

/**
 * ActiveFixture represents a fixture backed up by a [[modelClass|ActiveRecord class]] or a [[tableName|database table]].
 *
Qiang Xue committed
18 19
 * Either [[modelClass]] or [[tableName]] must be set. You should also provide fixture data in the file
 * specified by [[dataFile]] or overriding [[getData()]] if you want to use code to generate the fixture data.
Qiang Xue committed
20
 *
Qiang Xue committed
21 22
 * When the fixture is being loaded, it will first call [[resetTable()]] to remove any existing data in the table.
 * It will then populate the table with the data returned by [[getData()]].
Qiang Xue committed
23 24 25
 *
 * After the fixture is loaded, you can access the loaded data via the [[data]] property. If you set [[modelClass]],
 * you will also be able to retrieve an instance of [[modelClass]] with the populated data via [[getModel()]].
Qiang Xue committed
26
 *
27 28 29
 * @property TableSchema $tableSchema The schema information of the database table associated with this
 * fixture. This property is read-only.
 *
Qiang Xue committed
30 31 32
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
Qiang Xue committed
33
class ActiveFixture extends BaseActiveFixture
Qiang Xue committed
34 35 36 37 38 39 40
{
	/**
	 * @var string the name of the database table that this fixture is about. If this property is not set,
	 * the table name will be determined via [[modelClass]].
	 * @see modelClass
	 */
	public $tableName;
Qiang Xue committed
41
	/**
Qiang Xue committed
42 43
	 * @var string|boolean the file path or path alias of the data file that contains the fixture data
	 * to be returned by [[getData()]]. If this is not set, it will default to `FixturePath/data/TableName.php`,
Qiang Xue committed
44
	 * where `FixturePath` stands for the directory containing this fixture class, and `TableName` stands for the
Qiang Xue committed
45
	 * name of the table associated with this fixture. You can set this property to be false to prevent loading any data.
Qiang Xue committed
46 47
	 */
	public $dataFile;
Qiang Xue committed
48 49 50 51 52
	/**
	 * @var TableSchema the table schema for the table associated with this fixture
	 */
	private $_table;

Carsten Brandt committed
53

Qiang Xue committed
54 55 56 57 58 59 60 61 62 63 64 65
	/**
	 * @inheritdoc
	 */
	public function init()
	{
		parent::init();
		if (!isset($this->modelClass) && !isset($this->tableName)) {
			throw new InvalidConfigException('Either "modelClass" or "tableName" must be set.');
		}
	}

	/**
Qiang Xue committed
66 67 68 69 70 71 72
	 * Loads the fixture.
	 *
	 * The default implementation will first clean up the table by calling [[resetTable()]].
	 * It will then populate the table with the data returned by [[getData()]].
	 *
	 * If you override this method, you should consider calling the parent implementation
	 * so that the data returned by [[getData()]] can be populated into the table.
Qiang Xue committed
73
	 */
Qiang Xue committed
74
	public function load()
Qiang Xue committed
75
	{
Qiang Xue committed
76
		$this->resetTable();
Qiang Xue committed
77

Qiang Xue committed
78
		$table = $this->getTableSchema();
Qiang Xue committed
79

Qiang Xue committed
80 81 82 83 84 85 86 87 88 89 90 91
		foreach ($this->getData() as $alias => $row) {
			$this->db->createCommand()->insert($table->fullName, $row)->execute();
			if ($table->sequenceName !== null) {
				foreach ($table->primaryKey as $pk) {
					if (!isset($row[$pk])) {
						$row[$pk] = $this->db->getLastInsertID($table->sequenceName);
						break;
					}
				}
			}
			$this->data[$alias] = $row;
		}
Qiang Xue committed
92 93
	}

Qiang Xue committed
94
	/**
Qiang Xue committed
95 96 97
	 * Returns the fixture data.
	 * 
	 * The default implementation will try to return the fixture data by including the external file specified by [[dataFile]].
Qiang Xue committed
98
	 * The file should return an array of data rows (column name => column value), each corresponding to a row in the table.
Qiang Xue committed
99 100 101 102 103
	 *
	 * If the data file does not exist, an empty array will be returned.
	 *
	 * @return array the data rows to be inserted into the database table.
	 */
Qiang Xue committed
104
	protected function getData()
Qiang Xue committed
105
	{
Qiang Xue committed
106
		if ($this->dataFile === null) {
Qiang Xue committed
107 108
			$class = new \ReflectionClass($this);
			$dataFile = dirname($class->getFileName()) . '/data/' . $this->getTableSchema()->fullName . '.php';
Qiang Xue committed
109 110 111
			return is_file($dataFile) ? require($dataFile) : [];
		} else {
			return parent::getData();
Qiang Xue committed
112
		}
Qiang Xue committed
113 114 115
	}

	/**
Qiang Xue committed
116
	 * Removes all existing data from the specified table and resets sequence number to 1 (if any).
Qiang Xue committed
117 118
	 * This method is called before populating fixture data into the table associated with this fixture.
	 */
Qiang Xue committed
119
	protected function resetTable()
Qiang Xue committed
120
	{
Qiang Xue committed
121
		$table = $this->getTableSchema();
Qiang Xue committed
122 123 124 125 126
		$this->db->createCommand()->delete($table->fullName)->execute();
		if ($table->sequenceName !== null) {
			$this->db->createCommand()->resetSequence($table->fullName, 1)->execute();
		}
	}
Qiang Xue committed
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152

	/**
	 * @return TableSchema the schema information of the database table associated with this fixture.
	 * @throws \yii\base\InvalidConfigException if the table does not exist
	 */
	public function getTableSchema()
	{
		if ($this->_table !== null) {
			return $this->_table;
		}

		$db = $this->db;
		$tableName = $this->tableName;
		if ($tableName === null) {
			/** @var \yii\db\ActiveRecord $modelClass */
			$modelClass = $this->modelClass;
			$tableName = $modelClass::tableName();
		}

		$this->_table = $db->getSchema()->getTableSchema($tableName);
		if ($this->_table === null) {
			throw new InvalidConfigException("Table does not exist: {$tableName}");
		}

		return $this->_table;
	}
Qiang Xue committed
153
}