3 * base include file for SimpleTest
5 * @subpackage UnitTester
6 * @version $Id: shell_tester.php,v 1.19 2005/08/03 17:26:55 lastcraft Exp $
10 * include other SimpleTest class files
12 require_once(dirname(__FILE__
) . '/test_case.php');
16 * Wrapper for exec() functionality.
18 * @subpackage UnitTester
24 * Executes the shell comand and stashes the output.
27 function SimpleShell() {
28 $this->_output
= false;
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
36 * @return integer Exit code.
39 function execute($command) {
40 $this->_output
= false;
41 exec($command, $this->_output
, $ret);
46 * Accessor for the last output.
47 * @return string Output as text.
50 function getOutput() {
51 return implode("\n", $this->_output
);
55 * Accessor for the last output.
56 * @return array Output as array of lines.
59 function getOutputAsList() {
60 return $this->_output
;
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.
69 * @subpackage UnitTester
71 class ShellTestCase
extends SimpleTestCase
{
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.
83 function ShellTestCase($label = false) {
84 $this->SimpleTestCase($label);
85 $this->_current_shell
= &$this->_createShell();
86 $this->_last_status
= false;
87 $this->_last_command
= '';
91 * Executes a command and buffers the results.
92 * @param string $command Command to run.
93 * @return boolean True if zero exit code.
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.
107 function dumpOutput() {
108 $this->dump($this->getOutput());
112 * Accessor for the last output.
113 * @return string Output as text.
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.
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
141 function assertEqual($first, $second, $message = "%s") {
142 return $this->assert(
143 new EqualExpectation($first),
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
158 function assertNotEqual($first, $second, $message = "%s") {
159 return $this->assert(
160 new NotEqualExpectation($first),
166 * Tests the last status code from the shell.
167 * @param integer $status Expected status of last
169 * @param string $message Message to display.
170 * @return boolean True if pass.
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
183 * @param string $expected Expected output.
184 * @param string $message Message to display.
185 * @return boolean True if pass.
188 function assertOutput($expected, $message = "%s") {
189 $shell = &$this->_getShell();
190 return $this->assert(
191 new EqualExpectation($expected),
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.
204 function assertOutputPattern($pattern, $message = "%s") {
205 $shell = &$this->_getShell();
206 return $this->assert(
207 new PatternExpectation($pattern),
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.
220 function assertNoOutputPattern($pattern, $message = "%s") {
221 $shell = &$this->_getShell();
222 return $this->assert(
223 new NoPatternExpectation($pattern),
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.
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.
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.
261 function assertFilePattern($pattern, $path, $message = "%s") {
262 $shell = &$this->_getShell();
263 return $this->assert(
264 new PatternExpectation($pattern),
265 implode('', file($path)),
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.
278 function assertNoFilePattern($pattern, $path, $message = "%s") {
279 $shell = &$this->_getShell();
280 return $this->assert(
281 new NoPatternExpectation($pattern),
282 implode('', file($path)),
287 * Accessor for current shell. Used for testing the
289 * @return Shell Current shell.
292 function &_getShell() {
293 return $this->_current_shell
;
297 * Factory for the shell to run the command on.
298 * @return Shell New shell object.
301 function &_createShell() {
302 $shell = &new SimpleShell();