ActiveFixture.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. namespace yii\test;
  8. use yii\base\InvalidConfigException;
  9. use yii\db\ActiveRecord;
  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. * For more details and usage information on ActiveFixture, see the [guide article on fixtures](guide:test-fixtures).
  24. *
  25. * @property-read TableSchema $tableSchema The schema information of the database table associated with this
  26. * fixture.
  27. *
  28. * @author Qiang Xue <qiang.xue@gmail.com>
  29. * @since 2.0
  30. */
  31. class ActiveFixture extends BaseActiveFixture
  32. {
  33. /**
  34. * @var string|null the name of the database table that this fixture is about. If this property is not set,
  35. * the table name will be determined via [[modelClass]].
  36. * @see modelClass
  37. */
  38. public $tableName;
  39. /**
  40. * @var string|bool|null the file path or [path alias](guide:concept-aliases) of the data file that contains the fixture data
  41. * to be returned by [[getData()]]. If this is not set, it will default to `FixturePath/data/TableName.php`,
  42. * where `FixturePath` stands for the directory containing this fixture class, and `TableName` stands for the
  43. * name of the table associated with this fixture. You can set this property to be false to prevent loading any data.
  44. */
  45. public $dataFile;
  46. /**
  47. * @var TableSchema the table schema for the table associated with this fixture
  48. */
  49. private $_table;
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function init()
  54. {
  55. parent::init();
  56. if ($this->tableName === null) {
  57. if ($this->modelClass === null) {
  58. throw new InvalidConfigException('Either "modelClass" or "tableName" must be set.');
  59. }
  60. /** @var ActiveRecord $modelClass */
  61. $modelClass = $this->modelClass;
  62. $this->db = $modelClass::getDb();
  63. }
  64. }
  65. /**
  66. * Loads the fixture.
  67. *
  68. * It populate the table with the data returned by [[getData()]].
  69. *
  70. * If you override this method, you should consider calling the parent implementation
  71. * so that the data returned by [[getData()]] can be populated into the table.
  72. */
  73. public function load()
  74. {
  75. $this->data = [];
  76. $table = $this->getTableSchema();
  77. foreach ($this->getData() as $alias => $row) {
  78. $primaryKeys = $this->db->schema->insert($table->fullName, $row);
  79. $this->data[$alias] = array_merge($row, $primaryKeys);
  80. }
  81. if ($table->sequenceName !== null) {
  82. $this->db->createCommand()->executeResetSequence($table->fullName);
  83. }
  84. }
  85. /**
  86. * Returns the fixture data.
  87. *
  88. * The default implementation will try to return the fixture data by including the external file specified by [[dataFile]].
  89. * The file should return an array of data rows (column name => column value), each corresponding to a row in the table.
  90. *
  91. * If the data file does not exist, an empty array will be returned.
  92. *
  93. * @return array the data rows to be inserted into the database table.
  94. */
  95. protected function getData()
  96. {
  97. if ($this->dataFile === null) {
  98. if ($this->dataDirectory !== null) {
  99. $dataFile = $this->getTableSchema()->fullName . '.php';
  100. } else {
  101. $class = new \ReflectionClass($this);
  102. $dataFile = dirname($class->getFileName()) . '/data/' . $this->getTableSchema()->fullName . '.php';
  103. }
  104. return $this->loadData($dataFile, false);
  105. }
  106. return parent::getData();
  107. }
  108. /**
  109. * {@inheritdoc}
  110. */
  111. public function unload()
  112. {
  113. $this->resetTable();
  114. parent::unload();
  115. }
  116. /**
  117. * Removes all existing data from the specified table and resets sequence number to 1 (if any).
  118. * This method is called before populating fixture data into the table associated with this fixture.
  119. */
  120. protected function resetTable()
  121. {
  122. $table = $this->getTableSchema();
  123. $this->db->createCommand()->delete($table->fullName)->execute();
  124. if ($table->sequenceName !== null) {
  125. $this->db->createCommand()->executeResetSequence($table->fullName, 1);
  126. }
  127. }
  128. /**
  129. * @return TableSchema the schema information of the database table associated with this fixture.
  130. * @throws \yii\base\InvalidConfigException if the table does not exist
  131. */
  132. public function getTableSchema()
  133. {
  134. if ($this->_table !== null) {
  135. return $this->_table;
  136. }
  137. $db = $this->db;
  138. $tableName = $this->tableName;
  139. if ($tableName === null) {
  140. /** @var \yii\db\ActiveRecord $modelClass */
  141. $modelClass = $this->modelClass;
  142. $tableName = $modelClass::tableName();
  143. }
  144. $this->_table = $db->getSchema()->getTableSchema($tableName);
  145. if ($this->_table === null) {
  146. throw new InvalidConfigException("Table does not exist: {$tableName}");
  147. }
  148. return $this->_table;
  149. }
  150. }