summaryrefslogtreecommitdiff
path: root/tests/FunctionalTests/config.php
blob: 73491c6edbeba8ed55d6dee00d43b8f3032a5be3 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php

//error_reporting(E_ALL);
//restore_error_handler();

$SIMPLE_TEST = dirname(__FILE__).'/../UnitTests';

require_once($SIMPLE_TEST.'/simpletest/unit_tester.php');
require_once($SIMPLE_TEST.'/simpletest/web_tester.php');
require_once($SIMPLE_TEST.'/simpletest/mock_objects.php');
require_once($SIMPLE_TEST.'/simpletest/reporter.php');
require(dirname(__FILE__).'/selenium/php/selenium.php');
require_once(PradoTestConfig::framework().'/prado.php');

/** test configurations , OVERRIDE to suite your enviornment !!! **/
class PradoTestConfig
{
	//directories containing test files
	public function unit_test_groups()
	{
		return array();
	}

	//test directory base
	public function tests_directory()
	{
		return dirname(__FILE__).'/protected/';
	}

	//prado frame work directory
	public static function framework()
	{
		return realpath(dirname(__FILE__).'/../../framework/');
	}

	//do test that require mysql database connection?
	public function doMySQLTests()
	{
		return false;
	}

	//run the prado application
	public function runApplication($appUrl='tests.php', $class='PradoApplicationTester')
	{
		$app = new $class($this, $appUrl);
		$app->run();
	}

	//file patterns to accept for test
	public function acceptPattern()
	{
		return '/\w+\.php/';
	}

	public function rejectPattern()
	{
		return null;
	}

	public function getTestCase()
	{
		return isset($_GET['file']) ? $_GET['file'] : '';
	}
}

//set up the PradoApplication Testing stub.

class PradoApplicationTester extends TApplication
{
	protected $appUrl;
	protected $testConfig;

	public function __construct($config, $appUrl)
	{
		$this->appUrl = $appUrl;
		$this->testConfig = $config;
		parent::__construct();
	}

	public function run()
	{
		$this->initApplication();
	}

	public function getTestPage($file)
	{
		$parameter = $this->getTestServiceParameter($file);
		return $this->appUrl.'?page='.$parameter;
	}

	protected function getTestServiceParameter($file)
	{
		$file = strtr(realpath($file),'\\','/');
		$base = strtr(realpath($this->testConfig->tests_directory().'/pages/'),'\\','/');
		$search = array($base, '.php');
		$replace = array('', '');
		$pagePath = str_replace($search, $replace, $file);
		return strtr(trim($pagePath,'/'),'/','.');
	}
}

/** set up the tests **/

class PradoSimpleTester
{
	protected $tester;

	function __construct($tester)
	{
		$this->tester = $tester;
		$this->tester->runApplication();
	}

	function getTests($name='All Tests')
	{
		$unit_tests = new GroupTest($name);

		foreach($this->tester->unit_test_groups() as $group => $dir)
		{
			$unit_tests->addTestCase($this->testSuits($group, $dir));
		}
		return $unit_tests;
	}

	protected function testSuits($group, $path)
	{
		$suite = new GroupTest($group);
		$dir = dir($path);

		while (false !== ($entry = $dir->read()))
		{
			$file = realpath($path.'/'.$entry);
			$matchFile = $this->tester->getTestCase();
			if(is_file($file) && $this->filePatternMatch($file))
			{
				if(empty($matchFile) ||
					(!empty($matchFile)
						&& is_int(strpos($file, $matchFile))))
							$suite->addTestFile($file);
			}

		}
		$dir->close();
		return $suite;
	}

	protected function filePatternMatch($file)
	{
		$accept = $this->tester->acceptPattern();
		$reject = $this->tester->rejectPattern();

		$valid = true;
		if(!is_null($accept))
			$valid = $valid && preg_match($accept, $file);
		if(!is_null($reject))
			$valid = $valid && !preg_match($reject, $file);
		return $valid;
	}
}

?>