Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
 
3
if(!defined('PRADO_FRAMEWORK'))
4
	define('PRADO_FRAMEWORK',realpath(dirname(__FILE__).'/../../framework'));
5
 
6
$TEST_TOOLS = dirname(__FILE__);
7
 
8
require_once($TEST_TOOLS.'/simpletest/unit_tester.php');
9
require_once($TEST_TOOLS.'/simpletest/web_tester.php');
10
require_once($TEST_TOOLS.'/simpletest/mock_objects.php');
11
require_once($TEST_TOOLS.'/simpletest/reporter.php');
12
 
13
require_once(PRADO_FRAMEWORK.'/prado.php');
14
 
15
class TestFolder
16
{
17
	public $name='';
18
	public $url='';
19
	public $subFolders=array();
20
	public $testFiles=array();
21
 
22
	public function __construct($path,$rootPath,$rootUri)
23
	{
24
		$script = basename($_SERVER['SCRIPT_NAME']);
25
		$this->url="$rootUri/$script?target=".strtr(substr($path,strlen($rootPath)+1),"\\",'/');
26
		$this->name=basename($path);
27
		$dir=opendir($path);
28
		while(($entry=readdir($dir))!==false)
29
		{
30
			$fullpath="$path/$entry";
31
			if($entry!=='.' && $entry!=='..' && $entry!=='.svn' && is_dir($fullpath))
32
			{
33
				$folder=new TestFolder($fullpath,$rootPath,$rootUri);
34
				if(!empty($folder->subFolders) || !empty($folder->testFiles))
35
					$this->subFolders[]=$folder;
36
			}
37
			else if(is_file($fullpath) && (strncmp($entry,'ut',2)===0
38
						|| preg_match('/test.*\.php$/i', $entry)))
39
			{
40
				$this->testFiles[$entry]="$rootUri/$script?target=".strtr(substr($fullpath,strlen($rootPath)+1),"\\",'/');
41
			}
42
		}
43
		closedir($dir);
44
	}
45
 
46
	public function getHtml($level=0)
47
	{
48
		$str=str_repeat('&nbsp;',$level*4)."[ <a href=\"{$this->url}\">{$this->name}</a> ]<br/>\n";
49
		foreach($this->subFolders as $folder)
50
			$str.=$folder->getHtml($level+1);
51
		foreach($this->testFiles as $name=>$url)
52
			$str.=str_repeat('&nbsp;',($level+1)*4)."<a href=\"$url\">$name</a><br/>\n";
53
		return $str;
54
	}
55
}
56
 
57
class PradoUnitTester
58
{
59
	private $_root;
60
 
61
	function __construct($root, $app_dir='.')
62
	{
63
		$this->_root = $root;
64
		Prado::setPathOfAlias('Tests', $root);
65
		if($app_dir===null) $app_dir='.';
66
		$app = new TShellApplication($app_dir);
67
		$app->run();
68
	}
69
 
70
	function addTests($test,$path,$recursive)
71
	{
72
		$dir=opendir($path);
73
		while(($entry=readdir($dir))!==false)
74
		{
75
			if(is_file($path.'/'.$entry) && (strncmp($entry,'ut',2)===0||preg_match('/test.*\.php$/i', $entry)))
76
				$test->addTestFile($path.'/'.$entry);
77
			else if($entry!=='.' && $entry!=='..' && $entry!=='.svn' && is_dir($path.'/'.$entry) && $recursive)
78
				$this->addTests($test,$path.'/'.$entry,$recursive);
79
		}
80
		closedir($dir);
81
	}
82
 
83
	function run($reporter)
84
	{
85
		$rootPath=realpath($this->_root);
86
		$rootUri=dirname($_SERVER['PHP_SELF']);
87
		if(isset($_GET['target']))
88
		{
89
			$target=$_GET['target'];
90
			$recursive=true;
91
			$fullpath=realpath("$rootPath/$target");
92
			if($fullpath===false || strpos($fullpath,$rootPath)!==0)
93
				die('invalid test target');
94
 
95
			if(is_dir($fullpath))
96
			{
97
 
98
				$test=new GroupTest(basename($rootPath)."/$target");
99
				$this->addTests($test,$fullpath,$recursive);
100
				$test->run($reporter);
101
				//$test->run(new HtmlReporterWithCoverage('index.php',$rootPath));
102
			}
103
			else
104
			{
105
				$testClass=basename($fullpath,'.php');
106
				include_once($fullpath);
107
				$test=new $testClass(basename($rootPath)."/$target");
108
				$test->run($reporter);
109
			}
110
		}
111
		else
112
		{
113
			echo "<html>
114
		<head>
115
		<title>Prado Unit Tests</title>
116
		<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
117
		</head>
118
		<body>
119
		<h1>Prado Unit Tests</h1>
120
		";
121
			$root=new TestFolder($rootPath,$rootPath,$rootUri);
122
			echo $root->getHtml();
123
			echo "</body>\n</html>";
124
		}
125
	}
126
}
127
 
128
?>