| [ Index ] |
PHP Cross Reference of Limb3 |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * base include file for SimpleTest 4 * @package SimpleTest 5 * @subpackage UnitTester 6 * @version $Id: shell_tester.php 5999 2007-06-18 13:13:08Z pachanga $ 7 */ 8 9 /**#@+ 10 * include other SimpleTest class files 11 */ 12 require_once(dirname(__FILE__) . '/test_case.php'); 13 /**#@-*/ 14 15 /** 16 * Wrapper for exec() functionality. 17 * @package SimpleTest 18 * @subpackage UnitTester 19 */ 20 class SimpleShell { 21 var $_output; 22 23 /** 24 * Executes the shell comand and stashes the output. 25 * @access public 26 */ 27 function SimpleShell() { 28 $this->_output = false; 29 } 30 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 38 */ 39 function execute($command) { 40 $this->_output = false; 41 exec($command, $this->_output, $ret); 42 return $ret; 43 } 44 45 /** 46 * Accessor for the last output. 47 * @return string Output as text. 48 * @access public 49 */ 50 function getOutput() { 51 return implode("\n", $this->_output); 52 } 53 54 /** 55 * Accessor for the last output. 56 * @return array Output as array of lines. 57 * @access public 58 */ 59 function getOutputAsList() { 60 return $this->_output; 61 } 62 } 63 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 70 */ 71 class ShellTestCase extends SimpleTestCase { 72 var $_current_shell; 73 var $_last_status; 74 var $_last_command; 75 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 82 */ 83 function ShellTestCase($label = false) { 84 $this->SimpleTestCase($label); 85 $this->_current_shell = &$this->_createShell(); 86 $this->_last_status = false; 87 $this->_last_command = ''; 88 } 89 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 95 */ 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); 101 } 102 103 /** 104 * Dumps the output of the last command. 105 * @access public 106 */ 107 function dumpOutput() { 108 $this->dump($this->getOutput()); 109 } 110 111 /** 112 * Accessor for the last output. 113 * @return string Output as text. 114 * @access public 115 */ 116 function getOutput() { 117 $shell = &$this->_getShell(); 118 return $shell->getOutput(); 119 } 120 121 /** 122 * Accessor for the last output. 123 * @return array Output as array of lines. 124 * @access public 125 */ 126 function getOutputAsList() { 127 $shell = &$this->_getShell(); 128 return $shell->getOutputAsList(); 129 } 130 131 /** 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 139 */ 140 function assertTrue($result, $message = false) { 141 return $this->assert(new TrueExpectation(), $result, $message); 142 } 143 144 /** 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 153 */ 154 function assertFalse($result, $message = '%s') { 155 return $this->assert(new FalseExpectation(), $result, $message); 156 } 157 158 /** 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 167 */ 168 function assertEqual($first, $second, $message = "%s") { 169 return $this->assert( 170 new EqualExpectation($first), 171 $second, 172 $message); 173 } 174 175 /** 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 184 */ 185 function assertNotEqual($first, $second, $message = "%s") { 186 return $this->assert( 187 new NotEqualExpectation($first), 188 $second, 189 $message); 190 } 191 192 /** 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 199 */ 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); 205 } 206 207 /** 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 214 */ 215 function assertOutput($expected, $message = "%s") { 216 $shell = &$this->_getShell(); 217 return $this->assert( 218 new EqualExpectation($expected), 219 $shell->getOutput(), 220 $message); 221 } 222 223 /** 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 230 */ 231 function assertOutputPattern($pattern, $message = "%s") { 232 $shell = &$this->_getShell(); 233 return $this->assert( 234 new PatternExpectation($pattern), 235 $shell->getOutput(), 236 $message); 237 } 238 239 /** 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 246 */ 247 function assertNoOutputPattern($pattern, $message = "%s") { 248 $shell = &$this->_getShell(); 249 return $this->assert( 250 new NoPatternExpectation($pattern), 251 $shell->getOutput(), 252 $message); 253 } 254 255 /** 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 261 */ 262 function assertFileExists($path, $message = "%s") { 263 $message = sprintf($message, "File [$path] should exist"); 264 return $this->assertTrue(file_exists($path), $message); 265 } 266 267 /** 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 273 */ 274 function assertFileNotExists($path, $message = "%s") { 275 $message = sprintf($message, "File [$path] should not exist"); 276 return $this->assertFalse(file_exists($path), $message); 277 } 278 279 /** 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 287 */ 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); 294 } 295 296 /** 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 304 */ 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); 311 } 312 313 /** 314 * Accessor for current shell. Used for testing the 315 * the tester itself. 316 * @return Shell Current shell. 317 * @access protected 318 */ 319 function &_getShell() { 320 return $this->_current_shell; 321 } 322 323 /** 324 * Factory for the shell to run the command on. 325 * @return Shell New shell object. 326 * @access protected 327 */ 328 function &_createShell() { 329 $shell = &new SimpleShell(); 330 return $shell; 331 } 332 } 333 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Sat Nov 22 03:48:54 2008 | Cross-referenced by PHPXref 0.7 |