3 * base include file for SimpleTest
5 * @subpackage UnitTester
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 that 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 * 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
137 * @return boolean True on pass
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
149 * @param boolean $result Pass on false.
150 * @param string $message Message to display.
151 * @return boolean True on pass
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
168 function assertEqual($first, $second, $message = "%s") {
169 return $this->assert(
170 new EqualExpectation($first),
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
185 function assertNotEqual($first, $second, $message = "%s") {
186 return $this->assert(
187 new NotEqualExpectation($first),
193 * Tests the last status code from the shell.
194 * @param integer $status Expected status of last
196 * @param string $message Message to display.
197 * @return boolean True if pass.
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
210 * @param string $expected Expected output.
211 * @param string $message Message to display.
212 * @return boolean True if pass.
215 function assertOutput($expected, $message = "%s") {
216 $shell = &$this->_getShell();
217 return $this->assert(
218 new EqualExpectation($expected),
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.
231 function assertOutputPattern($pattern, $message = "%s") {
232 $shell = &$this->_getShell();
233 return $this->assert(
234 new PatternExpectation($pattern),
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.
247 function assertNoOutputPattern($pattern, $message = "%s") {
248 $shell = &$this->_getShell();
249 return $this->assert(
250 new NoPatternExpectation($pattern),
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.
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.
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.
288 function assertFilePattern($pattern, $path, $message = "%s") {
289 $shell = &$this->_getShell();
290 return $this->assert(
291 new PatternExpectation($pattern),
292 implode('', file($path)),
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.
305 function assertNoFilePattern($pattern, $path, $message = "%s") {
306 $shell = &$this->_getShell();
307 return $this->assert(
308 new NoPatternExpectation($pattern),
309 implode('', file($path)),
314 * Accessor for current shell. Used for testing the
316 * @return Shell Current shell.
319 function &_getShell() {
320 return $this->_current_shell
;
324 * Factory for the shell to run the command on.
325 * @return Shell New shell object.
328 function &_createShell() {
329 $shell = &new SimpleShell();