Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/site/app/tests/test_manager.php
blob: 01428efa1fc105d14d849b2cd111894484f3203a (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
/* SVN FILE: $Id: test_manager.php,v 1.1.1.1 2006/08/14 23:54:57 sancus%off.net Exp $ */
/**
 * Short description for file.
 *
 * Long description for file
 *
 * PHP versions 4 and 5
 *
 * CakePHP Test Suite <https://trac.cakephp.org/wiki/Developement/TestSuite>
 * Copyright (c) 2006, Larry E. Masters Shorewood, IL. 60431
 * Author(s): Larry E. Masters aka PhpNut <phpnut@gmail.com>
 *            Justin Scott aka fligtar <fligtar@gmail.com>
 *            Mike Shaver aka the_decider <shaver@mozilla.org>
 *
 * Portions modifiied from WACT Test Suite
 * Author(s): Harry Fuecks
 *            Jon Ramsey
 *            Jason E. Sweat
 *            Franco Ponticelli
 *            Lorenzo Alberton
 *
 *  Licensed under The Open Group Test Suite License
 *  Redistributions of files must retain the above copyright notice.
 *
 * @filesource
 * @author       Larry E. Masters aka PhpNut <phpnut@gmail.com>
 * @copyright    Copyright (c) 2006, Larry E. Masters Shorewood, IL. 60431
 * @link         http://www.phpnut.com/projects/
 * @package      tests
 * @subpackage   tests.lib
 * @since        CakePHP Test Suite v 1.0.0.0
 * @version      $Revision: 1.1.1.1 $
 * @modifiedby   $LastChangedBy: dho $
 * @lastmodified $Date: 2006/08/14 23:54:57 $
 * @license      http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
 */
/**
 * Short description for class.
 *
 * @package    tests
 * @subpackage tests.lib
 * @since      CakePHP Test Suite v 1.0.0.0
 */

DATABASE_CONFIG::useTestConfig();

class TestManager {
	var $_testExtension = array('.test.php', '.test.thtml', '.test.thtml.php');

	function runAllTests(&$reporter) {
		$manager =& new TestManager();

		$testCases =& $manager->_getTestFileList(TESTS);

		$test =& new GroupTest('All Test Cases');

		foreach ($testCases as $testCase) {
			$test->addTestFile($testCase);
		}
		        
		$test->run($reporter);
	}
	
    function runDirectory($directory, &$reporter) {
		$manager =& new TestManager();

		$testCases =& $manager->_getTestFileList(TESTS.$directory);

		$test =& new GroupTest('Directory Test: '.$directory);

		foreach ($testCases as $testCase) {
			$test->addTestFile($testCase);
		}
		        
		$test->run($reporter);
	}

	function runGroup($group, &$reporter) {

        $manager =& new TestManager();

        $testCases =& $group['cases'];

        $test =& new GroupTest('Group Test: '.$group['name']);

        foreach ($testCases as $testCase) {
            if (strpos($testCase, '*') !== false) {
                $testCase = str_replace('/*', '', $testCase);
                $dirCases = $manager->_getTestFileList(TESTS.$testCase);
                foreach ($dirCases as $dirCase) {
                    $test->addTestFile($dirCase);
                }
            }
            else {
                $test->addTestFile(TESTS.$testCase);
            }
        }
        
        $test->addTestFile(TESTS.'global.test.php');
                
        $test->run($reporter);
    }

	function runTestCase($testCaseFile, &$reporter) {
		$manager =& new TestManager();

	    $testCaseFileWithPath = TESTS.$testCaseFile;
		if (! file_exists($testCaseFileWithPath)) {
			trigger_error("Test case {$testCaseFile} cannot be found", E_USER_ERROR);
		}
		$test =& new GroupTest("Test Case: " . $testCaseFile);
        $test->addTestFile($testCaseFileWithPath);
		if ($testCaseFile != 'global.test.php') {
		    $test->addTestFile(TESTS.'global.test.php');
		}
		$test->run($reporter);
	}

	function addTestCasesFromDirectory(&$groupTest, $directory = '.') {
		$manager =& new TestManager();
		$testCases =& $manager->_getTestFileList($directory);
		foreach ($testCases as $testCase) {
			$groupTest->addTestFile($testCase);
		}
	}

	function &getTestCaseList($directory = '') {
        if (empty($directory)) {
            $directory = TESTS;
        }
		$manager =& new TestManager();
		$testCases =& $manager->_getTestCaseList($directory);

        sort($testCases);
		return $testCases;
	}

	function &_getTestCaseList($directory = '.') {
		$fileList =& $this->_getTestFileList($directory);
		$testCases = array();
		foreach ($fileList as $testCaseFile) {
			$testCases[$testCaseFile] = str_replace($directory . DS, '', $testCaseFile);
		}
		return $testCases;
	}

	function &_getTestFileList($directory = '.') {
		$return = $this->_getRecursiveFileList($directory, array(&$this, '_isTestCaseFile'));
		return $return;
	}

	function &_getRecursiveFileList($directory = '.', $fileTestFunction) {
		$dh = opendir($directory);
		if (! is_resource($dh)) {
			trigger_error("Couldn't open {$directory}", E_USER_ERROR);
		}

		$fileList = array();
		while ($file = readdir($dh)) {
			$filePath = $directory . DIRECTORY_SEPARATOR . $file;
			if (0 === strpos($file, '.')) {
				continue;
			}

			if (is_dir($filePath)) {
				$fileList = array_merge($fileList, $this->_getRecursiveFileList($filePath, $fileTestFunction));
			}
			if ($fileTestFunction[0]->$fileTestFunction[1]($file)) {
				$fileList[] = $filePath;
			}
		}
		closedir($dh);
		return $fileList;
	}

	function _isTestCaseFile($file) {
        foreach ($this->_testExtension as $extension) {
            if ($this->_hasExpectedExtension($file, $extension)) {
                return true;
            }
        }
        return false;
	}

	function _hasExpectedExtension($file, $extension) {
		return $extension == strtolower(substr($file, (0 - strlen($extension))));
	}
}
?>