BaseActiveFixture.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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\ArrayAccessTrait;
  10. use yii\base\InvalidConfigException;
  11. /**
  12. * BaseActiveFixture is the base class for fixture classes that support accessing fixture data as ActiveRecord objects.
  13. *
  14. * For more details and usage information on BaseActiveFixture, see the [guide article on fixtures](guide:test-fixtures).
  15. *
  16. * @author Qiang Xue <qiang.xue@gmail.com>
  17. * @since 2.0
  18. */
  19. abstract class BaseActiveFixture extends DbFixture implements \IteratorAggregate, \ArrayAccess, \Countable
  20. {
  21. use ArrayAccessTrait;
  22. /**
  23. * @var string the AR model class associated with this fixture.
  24. */
  25. public $modelClass;
  26. /**
  27. * @var array the data rows. Each array element represents one row of data (column name => column value).
  28. */
  29. public $data = [];
  30. /**
  31. * @var string|bool the file path or path alias of the data file that contains the fixture data
  32. * to be returned by [[getData()]]. You can set this property to be false to prevent loading any data.
  33. */
  34. public $dataFile;
  35. /**
  36. * @var \yii\db\ActiveRecord[] the loaded AR models
  37. */
  38. private $_models = [];
  39. /**
  40. * Returns the AR model by the specified model name.
  41. * A model name is the key of the corresponding data row in [[data]].
  42. * @param string $name the model name.
  43. * @return null|\yii\db\ActiveRecord the AR model, or null if the model cannot be found in the database
  44. * @throws \yii\base\InvalidConfigException if [[modelClass]] is not set.
  45. */
  46. public function getModel($name)
  47. {
  48. if (!isset($this->data[$name])) {
  49. return null;
  50. }
  51. if (array_key_exists($name, $this->_models)) {
  52. return $this->_models[$name];
  53. }
  54. if ($this->modelClass === null) {
  55. throw new InvalidConfigException('The "modelClass" property must be set.');
  56. }
  57. $row = $this->data[$name];
  58. /* @var $modelClass \yii\db\ActiveRecord */
  59. $modelClass = $this->modelClass;
  60. /* @var $model \yii\db\ActiveRecord */
  61. $model = new $modelClass;
  62. $keys = [];
  63. foreach ($model->primaryKey() as $key) {
  64. $keys[$key] = isset($row[$key]) ? $row[$key] : null;
  65. }
  66. return $this->_models[$name] = $modelClass::findOne($keys);
  67. }
  68. /**
  69. * Loads the fixture.
  70. *
  71. * The default implementation simply stores the data returned by [[getData()]] in [[data]].
  72. * You should usually override this method by putting the data into the underlying database.
  73. */
  74. public function load()
  75. {
  76. $this->data = $this->getData();
  77. }
  78. /**
  79. * Returns the fixture data.
  80. *
  81. * The default implementation will try to return the fixture data by including the external file specified by [[dataFile]].
  82. * The file should return the data array that will be stored in [[data]] after inserting into the database.
  83. *
  84. * @return array the data to be put into the database
  85. * @throws InvalidConfigException if the specified data file does not exist.
  86. */
  87. protected function getData()
  88. {
  89. if ($this->dataFile === false || $this->dataFile === null) {
  90. return [];
  91. }
  92. $dataFile = Yii::getAlias($this->dataFile);
  93. if (is_file($dataFile)) {
  94. return require($dataFile);
  95. } else {
  96. throw new InvalidConfigException("Fixture data file does not exist: {$this->dataFile}");
  97. }
  98. }
  99. /**
  100. * @inheritdoc
  101. */
  102. public function unload()
  103. {
  104. parent::unload();
  105. $this->data = [];
  106. $this->_models = [];
  107. }
  108. }