Permalink
Browse files

add Tester for automated tests

  • Loading branch information...
1 parent 0ce7718 commit 923b6c5556e2270d2c7595da9849bfba4131be6c @romaninsh romaninsh committed Jul 6, 2011
Showing with 95 additions and 0 deletions.
  1. +95 −0 lib/Page/Tester.php
View
@@ -0,0 +1,95 @@
+<?php
+class Page_Tester extends Page {
+ public $variances=array();
+ function setVariance($arr){
+ $this->variances=$arr;
+ foreach($arr as $key=>$item){
+ if(is_numeric($key))$key=$item;
+ $this->grid->addColumn('html',$key.'_inf',$key.' info');
+ $this->grid->addColumn('text,wrap',$key.'_res',$key.' result');
+ }
+ }
+ function init(){
+ parent::init();
+ $this->grid=$this->add('Grid');
+ $this->grid->addColumn('text','name');
+
+ //$this->setVariance(array('GiTemplate','SMlite'));
+ $this->setVariance(array('Test'));
+
+ $this->runTests();
+ }
+ function runTests($test_obj=null){
+
+ if(!$test_obj)$test_obj=$this;
+
+ $tested=array();
+ $data=array();
+ foreach(get_class_methods($test_obj) as $method){
+ $m='';
+ if(substr($method,0,5)=='test_'){
+ $m=substr($method,5);
+ }elseif(substr($method,0,8)=='prepare_'){
+ $m=substr($method,8);
+ }else continue;
+
+ // Do not retest same function even if it has both prepare and test
+ if($tested[$m])continue;$tested[$m]=true;
+
+ // Row contains test result data
+ $row=array('name'=>$m);
+
+ foreach($this->variances as $key=>$vari){
+ if(is_numeric($key))$key=$vari;
+
+ // Input is a result of preparation function
+ if(method_exists($test_obj,'prepare_'.$m)){
+ $input=$test_obj->{'prepare_'.$m}($vari,$method);
+ }else{
+ $input=$test_obj->prepare($vari,$method);
+ }
+
+ $test_func=method_exists($test_obj,'test_'.$m)?
+ 'test_'.$m:'test';
+
+ // Test speed
+ $me=memory_get_peak_usage();
+ $ms=microtime(true);
+ /*
+ $limit=20;$hl=round($limit /2);
+ for($i=0;$i<$limit;$i++){
+ //$result=call_user_func_array(array($test_obj,$test_func),$input);
+ */
+ $result=(string)$test_obj->$test_func($input[0],$input[1]);
+ //$this->$method($vari);
+ /*
+ if($i==$hl){
+ $meh=memory_get_peak_usage();
+ }
+ */
+ //}
+ $ms=microtime(true)-$ms;
+ $me=($mend=memory_get_peak_usage())-$me;
+ $x=$row[$key.'_inf']='Speed: '.round($ms,3).'<br/>Memory: '.$me;
+
+ $x=$row[$key.'_res']=$result;
+ }
+
+ $data[]=$row;
+ }
+ $this->grid->setStaticSource($data);
+ }
+ function expect($value,$expectation){
+ return $value==$expectation?'OK':'ERR';
+ }
+
+ function _prepare($t,$str){
+ $result='';
+
+ for($i=0;$i<100;$i++){
+ $result.=$str;
+ }
+ return array($this->add($t),$result);
+ }
+
+}

0 comments on commit 923b6c5

Please sign in to comment.