Fixing file upload params ($_FILES) normalization. Closes #75
[akelos.git] / vendor / simpletest / shell_tester.php
blob5ca53c1589876e5e3ffc1d95a2563d64a8fe903a
1 <?php
2 /**
3 * base include file for SimpleTest
4 * @package SimpleTest
5 * @subpackage UnitTester
6 * @version $Id: shell_tester.php,v 1.19 2005/08/03 17:26:55 lastcraft Exp $
7 */
9 /**#@+
10 * include other SimpleTest class files
12 require_once(dirname(__FILE__) . '/test_case.php');
13 /**#@-*/
15 /**
16 * Wrapper for exec() functionality.
17 * @package SimpleTest
18 * @subpackage UnitTester
20 class SimpleShell {
21 var $_output;
23 /**
24 * Executes the shell comand and stashes the output.
25 * @access public
27 function SimpleShell() {
28 $this->_output = false;
31 /**
32 * Actually runs the command. Does not trap the
33 * error stream output as this need PHP 4.3+.
34 * @param string $command The actual command line
35 * to run.
36 * @return integer Exit code.
37 * @access public
39 function execute($command) {
40 $this->_output = false;
41 exec($command, $this->_output, $ret);
42 return $ret;
45 /**
46 * Accessor for the last output.
47 * @return string Output as text.
48 * @access public
50 function getOutput() {
51 return implode("\n", $this->_output);
54 /**
55 * Accessor for the last output.
56 * @return array Output as array of lines.
57 * @access public
59 function getOutputAsList() {
60 return $this->_output;
64 /**
65 * Test case for testing of command line scripts and
66 * utilities. Usually scripts taht are external to the
67 * PHP code, but support it in some way.
68 * @package SimpleTest
69 * @subpackage UnitTester
71 class ShellTestCase extends SimpleTestCase {
72 var $_current_shell;
73 var $_last_status;
74 var $_last_command;
76 /**
77 * Creates an empty test case. Should be subclassed
78 * with test methods for a functional test case.
79 * @param string $label Name of test case. Will use
80 * the class name if none specified.
81 * @access public
83 function ShellTestCase($label = false) {
84 $this->SimpleTestCase($label);
85 $this->_current_shell = &$this->_createShell();
86 $this->_last_status = false;
87 $this->_last_command = '';
90 /**
91 * Executes a command and buffers the results.
92 * @param string $command Command to run.
93 * @return boolean True if zero exit code.
94 * @access public
96 function execute($command) {
97 $shell = &$this->_getShell();
98 $this->_last_status = $shell->execute($command);
99 $this->_last_command = $command;
100 return ($this->_last_status === 0);
104 * Dumps the output of the last command.
105 * @access public
107 function dumpOutput() {
108 $this->dump($this->getOutput());
112 * Accessor for the last output.
113 * @return string Output as text.
114 * @access public
116 function getOutput() {
117 $shell = &$this->_getShell();
118 return $shell->getOutput();
122 * Accessor for the last output.
123 * @return array Output as array of lines.
124 * @access public
126 function getOutputAsList() {
127 $shell = &$this->_getShell();
128 return $shell->getOutputAsList();
132 * Will trigger a pass if the two parameters have
133 * the same value only. Otherwise a fail. This
134 * is for testing hand extracted text, etc.
135 * @param mixed $first Value to compare.
136 * @param mixed $second Value to compare.
137 * @param string $message Message to display.
138 * @return boolean True on pass
139 * @access public
141 function assertEqual($first, $second, $message = "%s") {
142 return $this->assert(
143 new EqualExpectation($first),
144 $second,
145 $message);
149 * Will trigger a pass if the two parameters have
150 * a different value. Otherwise a fail. This
151 * is for testing hand extracted text, etc.
152 * @param mixed $first Value to compare.
153 * @param mixed $second Value to compare.
154 * @param string $message Message to display.
155 * @return boolean True on pass
156 * @access public
158 function assertNotEqual($first, $second, $message = "%s") {
159 return $this->assert(
160 new NotEqualExpectation($first),
161 $second,
162 $message);
166 * Tests the last status code from the shell.
167 * @param integer $status Expected status of last
168 * command.
169 * @param string $message Message to display.
170 * @return boolean True if pass.
171 * @access public
173 function assertExitCode($status, $message = "%s") {
174 $message = sprintf($message, "Expected status code of [$status] from [" .
175 $this->_last_command . "], but got [" .
176 $this->_last_status . "]");
177 return $this->assertTrue($status === $this->_last_status, $message);
181 * Attempt to exactly match the combined STDERR and
182 * STDOUT output.
183 * @param string $expected Expected output.
184 * @param string $message Message to display.
185 * @return boolean True if pass.
186 * @access public
188 function assertOutput($expected, $message = "%s") {
189 $shell = &$this->_getShell();
190 return $this->assert(
191 new EqualExpectation($expected),
192 $shell->getOutput(),
193 $message);
197 * Scans the output for a Perl regex. If found
198 * anywhere it passes, else it fails.
199 * @param string $pattern Regex to search for.
200 * @param string $message Message to display.
201 * @return boolean True if pass.
202 * @access public
204 function assertOutputPattern($pattern, $message = "%s") {
205 $shell = &$this->_getShell();
206 return $this->assert(
207 new PatternExpectation($pattern),
208 $shell->getOutput(),
209 $message);
213 * If a Perl regex is found anywhere in the current
214 * output then a failure is generated, else a pass.
215 * @param string $pattern Regex to search for.
216 * @param $message Message to display.
217 * @return boolean True if pass.
218 * @access public
220 function assertNoOutputPattern($pattern, $message = "%s") {
221 $shell = &$this->_getShell();
222 return $this->assert(
223 new NoPatternExpectation($pattern),
224 $shell->getOutput(),
225 $message);
229 * File existence check.
230 * @param string $path Full filename and path.
231 * @param string $message Message to display.
232 * @return boolean True if pass.
233 * @access public
235 function assertFileExists($path, $message = "%s") {
236 $message = sprintf($message, "File [$path] should exist");
237 return $this->assertTrue(file_exists($path), $message);
241 * File non-existence check.
242 * @param string $path Full filename and path.
243 * @param string $message Message to display.
244 * @return boolean True if pass.
245 * @access public
247 function assertFileNotExists($path, $message = "%s") {
248 $message = sprintf($message, "File [$path] should not exist");
249 return $this->assertFalse(file_exists($path), $message);
253 * Scans a file for a Perl regex. If found
254 * anywhere it passes, else it fails.
255 * @param string $pattern Regex to search for.
256 * @param string $path Full filename and path.
257 * @param string $message Message to display.
258 * @return boolean True if pass.
259 * @access public
261 function assertFilePattern($pattern, $path, $message = "%s") {
262 $shell = &$this->_getShell();
263 return $this->assert(
264 new PatternExpectation($pattern),
265 implode('', file($path)),
266 $message);
270 * If a Perl regex is found anywhere in the named
271 * file then a failure is generated, else a pass.
272 * @param string $pattern Regex to search for.
273 * @param string $path Full filename and path.
274 * @param string $message Message to display.
275 * @return boolean True if pass.
276 * @access public
278 function assertNoFilePattern($pattern, $path, $message = "%s") {
279 $shell = &$this->_getShell();
280 return $this->assert(
281 new NoPatternExpectation($pattern),
282 implode('', file($path)),
283 $message);
287 * Accessor for current shell. Used for testing the
288 * the tester itself.
289 * @return Shell Current shell.
290 * @access protected
292 function &_getShell() {
293 return $this->_current_shell;
297 * Factory for the shell to run the command on.
298 * @return Shell New shell object.
299 * @access protected
301 function &_createShell() {
302 $shell = &new SimpleShell();
303 return $shell;