Source for file shell_tester.php

Documentation is available at shell_tester.php

  1. <?php
  2. /**
  3.  *  base include file for SimpleTest
  4.  *  @package    SimpleTest
  5.  *  @subpackage UnitTester
  6.  *  @version    $Id: shell_tester.php 1723 2008-04-08 00:34:10Z lastcraft $
  7.  */
  8.  
  9. /**#@+
  10.  *  include other SimpleTest class files
  11.  */
  12. require_once(dirname(__FILE__'/test_case.php');
  13. /**#@-*/
  14.  
  15.  *    Wrapper for exec() functionality.
  16.  *    @package SimpleTest
  17.  *    @subpackage UnitTester
  18.  */
  19. class SimpleShell {
  20.     var $_output;
  21.  
  22.     /**
  23.      *    Executes the shell comand and stashes the output.
  24.      *    @access public
  25.      */
  26.     function SimpleShell({
  27.         $this->_output = false;
  28.     }
  29.  
  30.     /**
  31.      *    Actually runs the command. Does not trap the
  32.      *    error stream output as this need PHP 4.3+.
  33.      *    @param string $command    The actual command line
  34.      *                               to run.
  35.      *    @return integer           Exit code.
  36.      *    @access public
  37.      */
  38.     function execute($command{
  39.         $this->_output = false;
  40.         exec($command$this->_output$ret);
  41.         return $ret;
  42.     }
  43.  
  44.     /**
  45.      *    Accessor for the last output.
  46.      *    @return string        Output as text.
  47.      *    @access public
  48.      */
  49.     function getOutput({
  50.         return implode("\n"$this->_output);
  51.     }
  52.  
  53.     /**
  54.      *    Accessor for the last output.
  55.      *    @return array         Output as array of lines.
  56.      *    @access public
  57.      */
  58.     function getOutputAsList({
  59.         return $this->_output;
  60.     }
  61. }
  62.  
  63. /**
  64.  *    Test case for testing of command line scripts and
  65.  *    utilities. Usually scripts that are external to the
  66.  *    PHP code, but support it in some way.
  67.  *    @package SimpleTest
  68.  *    @subpackage UnitTester
  69.  */
  70. class ShellTestCase extends SimpleTestCase {
  71.     var $_current_shell;
  72.     var $_last_status;
  73.     var $_last_command;
  74.  
  75.     /**
  76.      *    Creates an empty test case. Should be subclassed
  77.      *    with test methods for a functional test case.
  78.      *    @param string $label     Name of test case. Will use
  79.      *                              the class name if none specified.
  80.      *    @access public
  81.      */
  82.     function ShellTestCase($label false{
  83.         $this->SimpleTestCase($label);
  84.         $this->_current_shell = &$this->_createShell();
  85.         $this->_last_status = false;
  86.         $this->_last_command = '';
  87.     }
  88.  
  89.     /**
  90.      *    Executes a command and buffers the results.
  91.      *    @param string $command     Command to run.
  92.      *    @return boolean            True if zero exit code.
  93.      *    @access public
  94.      */
  95.     function execute($command{
  96.         $shell &$this->_getShell();
  97.         $this->_last_status = $shell->execute($command);
  98.         $this->_last_command = $command;
  99.         return ($this->_last_status === 0);
  100.     }
  101.  
  102.     /**
  103.      *    Dumps the output of the last command.
  104.      *    @access public
  105.      */
  106.     function dumpOutput({
  107.         $this->dump($this->getOutput());
  108.     }
  109.  
  110.     /**
  111.      *    Accessor for the last output.
  112.      *    @return string        Output as text.
  113.      *    @access public
  114.      */
  115.     function getOutput({
  116.         $shell &$this->_getShell();
  117.         return $shell->getOutput();
  118.     }
  119.  
  120.     /**
  121.      *    Accessor for the last output.
  122.      *    @return array         Output as array of lines.
  123.      *    @access public
  124.      */
  125.     function getOutputAsList({
  126.         $shell &$this->_getShell();
  127.         return $shell->getOutputAsList();
  128.     }
  129.  
  130.     /**
  131.      *    Called from within the test methods to register
  132.      *    passes and failures.
  133.      *    @param boolean $result    Pass on true.
  134.      *    @param string $message    Message to display describing
  135.      *                               the test state.
  136.      *    @return boolean           True on pass
  137.      *    @access public
  138.      */
  139.     function assertTrue($result$message false{
  140.         return $this->assert(new TrueExpectation()$result$message);
  141.     }
  142.  
  143.     /**
  144.      *    Will be true on false and vice versa. False
  145.      *    is the PHP definition of false, so that null,
  146.      *    empty strings, zero and an empty array all count
  147.      *    as false.
  148.      *    @param boolean $result    Pass on false.
  149.      *    @param string $message    Message to display.
  150.      *    @return boolean           True on pass
  151.      *    @access public
  152.      */
  153.     function assertFalse($result$message '%s'{
  154.         return $this->assert(new FalseExpectation()$result$message);
  155.     }
  156.     
  157.     /**
  158.      *    Will trigger a pass if the two parameters have
  159.      *    the same value only. Otherwise a fail. This
  160.      *    is for testing hand extracted text, etc.
  161.      *    @param mixed $first          Value to compare.
  162.      *    @param mixed $second         Value to compare.
  163.      *    @param string $message       Message to display.
  164.      *    @return boolean              True on pass
  165.      *    @access public
  166.      */
  167.     function assertEqual($first$second$message "%s"{
  168.         return $this->assert(
  169.                 new EqualExpectation($first),
  170.                 $second,
  171.                 $message);
  172.     }
  173.     
  174.     /**
  175.      *    Will trigger a pass if the two parameters have
  176.      *    a different value. Otherwise a fail. This
  177.      *    is for testing hand extracted text, etc.
  178.      *    @param mixed $first           Value to compare.
  179.      *    @param mixed $second          Value to compare.
  180.      *    @param string $message        Message to display.
  181.      *    @return boolean               True on pass
  182.      *    @access public
  183.      */
  184.     function assertNotEqual($first$second$message "%s"{
  185.         return $this->assert(
  186.                 new NotEqualExpectation($first),
  187.                 $second,
  188.                 $message);
  189.     }
  190.  
  191.     /**
  192.      *    Tests the last status code from the shell.
  193.      *    @param integer $status   Expected status of last
  194.      *                              command.
  195.      *    @param string $message   Message to display.
  196.      *    @return boolean          True if pass.
  197.      *    @access public
  198.      */
  199.     function assertExitCode($status$message "%s"{
  200.         $message sprintf($message"Expected status code of [$status] from [.
  201.                 $this->_last_command . "], but got [" .
  202.                 $this->_last_status . "]");
  203.         return $this->assertTrue($status === $this->_last_status$message);
  204.     }
  205.  
  206.     /**
  207.      *    Attempt to exactly match the combined STDERR and
  208.      *    STDOUT output.
  209.      *    @param string $expected  Expected output.
  210.      *    @param string $message   Message to display.
  211.      *    @return boolean          True if pass.
  212.      *    @access public
  213.      */
  214.     function assertOutput($expected$message "%s"{
  215.         $shell &$this->_getShell();
  216.         return $this->assert(
  217.                 new EqualExpectation($expected),
  218.                 $shell->getOutput(),
  219.                 $message);
  220.     }
  221.  
  222.     /**
  223.      *    Scans the output for a Perl regex. If found
  224.      *    anywhere it passes, else it fails.
  225.      *    @param string $pattern    Regex to search for.
  226.      *    @param string $message    Message to display.
  227.      *    @return boolean           True if pass.
  228.      *    @access public
  229.      */
  230.     function assertOutputPattern($pattern$message "%s"{
  231.         $shell &$this->_getShell();
  232.         return $this->assert(
  233.                 new PatternExpectation($pattern),
  234.                 $shell->getOutput(),
  235.                 $message);
  236.     }
  237.  
  238.     /**
  239.      *    If a Perl regex is found anywhere in the current
  240.      *    output then a failure is generated, else a pass.
  241.      *    @param string $pattern    Regex to search for.
  242.      *    @param $message           Message to display.
  243.      *    @return boolean           True if pass.
  244.      *    @access public
  245.      */
  246.     function assertNoOutputPattern($pattern$message "%s"{
  247.         $shell &$this->_getShell();
  248.         return $this->assert(
  249.                 new NoPatternExpectation($pattern),
  250.                 $shell->getOutput(),
  251.                 $message);
  252.     }
  253.  
  254.     /**
  255.      *    File existence check.
  256.      *    @param string $path      Full filename and path.
  257.      *    @param string $message   Message to display.
  258.      *    @return boolean          True if pass.
  259.      *    @access public
  260.      */
  261.     function assertFileExists($path$message "%s"{
  262.         $message sprintf($message"File [$path] should exist");
  263.         return $this->assertTrue(file_exists($path)$message);
  264.     }
  265.  
  266.     /**
  267.      *    File non-existence check.
  268.      *    @param string $path      Full filename and path.
  269.      *    @param string $message   Message to display.
  270.      *    @return boolean          True if pass.
  271.      *    @access public
  272.      */
  273.     function assertFileNotExists($path$message "%s"{
  274.         $message sprintf($message"File [$path] should not exist");
  275.         return $this->assertFalse(file_exists($path)$message);
  276.     }
  277.  
  278.     /**
  279.      *    Scans a file for a Perl regex. If found
  280.      *    anywhere it passes, else it fails.
  281.      *    @param string $pattern    Regex to search for.
  282.      *    @param string $path       Full filename and path.
  283.      *    @param string $message    Message to display.
  284.      *    @return boolean           True if pass.
  285.      *    @access public
  286.      */
  287.     function assertFilePattern($pattern$path$message "%s"{
  288.         $shell &$this->_getShell();
  289.         return $this->assert(
  290.                 new PatternExpectation($pattern),
  291.                 implode(''file($path)),
  292.                 $message);
  293.     }
  294.  
  295.     /**
  296.      *    If a Perl regex is found anywhere in the named
  297.      *    file then a failure is generated, else a pass.
  298.      *    @param string $pattern    Regex to search for.
  299.      *    @param string $path       Full filename and path.
  300.      *    @param string $message    Message to display.
  301.      *    @return boolean           True if pass.
  302.      *    @access public
  303.      */
  304.     function assertNoFilePattern($pattern$path$message "%s"{
  305.         $shell &$this->_getShell();
  306.         return $this->assert(
  307.                 new NoPatternExpectation($pattern),
  308.                 implode(''file($path)),
  309.                 $message);
  310.     }
  311.  
  312.     /**
  313.      *    Accessor for current shell. Used for testing the
  314.      *    the tester itself.
  315.      *    @return Shell        Current shell.
  316.      *    @access protected
  317.      */
  318.     function &_getShell({
  319.         return $this->_current_shell;
  320.     }
  321.  
  322.     /**
  323.      *    Factory for the shell to run the command on.
  324.      *    @return Shell        New shell object.
  325.      *    @access protected
  326.      */
  327.     function &_createShell({
  328.         $shell &new SimpleShell();
  329.         return $shell;
  330.     }
  331. }
  332. ?>

Documentation generated on Sun, 04 May 2008 09:22:08 -0500 by phpDocumentor 1.3.0