测试类 (TEST.class.php)

  1 <?php
  2 /**
  3  * Test class
  4  *
  5  * Usage:
  6  * require 'TEST_CLASS_PATH';
  7  * $TEST = new TEST();
  8  * $TEST->setArg('server', 'a', 8);
  9  * $TEST->setArg('server', 'b', 2);
 10  * $TEST->setArg(1);
 11  * $TEST->run('@fortest.a');
 12  */
 13
 14 class TEST {
 15     /**
 16      * Private vars
 17      */
 18     private
 19         $args = array(),
 20         $startTime = null,
 21         $startMemory = null;
 22
 23     /**
 24      * Public method: __construct()
 25      */
 26     public function __construct(){
 27         $this->startTime = array_sum(explode(' ', microtime()));
 28         $this->startMemory = memory_get_usage();
 29     }
 30
 31     /**
 32      * Public method: setArg()
 33      */
 34     public function setArg(){
 35         $args = func_get_args();
 36
 37         // get type
 38         if (isset($args[2])){
 39             $type = strtolower($args[0]);
 40             $key = $args[1];
 41             $value = $args[2];
 42         } else 
 43             $type = '';
 44
 45         switch($type){
 46             case 'define':
 47                 define($key, $value);
 48                 break;
 49             case 'post':
 50                 $_POST[$key] = $value;
 51                 break;
 52             case 'get':
 53                 $_GET[$key] = $value;
 54                 break;
 55             case 'server':
 56                 $_SERVER[$key] = $value;
 57                 break;
 58             default:
 59                 $this->args[] = $args[0];
 60                 break;
 61         }
 62     }
 63
 64     /**
 65      * Public method: run()
 66      *
 67      * @param string $method
 68      */
 69     public function run($method){
 70         if (empty($method)) 
 71             return;
 72
 73         // parse action and class
 74         $strings = explode('.', $method);
 75         $action = array_pop($strings);
 76         $class = array_pop($strings);
 77
 78         echo '<pre>';
 79
 80         // include class file
 81         $path = '';
 82         if (count($strings) > 0){
 83             foreach($strings as $str)
 84                 $path .= ($path && DIRECTORY_SEPARATOR != substr($path, -1) ? DIRECTORY_SEPARATOR : ''). $str;
 85
 86             $path .= ($path ? DIRECTORY_SEPARATOR : ''). $class . '.class.php';
 87         } elseif (0 === strpos($class, '@')){
 88             $class = substr($class, 1);
 89             $path = $class. '.class.php';  
 90         }
 91
 92         if ($path)
 93             require $path;
 94
 95         // run action
 96         ob_start();
 97         if ($class)
 98             $return = call_user_func_array(array($class, $action), $this->args);
 99         else
100             $return = call_user_func_array($action, $this->args);
101
102         $output = ob_get_contents();
103         ob_end_clean();
104
105         // output response/output
106         echo "<strong>RESPONSE:</strong> /n";
107         var_dump($return);
108         echo "/n<strong>OUTPUT:</strong> /n";
109         var_dump($output);
110
111         echo "<hr /><strong>Time-consuming:</strong>/n". (array_sum(explode(' ', microtime()))-$this->startTime). " ms";
112         echo "/n/n<strong>Memory-usage:</strong>/n". (memory_get_usage()-$this->startMemory). " bytes";
113     }
114 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值