Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
719a721e
Commit
719a721e
authored
Jan 20, 2014
by
kate-kate
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
active fixture for mongodb extension
parent
65774b28
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
112 additions
and
0 deletions
+112
-0
ActiveFixture.php
extensions/mongodb/ActiveFixture.php
+112
-0
No files found.
extensions/mongodb/ActiveFixture.php
0 → 100644
View file @
719a721e
<?php
namespace
yii\mongodb
;
use
Yii
;
use
yii\base\InvalidConfigException
;
class
ActiveFixture
extends
\yii\test\BaseActiveFixture
{
public
$db
=
'mongodb'
;
/**
* @var string the name of the collection that this fixture is about. If this property is not set,
* the table name will be determined via [[modelClass]].
* @see modelClass
*/
public
$collectionName
;
public
$loadSchema
=
false
;
/**
* @var string the file path or path alias of the data file that contains the fixture data
* and will be loaded by [[loadData()]]. If this is not set, it will default to `FixturePath/data/TableName.php`,
* where `FixturePath` stands for the directory containing this fixture class, and `TableName` stands for the
* name of the table associated with this fixture.
*/
public
$dataFile
;
/**
* @var boolean whether to reset the collection associated with this fixture.
* By setting this property to be true, when [[loadData()]] is called, all existing data in the table
* will be removed and the sequence number (if any) will be reset.
*/
public
$resetCollection
=
true
;
/**
* @inheritdoc
*/
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 DB table and then populate it with the data
* returned by [[getData()]].
*/
protected
function
loadData
()
{
if
(
$this
->
resetCollection
)
{
$this
->
resetCollection
();
}
foreach
(
$this
->
getData
()
as
$alias
=>
$row
)
{
$this
->
getCollection
()
->
insert
(
$row
);
$this
->
data
[
$alias
]
=
$row
;
}
}
protected
function
getCollection
()
{
return
$this
->
db
->
getCollection
(
$this
->
getCollectionName
());
}
protected
function
getCollectionName
()
{
if
(
$this
->
collectionName
)
{
return
$this
->
collectionName
;
}
else
{
$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 table.
*
* If the data file does not exist, an empty array will be returned.
*
* @return array the data rows to be inserted into the collection.
*/
protected
function
getData
()
{
if
(
$this
->
dataFile
===
false
)
{
return
[];
}
if
(
$this
->
dataFile
!==
null
)
{
$dataFile
=
Yii
::
getAlias
(
$this
->
dataFile
);
}
else
{
$class
=
new
\ReflectionClass
(
$this
);
$dataFile
=
dirname
(
$class
->
getFileName
())
.
'/data/'
.
$this
->
getCollectionName
()
.
'.php'
;
}
return
is_file
(
$dataFile
)
?
require
(
$dataFile
)
:
[];
}
/**
* 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
function
resetCollection
()
{
$this
->
getCollection
()
->
remove
();
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment