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

Documentation generated on Sun, 31 Oct 2010 16:32:14 -0500 by phpDocumentor 1.4.3