|
如附件所示运行和显示结果
测试代码
PHP复制代码
require "TestCase.php";
class TestBasic extends CITestCase {
function __construct (){
parent ::__construct (__CLASS__);
}
function setUp (){
//echo "asdf\n";
parent ::setUp();
}
function testOne (){
$this->assert(1,1);
}
function testTransaction (){
//$this->service->db->initialize();
//$this->service->db->conn_id;
$this->load->model("articlemodel");
//$this->service->db->conn_id->BeginTrans();
//$this->service->db->conn_id->CommitTrans();
//echo $this->service->db;
$limit = 4;
$offset = 0;
$articles = $this->service->articlemodel->getPagination($limit , $offset+$limit);
$this->assert(4 , sizeof($articles));
$this->service->db->from('article');
$ret = $this->service->db->count_all_results();
$this->assert(53 , $ret);
$data = array(
'title' => "testasdfasdfasfd"
);
$this->service->db->where('id' , 57);
$this->service->db->update('article', $data);
$article = $this->service->articlemodel->getById(57);
//echo $article->title;
//$this->service->db->trans_commit();
//$this->service->db->trans_begin();
//$this->service->db->query("delete from article");
//$this->service->db->trans_rollback();
}
function testDelete (){
$amount = 53;
$this->service->db->from('article');
$ret = $this->service->db->count_all_results();
$this->assert($amount , $ret);
$this->service->db->where('id', 57);
$this->service->db->delete('article');
$this->service->db->from('article');
$ret = $this->service->db->count_all_results();
$this->assert($amount-1 , $ret);
$this->service->db->where('id', 58);
$this->service->db->delete('article');
$this->service->db->from('article');
$ret = $this->service->db->count_all_results();
$this->assert($amount-2 , $ret);
#$this->assert($amount-1 , $ret);
}
function testURIChinese (){
//echo preg_match('/^[\xa1-\xff]+$/' , "的爱上对方");
//echo "\n";
//echo preg_quote('/^[\xa1-\xff]+$/');
}
}
new TestBasic ();
复制代码
TestCase.php
PHP复制代码
class TestCase {
private $test_object_name;
private $test_object;
private $need_test_methods;
protected $test_results = array();
public $testV = "original value";
function __construct ($test){
//$this->test_object_name = $test;
$this->need_test_methods = array();
$this->runAll();
}
function setUp (){
//echo "setup22\n";
}
function tearDown (){
}
/*
public static function runAll(){
//echo __CLASS__;
//$str = ;
//echo $str;
eval("\$nowtest = new ".TestUtil::getTestObjectName()."();");
$nowtest->test();
$this->test_object_name = __CLASS__;
echo $this->test_object_name;
$this->test_object = eval("new ".$this->test_object_name."();");
echo $this->test_object->test();
}
*/
public function runAll (){
//echo "asdf";
TestUtil ::getAllTestMethod($this);
//echo $this->testV;
foreach($this->need_test_methods as $method){
$this->run($method , new TestResult ());
}
$this->getResult();
}
function run ($method , $ret){
$this->setUp();
$ret->run($this , $method);
$this->tearDown();
}
function getNextMethod (){
}
function getTestObject (){
}
function addResult ($ret){
array_push($this->test_results , $ret);
}
function getResult (){
echo "\n------------------\nresult\n------------------\n";
foreach($this->test_results as $result){
$result->getResult();
}
}
function echoResult (){
}
function addTestMethod ($method_name){
array_push($this->need_test_methods , $method_name);
}
/************************* assert_true *************/
function assert($wanted , $result){
if($wanted == $result){
return true;
}else{
throw new UnitTestException ("should be $wanted , but was $result");
}
}
}
class UnitTestException extends Exception {
function UnitTestException ($message , $code = 0){
parent ::__construct ($message , $code);
}
}
class TestResult {
//protected $failures = array();
//protected $errors = array();
protected $error;
protected $testmethod_name;
//construct
function TestResult (){
}
function run ($testObject , $testmethod){
$this->testmethod_name = $testmethod;
//echo $testmethod;
//echo "\n";
try {
eval("\$testObject->$testmethod();");
}catch (Exception $e){
$this->error = $e->getMessage();
}
$testObject->addResult($this);
}
private function isSuccess (){
return $this->error == null;
}
private function addError (){
}
private function addFailure (){
}
function getAllErrors (){
}
function getAllExceptions (){
}
function test (){
echo "asdf";
}
function getSuccOrErrorResult (){
if($this->isSuccess()){
return "success";
}else{
return "fail(cause: $this->error)";
}
}
function getResult (){
echo "$this->testmethod_name: \n\t".$this->getSuccOrErrorResult()."\n";
//error number:".sizeof($this->errors)."\texceptions number:".sizeof($this->failures)."\n";
}
}
class TestUtil {
public static function getTestObjectName (){
//require $_SERVER['PHP_SELF'];
$ret = str_replace(".php" , "" , $_SERVER['PHP_SELF']);
return str_replace(".PHP" , "" , $ret);
}
public static function getAllTestMethod ($testObject){
//echo "$testObject->testV \n";
$testObject->testV = "changed";
//echo "$testObject->testV \n";
$ref = new ReflectionObject ($testObject);
foreach($ref->getMethods() as $method){
if($method->isPublic()){
if(strstr($method->getName() , "test")){
$testObject->addTestMethod($method->getName());
}
/*
if(strstr($method->getName() , "test")){
echo strstr($method->getName() , "test");
echo "\n";
}else{
echo "----\n";
}
*/
}
}
}
}
/******************* extends for CI ********************/
require "../../index.php";
class CITestCase extends TestCase {
function __construct ($test){
$this->_ci_initialize ();
$this->service = &get_instance ();
//echo "sadf";
//$this->setUp();
parent ::__construct ($test);
//$this->tearDown();
}
function setUp (){
//echo "setup11\n";
if(!$this->service->db->conn_id){
$this->service->db->initialize();
}
//$this->service->db->conn_id->BeginTrans();
$this->service->db->trans_begin();
parent ::setUp();
}
function tearDown (){
if($this->service->db->conn_id){
//$this->service->db->conn_id->RollbackTrans();
$this->service->db->trans_rollback();
}
parent ::tearDown();
}
function _ci_initialize ()
{
// Assign all the class objects that were instantiated by the
// front controller to local class variables so that CI can be
// run as one big super object.
$classes = array(
'config' => 'Config',
'input' => 'Input',
'benchmark' => 'Benchmark',
'uri' => 'URI',
'output' => 'Output',
'lang' => 'Language'
);
foreach ($classes as $var => $class)
{
$this->$var =& load_class ($class);
}
// In PHP 5 the Loader class is run as a discreet
// class. In PHP 4 it extends the Controller
if (floor(phpversion()) >= 5)
{
$this->load =& load_class ('Loader');
$this->load->_ci_autoloader ();
}
else
{
$this->_ci_autoloader ();
// sync up the objects since PHP4 was working from a copy
foreach (array_keys(get_object_vars($this)) as $attribute)
{
if (is_object($this->$attribute))
{
$this->load->$attribute =& $this->$attribute;
}
}
}
}
}
/*******************/
/**
* 1,需要一个默认controller
* 2,不能有输出
* 3,错误不能捕获
*/
function test_delay_start (){
echo "prepare for test";
sleep(1);
echo ".";
sleep(1);
echo ".";
sleep(1);
echo ".\n";
//eval("new ".TestUtil::getTestObjectName()."();");
new TestSample ();
}
//test_delay_start();
复制代码 |
-
评分
-
查看全部评分
|