adding some strings
[moodle-linuxchix.git] / lib / simpletestlib / shell_tester.php
blobd47d2d6dce912d7755fb9b701a8aa780cdb8af69
1 <?php
2 /**
3 * base include file for SimpleTest
4 * @package SimpleTest
5 * @subpackage UnitTester
6 * @version $Id$
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 that 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 * Called from within the test methods to register
133 * passes and failures.
134 * @param boolean $result Pass on true.
135 * @param string $message Message to display describing
136 * the test state.
137 * @return boolean True on pass
138 * @access public
140 function assertTrue($result, $message = false) {
141 return $this->assert(new TrueExpectation(), $result, $message);
145 * Will be true on false and vice versa. False
146 * is the PHP definition of false, so that null,
147 * empty strings, zero and an empty array all count
148 * as false.
149 * @param boolean $result Pass on false.
150 * @param string $message Message to display.
151 * @return boolean True on pass
152 * @access public
154 function assertFalse($result, $message = '%s') {
155 return $this->assert(new FalseExpectation(), $result, $message);
159 * Will trigger a pass if the two parameters have
160 * the same value only. Otherwise a fail. This
161 * is for testing hand extracted text, etc.
162 * @param mixed $first Value to compare.
163 * @param mixed $second Value to compare.
164 * @param string $message Message to display.
165 * @return boolean True on pass
166 * @access public
168 function assertEqual($first, $second, $message = "%s") {
169 return $this->assert(
170 new EqualExpectation($first),
171 $second,
172 $message);
176 * Will trigger a pass if the two parameters have
177 * a different value. Otherwise a fail. This
178 * is for testing hand extracted text, etc.
179 * @param mixed $first Value to compare.
180 * @param mixed $second Value to compare.
181 * @param string $message Message to display.
182 * @return boolean True on pass
183 * @access public
185 function assertNotEqual($first, $second, $message = "%s") {
186 return $this->assert(
187 new NotEqualExpectation($first),
188 $second,
189 $message);
193 * Tests the last status code from the shell.
194 * @param integer $status Expected status of last
195 * command.
196 * @param string $message Message to display.
197 * @return boolean True if pass.
198 * @access public
200 function assertExitCode($status, $message = "%s") {
201 $message = sprintf($message, "Expected status code of [$status] from [" .
202 $this->_last_command . "], but got [" .
203 $this->_last_status . "]");
204 return $this->assertTrue($status === $this->_last_status, $message);
208 * Attempt to exactly match the combined STDERR and
209 * STDOUT output.
210 * @param string $expected Expected output.
211 * @param string $message Message to display.
212 * @return boolean True if pass.
213 * @access public
215 function assertOutput($expected, $message = "%s") {
216 $shell = &$this->_getShell();
217 return $this->assert(
218 new EqualExpectation($expected),
219 $shell->getOutput(),
220 $message);
224 * Scans the output for a Perl regex. If found
225 * anywhere it passes, else it fails.
226 * @param string $pattern Regex to search for.
227 * @param string $message Message to display.
228 * @return boolean True if pass.
229 * @access public
231 function assertOutputPattern($pattern, $message = "%s") {
232 $shell = &$this->_getShell();
233 return $this->assert(
234 new PatternExpectation($pattern),
235 $shell->getOutput(),
236 $message);
240 * If a Perl regex is found anywhere in the current
241 * output then a failure is generated, else a pass.
242 * @param string $pattern Regex to search for.
243 * @param $message Message to display.
244 * @return boolean True if pass.
245 * @access public
247 function assertNoOutputPattern($pattern, $message = "%s") {
248 $shell = &$this->_getShell();
249 return $this->assert(
250 new NoPatternExpectation($pattern),
251 $shell->getOutput(),
252 $message);
256 * File existence check.
257 * @param string $path Full filename and path.
258 * @param string $message Message to display.
259 * @return boolean True if pass.
260 * @access public
262 function assertFileExists($path, $message = "%s") {
263 $message = sprintf($message, "File [$path] should exist");
264 return $this->assertTrue(file_exists($path), $message);
268 * File non-existence check.
269 * @param string $path Full filename and path.
270 * @param string $message Message to display.
271 * @return boolean True if pass.
272 * @access public
274 function assertFileNotExists($path, $message = "%s") {
275 $message = sprintf($message, "File [$path] should not exist");
276 return $this->assertFalse(file_exists($path), $message);
280 * Scans a file for a Perl regex. If found
281 * anywhere it passes, else it fails.
282 * @param string $pattern Regex to search for.
283 * @param string $path Full filename and path.
284 * @param string $message Message to display.
285 * @return boolean True if pass.
286 * @access public
288 function assertFilePattern($pattern, $path, $message = "%s") {
289 $shell = &$this->_getShell();
290 return $this->assert(
291 new PatternExpectation($pattern),
292 implode('', file($path)),
293 $message);
297 * If a Perl regex is found anywhere in the named
298 * file then a failure is generated, else a pass.
299 * @param string $pattern Regex to search for.
300 * @param string $path Full filename and path.
301 * @param string $message Message to display.
302 * @return boolean True if pass.
303 * @access public
305 function assertNoFilePattern($pattern, $path, $message = "%s") {
306 $shell = &$this->_getShell();
307 return $this->assert(
308 new NoPatternExpectation($pattern),
309 implode('', file($path)),
310 $message);
314 * Accessor for current shell. Used for testing the
315 * the tester itself.
316 * @return Shell Current shell.
317 * @access protected
319 function &_getShell() {
320 return $this->_current_shell;
324 * Factory for the shell to run the command on.
325 * @return Shell New shell object.
326 * @access protected
328 function &_createShell() {
329 $shell = &new SimpleShell();
330 return $shell;