PHPUnit
[ class tree: PHPUnit ] [ index: PHPUnit ] [ all elements ]

Source for file HTML.php

Documentation is available at HTML.php

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

Documentation generated on Mon, 11 Mar 2019 14:00:13 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.