ActiveFixture.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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\mongodb;
  8. use Yii;
  9. use yii\base\InvalidConfigException;
  10. use yii\test\BaseActiveFixture;
  11. /**
  12. * ActiveFixture represents a fixture backed up by a [[modelClass|MongoDB ActiveRecord class]] or a [[collectionName|MongoDB collection]].
  13. *
  14. * Either [[modelClass]] or [[collectionName]] 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 [[resetCollection()]] to remove any existing data in the collection.
  18. * It will then populate the collection 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. * @author Paul Klimov <klimov.paul@gmail.com>
  24. * @since 2.0
  25. */
  26. class ActiveFixture extends BaseActiveFixture
  27. {
  28. /**
  29. * @var Connection|string the DB connection object or the application component ID of the DB connection.
  30. */
  31. public $db = 'mongodb';
  32. /**
  33. * @var string|array the collection name that this fixture is about. If this property is not set,
  34. * the collection name will be determined via [[modelClass]].
  35. * @see Connection::getCollection()
  36. */
  37. public $collectionName;
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function init()
  42. {
  43. parent::init();
  44. if (!isset($this->modelClass) && !isset($this->collectionName)) {
  45. throw new InvalidConfigException('Either "modelClass" or "collectionName" must be set.');
  46. }
  47. }
  48. /**
  49. * Loads the fixture data.
  50. * The default implementation will first reset the MongoDB collection and then populate it with the data
  51. * returned by [[getData()]].
  52. */
  53. public function load()
  54. {
  55. $this->resetCollection();
  56. $this->data = [];
  57. $data = $this->getData();
  58. if (empty($data)) {
  59. return;
  60. }
  61. $this->getCollection()->batchInsert($data);
  62. foreach ($data as $alias => $row) {
  63. $this->data[$alias] = $row;
  64. }
  65. }
  66. /**
  67. * Returns collection used by this fixture.
  68. * @return Collection related collection.
  69. */
  70. protected function getCollection()
  71. {
  72. return $this->db->getCollection($this->getCollectionName());
  73. }
  74. /**
  75. * Returns collection name used by this fixture.
  76. * @return array|string related collection name
  77. */
  78. protected function getCollectionName()
  79. {
  80. if ($this->collectionName) {
  81. return $this->collectionName;
  82. }
  83. /* @var $modelClass ActiveRecord */
  84. $modelClass = $this->modelClass;
  85. return $modelClass::collectionName();
  86. }
  87. /**
  88. * Returns the fixture data.
  89. *
  90. * This method is called by [[loadData()]] to get the needed fixture data.
  91. *
  92. * The default implementation will try to return the fixture data by including the external file specified by [[dataFile]].
  93. * The file should return an array of data rows (column name => column value), each corresponding to a row in the collection.
  94. *
  95. * If the data file does not exist, an empty array will be returned.
  96. *
  97. * @return array the data rows to be inserted into the collection.
  98. */
  99. protected function getData()
  100. {
  101. if ($this->dataFile === null) {
  102. $class = new \ReflectionClass($this);
  103. $collectionName = $this->getCollectionName();
  104. $dataFile = dirname($class->getFileName()) . '/data/' . (is_array($collectionName) ? implode('.', $collectionName) : $collectionName) . '.php';
  105. return is_file($dataFile) ? require($dataFile) : [];
  106. }
  107. return parent::getData();
  108. }
  109. /**
  110. * Removes all existing data from the specified collection and resets sequence number if any.
  111. * This method is called before populating fixture data into the collection associated with this fixture.
  112. */
  113. protected function resetCollection()
  114. {
  115. $this->getCollection()->remove();
  116. }
  117. }