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

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