home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / PHPUnit / GUI / HTML.php next >
Encoding:
PHP Script  |  2004-10-01  |  7.2 KB  |  206 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.13 2004/09/28 06:52:48 sebastian Exp $
  16. //
  17.  
  18. /**
  19.  * This class provides a HTML GUI.
  20.  *
  21.  * @author      Wolfram Kriesing <wolfram@kriesing.de>
  22.  * @license     http://www.php.net/license/3_0.txt The PHP License, Version 3.0
  23.  * @category    PHP
  24.  * @package     PHPUnit
  25.  * @subpackage  GUI
  26.  */
  27. class PHPUnit_GUI_HTML
  28. {
  29.  
  30.     var $_suites = array();
  31.  
  32.     /**
  33.     *   the current implementation of PHPUnit is designed
  34.     *   this way that adding a suite to another suite only
  35.     *   grabs all the tests and adds them to the suite, so you
  36.     *   have no chance to find out which test goes with which suite
  37.     *   therefore you can simply pass an array of suites to this constructor here
  38.     *
  39.     *   @param  array   The suites to be tested. If not given, then you might
  40.     *                   be using the SetupDecorator, which detects them automatically
  41.     *                   when calling getSuitesFromDir()
  42.     *
  43.     */
  44.     function PHPUnit_GUI_HTML($suites=array())
  45.     {
  46.         if (!is_array($suites)) {
  47.             $this->_suites = array($suites);
  48.         } else {
  49.             $this->_suites = $suites;
  50.         }
  51.     }
  52.  
  53.     /**
  54.     *   Add suites to the GUI
  55.     *
  56.     *   @param  object  this should be an instance of PHPUnit_TestSuite
  57.     */
  58.     function addSuites($suites)
  59.     {
  60.         $this->_suites = array_merge($this->_suites,$suites);
  61.     }
  62.  
  63.     /**
  64.     *   this prints the HTML code straight out
  65.     *
  66.     */
  67.     function show()
  68.     {
  69.         $showPassed=FALSE;
  70.         $submitted = @$_REQUEST['submitted'];
  71.         if ($submitted) {
  72.             $showPassed = @$_REQUEST['showOK'] ? TRUE : FALSE;
  73.         }
  74.  
  75.         $suiteResults = array();
  76.         foreach ($this->_suites as $aSuite) {
  77.             $aSuiteResult = array();
  78.             // remove the first directory's name from the test-suite name, since it
  79.             // mostly is something like 'tests' or alike
  80.             $removablePrefix = explode('_',$aSuite->getName());
  81.             $aSuiteResult['name'] = str_replace($removablePrefix[0].'_', '', $aSuite->getName());
  82.             if ($submitted && isset($_REQUEST[$aSuiteResult['name']])) {
  83.                 $result = PHPUnit::run($aSuite);
  84.  
  85.                 $aSuiteResult['counts']['run'] = $result->runCount();
  86.                 $aSuiteResult['counts']['error'] = $result->errorCount();
  87.                 $aSuiteResult['counts']['failure'] = $result->failureCount();
  88.  
  89.                 $aSuiteResult['results'] = $this->_prepareResult($result,$showPassed);
  90.  
  91.                 $per = 100/$result->runCount();
  92.                 $failed = ($per*$result->errorCount())+($per*$result->failureCount());
  93.                 $aSuiteResult['percent'] = round(100-$failed,2);
  94.             } else {
  95.                 $aSuiteResult['addInfo'] = 'NOT EXECUTED';
  96.             }
  97.  
  98.             $suiteResults[] = $aSuiteResult;
  99.         }
  100.  
  101.         $final['name'] = 'OVERALL RESULT';
  102.         $final['counts'] = array();
  103.         $final['percent'] = 0;
  104.         $numExecutedTests = 0;
  105.         foreach ($suiteResults as $aSuiteResult) {
  106.             if (sizeof(@$aSuiteResult['counts'])) {
  107.                 foreach ($aSuiteResult['counts'] as $key=>$aCount) {
  108.                     if (!isset($final['counts'][$key])) {
  109.                         $final['counts'][$key] = 0;
  110.                     }
  111.                     $final['counts'][$key] += $aCount;
  112.                 }
  113.             }
  114.         }
  115.         if (isset($final['counts']['run'])) {
  116.             $per = 100/$final['counts']['run'];
  117.             $failed = ($per*$final['counts']['error'])+($per*$final['counts']['failure']);
  118.             $final['percent'] = round(100-$failed,2);
  119.         } else {
  120.             $final['percent'] = 0;
  121.         }
  122.         array_unshift($suiteResults,$final);
  123.  
  124.         include 'PHPUnit/GUI/HTML.tpl';
  125.     }
  126.  
  127.     function _prepareResult($result,$showPassed)
  128.     {
  129.         $ret = array();
  130.         $failures = $result->failures();
  131.         foreach($failures as $aFailure) {
  132.             $ret['failures'][] = $this->_prepareFailure($aFailure);
  133.         }
  134.  
  135.         $errors = $result->errors();
  136.         foreach($errors as $aError) {
  137.             $ret['errors'][] = $this->_prepareErrors($aError);
  138.         }
  139.  
  140.         if ($showPassed) {
  141.             $passed = $result->passedTests();
  142.             foreach($passed as $aPassed) {
  143.                 $ret['passed'][] = $this->_preparePassedTests($aPassed);
  144.             }
  145.         }
  146.  
  147.         return $ret;
  148.     }
  149.  
  150.     function _prepareFailure($failure)
  151.     {
  152.         $test = $failure->failedTest();
  153.         $ret['testName'] = $test->getName();
  154.  
  155.         $exception = $failure->thrownException();
  156.         // a serialized string starts with a 'character:decimal:{'
  157.         // if so we try to unserialize it
  158.         // this piece of the regular expression is for detecting a serialized
  159.         // type like 'a:3:' for an array with three element or an object i.e. 'O:12:"class":3'
  160.         $serialized = '(\w:\d+:(?:"[^"]+":\d+:)?\{.*\})';
  161.         // Spaces might make a diff, so we shall show them properly (since a
  162.         // user agent ignores them).
  163.         if (preg_match('/^(.*)expected ' . $serialized . ', actual ' .
  164.             $serialized . '$/sU', $exception, $matches)) {
  165.             ob_start();
  166.             print_r(unserialize($matches[2]));
  167.             $ret['expected'] = htmlspecialchars($matches[1]) . "<pre>" .
  168.                 htmlspecialchars(rtrim(ob_get_contents())) . "</pre>";
  169.             // Improved compatibility, ob_clean() would be PHP >= 4.2.0 only.
  170.             ob_end_clean();
  171.             ob_start();
  172.             print_r(unserialize($matches[3]));
  173.             $ret['actual'] = htmlspecialchars($matches[1]) . "<pre>" .
  174.                 htmlspecialchars(rtrim(ob_get_contents())) . "</pre>";
  175.             ob_end_clean();
  176.         } elseif (preg_match('/^(.*)expected (.*), actual (.*)$/sU', $exception,
  177.             $matches)) {
  178.             $ret['expected'] = nl2br(str_replace(" ", " ",
  179.                 htmlspecialchars($matches[1] . $matches[2])));
  180.             $ret['actual'] = nl2br(str_replace(" ", " ",
  181.                 htmlspecialchars($matches[1] . $matches[3])));
  182.         } else {
  183.             $ret['message'] = nl2br(str_replace(" ", " ",
  184.                 htmlspecialchars($exception)));
  185.         }
  186.  
  187.         return $ret;
  188.     }
  189.  
  190.     function _preparePassedTests($passed)
  191.     {
  192.         $ret['testName'] = $passed->getName();
  193.         return $ret;
  194.     }
  195.  
  196.     function _prepareError($error)
  197.     {
  198.         $ret['testName'] = $error->getName();
  199.         $ret['message'] = $error->toString();
  200.         return $ret;
  201.     }
  202.  
  203. }
  204.  
  205. ?>
  206.