home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / HTML.php < prev    next >
Encoding:
PHP Script  |  2004-03-24  |  6.6 KB  |  192 lines

  1. <?php
  2. //
  3. // +------------------------------------------------------------------------+
  4. // | PEAR :: PHPUnit                                                        |
  5. // +------------------------------------------------------------------------+
  6. // | Copyright (c) 2002-2003 Sebastian Bergmann <sb@sebastian-bergmann.de>. |
  7. // +------------------------------------------------------------------------+
  8. // | This source file is subject to version 3.00 of the PHP License,        |
  9. // | that is available at http://www.php.net/license/3_0.txt.               |
  10. // | If you did not receive a copy of the PHP license and are unable to     |
  11. // | obtain it through the world-wide-web, please send a note to            |
  12. // | license@php.net so we can mail you a copy immediately.                 |
  13. // +------------------------------------------------------------------------+
  14. //
  15. // $Id: HTML.php,v 1.7 2003/05/15 16:36:16 cain Exp $
  16. //
  17.  
  18. /**
  19. *   This class provides a HTML GUI.
  20. *
  21. *   @author Wolfram Kriesing <wolfram@kriesing.de>
  22. *
  23. */
  24. class PHPUnit_GUI_HTML
  25. {
  26.  
  27.     var $_suites = array();
  28.  
  29.     /**
  30.     *   the current implementation of PHPUnit is designed
  31.     *   this way that adding a suite to another suite only
  32.     *   grabs all the tests and adds them to the suite, so you
  33.     *   have no chance to find out which test goes with which suite
  34.     *   therefore you can simply pass an array of suites to this constructor here
  35.     *
  36.     *   @param  array   The suites to be tested. If not given, then you might
  37.     *                   be using the SetupDecorator, which detects them automatically
  38.     *                   when calling getSuitesFromDir()
  39.     *
  40.     */
  41.     function PHPUnit_GUI_HTML($suites=array())
  42.     {
  43.         if (!is_array($suites)) {
  44.             $this->_suites = array($suites);
  45.         } else {
  46.             $this->_suites = $suites;
  47.         }
  48.     }
  49.     
  50.     /**
  51.     *   Add suites to the GUI
  52.     *
  53.     *   @param  object  this should be an instance of PHPUnit_TestSuite
  54.     */
  55.     function addSuites($suites)
  56.     {
  57.         $this->_suites = array_merge($this->_suites,$suites);
  58.     }
  59.  
  60.     /**
  61.     *   this prints the HTML code straight out
  62.     *
  63.     */
  64.     function show()
  65.     {
  66.         $showPassed=false;
  67.         $submitted = @$_REQUEST['submitted'];
  68.         if ($submitted) {
  69.             $showPassed = @$_REQUEST['showOK'] ? true : false;
  70.         }
  71.         
  72.         $suiteResults = array();
  73.         foreach ($this->_suites as $aSuite) {
  74.             $aSuiteResult = array();
  75.             // remove the first directory's name from the test-suite name, since it
  76.             // mostly is something like 'tests' or alike 
  77.             $removablePrefix = explode('_',$aSuite->getName());
  78.             $aSuiteResult['name'] = str_replace($removablePrefix[0].'_', '', $aSuite->getName());
  79.             if ($submitted && isset($_REQUEST[$aSuiteResult['name']])) {
  80.                 $result = PHPUnit::run($aSuite);
  81.  
  82.                 $aSuiteResult['counts']['run'] = $result->runCount();
  83.                 $aSuiteResult['counts']['error'] = $result->errorCount();
  84.                 $aSuiteResult['counts']['failure'] = $result->failureCount();
  85.  
  86.                 $aSuiteResult['results'] = $this->_prepareResult($result,$showPassed);
  87.  
  88.                 $per = 100/$result->runCount();
  89.                 $failed = ($per*$result->errorCount())+($per*$result->failureCount());
  90.                 $aSuiteResult['percent'] = round(100-$failed,2);
  91.             } else {
  92.                 $aSuiteResult['addInfo'] = 'NOT EXECUTED';
  93.             }
  94.             
  95.             $suiteResults[] = $aSuiteResult;
  96.         }
  97.  
  98.         $final['name'] = 'OVERALL RESULT';
  99.         $final['counts'] = array();
  100.         $final['percent'] = 0;
  101.         $numExecutedTests = 0;
  102.         foreach ($suiteResults as $aSuiteResult) {
  103.             if (sizeof(@$aSuiteResult['counts'])) {
  104.                 foreach ($aSuiteResult['counts'] as $key=>$aCount) {
  105.                     if (!isset($final['counts'][$key])) {
  106.                         $final['counts'][$key] = 0;
  107.                     }
  108.                     $final['counts'][$key] += $aCount;
  109.                 }
  110.             }
  111.         }
  112.         if (isset($final['counts']['run'])) {
  113.             $per = 100/$final['counts']['run'];
  114.             $failed = ($per*$final['counts']['error'])+($per*$final['counts']['failure']);
  115.             $final['percent'] = round(100-$failed,2);
  116.         } else {
  117.             $final['percent'] = 0;
  118.         }
  119.         array_unshift($suiteResults,$final);
  120.           
  121.         include 'PHPUnit/GUI/HTML.tpl';
  122.     }
  123.  
  124.     function _prepareResult($result,$showPassed)
  125.     {
  126.         $ret = array();
  127.         $failures = $result->failures();
  128.         foreach($failures as $aFailure) {
  129.             $ret['failures'][] = $this->_prepareFailure($aFailure);
  130.         }
  131.         
  132.         $errors = $result->errors();
  133.         foreach($errors as $aError) {
  134.             $ret['errors'][] = $this->_prepareErrors($aError);
  135.         }
  136.         
  137.         if ($showPassed) {
  138.             $passed = $result->passedTests();
  139.             foreach($passed as $aPassed) {
  140.                 $ret['passed'][] = $this->_preparePassedTests($aPassed);
  141.             }
  142.         }
  143.         
  144.         return $ret;
  145.     }
  146.     
  147.     function _prepareFailure($failure)
  148.     {
  149.         $test = $failure->failedTest();            
  150.         $ret['testName'] = $test->getName();
  151.  
  152.         $exception = $failure->thrownException();
  153.         // if there are () around it then we seem to have a serialized data
  154.         if (preg_match('/expected\s.*\), actual.*/',$exception)) {
  155. // this might be unnecessary since it only works for arrays...        
  156.             ob_start();
  157.             print_r(unserialize(preg_replace('/expected\s(.*), actual.*/','$1',$exception)));
  158.             $ret['expected'] = ob_get_contents();
  159.             ob_clean();
  160.             print_r(unserialize(preg_replace('/expected\s.*, actual (.*)/','$1',$exception)));
  161.             $ret['actual'] = ob_get_contents();
  162.             ob_end_clean();
  163.         } else {
  164.             // spaces might make a diff, so we shall show them properly (since a user agent ignores them)
  165.             if (preg_match('/expected\s.*, actual.*/',$exception)) {
  166.                 $ret['expected'] = preg_replace('/expected\s(.*), actual.*/','$1',$exception);
  167.                 $ret['actual'] = preg_replace('/expected\s.*, actual (.*)/','$1',$exception);
  168.             } else {
  169.                 $ret['message'] = str_replace(' ',' ',$exception);
  170.             }
  171.         }
  172.         
  173.         return $ret;
  174.     }
  175.     
  176.     function _preparePassedTests($passed)
  177.     {
  178.         $ret['testName'] = $passed->getName();
  179.         return $ret;
  180.     }
  181.     
  182.     function _prepareError($error)
  183.     {
  184.         $ret['testName'] = $error->getName();
  185.         $ret['message'] = $error->toString();
  186.         return $ret;
  187.     }
  188.     
  189. }
  190.  
  191. ?>
  192.