1,首先把测试预加载数据导出成xml 命令如下
导出整个库
mysqldump --xml -uroot -p123456 cms --no-create-info>file.xml
导出库中某表
mysqldump --xml -uroot -p123456 cms pingzheng --no-create-info>file.xml
测试目录结构
2 model 测试基类BaseModelTest .php
<?php
/**
* 测试model 基类
*
*/
class BaseModelTest extends PHPUnit_Extensions_Database_TestCase{
//数据库PDO
static private $pdo = null;
static private $loadDataSet = false;
//数据库连接对象
private $conn = null;
/**
* 数据库连接
*
* @return
*/
final public function getConnection()
{
if ($this->conn === null) {
if (self::$pdo == null) {
self::$pdo = new PDO('mysql:host=localhost;dbname=cms;charset=utf8', 'root', '');
}
$this->conn = $this->createDefaultDBConnection(self::$pdo,"cms");
}
return $this->createDefaultDBConnection(self::$pdo, 'cms');
}
/**
* 数据集,每次实例化此类时,自动会员数据集 导入到数据库,即把file.xml 的导入库中
*
*/
public function getDataSet()
{
//防止实例化时再此导入数据集
if(!self::$loadDataSet){
self::$loadDataSet = true;
return $this->createMySQLXMLDataSet("model/file.xml");
}else{
return new PHPUnit_Extensions_Database_DataSet_QueryDataSet($this->getConnection());;
}
}
}
3 业务类与测试用例类
业务凭证类
<?php
/**
* classname:PingzhengModel
* desc:凭证数据访问对象
* create:2012-05-07
* author: xxxxx
*/
class PingzhengModel extends BasicModel
{
/**
* var:$tableName
* type:string
* access:protectted
* desc:设置Model的真实表名
* create:2012-05-07
* author:xxxx
*/
protected $tableName = "pingzheng";
/**
* function:shenhe
* desc:修改凭证审核状态
* create:2012-05-07
* author:xxxxx
* params:[string][$pingzhengid][][凭证id]
* params:[array][$shenhexinxi][][凭证审核信息(array("zhuangtai"=>1,"xingming"=>"xxx","shenherenid"=>1))]
* return:[boolean][成功true,否则false]
*/
public function shenhe($pingzhengid,$shenhexinxi){
$where = $data = array();
$where["pingzheng_id"] = intval($pingzhengid);
$data["shenhe_zhuangtai"] = $shenhexinxi["zhuangtai"];
$data["shenheren_id"] = $shenhexinxi["shenherenid"];
$data["shenheren_xingming"] = $shenhexinxi["xingming"];
$data["shenhe_shijian"] = time();
$ret = $this->where($where)->save($data);
if($ret === false){
return false;
}else{
return true;
}
}
}
?>
凭证测试类
<?php
/**
* 凭证model 测试类
*
*/
class Pingzheng extends BaseModelTest{
/**
* 审核凭证
*/
public function testshenhe(){
$pingzheng_mo = D("Pingzheng");
$data = array(
"zhuangtai"=>1,
"shenherenid"=>1,
"xingming"=>"xxxxx"
);
$pingzheng_ret = $pingzheng_mo->shenhe(292,$data);
$this->assertTrue($pingzheng_ret);
$pingzheng_ret1 = $pingzheng_mo->shenhe(999,$data);
$this->assertFalse($pingzheng_ret1);
}
}
?>
业务会员账户类HuiyuanZhanghu.php
<?php
/**
* classname:HuiyuanZhanghuModel
* desc:会员账户数据访问对象
* create:2012-05-07
* author:
*/
class HuiyuanZhanghuModel extends BasicModel
{
/**
* var:$tableName
* type:string
* access:protectted
* desc:设置Model的真实表名
* create:2012-05-07
* author:
*/
protected $tableName = "zhanghu";
private $loadDataSet = false;
/**
* 重写BaseModelTest 的getDataSet 以便加载会员账户预加载数据
*/
public function getDataSet()
{
//防止实例化时再此导入数据集
if(!$this->loadDataSet){
$this->loadDataSet = true;
return $this->createMySQLXMLDataSet(“file3.xml");
}else{
return new PHPUnit_Extensions_Database_DataSet_QueryDataSet($this->getConnection());;
}
}
/**
* function:addZhanghuJine
* desc:增加账户金额
* create:2012-05-07
* author:
* params:[int][$huiyuanid][0][会员id]
* params:[decimal][$jine][][增加的金额]
* return:[boolean][如果增加成功 返回true,否则返回false]
*/
public function addZhanghuJine($huiyuanid,$jine){
//验证会员id,与金额的合法性
if(intval($huiyuanid) <= 0 || !checkCurrency($jine)){
return false;
}
//增加货款账户金额,可用金额
$where = $data = array();
$where["huiyuan_id"] = intval($huiyuanid);
$data["zhanghu_jine"] = array("exp","zhanghu_jine + ".$jine);
$data["keyong_jine"] = array("exp","keyong_jine + ".$jine);
$ret = $this->where($where)->save($data);
if($ret === false){
return false;
}
return true;
}
}
会员账户测试类
<?php
/**
* 会员账户model 测试类
*
*/
class HuiyuanZhanghu extends BaseModelTest{
/**
* 增加账户金额
*/
public function testaddZhanghuJine(){
$huiyuan_mo = D("HuiyuanZhanghu");
$zhanghu_ret = $huiyuan_mo->addZhanghuJine(1,10.05);
$this->assertEquals(true, $zhanghu_ret);
}
}
?>
测试套件ModelSuite.php
<?php
require_once 'ModelInit.php';
require_once 'PHPUnit/Autoload.php';
require_once 'model/BaseModelTest.php';
class ModelSuite{
public static function main() {
PHPUnit_TextUI_TestRunner::run(self::suite());
}
public static function suite() {
$suite = new PHPUnit_Framework_TestSuite("ModelTestCase");
$suite->addTestFile('model/HuiyuanZhanghu.php');
$suite->addTestFile('model/Pingzheng.php');
return $suite;
}
}
?>
执行测试套件
phpunit ModelSuite.php