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 1786 2008-04-26 17:32:20Z pp11 $
  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.     private $output;
  22.  
  23.     /**
  24.      *    Executes the shell comand and stashes the output.
  25.      *    @access public
  26.      */
  27.     function __construct({
  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.     private $current_shell;
  73.     private $last_status;
  74.     private $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 __construct($label false{
  84.         parent::__construct($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.         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.         return $this->assert(
  306.                 new NoPatternExpectation($pattern),
  307.                 implode(''file($path)),
  308.                 $message);
  309.     }
  310.  
  311.     /**
  312.      *    Accessor for current shell. Used for testing the
  313.      *    the tester itself.
  314.      *    @return Shell        Current shell.
  315.      *    @access protected
  316.      */
  317.     protected function getShell({
  318.         return $this->current_shell;
  319.     }
  320.  
  321.     /**
  322.      *    Factory for the shell to run the command on.
  323.      *    @return Shell        New shell object.
  324.      *    @access protected
  325.      */
  326.     protected function createShell({
  327.         return new SimpleShell();
  328.     }
  329. }
  330. ?>

Documentation generated on Thu, 01 Oct 2009 20:55:48 -0500 by phpDocumentor 1.4.2