Source for file reporter.php

Documentation is available at reporter.php

  1. <?php
  2. /**
  3.  *  base include file for SimpleTest
  4.  *  @package    SimpleTest
  5.  *  @subpackage UnitTester
  6.  *  @version    $Id: reporter.php 1702 2008-03-25 00:08:04Z lastcraft $
  7.  */
  8.  
  9. /**#@+
  10.  *  include other SimpleTest class files
  11.  */
  12. require_once(dirname(__FILE__'/scorer.php');
  13. /**#@-*/
  14.  
  15.  *    Sample minimal test displayer. Generates only
  16.  *    failure messages and a pass count.
  17.  *    @package SimpleTest
  18.  *    @subpackage UnitTester
  19.  */
  20. class HtmlReporter extends SimpleReporter {
  21.     var $_character_set;
  22.  
  23.     /**
  24.      *    Does nothing yet. The first output will
  25.      *    be sent on the first test start. For use
  26.      *    by a web browser.
  27.      *    @access public
  28.      */
  29.     function HtmlReporter($character_set 'ISO-8859-1'{
  30.         $this->SimpleReporter();
  31.         $this->_character_set = $character_set;
  32.     }
  33.  
  34.     /**
  35.      *    Paints the top of the web page setting the
  36.      *    title to the name of the starting test.
  37.      *    @param string $test_name      Name class of test.
  38.      *    @access public
  39.      */
  40.     function paintHeader($test_name{
  41.         $this->sendNoCacheHeaders();
  42.         print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">";
  43.         print "<html>\n<head>\n<title>$test_name</title>\n";
  44.         print "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=" .
  45.                 $this->_character_set . "\">\n";
  46.         print "<style type=\"text/css\">\n";
  47.         print $this->_getCss("\n";
  48.         print "</style>\n";
  49.         print "</head>\n<body>\n";
  50.         print "<h1>$test_name</h1>\n";
  51.         flush();
  52.     }
  53.  
  54.     /**
  55.      *    Send the headers necessary to ensure the page is
  56.      *    reloaded on every request. Otherwise you could be
  57.      *    scratching your head over out of date test data.
  58.      *    @access public
  59.      *    @static
  60.      */
  61.     function sendNoCacheHeaders({
  62.         if (headers_sent()) {
  63.             header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
  64.             header("Last-Modified: " gmdate("D, d M Y H:i:s"" GMT");
  65.             header("Cache-Control: no-store, no-cache, must-revalidate");
  66.             header("Cache-Control: post-check=0, pre-check=0"false);
  67.             header("Pragma: no-cache");
  68.         }
  69.     }
  70.  
  71.     /**
  72.      *    Paints the CSS. Add additional styles here.
  73.      *    @return string            CSS code as text.
  74.      *    @access protected
  75.      */
  76.     function _getCss({
  77.         return ".fail { background-color: inherit; color: red; }" .
  78.                 ".pass { background-color: inherit; color: green; }" .
  79.                 " pre { background-color: lightgray; color: inherit; }";
  80.     }
  81.  
  82.     /**
  83.      *    Paints the end of the test with a summary of
  84.      *    the passes and failures.
  85.      *    @param string $test_name        Name class of test.
  86.      *    @access public
  87.      */
  88.     function paintFooter($test_name{
  89.         $colour ($this->getFailCount($this->getExceptionCount("red" "green");
  90.         print "<div style=\"";
  91.         print "padding: 8px; margin-top: 1em; background-color: $colour; color: white;";
  92.         print "\">";
  93.         print $this->getTestCaseProgress("/" $this->getTestCaseCount();
  94.         print " test cases complete:\n";
  95.         print "<strong>" $this->getPassCount("</strong> passes, ";
  96.         print "<strong>" $this->getFailCount("</strong> fails and ";
  97.         print "<strong>" $this->getExceptionCount("</strong> exceptions.";
  98.         print "</div>\n";
  99.         print "</body>\n</html>\n";
  100.     }
  101.  
  102.     /**
  103.      *    Paints the test failure with a breadcrumbs
  104.      *    trail of the nesting test suites below the
  105.      *    top level test.
  106.      *    @param string $message    Failure message displayed in
  107.      *                               the context of the other tests.
  108.      *    @access public
  109.      */
  110.     function paintFail($message{
  111.         parent::paintFail($message);
  112.         print "<span class=\"fail\">Fail</span>: ";
  113.         $breadcrumb $this->getTestList();
  114.         array_shift($breadcrumb);
  115.         print implode(" -&gt; "$breadcrumb);
  116.         print " -&gt; " $this->_htmlEntities($message"<br />\n";
  117.     }
  118.  
  119.     /**
  120.      *    Paints a PHP error.
  121.      *    @param string $message        Message is ignored.
  122.      *    @access public
  123.      */
  124.     function paintError($message{
  125.         parent::paintError($message);
  126.         print "<span class=\"fail\">Exception</span>: ";
  127.         $breadcrumb $this->getTestList();
  128.         array_shift($breadcrumb);
  129.         print implode(" -&gt; "$breadcrumb);
  130.         print " -&gt; <strong>" $this->_htmlEntities($message"</strong><br />\n";
  131.     }
  132.  
  133.     /**
  134.      *    Paints a PHP exception.
  135.      *    @param Exception $exception        Exception to display.
  136.      *    @access public
  137.      */
  138.     function paintException($exception{
  139.         parent::paintException($exception);
  140.         print "<span class=\"fail\">Exception</span>: ";
  141.         $breadcrumb $this->getTestList();
  142.         array_shift($breadcrumb);
  143.         print implode(" -&gt; "$breadcrumb);
  144.         $message 'Unexpected exception of type [' get_class($exception.
  145.                 '] with message ['$exception->getMessage(.
  146.                 '] in ['$exception->getFile(.
  147.                 ' line ' $exception->getLine(']';
  148.         print " -&gt; <strong>" $this->_htmlEntities($message"</strong><br />\n";
  149.     }
  150.     
  151.     /**
  152.      *    Prints the message for skipping tests.
  153.      *    @param string $message    Text of skip condition.
  154.      *    @access public
  155.      */
  156.     function paintSkip($message{
  157.         parent::paintSkip($message);
  158.         print "<span class=\"pass\">Skipped</span>: ";
  159.         $breadcrumb $this->getTestList();
  160.         array_shift($breadcrumb);
  161.         print implode(" -&gt; "$breadcrumb);
  162.         print " -&gt; " $this->_htmlEntities($message"<br />\n";
  163.     }
  164.  
  165.     /**
  166.      *    Paints formatted text such as dumped variables.
  167.      *    @param string $message        Text to show.
  168.      *    @access public
  169.      */
  170.     function paintFormattedMessage($message{
  171.         print '<pre>' $this->_htmlEntities($message'</pre>';
  172.     }
  173.  
  174.     /**
  175.      *    Character set adjusted entity conversion.
  176.      *    @param string $message    Plain text or Unicode message.
  177.      *    @return string            Browser readable message.
  178.      *    @access protected
  179.      */
  180.     function _htmlEntities($message{
  181.         return htmlentities($messageENT_COMPAT$this->_character_set);
  182.     }
  183. }
  184.  
  185. /**
  186.  *    Sample minimal test displayer. Generates only
  187.  *    failure messages and a pass count. For command
  188.  *    line use. I've tried to make it look like JUnit,
  189.  *    but I wanted to output the errors as they arrived
  190.  *    which meant dropping the dots.
  191.  *    @package SimpleTest
  192.  *    @subpackage UnitTester
  193.  */
  194. class TextReporter extends SimpleReporter {
  195.  
  196.     /**
  197.      *    Does nothing yet. The first output will
  198.      *    be sent on the first test start.
  199.      *    @access public
  200.      */
  201.     function TextReporter({
  202.         $this->SimpleReporter();
  203.     }
  204.  
  205.     /**
  206.      *    Paints the title only.
  207.      *    @param string $test_name        Name class of test.
  208.      *    @access public
  209.      */
  210.     function paintHeader($test_name{
  211.         if (SimpleReporter::inCli()) {
  212.             header('Content-type: text/plain');
  213.         }
  214.         print "$test_name\n";
  215.         flush();
  216.     }
  217.  
  218.     /**
  219.      *    Paints the end of the test with a summary of
  220.      *    the passes and failures.
  221.      *    @param string $test_name        Name class of test.
  222.      *    @access public
  223.      */
  224.     function paintFooter($test_name{
  225.         if ($this->getFailCount($this->getExceptionCount(== 0{
  226.             print "OK\n";
  227.         else {
  228.             print "FAILURES!!!\n";
  229.         }
  230.         print "Test cases run: " $this->getTestCaseProgress(.
  231.                 "/" $this->getTestCaseCount(.
  232.                 ", Passes: " $this->getPassCount(.
  233.                 ", Failures: " $this->getFailCount(.
  234.                 ", Exceptions: " $this->getExceptionCount("\n";
  235.     }
  236.  
  237.     /**
  238.      *    Paints the test failure as a stack trace.
  239.      *    @param string $message    Failure message displayed in
  240.      *                               the context of the other tests.
  241.      *    @access public
  242.      */
  243.     function paintFail($message{
  244.         parent::paintFail($message);
  245.         print $this->getFailCount("$message\n";
  246.         $breadcrumb $this->getTestList();
  247.         array_shift($breadcrumb);
  248.         print "\tin " implode("\n\tin "array_reverse($breadcrumb));
  249.         print "\n";
  250.     }
  251.  
  252.     /**
  253.      *    Paints a PHP error or exception.
  254.      *    @param string $message        Message to be shown.
  255.      *    @access public
  256.      *    @abstract
  257.      */
  258.     function paintError($message{
  259.         parent::paintError($message);
  260.         print "Exception " $this->getExceptionCount("!\n$message\n";
  261.         $breadcrumb $this->getTestList();
  262.         array_shift($breadcrumb);
  263.         print "\tin " implode("\n\tin "array_reverse($breadcrumb));
  264.         print "\n";
  265.     }
  266.  
  267.     /**
  268.      *    Paints a PHP error or exception.
  269.      *    @param Exception $exception      Exception to describe.
  270.      *    @access public
  271.      *    @abstract
  272.      */
  273.     function paintException($exception{
  274.         parent::paintException($exception);
  275.         $message 'Unexpected exception of type [' get_class($exception.
  276.                 '] with message ['$exception->getMessage(.
  277.                 '] in ['$exception->getFile(.
  278.                 ' line ' $exception->getLine(']';
  279.         print "Exception " $this->getExceptionCount("!\n$message\n";
  280.         $breadcrumb $this->getTestList();
  281.         array_shift($breadcrumb);
  282.         print "\tin " implode("\n\tin "array_reverse($breadcrumb));
  283.         print "\n";
  284.     }
  285.     
  286.     /**
  287.      *    Prints the message for skipping tests.
  288.      *    @param string $message    Text of skip condition.
  289.      *    @access public
  290.      */
  291.     function paintSkip($message{
  292.         parent::paintSkip($message);
  293.         print "Skip: $message\n";
  294.     }
  295.  
  296.     /**
  297.      *    Paints formatted text such as dumped variables.
  298.      *    @param string $message        Text to show.
  299.      *    @access public
  300.      */
  301.     function paintFormattedMessage($message{
  302.         print "$message\n";
  303.         flush();
  304.     }
  305. }
  306.  
  307. /**
  308.  *    Runs just a single test group, a single case or
  309.  *    even a single test within that case.
  310.  *    @package SimpleTest
  311.  *    @subpackage UnitTester
  312.  */
  313.     var $_just_this_case = false;
  314.     var $_just_this_test = false;
  315.     var $_on;
  316.     
  317.     /**
  318.      *    Selects the test case or group to be run,
  319.      *    and optionally a specific test.
  320.      *    @param SimpleScorer $reporter    Reporter to receive events.
  321.      *    @param string $just_this_case    Only this case or group will run.
  322.      *    @param string $just_this_test    Only this test method will run.
  323.      */
  324.     function SelectiveReporter(&$reporter$just_this_case false$just_this_test false{
  325.         if (isset($just_this_case&& $just_this_case{
  326.             $this->_just_this_case = strtolower($just_this_case);
  327.             $this->_off();
  328.         else {
  329.             $this->_on();
  330.         }
  331.         if (isset($just_this_test&& $just_this_test{
  332.             $this->_just_this_test = strtolower($just_this_test);
  333.         }
  334.         $this->SimpleReporterDecorator($reporter);
  335.     }
  336.  
  337.     /**
  338.      *    Compares criteria to actual the case/group name.
  339.      *    @param string $test_case    The incoming test.
  340.      *    @return boolean             True if matched.
  341.      *    @access protected
  342.      */
  343.     function _matchesTestCase($test_case{
  344.         return $this->_just_this_case == strtolower($test_case);
  345.     }
  346.  
  347.     /**
  348.      *    Compares criteria to actual the test name. If no
  349.      *    name was specified at the beginning, then all tests
  350.      *    can run.
  351.      *    @param string $method       The incoming test method.
  352.      *    @return boolean             True if matched.
  353.      *    @access protected
  354.      */
  355.     function _shouldRunTest($test_case$method{
  356.         if ($this->_isOn(|| $this->_matchesTestCase($test_case)) {
  357.             if ($this->_just_this_test{
  358.                 return $this->_just_this_test == strtolower($method);
  359.             else {
  360.                 return true;
  361.             }
  362.         }
  363.         return false;
  364.     }
  365.     
  366.     /**
  367.      *    Switch on testing for the group or subgroup.
  368.      *    @access private
  369.      */
  370.     function _on({
  371.         $this->_on = true;
  372.     }
  373.     
  374.     /**
  375.      *    Switch off testing for the group or subgroup.
  376.      *    @access private
  377.      */
  378.     function _off({
  379.         $this->_on = false;
  380.     }
  381.     
  382.     /**
  383.      *    Is this group actually being tested?
  384.      *    @return boolean     True if the current test group is active.
  385.      *    @access private
  386.      */
  387.     function _isOn({
  388.         return $this->_on;
  389.     }
  390.  
  391.     /**
  392.      *    Veto everything that doesn't match the method wanted.
  393.      *    @param string $test_case       Name of test case.
  394.      *    @param string $method          Name of test method.
  395.      *    @return boolean                True if test should be run.
  396.      *    @access public
  397.      */
  398.     function shouldInvoke($test_case$method{
  399.         if ($this->_shouldRunTest($test_case$method)) {
  400.             return $this->_reporter->shouldInvoke($test_case$method);
  401.         }
  402.         return false;
  403.     }
  404.  
  405.     /**
  406.      *    Paints the start of a group test.
  407.      *    @param string $test_case     Name of test or other label.
  408.      *    @param integer $size         Number of test cases starting.
  409.      *    @access public
  410.      */
  411.     function paintGroupStart($test_case$size{
  412.         if ($this->_just_this_case && $this->_matchesTestCase($test_case)) {
  413.             $this->_on();
  414.         }
  415.         $this->_reporter->paintGroupStart($test_case$size);
  416.     }
  417.  
  418.     /**
  419.      *    Paints the end of a group test.
  420.      *    @param string $test_case     Name of test or other label.
  421.      *    @access public
  422.      */
  423.     function paintGroupEnd($test_case{
  424.         $this->_reporter->paintGroupEnd($test_case);
  425.         if ($this->_just_this_case && $this->_matchesTestCase($test_case)) {
  426.             $this->_off();
  427.         }
  428.     }
  429. }
  430.  
  431. /**
  432.  *    Suppresses skip messages.
  433.  *    @package SimpleTest
  434.  *    @subpackage UnitTester
  435.  */
  436.     
  437.     /**
  438.      *    Does nothing.
  439.      *    @param string $message    Text of skip condition.
  440.      *    @access public
  441.      */
  442.     function paintSkip($message}
  443. }
  444. ?>

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