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 }
