-
-
Notifications
You must be signed in to change notification settings - Fork 71
/
RunTests.php
189 lines (150 loc) 路 4.02 KB
/
RunTests.php
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
Nette Test Framework (v0.1)
---------------------------
<?php
require_once dirname(__FILE__) . '/NetteTestCase.php';
/**
* Help
*/
if (!isset($_SERVER['argv'][1])) { ?>
Usage:
php RunTests.php [options] [file or directory]
Options:
-p <php> Specify PHP-CGI executable to run.
-c <path> Look for php.ini in directory <path> or use <path> as php.ini.
-d key=val Define INI entry 'key' with value 'val'.
<?php
}
/**
* Execute tests
*/
try {
$manager = new NetteTestRunner;
$manager->parseArguments();
$res = $manager->run();
die($res ? 1 : 0);
} catch (Exception $e) {
echo 'Error: ', $e->getMessage(), "\n";
die(2);
}
/**
* Test runner.
*
* @author David Grudl
* @package Nette\Test
*/
class NetteTestRunner
{
const OUTPUT = 'output';
const EXPECTED = 'expect';
const HEADERS = 'headers';
/** @var string PHP-CGI.exe commandline */
public $cmdLine;
/** @var string */
public $path;
/**
* Runs all tests.
* @param string path
* @return void
*/
public function run()
{
$count = 0;
$failed = $passed = array();
if (is_file($this->path)) {
$files = array($this->path);
} else {
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->path));
}
foreach ($files as $entry) {
$entry = (string) $entry;
$info = pathinfo($entry);
if (!isset($info['extension']) || $info['extension'] !== 'phpt') {
continue;
}
$count++;
$testCase = new NetteTestCase($entry, $this->cmdLine);
try {
$testCase->run();
echo '.';
$passed[] = array($testCase->getName(), $entry);
} catch (NetteTestCaseException $e) {
echo 'F';
$failed[] = array($testCase->getName(), $entry, $e->getMessage());
$this->log($entry, $testCase->getOutput(), self::OUTPUT);
$this->log($entry, $testCase->getExpectedOutput(), self::EXPECTED);
if ($testCase->getExpectedHeaders() !== NULL) {
$this->log($entry, $testCase->getHeaders(), self::OUTPUT, self::HEADERS);
$this->log($entry, $testCase->getExpectedHeaders(), self::EXPECTED, self::HEADERS);
}
}
}
$failures = count($failed);
if (!$count) {
echo "No tests found\n";
} elseif ($failures) {
echo "\n\nFailures:\n";
foreach ($failed as $i => $item) {
list($name, $file, $message) = $item;
echo "\n", ($i + 1), ") $name\n $message\n $file\n";
}
echo "\nFAILURES! ($count tests, $failures failures)\n";
return FALSE;
} else {
echo "\n\nOK ($count tests)\n";
}
return TRUE;
}
/**
* Returns output file for logging.
* @param string
* @param string
* @param string
* @param string
* @return void
*/
public function log($testFile, $content, $type, $section = '')
{
$file = dirname($testFile) . '/' . $type . '/' . basename($testFile, '.phpt') . ($section ? ".$section" : '') . '.raw';
@mkdir(dirname($file)); // intentionally @
file_put_contents($file, $content);
}
/**
* Parses command line arguments.
* @return void
*/
public function parseArguments()
{
$phpExecutable = 'php-cgi.exe'; // path to PHP CGI executable that is used to run the test scripts
$phpIni = dirname(__FILE__) . '/php.ini'; // path in which to look for php.ini
$phpArgs = '';
$this->path = getcwd(); // current directory
$args = new ArrayIterator(array_slice(isset($_SERVER['argv']) ? $_SERVER['argv'] : array(), 1));
foreach ($args as $arg) {
$opt = preg_replace('#/|-+#A', '', $arg);
if ($opt === $arg) {
$this->path = $arg;
} else switch ($opt) {
case 'p':
$args->next();
$phpExecutable = $args->current();
break;
case 'c':
$args->next();
$phpIni = $args->current();
break;
case 'd':
$args->next();
$phpArgs .= ' -d ' . escapeshellarg($args->current());
break;
default:
echo "Unknown option -$opt\n";
exit;
}
}
$real = realpath($phpExecutable);
if (!$real) {
throw new Exception("Missing PHP executable file '$phpExecutable'.");
}
$this->cmdLine = escapeshellarg($real) . $phpArgs . ' -c ' . escapeshellarg($phpIni) . " %input > %output";
}
}