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
a5245ad0
Commit
a5245ad0
authored
Sep 07, 2014
by
Ragazzo
Committed by
Qiang Xue
Sep 07, 2014
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added array fixture class
parent
92f7b3fb
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
150 additions
and
0 deletions
+150
-0
ArrayFixture.php
framework/test/ArrayFixture.php
+76
-0
ArrayFixtureTest.php
tests/unit/framework/test/ArrayFixtureTest.php
+58
-0
array_fixture.php
tests/unit/framework/test/data/array_fixture.php
+16
-0
No files found.
framework/test/ArrayFixture.php
0 → 100644
View file @
a5245ad0
<?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\ArrayAccessTrait
;
use
yii\base\InvalidParamException
;
/**
* Base array fixture class, that supports loading data from data files.
*
* @author Mark Jebri <mark.github@yandex.ru>
* @since 2.0
*/
class
ArrayFixture
extends
Fixture
implements
\IteratorAggregate
,
\ArrayAccess
,
\Countable
{
use
ArrayAccessTrait
;
/**
* @var array the data rows. Each array element represents one row of data (column name => column value).
*/
public
$data
=
[];
/**
* @var string|boolean the file path or path alias of the data file that contains the fixture data
* to be returned by [[getData()]]. You can set this property to be false to prevent loading any data.
*/
public
$dataFile
;
/**
* Loads the fixture.
*
* The default implementation simply stores the data returned by [[getData()]] in [[data]].
* You should usually override this method by putting the data into the underlying database.
*/
public
function
load
()
{
$this
->
data
=
$this
->
getData
();
}
/**
* Returns the fixture data.
*
* The default implementation will try to return the fixture data by including the external file specified by [[dataFile]].
* The file should return the data array that will be stored in [[data]] after inserting into the database.
*
* @return array the data to be put into the database
* @throws InvalidConfigException if the specified data file does not exist.
*/
protected
function
getData
()
{
if
(
$this
->
dataFile
===
false
||
$this
->
dataFile
===
null
)
{
return
[];
}
$dataFile
=
Yii
::
getAlias
(
$this
->
dataFile
);
if
(
is_file
(
$dataFile
))
{
return
require
(
$dataFile
);
}
else
{
throw
new
InvalidConfigException
(
"Fixture data file does not exist:
{
$this
->
dataFile
}
"
);
}
}
/**
* @inheritdoc
*/
public
function
unload
()
{
parent
::
unload
();
$this
->
data
=
[];
}
}
tests/unit/framework/test/ArrayFixtureTest.php
0 → 100644
View file @
a5245ad0
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace
yiiunit\framework\test
;
use
yiiunit\TestCase
;
use
yii\test\ArrayFixture
;
class
ArrayFixtureTest
extends
TestCase
{
/**
* @var \yii\test\ArrayFixture
*/
private
$_fixture
;
protected
function
setUp
()
{
parent
::
setUp
();
$this
->
_fixture
=
new
ArrayFixture
();
}
public
function
testLoadUnloadParticularFile
()
{
$this
->
_fixture
->
dataFile
=
'@yiiunit/framework/test/data/array_fixture.php'
;
$this
->
assertEmpty
(
$this
->
_fixture
->
data
,
'fixture data should be empty'
);
$this
->
_fixture
->
load
();
$this
->
assertCount
(
2
,
$this
->
_fixture
->
data
,
'fixture data should match needed total count'
);
$this
->
assertEquals
(
'customer1'
,
$this
->
_fixture
[
'customer1'
][
'name'
],
'first fixture data should match'
);
$this
->
assertEquals
(
'customer2@example.com'
,
$this
->
_fixture
[
'customer2'
][
'email'
],
'second fixture data should match'
);
}
public
function
testNothingToLoad
()
{
$this
->
_fixture
->
dataFile
=
false
;
$this
->
assertEmpty
(
$this
->
_fixture
->
data
,
'fixture data should be empty'
);
$this
->
_fixture
->
load
();
$this
->
assertEmpty
(
$this
->
_fixture
->
data
,
'fixture data should not be loaded'
);
}
/**
* @expectedException yii\base\InvalidParamException
*/
public
function
testWrongDataFileException
()
{
$this
->
_fixture
->
dataFile
=
'@wrong/fixtures/data/path/alias'
;
$this
->
_fixture
->
load
();
}
}
tests/unit/framework/test/data/array_fixture.php
0 → 100644
View file @
a5245ad0
<?php
return
[
'customer1'
=>
[
'email'
=>
'customer1@example.com'
,
'name'
=>
'customer1'
,
'address'
=>
'address1'
,
'status'
=>
1
,
],
'customer2'
=>
[
'email'
=>
'customer2@example.com'
,
'name'
=>
'customer2'
,
'address'
=>
'address2'
,
'status'
=>
2
,
],
];
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