summaryrefslogtreecommitdiff
path: root/tests/UnitTests/framework/index.php
blob: 802e979efe23b9d26994a821c43b1a83767c41a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php

class TestFolder
{
	public $name='';
	public $url='';
	public $subFolders=array();
	public $testFiles=array();

	public function __construct($path,$rootPath,$rootUri)
	{
		$this->url="$rootUri?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!=='..' && 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)
			{
				$this->testFiles[$entry]="$rootUri?target=".strtr(substr($fullpath,strlen($rootPath)+1),"\\",'/');
			}
		}
		closedir($dir);
	}

	public function getHtml($level=0)
	{
		$str=str_repeat('&nbsp;',$level*4)."[ <a href=\"{$this->url}\">{$this->name}</a> ]<br/>\n";
		foreach($this->subFolders as $folder)
			$str.=$folder->getHtml($level+1);
		foreach($this->testFiles as $name=>$url)
			$str.=str_repeat('&nbsp;',($level+1)*4)."<a href=\"$url\">$name</a><br/>\n";
		return $str;
	}
}


function addTests($test,$path,$recursive)
{
	$dir=opendir($path);
	while(($entry=readdir($dir))!==false)
	{
		if(is_file($path.'/'.$entry) && strncmp($entry,'ut',2)===0)
			$test->addTestFile($path.'/'.$entry);
		else if($entry!=='.' && $entry!=='..' && is_dir($path.'/'.$entry) && $recursive)
			addTests($test,$path.'/'.$entry,$recursive);
	}
	closedir($dir);
}


$rootPath=realpath(dirname(__FILE__));
$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');

	require_once($rootPath.'/common.php');

	if(is_dir($fullpath))
	{
		$test=new GroupTest(basename($rootPath)."/$target");
		addTests($test,$fullpath,$recursive);
		$test->run(new HtmlReporterWithCoverage(__FILE__,$rootPath));
	}
	else
	{
		$testClass=basename($fullpath,'.php');
		require_once($fullpath);
		$test=new $testClass(basename($rootPath)."/$target");
		$test->run(new HtmlReporter());
	}
}
else
{
	echo "<html>
<head>
<title>utList</title>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
</head>
<body>
<h1>Prado Framework Unit Tests</h1>
";
	$root=new TestFolder($rootPath,$rootPath,$rootUri);
	echo $root->getHtml();
	echo "</body>\n</html>";
}

?>