ActiveFixture.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. * For more details and usage information on ActiveFixture, see the [guide article on fixtures](guide:test-fixtures).
  24. *
  25. * @property TableSchema $tableSchema The schema information of the database table associated with this
  26. * fixture. This property is read-only.
  27. *
  28. * @author Qiang Xue <qiang.xue@gmail.com>
  29. * @since 2.0
  30. */
  31. class ActiveFixture extends BaseActiveFixture
  32. {
  33. /**
  34. * @var string 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 the file path or path alias 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->modelClass === null && $this->tableName === null) {
  57. throw new InvalidConfigException('Either "modelClass" or "tableName" must be set.');
  58. }
  59. }
  60. /**
  61. * Loads the fixture.
  62. *
  63. * The default implementation will first clean up the table by calling [[resetTable()]].
  64. * It will then populate the table with the data returned by [[getData()]].
  65. *
  66. * If you override this method, you should consider calling the parent implementation
  67. * so that the data returned by [[getData()]] can be populated into the table.
  68. */
  69. public function load()
  70. {
  71. $this->resetTable();
  72. $this->data = [];
  73. $table = $this->getTableSchema();
  74. foreach ($this->getData() as $alias => $row) {
  75. $primaryKeys = $this->db->schema->insert($table->fullName, $row);
  76. $this->data[$alias] = array_merge($row, $primaryKeys);
  77. }
  78. }
  79. /**
  80. * Returns the fixture data.
  81. *
  82. * The default implementation will try to return the fixture data by including the external file specified by [[dataFile]].
  83. * The file should return an array of data rows (column name => column value), each corresponding to a row in the table.
  84. *
  85. * If the data file does not exist, an empty array will be returned.
  86. *
  87. * @return array the data rows to be inserted into the database table.
  88. */
  89. protected function getData()
  90. {
  91. if ($this->dataFile === null) {
  92. $class = new \ReflectionClass($this);
  93. $dataFile = dirname($class->getFileName()) . '/data/' . $this->getTableSchema()->fullName . '.php';
  94. return is_file($dataFile) ? require($dataFile) : [];
  95. } else {
  96. return parent::getData();
  97. }
  98. }
  99. /**
  100. * Removes all existing data from the specified table and resets sequence number to 1 (if any).
  101. * This method is called before populating fixture data into the table associated with this fixture.
  102. */
  103. protected function resetTable()
  104. {
  105. $table = $this->getTableSchema();
  106. $this->db->createCommand()->delete($table->fullName)->execute();
  107. if ($table->sequenceName !== null) {
  108. $this->db->createCommand()->resetSequence($table->fullName, 1)->execute();
  109. }
  110. }
  111. /**
  112. * @return TableSchema the schema information of the database table associated with this fixture.
  113. * @throws \yii\base\InvalidConfigException if the table does not exist
  114. */
  115. public function getTableSchema()
  116. {
  117. if ($this->_table !== null) {
  118. return $this->_table;
  119. }
  120. $db = $this->db;
  121. $tableName = $this->tableName;
  122. if ($tableName === null) {
  123. /* @var $modelClass \yii\db\ActiveRecord */
  124. $modelClass = $this->modelClass;
  125. $tableName = $modelClass::tableName();
  126. }
  127. $this->_table = $db->getSchema()->getTableSchema($tableName);
  128. if ($this->_table === null) {
  129. throw new InvalidConfigException("Table does not exist: {$tableName}");
  130. }
  131. return $this->_table;
  132. }
  133. }