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
18291c5b
Commit
18291c5b
authored
Jan 17, 2014
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added FixtureTrait.
parent
0e7de3e3
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
181 additions
and
13 deletions
+181
-13
ActiveFixture.php
framework/test/ActiveFixture.php
+32
-13
FixtureTrait.php
framework/test/FixtureTrait.php
+149
-0
No files found.
framework/test/ActiveFixture.php
View file @
18291c5b
...
...
@@ -48,6 +48,13 @@ class ActiveFixture extends Fixture
*/
public
$tableName
;
/**
* @var string|boolean 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. You may set this property to be false to disable loading data.
*/
public
$dataFile
;
/**
* @var array the data rows. Each array element represents one row of data (column name => column value).
*/
public
$rows
;
...
...
@@ -82,9 +89,10 @@ class ActiveFixture extends Fixture
*/
public
function
load
()
{
$table
=
$this
->
getTableSchema
();
$this
->
resetTable
(
$table
);
$this
->
initSchema
();
$table
=
$this
->
getTableSchema
();
$this
->
resetTable
();
$this
->
rows
=
[];
foreach
(
$this
->
loadData
()
as
$alias
=>
$row
)
{
$this
->
db
->
createCommand
()
->
insert
(
$table
->
fullName
,
$row
)
->
execute
();
...
...
@@ -158,34 +166,45 @@ class ActiveFixture extends Fixture
}
/**
* Initializes the database schema.
* This method is called by [[load()]] before loading data.
* You may override this method to prepare necessary database schema changes.
*/
protected
function
initSchema
()
{
}
/**
* Loads fixture data.
*
* The default implementation will look for a file under the `data` sub-directory of the directory containing
* the fixture class. The file name is assumed to be the same as the table name (with schema prefix if necessary).
* For example, the `tbl_user` table corresponds to the `data/tbl_user.php` file; the `test.tbl_post` table (`test`
* is the non-default schema name) corresponds to `data/test.tbl_user.php`. The file should return an array
* of data rows (column name => column value), each corresponding to a row in the table.
* The default implementation will try to load 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.
*
* You may override this method if you want to change the default way of data loading.
*
* @return array the data rows to be inserted into the database table.
*/
protected
function
loadData
()
{
$class
=
new
\ReflectionClass
(
$this
);
$dataFile
=
dirname
(
$class
->
getFileName
())
.
'/data/'
.
$this
->
getTableSchema
()
->
fullName
.
'.php'
;
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
->
getTableSchema
()
->
fullName
.
'.php'
;
}
return
is_file
(
$dataFile
)
?
require
(
$dataFile
)
:
[];
}
/**
* Removes all existing data from the specified table and resets sequence number if any.
* This method is called before populating fixture data into the table associated with this fixture.
* @param TableSchema $table the table to be reset.
*/
protected
function
resetTable
(
$table
)
protected
function
resetTable
()
{
$table
=
$this
->
getTableSchema
();
$this
->
db
->
createCommand
()
->
delete
(
$table
->
fullName
)
->
execute
();
if
(
$table
->
sequenceName
!==
null
)
{
$this
->
db
->
createCommand
()
->
resetSequence
(
$table
->
fullName
,
1
)
->
execute
();
...
...
framework/test/FixtureTrait.php
0 → 100644
View file @
18291c5b
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace
yii\test
;
use
Yii
;
use
yii\base\InvalidConfigException
;
/**
* FixtureTrait provides functionalities for loading, unloading and accessing fixtures for a test case.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
trait
FixtureTrait
{
/**
* Declares the fixtures that are needed by the current test case.
* The return value of this method must be an array of fixture configurations. For example,
*
* ```php
* [
* // anonymous fixture
* PostFixture::className(),
* // "users" fixture
* 'users' => UserFixture::className(),
* // "cache" fixture with configuration
* 'cache' => [
* 'class' => CacheFixture::className(),
* 'host' => 'xxx',
* ],
* ]
* ```
*
* @return array the fixtures needed by the current test case
*/
public
function
fixtures
()
{
return
[];
}
/**
* @var array the list of fixture objects available for the current test.
* The array keys are the corresponding fixture class names.
* The fixtures are listed in their dependency order. That is, fixture A is listed before B
* if B depends on A.
*/
private
$_fixtures
;
/**
* @var array the fixture class names indexed by the corresponding fixture names (aliases).
*/
private
$_fixtureAliases
;
/**
* Loads the fixtures.
* This method will load the fixtures specified by `$fixtures` or [[fixtures()]].
* @param array $fixtures the fixtures to loaded. If not set, [[fixtures()]] will be loaded instead.
* @throws InvalidConfigException if fixtures are not properly configured or if a circular dependency among
* the fixtures is detected.
*/
public
function
loadFixtures
(
$fixtures
=
null
)
{
if
(
$fixtures
===
null
)
{
$fixtures
=
$this
->
fixtures
();
}
// normalize fixture configurations
$config
=
[];
// configuration provided in test case
$this
->
_fixtureAliases
=
[];
foreach
(
$fixtures
as
$name
=>
$fixture
)
{
if
(
!
is_array
(
$fixture
))
{
$fixtures
[
$name
]
=
[
'class'
=>
$fixture
];
}
elseif
(
!
isset
(
$fixture
[
'class'
]))
{
throw
new
InvalidConfigException
(
"You must specify 'class' for the fixture '
$name
'."
);
}
$config
[
$fixture
[
'class'
]]
=
$fixture
;
$this
->
_fixtureAliases
[
$name
]
=
$fixture
[
'class'
];
}
// create fixture instances
$this
->
_fixtures
=
[];
$stack
=
$fixtures
;
while
((
$fixture
=
array_pop
(
$stack
))
!==
null
)
{
if
(
$fixture
instanceof
Fixture
)
{
$class
=
get_class
(
$fixture
);
unset
(
$this
->
_fixtures
[
$class
]);
// unset so that the fixture is added to the last in the next line
$this
->
_fixtures
[
$class
]
=
$fixture
;
}
else
{
$class
=
$fixture
[
'class'
];
if
(
!
isset
(
$this
->
_fixtures
[
$class
]))
{
$this
->
_fixtures
[
$class
]
=
false
;
$stack
[]
=
$fixture
=
Yii
::
createObject
(
$fixture
);
foreach
(
$fixture
->
depends
as
$dep
)
{
// need to use the configuration provided in test case
$stack
[]
=
isset
(
$config
[
$dep
])
?
$config
[
$dep
]
:
[
'class'
=>
$dep
];
}
}
elseif
(
$this
->
_fixtures
[
$class
]
===
false
)
{
throw
new
InvalidConfigException
(
"A circular dependency is detected for fixture '
$class
'."
);
}
}
}
// load fixtures
/** @var Fixture $fixture */
foreach
(
$this
->
_fixtures
as
$fixture
)
{
$fixture
->
beforeLoad
();
}
foreach
(
$this
->
_fixtures
as
$fixture
)
{
$fixture
->
load
();
}
foreach
(
$this
->
_fixtures
as
$fixture
)
{
$fixture
->
afterLoad
();
}
}
/**
* Unloads all existing fixtures.
*/
public
function
unloadFixtures
()
{
/** @var Fixture $fixture */
foreach
(
array_reverse
(
$this
->
_fixtures
)
as
$fixture
)
{
$fixture
->
unload
();
}
}
/**
* @return array the loaded fixtures for the current test case
*/
public
function
getFixtures
()
{
return
$this
->
_fixtures
;
}
/**
* Returns the named fixture.
* @param string $name the fixture alias or class name
* @return Fixture the fixture object, or null if the named fixture does not exist.
*/
public
function
getFixture
(
$name
)
{
$class
=
isset
(
$this
->
_fixtureAliases
[
$name
])
?
$this
->
_fixtureAliases
[
$name
]
:
$name
;
return
isset
(
$this
->
_fixtures
[
$class
])
?
$this
->
_fixtures
[
$class
]
:
null
;
}
}
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