Class yii\mongodb\ActiveFixture
Inheritance | yii\mongodb\ActiveFixture » yii\test\BaseActiveFixture |
---|---|
Available since extension's version | 2.0 |
Source Code | https://github.com/yiisoft/yii2-mongodb/blob/master/ActiveFixture.php |
ActiveFixture represents a fixture backed up by a modelClass or a MongoDB collection.
Either modelClass or $collectionName must be set. You should also provide fixture data in the file specified by dataFile or overriding getData() if you want to use code to generate the fixture data.
When the fixture is being loaded, it will first call resetCollection() to remove any existing data in the collection. It will then populate the collection with the data returned by getData().
After the fixture is loaded, you can access the loaded data via the data property. If you set modelClass, you will also be able to retrieve an instance of modelClass with the populated data via getModel().
Public Properties
Property | Type | Description | Defined By |
---|---|---|---|
$collectionName | string|array | The collection name that this fixture is about. | yii\mongodb\ActiveFixture |
$db | yii\mongodb\Connection|string | The DB connection object or the application component ID of the DB connection. | yii\mongodb\ActiveFixture |
Public Methods
Method | Description | Defined By |
---|---|---|
init() | yii\mongodb\ActiveFixture | |
load() | Loads the fixture data. | yii\mongodb\ActiveFixture |
Protected Methods
Method | Description | Defined By |
---|---|---|
getCollection() | Returns collection used by this fixture. | yii\mongodb\ActiveFixture |
getCollectionName() | Returns collection name used by this fixture. | yii\mongodb\ActiveFixture |
getData() | Returns the fixture data. | yii\mongodb\ActiveFixture |
resetCollection() | Removes all existing data from the specified collection and resets sequence number if any. | yii\mongodb\ActiveFixture |
Property Details
The collection name that this fixture is about. If this property is not set, the collection name will be determined via modelClass.
See also yii\mongodb\Connection::getCollection().
The DB connection object or the application component ID of the DB connection.
Method Details
Returns collection used by this fixture.
protected yii\mongodb\Collection getCollection ( ) | ||
return | yii\mongodb\Collection |
Related collection. |
---|
protected function getCollection()
{
return $this->db->getCollection($this->getCollectionName());
}
Returns collection name used by this fixture.
protected array|string getCollectionName ( ) | ||
return | array|string |
Related collection name |
---|
protected function getCollectionName()
{
if ($this->collectionName) {
return $this->collectionName;
} else {
/* @var $modelClass ActiveRecord */
$modelClass = $this->modelClass;
return $modelClass::collectionName();
}
}
Returns the fixture data.
This method is called by loadData() to get the needed fixture data.
The default implementation will try to return the fixture data by including the external file specified by dataFile. The file should return an array of data rows (column name => column value), each corresponding to a row in the collection.
If the data file does not exist, an empty array will be returned.
protected array getData ( ) | ||
return | array |
The data rows to be inserted into the collection. |
---|
protected function getData()
{
if ($this->dataFile === null) {
$class = new \ReflectionClass($this);
$dataFile = dirname($class->getFileName()) . '/data/' . $this->getCollectionName() . '.php';
return is_file($dataFile) ? require($dataFile) : [];
} else {
return parent::getData();
}
}
public void init ( ) |
public function init()
{
parent::init();
if (!isset($this->modelClass) && !isset($this->collectionName)) {
throw new InvalidConfigException('Either "modelClass" or "collectionName" must be set.');
}
}
Loads the fixture data.
The default implementation will first reset the MongoDB collection and then populate it with the data returned by getData().
public void load ( ) |
public function load()
{
$this->resetCollection();
$this->data = [];
$data = $this->getData();
if (empty($data)) {
return;
}
$this->getCollection()->batchInsert($data);
foreach ($data as $alias => $row) {
$this->data[$alias] = $row;
}
}
Removes all existing data from the specified collection and resets sequence number if any.
This method is called before populating fixture data into the collection associated with this fixture.
protected void resetCollection ( ) |
protected function resetCollection()
{
$this->getCollection()->remove();
}