3 * base include file for SimpleTest
5 * @subpackage UnitTester
6 * @version $Id: shell_tester.php,v 1.14 2004/08/17 18:18:32 lastcraft Exp $
10 * include other SimpleTest class files
12 require_once(dirname(__FILE__
) . '/simple_test.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 * Tests the last status code from the shell.
133 * @param integer $status Expected status of last
135 * @param string $message Message to display.
136 * @return boolean True if pass.
139 function assertExitCode($status, $message = "%s") {
140 $message = sprintf($message, "Expected status code of [$status] from [" .
141 $this->_last_command
. "], but got [" .
142 $this->_last_status
. "]");
143 return $this->assertTrue($status === $this->_last_status
, $message);
147 * Attempt to exactly match the combined STDERR and
149 * @param string $expected Expected output.
150 * @param string $message Message to display.
151 * @return boolean True if pass.
154 function assertOutput($expected, $message = "%s") {
155 $shell = &$this->_getShell();
156 return $this->assertExpectation(
157 new EqualExpectation($expected),
163 * Scans the output for a Perl regex. If found
164 * anywhere it passes, else it fails.
165 * @param string $pattern Regex to search for.
166 * @param string $message Message to display.
167 * @return boolean True if pass.
170 function assertOutputPattern($pattern, $message = "%s") {
171 $shell = &$this->_getShell();
172 return $this->assertExpectation(
173 new WantedPatternExpectation($pattern),
179 * If a Perl regex is found anywhere in the current
180 * output then a failure is generated, else a pass.
181 * @param string $pattern Regex to search for.
182 * @param $message Message to display.
183 * @return boolean True if pass.
186 function assertNoOutputPattern($pattern, $message = "%s") {
187 $shell = &$this->_getShell();
188 return $this->assertExpectation(
189 new UnwantedPatternExpectation($pattern),
195 * File existence check.
196 * @param string $path Full filename and path.
197 * @param string $message Message to display.
198 * @return boolean True if pass.
201 function assertFileExists($path, $message = "%s") {
202 $message = sprintf($message, "File [$path] should exist");
203 return $this->assertTrue(file_exists($path), $message);
207 * File non-existence check.
208 * @param string $path Full filename and path.
209 * @param string $message Message to display.
210 * @return boolean True if pass.
213 function assertFileNotExists($path, $message = "%s") {
214 $message = sprintf($message, "File [$path] should not exist");
215 return $this->assertFalse(file_exists($path), $message);
219 * Scans a file for a Perl regex. If found
220 * anywhere it passes, else it fails.
221 * @param string $pattern Regex to search for.
222 * @param string $path Full filename and path.
223 * @param string $message Message to display.
224 * @return boolean True if pass.
227 function assertFilePattern($pattern, $path, $message = "%s") {
228 $shell = &$this->_getShell();
229 return $this->assertExpectation(
230 new WantedPatternExpectation($pattern),
231 implode('', file($path)),
236 * If a Perl regex is found anywhere in the named
237 * file then a failure is generated, else a pass.
238 * @param string $pattern Regex to search for.
239 * @param string $path Full filename and path.
240 * @param string $message Message to display.
241 * @return boolean True if pass.
244 function assertNoFilePattern($pattern, $path, $message = "%s") {
245 $shell = &$this->_getShell();
246 return $this->assertExpectation(
247 new UnwantedPatternExpectation($pattern),
248 implode('', file($path)),
253 * Accessor for current shell. Used for testing the
255 * @return Shell Current shell.
258 function &_getShell() {
259 return $this->_current_shell
;
263 * Factory for the shell to run the command on.
264 * @return Shell New shell object.
267 function &_createShell() {
268 return new SimpleShell();