Source for file scorer.php

Documentation is available at scorer.php

  1. <?php
  2. /**
  3.  *  base include file for SimpleTest
  4.  *  @package    SimpleTest
  5.  *  @subpackage UnitTester
  6.  *  @version    $Id: scorer.php 1723 2008-04-08 00:34:10Z lastcraft $
  7.  */
  8.  
  9. /**#@+*/
  10. require_once(dirname(__FILE__'/invoker.php');
  11. /**#@-*/
  12.  
  13.  *    Can receive test events and display them. Display
  14.  *    is achieved by making display methods available
  15.  *    and visiting the incoming event.
  16.  *    @package SimpleTest
  17.  *    @subpackage UnitTester
  18.  *    @abstract
  19.  */
  20. class SimpleScorer {
  21.     var $_passes;
  22.     var $_fails;
  23.     var $_exceptions;
  24.     var $_is_dry_run;
  25.  
  26.     /**
  27.      *    Starts the test run with no results.
  28.      *    @access public
  29.      */
  30.     function SimpleScorer({
  31.         $this->_passes = 0;
  32.         $this->_fails = 0;
  33.         $this->_exceptions = 0;
  34.         $this->_is_dry_run = false;
  35.     }
  36.  
  37.     /**
  38.      *    Signals that the next evaluation will be a dry
  39.      *    run. That is, the structure events will be
  40.      *    recorded, but no tests will be run.
  41.      *    @param boolean $is_dry        Dry run if true.
  42.      *    @access public
  43.      */
  44.     function makeDry($is_dry true{
  45.         $this->_is_dry_run = $is_dry;
  46.     }
  47.  
  48.     /**
  49.      *    The reporter has a veto on what should be run.
  50.      *    @param string $test_case_name  name of test case.
  51.      *    @param string $method          Name of test method.
  52.      *    @access public
  53.      */
  54.     function shouldInvoke($test_case_name$method{
  55.         return $this->_is_dry_run;
  56.     }
  57.  
  58.     /**
  59.      *    Can wrap the invoker in preperation for running
  60.      *    a test.
  61.      *    @param SimpleInvoker $invoker   Individual test runner.
  62.      *    @return SimpleInvoker           Wrapped test runner.
  63.      *    @access public
  64.      */
  65.     function &createInvoker(&$invoker{
  66.         return $invoker;
  67.     }
  68.  
  69.     /**
  70.      *    Accessor for current status. Will be false
  71.      *    if there have been any failures or exceptions.
  72.      *    Used for command line tools.
  73.      *    @return boolean        True if no failures.
  74.      *    @access public
  75.      */
  76.     function getStatus({
  77.         if ($this->_exceptions + $this->_fails > 0{
  78.             return false;
  79.         }
  80.         return true;
  81.     }
  82.  
  83.     /**
  84.      *    Paints the start of a group test.
  85.      *    @param string $test_name     Name of test or other label.
  86.      *    @param integer $size         Number of test cases starting.
  87.      *    @access public
  88.      */
  89.     function paintGroupStart($test_name$size{
  90.     }
  91.  
  92.     /**
  93.      *    Paints the end of a group test.
  94.      *    @param string $test_name     Name of test or other label.
  95.      *    @access public
  96.      */
  97.     function paintGroupEnd($test_name{
  98.     }
  99.  
  100.     /**
  101.      *    Paints the start of a test case.
  102.      *    @param string $test_name     Name of test or other label.
  103.      *    @access public
  104.      */
  105.     function paintCaseStart($test_name{
  106.     }
  107.  
  108.     /**
  109.      *    Paints the end of a test case.
  110.      *    @param string $test_name     Name of test or other label.
  111.      *    @access public
  112.      */
  113.     function paintCaseEnd($test_name{
  114.     }
  115.  
  116.     /**
  117.      *    Paints the start of a test method.
  118.      *    @param string $test_name     Name of test or other label.
  119.      *    @access public
  120.      */
  121.     function paintMethodStart($test_name{
  122.     }
  123.  
  124.     /**
  125.      *    Paints the end of a test method.
  126.      *    @param string $test_name     Name of test or other label.
  127.      *    @access public
  128.      */
  129.     function paintMethodEnd($test_name{
  130.     }
  131.  
  132.     /**
  133.      *    Increments the pass count.
  134.      *    @param string $message        Message is ignored.
  135.      *    @access public
  136.      */
  137.     function paintPass($message{
  138.         $this->_passes++;
  139.     }
  140.  
  141.     /**
  142.      *    Increments the fail count.
  143.      *    @param string $message        Message is ignored.
  144.      *    @access public
  145.      */
  146.     function paintFail($message{
  147.         $this->_fails++;
  148.     }
  149.  
  150.     /**
  151.      *    Deals with PHP 4 throwing an error.
  152.      *    @param string $message    Text of error formatted by
  153.      *                               the test case.
  154.      *    @access public
  155.      */
  156.     function paintError($message{
  157.         $this->_exceptions++;
  158.     }
  159.  
  160.     /**
  161.      *    Deals with PHP 5 throwing an exception.
  162.      *    @param Exception $exception    The actual exception thrown.
  163.      *    @access public
  164.      */
  165.     function paintException($exception{
  166.         $this->_exceptions++;
  167.     }
  168.     
  169.     /**
  170.      *    Prints the message for skipping tests.
  171.      *    @param string $message    Text of skip condition.
  172.      *    @access public
  173.      */
  174.     function paintSkip($message{
  175.     }
  176.  
  177.     /**
  178.      *    Accessor for the number of passes so far.
  179.      *    @return integer       Number of passes.
  180.      *    @access public
  181.      */
  182.     function getPassCount({
  183.         return $this->_passes;
  184.     }
  185.  
  186.     /**
  187.      *    Accessor for the number of fails so far.
  188.      *    @return integer       Number of fails.
  189.      *    @access public
  190.      */
  191.     function getFailCount({
  192.         return $this->_fails;
  193.     }
  194.  
  195.     /**
  196.      *    Accessor for the number of untrapped errors
  197.      *    so far.
  198.      *    @return integer       Number of exceptions.
  199.      *    @access public
  200.      */
  201.     function getExceptionCount({
  202.         return $this->_exceptions;
  203.     }
  204.  
  205.     /**
  206.      *    Paints a simple supplementary message.
  207.      *    @param string $message        Text to display.
  208.      *    @access public
  209.      */
  210.     function paintMessage($message{
  211.     }
  212.  
  213.     /**
  214.      *    Paints a formatted ASCII message such as a
  215.      *    variable dump.
  216.      *    @param string $message        Text to display.
  217.      *    @access public
  218.      */
  219.     function paintFormattedMessage($message{
  220.     }
  221.  
  222.     /**
  223.      *    By default just ignores user generated events.
  224.      *    @param string $type        Event type as text.
  225.      *    @param mixed $payload      Message or object.
  226.      *    @access public
  227.      */
  228.     function paintSignal($type$payload{
  229.     }
  230. }
  231.  
  232. /**
  233.  *    Recipient of generated test messages that can display
  234.  *    page footers and headers. Also keeps track of the
  235.  *    test nesting. This is the main base class on which
  236.  *    to build the finished test (page based) displays.
  237.  *    @package SimpleTest
  238.  *    @subpackage UnitTester
  239.  */
  240. class SimpleReporter extends SimpleScorer {
  241.     var $_test_stack;
  242.     var $_size;
  243.     var $_progress;
  244.  
  245.     /**
  246.      *    Starts the display with no results in.
  247.      *    @access public
  248.      */
  249.     function SimpleReporter({
  250.         $this->SimpleScorer();
  251.         $this->_test_stack = array();
  252.         $this->_size = null;
  253.         $this->_progress = 0;
  254.     }
  255.     
  256.     /**
  257.      *    Gets the formatter for variables and other small
  258.      *    generic data items.
  259.      *    @return SimpleDumper          Formatter.
  260.      *    @access public
  261.      */
  262.     function getDumper({
  263.         return new SimpleDumper();
  264.     }
  265.  
  266.     /**
  267.      *    Paints the start of a group test. Will also paint
  268.      *    the page header and footer if this is the
  269.      *    first test. Will stash the size if the first
  270.      *    start.
  271.      *    @param string $test_name   Name of test that is starting.
  272.      *    @param integer $size       Number of test cases starting.
  273.      *    @access public
  274.      */
  275.     function paintGroupStart($test_name$size{
  276.         if (isset($this->_size)) {
  277.             $this->_size = $size;
  278.         }
  279.         if (count($this->_test_stack== 0{
  280.             $this->paintHeader($test_name);
  281.         }
  282.         $this->_test_stack[$test_name;
  283.     }
  284.  
  285.     /**
  286.      *    Paints the end of a group test. Will paint the page
  287.      *    footer if the stack of tests has unwound.
  288.      *    @param string $test_name   Name of test that is ending.
  289.      *    @param integer $progress   Number of test cases ending.
  290.      *    @access public
  291.      */
  292.     function paintGroupEnd($test_name{
  293.         array_pop($this->_test_stack);
  294.         if (count($this->_test_stack== 0{
  295.             $this->paintFooter($test_name);
  296.         }
  297.     }
  298.  
  299.     /**
  300.      *    Paints the start of a test case. Will also paint
  301.      *    the page header and footer if this is the
  302.      *    first test. Will stash the size if the first
  303.      *    start.
  304.      *    @param string $test_name   Name of test that is starting.
  305.      *    @access public
  306.      */
  307.     function paintCaseStart($test_name{
  308.         if (isset($this->_size)) {
  309.             $this->_size = 1;
  310.         }
  311.         if (count($this->_test_stack== 0{
  312.             $this->paintHeader($test_name);
  313.         }
  314.         $this->_test_stack[$test_name;
  315.     }
  316.  
  317.     /**
  318.      *    Paints the end of a test case. Will paint the page
  319.      *    footer if the stack of tests has unwound.
  320.      *    @param string $test_name   Name of test that is ending.
  321.      *    @access public
  322.      */
  323.     function paintCaseEnd($test_name{
  324.         $this->_progress++;
  325.         array_pop($this->_test_stack);
  326.         if (count($this->_test_stack== 0{
  327.             $this->paintFooter($test_name);
  328.         }
  329.     }
  330.  
  331.     /**
  332.      *    Paints the start of a test method.
  333.      *    @param string $test_name   Name of test that is starting.
  334.      *    @access public
  335.      */
  336.     function paintMethodStart($test_name{
  337.         $this->_test_stack[$test_name;
  338.     }
  339.  
  340.     /**
  341.      *    Paints the end of a test method. Will paint the page
  342.      *    footer if the stack of tests has unwound.
  343.      *    @param string $test_name   Name of test that is ending.
  344.      *    @access public
  345.      */
  346.     function paintMethodEnd($test_name{
  347.         array_pop($this->_test_stack);
  348.     }
  349.  
  350.     /**
  351.      *    Paints the test document header.
  352.      *    @param string $test_name     First test top level
  353.      *                                  to start.
  354.      *    @access public
  355.      *    @abstract
  356.      */
  357.     function paintHeader($test_name{
  358.     }
  359.  
  360.     /**
  361.      *    Paints the test document footer.
  362.      *    @param string $test_name        The top level test.
  363.      *    @access public
  364.      *    @abstract
  365.      */
  366.     function paintFooter($test_name{
  367.     }
  368.  
  369.     /**
  370.      *    Accessor for internal test stack. For
  371.      *    subclasses that need to see the whole test
  372.      *    history for display purposes.
  373.      *    @return array     List of methods in nesting order.
  374.      *    @access public
  375.      */
  376.     function getTestList({
  377.         return $this->_test_stack;
  378.     }
  379.  
  380.     /**
  381.      *    Accessor for total test size in number
  382.      *    of test cases. Null until the first
  383.      *    test is started.
  384.      *    @return integer   Total number of cases at start.
  385.      *    @access public
  386.      */
  387.     function getTestCaseCount({
  388.         return $this->_size;
  389.     }
  390.  
  391.     /**
  392.      *    Accessor for the number of test cases
  393.      *    completed so far.
  394.      *    @return integer   Number of ended cases.
  395.      *    @access public
  396.      */
  397.     function getTestCaseProgress({
  398.         return $this->_progress;
  399.     }
  400.  
  401.     /**
  402.      *    Static check for running in the comand line.
  403.      *    @return boolean        True if CLI.
  404.      *    @access public
  405.      *    @static
  406.      */
  407.     function inCli({
  408.         return php_sapi_name(== 'cli';
  409.     }
  410. }
  411.  
  412. /**
  413.  *    For modifying the behaviour of the visual reporters.
  414.  *    @package SimpleTest
  415.  *    @subpackage UnitTester
  416.  */
  417.     var $_reporter;
  418.  
  419.     /**
  420.      *    Mediates between the reporter and the test case.
  421.      *    @param SimpleScorer $reporter       Reporter to receive events.
  422.      */
  423.     function SimpleReporterDecorator(&$reporter{
  424.         $this->_reporter = &$reporter;
  425.     }
  426.  
  427.     /**
  428.      *    Signals that the next evaluation will be a dry
  429.      *    run. That is, the structure events will be
  430.      *    recorded, but no tests will be run.
  431.      *    @param boolean $is_dry        Dry run if true.
  432.      *    @access public
  433.      */
  434.     function makeDry($is_dry true{
  435.         $this->_reporter->makeDry($is_dry);
  436.     }
  437.  
  438.     /**
  439.      *    Accessor for current status. Will be false
  440.      *    if there have been any failures or exceptions.
  441.      *    Used for command line tools.
  442.      *    @return boolean        True if no failures.
  443.      *    @access public
  444.      */
  445.     function getStatus({
  446.         return $this->_reporter->getStatus();
  447.     }
  448.  
  449.     /**
  450.      *    The reporter has a veto on what should be run.
  451.      *    @param string $test_case_name  name of test case.
  452.      *    @param string $method          Name of test method.
  453.      *    @return boolean                True if test should be run.
  454.      *    @access public
  455.      */
  456.     function shouldInvoke($test_case_name$method{
  457.         return $this->_reporter->shouldInvoke($test_case_name$method);
  458.     }
  459.  
  460.     /**
  461.      *    Can wrap the invoker in preperation for running
  462.      *    a test.
  463.      *    @param SimpleInvoker $invoker   Individual test runner.
  464.      *    @return SimpleInvoker           Wrapped test runner.
  465.      *    @access public
  466.      */
  467.     function &createInvoker(&$invoker{
  468.         return $this->_reporter->createInvoker($invoker);
  469.     }
  470.     
  471.     /**
  472.      *    Gets the formatter for variables and other small
  473.      *    generic data items.
  474.      *    @return SimpleDumper          Formatter.
  475.      *    @access public
  476.      */
  477.     function getDumper({
  478.         return $this->_reporter->getDumper();
  479.     }
  480.  
  481.     /**
  482.      *    Paints the start of a group test.
  483.      *    @param string $test_name     Name of test or other label.
  484.      *    @param integer $size         Number of test cases starting.
  485.      *    @access public
  486.      */
  487.     function paintGroupStart($test_name$size{
  488.         $this->_reporter->paintGroupStart($test_name$size);
  489.     }
  490.  
  491.     /**
  492.      *    Paints the end of a group test.
  493.      *    @param string $test_name     Name of test or other label.
  494.      *    @access public
  495.      */
  496.     function paintGroupEnd($test_name{
  497.         $this->_reporter->paintGroupEnd($test_name);
  498.     }
  499.  
  500.     /**
  501.      *    Paints the start of a test case.
  502.      *    @param string $test_name     Name of test or other label.
  503.      *    @access public
  504.      */
  505.     function paintCaseStart($test_name{
  506.         $this->_reporter->paintCaseStart($test_name);
  507.     }
  508.  
  509.     /**
  510.      *    Paints the end of a test case.
  511.      *    @param string $test_name     Name of test or other label.
  512.      *    @access public
  513.      */
  514.     function paintCaseEnd($test_name{
  515.         $this->_reporter->paintCaseEnd($test_name);
  516.     }
  517.  
  518.     /**
  519.      *    Paints the start of a test method.
  520.      *    @param string $test_name     Name of test or other label.
  521.      *    @access public
  522.      */
  523.     function paintMethodStart($test_name{
  524.         $this->_reporter->paintMethodStart($test_name);
  525.     }
  526.  
  527.     /**
  528.      *    Paints the end of a test method.
  529.      *    @param string $test_name     Name of test or other label.
  530.      *    @access public
  531.      */
  532.     function paintMethodEnd($test_name{
  533.         $this->_reporter->paintMethodEnd($test_name);
  534.     }
  535.  
  536.     /**
  537.      *    Chains to the wrapped reporter.
  538.      *    @param string $message        Message is ignored.
  539.      *    @access public
  540.      */
  541.     function paintPass($message{
  542.         $this->_reporter->paintPass($message);
  543.     }
  544.  
  545.     /**
  546.      *    Chains to the wrapped reporter.
  547.      *    @param string $message        Message is ignored.
  548.      *    @access public
  549.      */
  550.     function paintFail($message{
  551.         $this->_reporter->paintFail($message);
  552.     }
  553.  
  554.     /**
  555.      *    Chains to the wrapped reporter.
  556.      *    @param string $message    Text of error formatted by
  557.      *                               the test case.
  558.      *    @access public
  559.      */
  560.     function paintError($message{
  561.         $this->_reporter->paintError($message);
  562.     }
  563.  
  564.     /**
  565.      *    Chains to the wrapped reporter.
  566.      *    @param Exception $exception        Exception to show.
  567.      *    @access public
  568.      */
  569.     function paintException($exception{
  570.         $this->_reporter->paintException($exception);
  571.     }
  572.     
  573.     /**
  574.      *    Prints the message for skipping tests.
  575.      *    @param string $message    Text of skip condition.
  576.      *    @access public
  577.      */
  578.     function paintSkip($message{
  579.         $this->_reporter->paintSkip($message);
  580.     }
  581.  
  582.     /**
  583.      *    Chains to the wrapped reporter.
  584.      *    @param string $message        Text to display.
  585.      *    @access public
  586.      */
  587.     function paintMessage($message{
  588.         $this->_reporter->paintMessage($message);
  589.     }
  590.  
  591.     /**
  592.      *    Chains to the wrapped reporter.
  593.      *    @param string $message        Text to display.
  594.      *    @access public
  595.      */
  596.     function paintFormattedMe