ActiveFixture.php 5.5 KB

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