From 61bb16ee2e5f0a66234e1575242169a10fde47b5 Mon Sep 17 00:00:00 2001
From: xue <>
Date: Fri, 7 Jul 2006 14:54:15 +0000
Subject: Merge from 3.0 branch till 1253.
---
tests/test_tools/unit_tests.php | 134 ++++++++++++++++++++++++++++++++++++++++
1 file changed, 134 insertions(+)
create mode 100644 tests/test_tools/unit_tests.php
(limited to 'tests/test_tools/unit_tests.php')
diff --git a/tests/test_tools/unit_tests.php b/tests/test_tools/unit_tests.php
new file mode 100644
index 00000000..732c6826
--- /dev/null
+++ b/tests/test_tools/unit_tests.php
@@ -0,0 +1,134 @@
+initApplication();
+ }
+}
+
+class TestFolder
+{
+ public $name='';
+ public $url='';
+ public $subFolders=array();
+ public $testFiles=array();
+
+ public function __construct($path,$rootPath,$rootUri)
+ {
+ $script = basename($_SERVER['SCRIPT_NAME']);
+ $this->url="$rootUri/$script?target=".strtr(substr($path,strlen($rootPath)+1),"\\",'/');
+ $this->name=basename($path);
+ $dir=opendir($path);
+ while(($entry=readdir($dir))!==false)
+ {
+ $fullpath="$path/$entry";
+ if($entry!=='.' && $entry!=='..' && $entry!=='.svn' && is_dir($fullpath))
+ {
+ $folder=new TestFolder($fullpath,$rootPath,$rootUri);
+ if(!empty($folder->subFolders) || !empty($folder->testFiles))
+ $this->subFolders[]=$folder;
+ }
+ else if(is_file($fullpath) && (strncmp($entry,'ut',2)===0
+ || preg_match('/test.*\.php/', strtolower($entry))))
+ {
+ $this->testFiles[$entry]="$rootUri/$script?target=".strtr(substr($fullpath,strlen($rootPath)+1),"\\",'/');
+ }
+ }
+ closedir($dir);
+ }
+
+ public function getHtml($level=0)
+ {
+ $str=str_repeat(' ',$level*4)."[ url}\">{$this->name} ]
\n";
+ foreach($this->subFolders as $folder)
+ $str.=$folder->getHtml($level+1);
+ foreach($this->testFiles as $name=>$url)
+ $str.=str_repeat(' ',($level+1)*4)."$name
\n";
+ return $str;
+ }
+}
+
+class PradoUnitTester
+{
+ private $_root;
+
+ function __construct($root, $app_dir)
+ {
+ $this->_root = $root;
+ $app = new TMockApplication($app_dir);
+ $app->run();
+ }
+
+ function addTests($test,$path,$recursive)
+ {
+ $dir=opendir($path);
+ while(($entry=readdir($dir))!==false)
+ {
+ if(is_file($path.'/'.$entry) && (strncmp($entry,'ut',2)===0||preg_match('/test.*\.php/', strtolower($entry))))
+ $test->addTestFile($path.'/'.$entry);
+ else if($entry!=='.' && $entry!=='..' && $entry!=='.svn' && is_dir($path.'/'.$entry) && $recursive)
+ $this->addTests($test,$path.'/'.$entry,$recursive);
+ }
+ closedir($dir);
+ }
+
+ function run($reporter)
+ {
+ $rootPath=realpath($this->_root);
+ $rootUri=dirname($_SERVER['PHP_SELF']);
+ if(isset($_GET['target']))
+ {
+ $target=$_GET['target'];
+ $recursive=true;
+ $fullpath=realpath("$rootPath/$target");
+ if($fullpath===false || strpos($fullpath,$rootPath)!==0)
+ die('invalid test target');
+
+ if(is_dir($fullpath))
+ {
+
+ $test=new GroupTest(basename($rootPath)."/$target");
+ $this->addTests($test,$fullpath,$recursive);
+ $test->run($reporter);
+ //$test->run(new HtmlReporterWithCoverage('index.php',$rootPath));
+ }
+ else
+ {
+ $testClass=basename($fullpath,'.php');
+ include_once($fullpath);
+ $test=new $testClass(basename($rootPath)."/$target");
+ $test->run($reporter);
+ }
+ }
+ else
+ {
+ echo "
+