Source for file eclipse.php

Documentation is available at eclipse.php

  1. <?php
  2. /**
  3.  *  base include file for eclipse plugin
  4.  *  @package    SimpleTest
  5.  *  @subpackage Eclipse
  6.  *  @version    $Id: eclipse.php 1723 2008-04-08 00:34:10Z lastcraft $
  7.  */
  8. /**#@+
  9.  * simpletest include files
  10.  */
  11. include_once 'unit_tester.php';
  12. include_once 'test_case.php';
  13. include_once 'invoker.php';
  14. include_once 'socket.php';
  15. include_once 'mock_objects.php';
  16. /**#@-*/
  17.  
  18.  *  base reported class for eclipse plugin
  19.  *  @package    SimpleTest
  20.  *  @subpackage Eclipse
  21.  */
  22. class EclipseReporter extends SimpleScorer {
  23.     
  24.     /**
  25.      *    Reporter to be run inside of Eclipse interface.
  26.      *    @param object $listener   Eclipse listener (?).
  27.      *    @param boolean $cc        Whether to include test coverage.
  28.      */
  29.     function EclipseReporter(&$listener$cc=false){
  30.         $this->_listener &$listener;
  31.         $this->SimpleScorer();
  32.         $this->_case "";
  33.         $this->_group "";
  34.         $this->_method "";
  35.         $this->_cc $cc;
  36.         $this->_error false;
  37.         $this->_fail false;
  38.     }
  39.     
  40.     /**
  41.      *    Means to display human readable object comparisons.
  42.      *    @return SimpleDumper        Visual comparer.
  43.      */
  44.     function getDumper({
  45.         return new SimpleDumper();
  46.     }
  47.     
  48.     /**
  49.      *    Localhost connection from Eclipse.
  50.      *    @param integer $port      Port to connect to Eclipse.
  51.      *    @param string $host       Normally localhost.
  52.      *    @return SimpleSocket      Connection to Eclipse.
  53.      */
  54.     function &createListener($port$host="127.0.0.1"){
  55.         $tmplistener &new SimpleSocket($host$port5);
  56.         return $tmplistener;
  57.     }
  58.     
  59.     /**
  60.      *    Wraps the test in an output buffer.
  61.      *    @param SimpleInvoker $invoker     Current test runner.
  62.      *    @return EclipseInvoker            Decorator with output buffering.
  63.      *    @access public
  64.      */
  65.     function &createInvoker(&$invoker){
  66.         $eclinvoker &new EclipseInvoker($invoker$this->_listener);
  67.         return $eclinvoker;
  68.     }
  69.     
  70.     /**
  71.      *    C style escaping.
  72.      *    @param string $raw    String with backslashes, quotes and whitespace.
  73.      *    @return string        Replaced with C backslashed tokens.
  74.      */
  75.     function escapeVal($raw){
  76.         $needle array("\\","\"","/","\b","\f","\n","\r","\t");
  77.         $replace array('\\\\','\"','\/','\b','\f','\n','\r','\t');
  78.         return str_replace($needle$replace$raw);
  79.     }
  80.     
  81.     /**
  82.      *    Stash the first passing item. Clicking the test
  83.      *    item goes to first pass.
  84.      *    @param string $message    Test message, but we only wnat the first.
  85.      *    @access public
  86.      */
  87.     function paintPass($message){
  88.         if ($this->_pass){
  89.             $this->_message $this->escapeVal($message);
  90.         }
  91.         $this->_pass true;
  92.     }
  93.     
  94.     /**
  95.      *    Stash the first failing item. Clicking the test
  96.      *    item goes to first fail.
  97.      *    @param string $message    Test message, but we only wnat the first.
  98.      *    @access public
  99.      */
  100.     function paintFail($message){
  101.         //only get the first failure or error
  102.         if ($this->_fail && $this->_error){
  103.             $this->_fail true;
  104.             $this->_message $this->escapeVal($message);
  105.             $this->_listener->write('{status:"fail",message:"'.$this->_message.'",group:"'.$this->_group.'",case:"'.$this->_case.'",method:"'.$this->_method.'"}');
  106.         }
  107.     }
  108.     
  109.     /**
  110.      *    Stash the first error. Clicking the test
  111.      *    item goes to first error.
  112.      *    @param string $message    Test message, but we only wnat the first.
  113.      *    @access public
  114.      */
  115.     function paintError($message){
  116.         if ($this->_fail && $this->_error){
  117.             $this->_error true;
  118.             $this->_message $this->escapeVal($message);
  119.             $this->_listener->write('{status:"error",message:"'.$this->_message.'",group:"'.$this->_group.'",case:"'.$this->_case.'",method:"'.$this->_method.'"}');
  120.         }
  121.     }
  122.     
  123.     
  124.     /**
  125.      *    Stash the first exception. Clicking the test
  126.      *    item goes to first message.
  127.      *    @param string $message    Test message, but we only wnat the first.
  128.      *    @access public
  129.      */
  130.     function paintException($exception){
  131.         if ($this->_fail && $this->_error){
  132.             $this->_error true;
  133.             $message 'Unexpected exception of type[' get_class($exception.
  134.                     '] with message [' $exception->getMessage('] in [' .
  135.                     $exception->getFile(.' line '$exception->getLine(']';
  136.             $this->_message $this->escapeVal($message);
  137.             $this->_listener->write(
  138.                     '{status:"error",message:"' $this->_message '",group:"' .
  139.                     $this->_group '",case:"' $this->_case '",method:"' $this->_method
  140.                     . '"}');
  141.         }
  142.     }
  143.     
  144.  
  145.     /**
  146.      *    We don't display any special header.
  147.      *    @param string $test_name     First test top level
  148.      *                                  to start.
  149.      *    @access public
  150.      */
  151.     function paintHeader($test_name{
  152.     }
  153.  
  154.     /**
  155.      *    We don't display any special footer.
  156.      *    @param string $test_name        The top level test.
  157.      *    @access public
  158.      */
  159.     function paintFooter($test_name{
  160.     }
  161.     
  162.     /**
  163.      *    Paints nothing at the start of a test method, but stash
  164.      *    the method name for later.
  165.      *    @param string $test_name   Name of test that is starting.
  166.      *    @access public
  167.      */
  168.     function paintMethodStart($method{
  169.         $this->_pass false;
  170.         $this->_fail false;
  171.         $this->_error false;
  172.         $this->_method $this->escapeVal($method);
  173.     }
  174.         
  175.     /**
  176.      *    Only send one message if the test passes, after that
  177.      *    suppress the message.
  178.      *    @param string $test_name   Name of test that is ending.
  179.      *    @access public
  180.      */
  181.     function paintMethodEnd($method){   
  182.         if ($this->_fail || $this->_error || $this->_pass){
  183.         else {
  184.             $this->_listener->write(
  185.                         '{status:"pass",message:"' $this->_message '",group:"' .
  186.                         $this->_group '",case:"' $this->_case '",method:"' .
  187.                         $this->_method '"}');
  188.         }
  189.     }
  190.     
  191.     /**
  192.      *    Stashes the test case name for the later failure message.
  193.      *    @param string $test_name     Name of test or other label.
  194.      *    @access public
  195.      */
  196.     function paintCaseStart($case){
  197.         $this->_case $this->escapeVal($case);
  198.     }
  199.     
  200.     /**
  201.      *    Drops the name.
  202.      *    @param string $test_name     Name of test or other label.
  203.      *    @access public
  204.      */
  205.     function paintCaseEnd($case){
  206.         $this->_case "";
  207.     }
  208.     
  209.     /**
  210.      *    Stashes the name of the test suite. Starts test coverage
  211.      *    if enabled.
  212.      *    @param string $group     Name of test or other label.
  213.      *    @param integer $size     Number of test cases starting.
  214.      *    @access public
  215.      */
  216.     function paintGroupStart($group$size){
  217.         $this->_group $this->escapeVal($group);
  218.         if ($this->_cc){
  219.             if (extension_loaded('xdebug')){
  220.                 xdebug_start_code_coverage(XDEBUG_CC_UNUSED XDEBUG_CC_DEAD_CODE)
  221.             }
  222.         }
  223.     }
  224.  
  225.     /**
  226.      *    Paints coverage report if enabled.
  227.      *    @param string $group     Name of test or other label.
  228.      *    @access public
  229.      */
  230.     function paintGroupEnd($group){
  231.         $this->_group "";
  232.         $cc "";
  233.         if ($this->_cc){
  234.             if (extension_loaded('xdebug')){
  235.                 $arrfiles xdebug_get_code_coverage();
  236.                 xdebug_stop_code_coverage();
  237.                 $thisdir dirname(__FILE__);
  238.                 $thisdirlen strlen($thisdir);
  239.                 foreach ($arrfiles as $index=>$file){
  240.                     if (substr($index0$thisdirlen)===$thisdir){
  241.                         continue;
  242.                     }
  243.                     $lcnt 0;
  244.                     $ccnt 0;
  245.                     foreach ($file as $line){
  246.                         if ($line == -2){
  247.                             continue;
  248.                         }
  249.                         $lcnt++;
  250.                         if ($line==1){
  251.                             $ccnt++;
  252.                         }
  253.                     }
  254.                     if ($lcnt 0){
  255.                         $cc .= round(($ccnt/$lcnt1002'%';
  256.                     }else{
  257.                         $cc .= "0.00%";
  258.                     }
  259.                     $cc.= "\t"$index "\n";
  260.                 }
  261.             }
  262.         }
  263.         $this->_listener->write('{status:"coverage",message:"' .
  264.                                 EclipseReporter::escapeVal($cc'"}');
  265.     }
  266. }
  267.  
  268. /**
  269.  *  Invoker decorator for Eclipse. Captures output until
  270.  *  the end of the test.
  271.  *  @package    SimpleTest
  272.  *  @subpackage Eclipse
  273.  */
  274.     function EclipseInvoker(&$invoker&$listener{
  275.         $this->_listener &$listener;
  276.         $this->SimpleInvokerDecorator($invoker);
  277.     }
  278.     
  279.     /**
  280.      *    Starts output buffering.
  281.      *    @param string $method    Test method to call.
  282.      *    @access public
  283.      */
  284.     function before($method){
  285.         ob_start();
  286.         $this->_invoker->before($method);
  287.     }
  288.  
  289.     /**
  290.      *    Stops output buffering and send the captured output
  291.      *    to the listener.
  292.      *    @param string $method    Test method to call.
  293.      *    @access public
  294.      */
  295.     function after($method{
  296.         $this->_invoker->after($method);
  297.         $output ob_get_contents();
  298.         ob_end_clean();
  299.         if ($output !== ""){
  300.             $result $this->_listener->write('{status:"info",message:"' .
  301.                                               EclipseReporter::escapeVal($output'"}');
  302.         }
  303.     }
  304. }
  305. ?>

Documentation generated on Sun, 04 May 2008 09:21:29 -0500 by phpDocumentor 1.3.0