免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1258 | 回复: 0

YII的固定状态模式测试(Fixtures Test) [复制链接]

论坛徽章:
0
发表于 2011-12-20 09:48 |显示全部楼层
  1. class ProjectTest extends CDbTestCase {
  2.     //test fixtures
  3.     public $fixtures = array(
  4.         'projects' => 'Project', //可以直接在这里$this->projects引用里面的数据
  5.     );

  6.     public function testCreate(){
  7.         //由于设置了fixturs数据,所以在测试开始的时候,fixture就会填充到表中
  8.         $newProject = new Project;
  9.         $newProjectName = 'test project creation';
  10.         $newProject->setAttributes(array(
  11.             'name' => $newProjectName,
  12.             'description' => 'test project creation description',
  13.         ));

  14.         $this->assertTrue($newProject->save(false));

  15.         //读取刚刚建立的project
  16.         $retrivedLastProject = Project::model()->findByPk($newProject->id);
  17.         //var_dump($retrivedLastProject->id); //4
  18.         $this->assertTrue($retrivedLastProject instanceof Project);
  19.         $this->assertEquals($retrivedLastProject->name, $newProjectName);
  20.     }

  21.     public function testRead(){
  22.         /**从表trackstar_test.tbl_project以AR对象的方式返回一行数据,project1表示的是fixtures设置的key,它对应的值:
  23.         'project1' => array(
  24.             'name' => 'fixture data for project1 name',
  25.             'description' => 'fixture data for project1 description.',
  26.             'create_time' => '2010-03-19 22:25:21',
  27.             'create_user_id' => '1',
  28.             'update_time' => '2010-03-19 22:25:22',
  29.             'update_user_id' => '2',
  30.         ),
  31.          */
  32.         $retrivedProject = $this->projects('project1');
  33.         $this->assertTrue($retrivedProject instanceof Project);
  34.         $this->assertEquals($retrivedProject->name, 'fixture data for project1 name');
  35.     }

  36.     public function testDelete(){
  37.         $project = $this->projects('project3');

  38.         $savedProjectId = $project->id;
  39.         $this->assertTrue($project->delete());
  40.         $criteria = new CDbCriteria(array(
  41.             'condition' => 'id=:id',
  42.             'params' => array(
  43.                 ':id' => $savedProjectId,
  44.             ),
  45.         ));
  46.         $this->assertEquals(NULL, Project::model()->find($criteria));
  47.     }

  48.     public function testUpdate(){
  49.         $project = $this->projects('project2');
  50.         $updateTheSecondProjectName = 'Update the second project name';
  51.         $project->name = $updateTheSecondProjectName;
  52.         $this->assertTrue($project->save(FALSE));
  53.         //读取刚刚更新的数据
  54.         $updateProject = Project::model()->findByPk($project->id);
  55.         $this->assertTrue($updateProject instanceof Project);
  56.         $this->assertEquals($updateProject->name, $updateTheSecondProjectName);
  57.     }
  58. }
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP