146 lines
5.2KB

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