您的位置:首页 > 其它

Configuring Gii and test CRUD

2012-05-02 16:28 281 查看
2012.5.3

>>>Configuring Gii and test CRUD<<<

1. Edit protected/config/main.php,去掉下列语句的注释

'modules'=>array(

'gii'=>array(

'class'=>'system.gii.GiiModule',

'password'=>'[add_your_password_here]',

),

2. visit: http://localhost/yii/power/index.php?r=gii
3. select "Model Generator"

enter:

pwr_ for Table Prefix

pwr_user for Table Name

and others are created automatically.

click "Preview" button to view the source code of Usr.php

click "Generate" to create the real file.

4. Creating the unit test file protected/tests/unit/UserTest.php

<?php

class UserTest extends CDbTestCase

{

public function testCRUD()

{

//Create a new User

$newUser=new User;

$newUserName = 'tuser1';

$newUser->setAttributes(

array(

'user_id' => $newUserName,

'user_pwd' => 'tuser1',

)

);

//insert new user record to database

$this->assertTrue($newUser->save(false));

//READ back the newly created project

$retrievedUser=User::model()->findByPk($newUser->user_id);

$this->assertTrue($retrievedUser instanceof User);

$this->assertEquals($newUserName,$retrievedUser->user_id);

//UPDATE the newly created user

$updatedUserName = 'utser1';

$newUser->user_id = $updatedUserName;

$this->assertTrue($newUser->save(false));

//read back the record again to ensure the update worked

$updatedUser=User::model()->findByPk($newUser->user_id);

$this->assertTrue($updatedUser instanceof User);

$this->assertEquals($updatedUserName,$updatedUser->user_id);

//DELETE the user

//$newUserId = $newUser->user_id;

//$this->assertTrue($newUser->delete());

//$deletedUser=User::model()->findByPk($newUserId);

//$this->assertEquals(NULL,$deletedUser);

}

}

5. 执行

C:\xampp\yii\power\protected\tests> phpunit unit/UserTest.php
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: