考虑一个名为 user 的控制器,其代码如下
class User_Controller extends TinyMVC_Controller
{
function index()
{
$oUserEntity = new User_Entity_Model();
$oUserEntity->guid = 1;
$oUserEntity->name = 'aaaaaa';
$oUserEntity->username = 'aaaaaaa';
$oUserEntity->password = 'newnew';
$oUserEntity->email = 'aaaaaa@gmail.com';
$oUserEntity->language = 'en';
$oUserEntity->code = 'xyz';
$oUserEntity->save();
}
}
?>
及其对应的名为 user_entity_model.php 的模型,
<?php
class User_Entity_Model extends Entity_Model
{
/* some code..*/
public function save() {
// Save generic stuff
if (!parent::save()) {
return false;
}
// Now save specific stuff
return $this->create_user_entity($this->get('guid'), $this->get('name'), $this->get('username'),
$this->get('password'), $this->get('email'), $this->get('language'), $this->get('code'));
}
public function create_user_entity($guid, $name, $username, $password,$email, $language, $code) {
global $CONFIG;
$guid = (int)$guid;
$query = "INSERT into users_entity
(guid, name, username, password, sapcode, salt, email, language, code) values ($guid, '$name', '$username', '$password', '$email', '$language', '$code')";
$result = $this->db->query($query);
return $guid;
}
}
我使用 simpletest 来测试代码。因此,为了测试函数 save(),我创建了测试文件夹和测试文件 userEntityTest.php
class userEntityTest extends UnitTestCase
{
function testSave(){
$oUserEntity = new User_Entity_Model();
$oUserEntity->guid = 1;
$oUserEntity->name = 'aaaa';
$oUserEntity->username = 'aaaa';
$oUserEntity->password = 'newnew';
$oUserEntity->email = 'cool123@gmail.com';
$oUserEntity->language = 'en';
$oUserEntity->code = 'xyz';
$guid = $oUserEntity->save();
$cp = new UnitTestCase();
$cp->AssertNotEqual($guid, 0);
}
}
在浏览器中执行测试时出现以下错误
(!)致命错误:在第 3 行的 C:\wamp\www\career portal\tinymvc\myapp\models\user_entity_model.php 中找不到类 'Entity_Model'
userEntityTest.php 失败:-> Bad TestSuite [userEntityTest.php] 错误 [[userEntityTest.php] 中没有可运行的测试用例] 0/0 测试用例完成:0 次通过,1 次失败和 0 次异常。
请帮助我成功运行测试。