Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/simpletestlib/shell_tester.php
Go to the documentation of this file.
00001 <?php
00012 require_once(dirname(__FILE__) . '/test_case.php');
00020 class SimpleShell {
00021     var $_output;
00022 
00027     function SimpleShell() {
00028         $this->_output = false;
00029     }
00030 
00039     function execute($command) {
00040         $this->_output = false;
00041         exec($command, $this->_output, $ret);
00042         return $ret;
00043     }
00044 
00050     function getOutput() {
00051         return implode("\n", $this->_output);
00052     }
00053 
00059     function getOutputAsList() {
00060         return $this->_output;
00061     }
00062 }
00063 
00071 class ShellTestCase extends SimpleTestCase {
00072     var $_current_shell;
00073     var $_last_status;
00074     var $_last_command;
00075 
00083     function ShellTestCase($label = false) {
00084         $this->SimpleTestCase($label);
00085         $this->_current_shell = &$this->_createShell();
00086         $this->_last_status = false;
00087         $this->_last_command = '';
00088     }
00089 
00096     function execute($command) {
00097         $shell = &$this->_getShell();
00098         $this->_last_status = $shell->execute($command);
00099         $this->_last_command = $command;
00100         return ($this->_last_status === 0);
00101     }
00102 
00107     function dumpOutput() {
00108         $this->dump($this->getOutput());
00109     }
00110 
00116     function getOutput() {
00117         $shell = &$this->_getShell();
00118         return $shell->getOutput();
00119     }
00120 
00126     function getOutputAsList() {
00127         $shell = &$this->_getShell();
00128         return $shell->getOutputAsList();
00129     }
00130 
00140     function assertTrue($result, $message = false) {
00141         return $this->assert(new TrueExpectation(), $result, $message);
00142     }
00143 
00154     function assertFalse($result, $message = '%s') {
00155         return $this->assert(new FalseExpectation(), $result, $message);
00156     }
00157     
00168     function assertEqual($first, $second, $message = "%s") {
00169         return $this->assert(
00170                 new EqualExpectation($first),
00171                 $second,
00172                 $message);
00173     }
00174     
00185     function assertNotEqual($first, $second, $message = "%s") {
00186         return $this->assert(
00187                 new NotEqualExpectation($first),
00188                 $second,
00189                 $message);
00190     }
00191 
00200     function assertExitCode($status, $message = "%s") {
00201         $message = sprintf($message, "Expected status code of [$status] from [" .
00202                 $this->_last_command . "], but got [" .
00203                 $this->_last_status . "]");
00204         return $this->assertTrue($status === $this->_last_status, $message);
00205     }
00206 
00215     function assertOutput($expected, $message = "%s") {
00216         $shell = &$this->_getShell();
00217         return $this->assert(
00218                 new EqualExpectation($expected),
00219                 $shell->getOutput(),
00220                 $message);
00221     }
00222 
00231     function assertOutputPattern($pattern, $message = "%s") {
00232         $shell = &$this->_getShell();
00233         return $this->assert(
00234                 new PatternExpectation($pattern),
00235                 $shell->getOutput(),
00236                 $message);
00237     }
00238 
00247     function assertNoOutputPattern($pattern, $message = "%s") {
00248         $shell = &$this->_getShell();
00249         return $this->assert(
00250                 new NoPatternExpectation($pattern),
00251                 $shell->getOutput(),
00252                 $message);
00253     }
00254 
00262     function assertFileExists($path, $message = "%s") {
00263         $message = sprintf($message, "File [$path] should exist");
00264         return $this->assertTrue(file_exists($path), $message);
00265     }
00266 
00274     function assertFileNotExists($path, $message = "%s") {
00275         $message = sprintf($message, "File [$path] should not exist");
00276         return $this->assertFalse(file_exists($path), $message);
00277     }
00278 
00288     function assertFilePattern($pattern, $path, $message = "%s") {
00289         $shell = &$this->_getShell();
00290         return $this->assert(
00291                 new PatternExpectation($pattern),
00292                 implode('', file($path)),
00293                 $message);
00294     }
00295 
00305     function assertNoFilePattern($pattern, $path, $message = "%s") {
00306         $shell = &$this->_getShell();
00307         return $this->assert(
00308                 new NoPatternExpectation($pattern),
00309                 implode('', file($path)),
00310                 $message);
00311     }
00312 
00319     function &_getShell() {
00320         return $this->_current_shell;
00321     }
00322 
00328     function &_createShell() {
00329         $shell = new SimpleShell();
00330         return $shell;
00331     }
00332 }
00333 ?>
 All Data Structures Namespaces Files Functions Variables Enumerations