Source for file webunit_reporter.php

Documentation is available at webunit_reporter.php

  1. <?php
  2. /**
  3.  *    base include file for SimpleTest PUnit reporter
  4.  *    @package    SimpleTest
  5.  *    @subpackage    UnitTester
  6.  *    @version    $Id: webunit_reporter.php 1631 2008-01-20 14:31:16Z pp11 $
  7.  */
  8.  
  9. /**
  10.  * @ignore    originally defined in simple_test.php
  11.  */
  12. if (!defined("SIMPLE_TEST")) {
  13.     define("SIMPLE_TEST""simpletest/");
  14. }
  15. require_once(SIMPLE_TEST 'runner.php');
  16. require_once(SIMPLE_TEST 'reporter.php');
  17. /**
  18.  * Main sprintf template for the start of the page.
  19.  * Sequence of parameters is:
  20.  * - title - string
  21.  * - script path - string
  22.  * - script path - string
  23.  * - css path - string
  24.  * - additional css - string
  25.  * - title - string
  26.  * - image path - string
  27.  */
  28. define('SIMPLETEST_WEBUNIT_HEAD'<<<EOS
  29. <html>
  30. <head>
  31. <title>%s</title>
  32. <script type="text/javascript" src="%sx.js"></script>
  33. <script type="text/javascript" src="%swebunit.js"></script>
  34. <link rel="stylesheet" type="text/css" href="%swebunit.css" title="Default"></link>
  35. <style type="text/css">
  36. %s
  37. </style>
  38. </head>
  39. <body>
  40. <div id="wait">
  41.     <h1>&nbsp;Running %s&nbsp;</h1>
  42.     Please wait...<br />
  43.     <img src="%swait.gif" border="0"><br />&nbsp;
  44. </div>
  45. <script type="text/javascript">
  46. wait_start();
  47. </script>
  48. <div id="webunit">
  49.     <div id="run"></div><br />
  50.     <div id="tabs">
  51.         <div id="visible_tab">visible tab content</div>
  52.         &nbsp;&nbsp;<span id="failtab" class="activetab">&nbsp;&nbsp;<a href="javascript:activate_tab('fail');">Fail</a>&nbsp;&nbsp;</span>
  53.         <span id="treetab" class="inactivetab">&nbsp;&nbsp;<a href="javascript:activate_tab('tree');">Tree</a>&nbsp;&nbsp;</span>
  54.     </div>
  55.     <div id="msg">Click on a failed test case method in the tree tab to view output here.</div>
  56. </div>
  57. <div id="fail"></div>
  58. <div id="tree"></div>
  59. <!-- open a new script to capture js vars as the tests run -->
  60. <script type="text/javascript">
  61. layout();
  62.  
  63. EOS
  64. );
  65.  
  66. /**
  67.  *    Not used yet.
  68.  *  May be needed for localized styles we need at runtime, not in the stylesheet.
  69.  */
  70. define('SIMPLETEST_WEBUNIT_CSS''/* this space reseved for future use */');
  71.  
  72.     /**
  73.      *    Sample minimal test displayer. Generates only
  74.      *    failure messages and a pass count.
  75.      *      @package SimpleTest
  76.      *      @subpackage UnitTester
  77.      */
  78.     class WebUnitReporter extends SimpleReporter {
  79.         /**
  80.          *    @var string Base directory for PUnit script, images and style sheets.
  81.          *     Needs to be a relative path from where the test scripts are run
  82.          *     (and obviously, visible in the document root).
  83.          */
  84.         var $path;
  85.         
  86.         /**
  87.          *    Does nothing yet. The first output will
  88.          *    be sent on the first test start. For use
  89.          *    by a web browser.
  90.          *    @access public
  91.          */
  92.         function WebUnitReporter($path='../ui/'{
  93.             $this->SimpleReporter();
  94.             $this->path = $path;
  95.         }
  96.         
  97.         /**
  98.          *    Paints the top of the web page setting the
  99.          *    title to the name of the starting test.
  100.          *    @param string $test_name      Name class of test.
  101.          *    @access public
  102.          */
  103.         function paintHeader($test_name{
  104.             $this->sendNoCacheHeaders();
  105.             echo sprintf(
  106.                 SIMPLETEST_WEBUNIT_HEAD
  107.                 ,$test_name
  108.                 ,$this->path.'js/'
  109.                 ,$this->path.'js/'
  110.                 ,$this->path.'css/'
  111.                 ,$this->_getCss()
  112.                 ,$test_name
  113.                 ,$this->path.'img/'
  114.                 );
  115.             flush();
  116.         }
  117.         
  118.         /**
  119.          *    Send the headers necessary to ensure the page is
  120.          *    reloaded on every request. Otherwise you could be
  121.          *    scratching your head over out of date test data.
  122.          *    @access public
  123.          *    @static
  124.          */
  125.         function sendNoCacheHeaders({
  126.             header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
  127.             header("Last-Modified: " gmdate("D, d M Y H:i:s"" GMT");
  128.             header("Cache-Control: no-store, no-cache, must-revalidate");
  129.             header("Cache-Control: post-check=0, pre-check=0"false);
  130.             header("Pragma: no-cache");
  131.         }
  132.         
  133.         /**
  134.          *    Paints the CSS. Add additional styles here.
  135.          *    @return string            CSS code as text.
  136.          *    @access protected
  137.          */
  138.         function _getCss({
  139.             return SIMPLETEST_WEBUNIT_CSS;
  140.         }
  141.         
  142.         /**
  143.          *    Paints the end of the test with a summary of
  144.          *    the passes and failures.
  145.          *    @param string $test_name        Name class of test.
  146.          *    @access public
  147.          */
  148.         function paintFooter($test_name{
  149.             echo 'make_tree();</script>'.$this->outputScript("xHide('wait');");
  150.             $colour ($this->getFailCount($this->getExceptionCount("red" "green");
  151.             $content "<h1>$test_name</h1>\n";
  152.             $content .= "<div style=\"";
  153.             $content .= "padding: 8px; margin-top: 1em; background-color: $colour; color: white;";
  154.             $content .= "\">";
  155.             $content .= $this->getTestCaseProgress("/" $this->getTestCaseCount();
  156.             $content .= " test cases complete:\n";
  157.             $content .= "<strong>" $this->getPassCount("</strong> passes, ";
  158.             $content .= "<strong>" $this->getFailCount("</strong> fails and ";
  159.             $content .= "<strong>" $this->getExceptionCount("</strong> exceptions.";
  160.             $content .= "</div>\n";
  161.  
  162.             echo $this->outputScript('foo = "'.$this->toJsString($content).'";'."\nset_div_content('run', foo);");
  163.             echo "\n</body>\n</html>\n";
  164.         }
  165.         
  166.         
  167.         /**
  168.          *    Paints formatted text such as dumped variables.
  169.          *    @param string $message        Text to show.
  170.          *    @access public
  171.          */
  172.         function paintFormattedMessage($message{
  173.            echo "add_log(\"".$this->toJsString("<pre>$message</pre>"true)."\");\n";
  174.         }
  175.         
  176.         /**
  177.          *    Paints the start of a group test. Will also paint
  178.          *    the page header and footer if this is the
  179.          *    first test. Will stash the size if the first
  180.          *    start.
  181.          *    @param string $test_name   Name of test that is starting.
  182.          *    @param integer $size       Number of test cases starting.
  183.          *    @access public
  184.          */
  185.         function paintGroupStart($test_name$size{
  186.              Parent::paintGroupStart($test_name$size);
  187.              echo "add_group('$test_name');\n";
  188.         }
  189.  
  190.          /**
  191.           *    Paints the start of a test case. Will also paint
  192.           *    the page header and footer if this is the
  193.           *    first test. Will stash the size if the first
  194.           *    start.
  195.           *    @param string $test_name   Name of test that is starting.
  196.           *    @access public
  197.           */
  198.          function paintCaseStart($test_name{
  199.              Parent::paintCaseStart($test_name);
  200.              echo "add_case('$test_name');\n";
  201.          }
  202.  
  203.  
  204.          /**
  205.           *    Paints the start of a test method.
  206.           *    @param string $test_name   Name of test that is starting.
  207.           *    @access public
  208.           */
  209.          function paintMethodStart($test_name{
  210.              Parent::paintMethodStart($test_name);
  211.              echo "add_method('$test_name');\n";
  212.          }
  213.  
  214.          /**
  215.           *    Paints the end of a test method.
  216.           *    @param string $test_name   Name of test that is ending.
  217.           *    @access public
  218.           */
  219.          function paintMethodEnd($test_name{
  220.              Parent::paintMethodEnd($test_name);
  221.          }
  222.  
  223.          /**
  224.           *    Paints the test failure with a breadcrumbs
  225.           *    trail of the nesting test suites below the
  226.           *    top level test.
  227.           *    @param string $message    Failure message displayed in
  228.           *                                the context of the other tests.
  229.           *    @access public
  230.           */
  231.          function paintFail($message{
  232.              parent::paintFail($message);
  233.              $msg "<span class=\"fail\">Fail</span>: ";
  234.              $breadcrumb $this->getTestList();
  235.              array_shift($breadcrumb);
  236.              $msg .= implode("-&gt;"$breadcrumb);
  237.              $msg .= "-&gt;" htmlentities($message"<br />";
  238.              echo "add_fail('$msg');\n";
  239.          }
  240.  
  241.         /**
  242.          *    Paints a PHP error or exception.
  243.          *    @param string $message        Message is ignored.
  244.          *    @access public
  245.          *    @abstract
  246.          */
  247.         function paintException($message{
  248.             parent::paintException($message);
  249.             $msg "<span class=\"fail\">Exception</span>: ";
  250.             $breadcrumb $this->getTestList();
  251.             array_shift($breadcrumb);
  252.             $msg .= implode("-&gt;"$breadcrumb);
  253.             $msg .= "-&gt;<strong>" htmlentities($message"</strong><br />";
  254.             echo "add_fail('$msg');\n";
  255.         }
  256.  
  257.         /**
  258.          * Returns the script passed in wrapped in script tags.
  259.          *
  260.          * @param    string    $script        the script to output
  261.          * @return    string    the script wrapped with script tags
  262.          */
  263.         function outputScript($script)
  264.         {
  265.             return "<script type=\"text/javascript\">\n".$script."\n</script>\n";
  266.         }
  267.         
  268.         
  269.         /**
  270.          *    Transform a string into a format acceptable to JavaScript
  271.          *  @param string $str    the string to transform
  272.          *    @return    string 
  273.          */
  274.         function toJsString($str$preserveCr=false{
  275.             $cr ($preserveCr'\\n' '';
  276.             return str_replace(
  277.                 array('"'
  278.                     ,"\n")
  279.                 ,array('\"'
  280.                     ,"$cr\"\n\t+\"")
  281.                 ,$str
  282.                 );
  283.         }
  284.     }
  285.     
  286. ?>

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