Chinaunix
标题:
YII的固定状态模式测试(Fixtures Test)
[打印本页]
作者:
liu1084
时间:
2011-12-20 09:48
标题:
YII的固定状态模式测试(Fixtures Test)
class
ProjectTest
extends
CDbTestCase
{
/
/
test fixtures
public $fixtures
=
array
(
'projects'
=
>
'Project'
,
/
/
可以直接在这里$this
-
>
projects引用里面的数据
)
;
public function testCreate
(
)
{
/
/
由于设置了fixturs数据,所以在测试开始的时候,fixture就会填充到表中
$newProject
=
new Project
;
$newProjectName
=
'test project creation'
;
$newProject
-
>
setAttributes
(
array
(
'name'
=
>
$newProjectName
,
'description'
=
>
'test project creation description'
,
)
)
;
$this
-
>
assertTrue
(
$
newProject
-
>
save
(
false
)
)
;
/
/
读取刚刚建立的project
$retrivedLastProject
=
Project
:
:
model
(
)
-
>
findByPk
(
$
newProject
-
>
id
)
;
/
/
var_dump
(
$
retrivedLastProject
-
>
id
)
;
/
/
4
$this
-
>
assertTrue
(
$
retrivedLastProject instanceof Project
)
;
$this
-
>
assertEquals
(
$
retrivedLastProject
-
>
name
,
$newProjectName
)
;
}
public function testRead
(
)
{
/
*
*
从表trackstar_test
.
tbl_project以AR对象的方式返回一行数据,project1表示的是fixtures设置的key,它对应的值:
'project1'
=
>
array
(
'name'
=
>
'fixture data for project1 name'
,
'description'
=
>
'fixture data for project1 description.'
,
'create_time'
=
>
'2010-03-19 22:25:21'
,
'create_user_id'
=
>
'1'
,
'update_time'
=
>
'2010-03-19 22:25:22'
,
'update_user_id'
=
>
'2'
,
)
,
*
/
$retrivedProject
=
$this
-
>
projects
(
'project1'
)
;
$this
-
>
assertTrue
(
$
retrivedProject instanceof Project
)
;
$this
-
>
assertEquals
(
$
retrivedProject
-
>
name
,
'fixture data for project1 name'
)
;
}
public function testDelete
(
)
{
$project
=
$this
-
>
projects
(
'project3'
)
;
$savedProjectId
=
$project
-
>
id
;
$this
-
>
assertTrue
(
$
project
-
>
delete
(
)
)
;
$criteria
=
new CDbCriteria
(
array
(
'condition'
=
>
'id=:id'
,
'params'
=
>
array
(
':id'
=
>
$savedProjectId
,
)
,
)
)
;
$this
-
>
assertEquals
(
NULL
,
Project
:
:
model
(
)
-
>
find
(
$
criteria
)
)
;
}
public function testUpdate
(
)
{
$project
=
$this
-
>
projects
(
'project2'
)
;
$updateTheSecondProjectName
=
'Update the second project name'
;
$project
-
>
name
=
$updateTheSecondProjectName
;
$this
-
>
assertTrue
(
$
project
-
>
save
(
FALSE
)
)
;
/
/
读取刚刚更新的数据
$updateProject
=
Project
:
:
model
(
)
-
>
findByPk
(
$
project
-
>
id
)
;
$this
-
>
assertTrue
(
$
updateProject instanceof Project
)
;
$this
-
>
assertEquals
(
$
updateProject
-
>
name
,
$updateTheSecondProjectName
)
;
}
}
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2