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
106e9124
Commit
106e9124
authored
Jan 17, 2014
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added tests for Fixture feature.
parent
e0bc9f40
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
249 additions
and
1 deletion
+249
-1
ActiveFixture.php
framework/test/ActiveFixture.php
+1
-0
FixtureTrait.php
framework/test/FixtureTrait.php
+1
-1
ActiveFixtureTest.php
tests/unit/framework/test/ActiveFixtureTest.php
+75
-0
FixtureTest.php
tests/unit/framework/test/FixtureTest.php
+156
-0
tbl_customer.php
tests/unit/framework/test/data/tbl_customer.php
+16
-0
No files found.
framework/test/ActiveFixture.php
View file @
106e9124
...
@@ -94,6 +94,7 @@ class ActiveFixture extends Fixture implements \IteratorAggregate, \ArrayAccess,
...
@@ -94,6 +94,7 @@ class ActiveFixture extends Fixture implements \IteratorAggregate, \ArrayAccess,
$table
=
$this
->
getTableSchema
();
$table
=
$this
->
getTableSchema
();
$this
->
resetTable
();
$this
->
resetTable
();
$this
->
rows
=
[];
$this
->
rows
=
[];
$this
->
_models
=
[];
foreach
(
$this
->
loadData
()
as
$alias
=>
$row
)
{
foreach
(
$this
->
loadData
()
as
$alias
=>
$row
)
{
$this
->
db
->
createCommand
()
->
insert
(
$table
->
fullName
,
$row
)
->
execute
();
$this
->
db
->
createCommand
()
->
insert
(
$table
->
fullName
,
$row
)
->
execute
();
if
(
$table
->
sequenceName
!==
null
)
{
if
(
$table
->
sequenceName
!==
null
)
{
...
...
framework/test/FixtureTrait.php
View file @
106e9124
...
@@ -73,7 +73,7 @@ trait FixtureTrait
...
@@ -73,7 +73,7 @@ trait FixtureTrait
$this
->
_fixtureAliases
=
[];
$this
->
_fixtureAliases
=
[];
foreach
(
$fixtures
as
$name
=>
$fixture
)
{
foreach
(
$fixtures
as
$name
=>
$fixture
)
{
if
(
!
is_array
(
$fixture
))
{
if
(
!
is_array
(
$fixture
))
{
$fixtures
[
$name
]
=
[
'class'
=>
$fixture
];
$fixtures
[
$name
]
=
$fixture
=
[
'class'
=>
$fixture
];
}
elseif
(
!
isset
(
$fixture
[
'class'
]))
{
}
elseif
(
!
isset
(
$fixture
[
'class'
]))
{
throw
new
InvalidConfigException
(
"You must specify 'class' for the fixture '
$name
'."
);
throw
new
InvalidConfigException
(
"You must specify 'class' for the fixture '
$name
'."
);
}
}
...
...
tests/unit/framework/test/ActiveFixtureTest.php
0 → 100644
View file @
106e9124
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace
yiiunit\framework\test
;
use
yii\test\ActiveFixture
;
use
yii\test\FixtureTrait
;
use
yiiunit\data\ar\Customer
;
use
yiiunit\framework\db\DatabaseTestCase
;
class
CustomerFixture
extends
ActiveFixture
{
public
$modelClass
=
'yiiunit\data\ar\Customer'
;
}
class
MyDbTestCase
{
use
FixtureTrait
;
public
function
fixtures
()
{
return
[
'customers'
=>
CustomerFixture
::
className
(),
];
}
}
/**
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class
ActiveFixtureTest
extends
DatabaseTestCase
{
public
function
setUp
()
{
parent
::
setUp
();
\Yii
::
$app
->
setComponent
(
'db'
,
$this
->
getConnection
());
Customer
::
$db
=
$this
->
getConnection
();
}
public
function
tearDown
()
{
parent
::
tearDown
();
}
public
function
testGetRows
()
{
$test
=
new
MyDbTestCase
();
$test
->
loadFixtures
();
$fixture
=
$test
->
getFixture
(
'customers'
);
$this
->
assertEquals
(
CustomerFixture
::
className
(),
get_class
(
$fixture
));
$this
->
assertEquals
(
2
,
count
(
$fixture
));
$this
->
assertEquals
(
1
,
$fixture
[
'customer1'
][
'id'
]);
$this
->
assertEquals
(
'customer1@example.com'
,
$fixture
[
'customer1'
][
'email'
]);
$this
->
assertEquals
(
2
,
$fixture
[
'customer2'
][
'id'
]);
$this
->
assertEquals
(
'customer2@example.com'
,
$fixture
[
'customer2'
][
'email'
]);
}
public
function
testGetModel
()
{
$test
=
new
MyDbTestCase
();
$test
->
loadFixtures
();
$fixture
=
$test
->
getFixture
(
'customers'
);
$this
->
assertEquals
(
Customer
::
className
(),
get_class
(
$fixture
->
getModel
(
'customer1'
)));
$this
->
assertEquals
(
1
,
$fixture
->
getModel
(
'customer1'
)
->
id
);
$this
->
assertEquals
(
'customer1@example.com'
,
$fixture
->
getModel
(
'customer1'
)
->
email
);
$this
->
assertEquals
(
2
,
$fixture
->
getModel
(
'customer2'
)
->
id
);
$this
->
assertEquals
(
'customer2@example.com'
,
$fixture
->
getModel
(
'customer2'
)
->
email
);
}
}
tests/unit/framework/test/FixtureTest.php
0 → 100644
View file @
106e9124
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace
yiiunit\framework\test
;
use
yii\test\Fixture
;
use
yii\test\FixtureTrait
;
use
yiiunit\TestCase
;
class
Fixture1
extends
Fixture
{
public
$depends
=
[
'yiiunit\framework\test\Fixture2'
];
public
function
load
()
{
MyTestCase
::
$load
.=
'1'
;
}
public
function
unload
()
{
MyTestCase
::
$unload
.=
'1'
;
}
}
class
Fixture2
extends
Fixture
{
public
$depends
=
[
'yiiunit\framework\test\Fixture3'
];
public
function
load
()
{
MyTestCase
::
$load
.=
'2'
;
}
public
function
unload
()
{
MyTestCase
::
$unload
.=
'2'
;
}
}
class
Fixture3
extends
Fixture
{
public
function
load
()
{
MyTestCase
::
$load
.=
'3'
;
}
public
function
unload
()
{
MyTestCase
::
$unload
.=
'3'
;
}
}
class
MyTestCase
{
use
FixtureTrait
;
public
$scenario
=
1
;
public
static
$load
;
public
static
$unload
;
public
function
fixtures
()
{
switch
(
$this
->
scenario
)
{
case
0
:
return
[];
case
1
:
return
[
'fixture1'
=>
Fixture1
::
className
(),
];
case
2
:
return
[
'fixture2'
=>
Fixture2
::
className
(),
];
case
3
:
return
[
'fixture3'
=>
Fixture3
::
className
(),
];
case
4
:
return
[
'fixture1'
=>
Fixture1
::
className
(),
'fixture2'
=>
Fixture2
::
className
(),
];
case
5
:
return
[
'fixture2'
=>
Fixture2
::
className
(),
'fixture3'
=>
Fixture3
::
className
(),
];
case
6
:
return
[
'fixture1'
=>
Fixture1
::
className
(),
'fixture3'
=>
Fixture3
::
className
(),
];
case
7
:
default
:
return
[
'fixture1'
=>
Fixture1
::
className
(),
'fixture2'
=>
Fixture2
::
className
(),
'fixture3'
=>
Fixture3
::
className
(),
];
}
}
}
class
FixtureTest
extends
TestCase
{
public
function
testDependencies
()
{
foreach
(
$this
->
getDependencyTests
()
as
$scenario
=>
$result
)
{
$test
=
new
MyTestCase
();
$test
->
scenario
=
$scenario
;
$test
->
loadFixtures
();
foreach
(
$result
as
$name
=>
$loaded
)
{
$this
->
assertEquals
(
$loaded
,
$test
->
getFixture
(
$name
)
!==
null
,
"Verifying scenario
$scenario
fixture
$name
"
);
}
}
}
public
function
testLoadSequence
()
{
foreach
(
$this
->
getLoadSequenceTests
()
as
$scenario
=>
$result
)
{
$test
=
new
MyTestCase
();
$test
->
scenario
=
$scenario
;
MyTestCase
::
$load
=
''
;
MyTestCase
::
$unload
=
''
;
$test
->
loadFixtures
();
$this
->
assertEquals
(
$result
[
0
],
MyTestCase
::
$load
,
"Verifying scenario
$scenario
load sequence"
);
$test
->
unloadFixtures
();
$this
->
assertEquals
(
$result
[
1
],
MyTestCase
::
$unload
,
"Verifying scenario
$scenario
unload sequence"
);
}
}
protected
function
getDependencyTests
()
{
return
[
0
=>
[
'fixture1'
=>
false
,
'fixture2'
=>
false
,
'fixture3'
=>
false
],
1
=>
[
'fixture1'
=>
true
,
'fixture2'
=>
false
,
'fixture3'
=>
false
],
2
=>
[
'fixture1'
=>
false
,
'fixture2'
=>
true
,
'fixture3'
=>
false
],
3
=>
[
'fixture1'
=>
false
,
'fixture2'
=>
false
,
'fixture3'
=>
true
],
4
=>
[
'fixture1'
=>
true
,
'fixture2'
=>
true
,
'fixture3'
=>
false
],
5
=>
[
'fixture1'
=>
false
,
'fixture2'
=>
true
,
'fixture3'
=>
true
],
6
=>
[
'fixture1'
=>
true
,
'fixture2'
=>
false
,
'fixture3'
=>
true
],
7
=>
[
'fixture1'
=>
true
,
'fixture2'
=>
true
,
'fixture3'
=>
true
],
];
}
protected
function
getLoadSequenceTests
()
{
return
[
0
=>
[
''
,
''
],
1
=>
[
'321'
,
'123'
],
2
=>
[
'32'
,
'23'
],
3
=>
[
'3'
,
'3'
],
4
=>
[
'321'
,
'123'
],
5
=>
[
'32'
,
'23'
],
6
=>
[
'321'
,
'123'
],
7
=>
[
'321'
,
'123'
],
];
}
}
tests/unit/framework/test/data/tbl_customer.php
0 → 100644
View file @
106e9124
<?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