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

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