home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / PHPUnit2 / Util / CodeCoverage / Renderer.php
Encoding:
PHP Script  |  2008-07-02  |  6.6 KB  |  226 lines

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3.  
  4. /**
  5.  * PHP Version 5
  6.  *
  7.  * Copyright (c) 2002-2006, Sebastian Bergmann <sb@sebastian-bergmann.de>.
  8.  * All rights reserved.
  9.  *
  10.  * Redistribution and use in source and binary forms, with or without
  11.  * modification, are permitted provided that the following conditions
  12.  * are met:
  13.  *
  14.  *   * Redistributions of source code must retain the above copyright
  15.  *     notice, this list of conditions and the following disclaimer.
  16.  * 
  17.  *   * Redistributions in binary form must reproduce the above copyright
  18.  *     notice, this list of conditions and the following disclaimer in
  19.  *     the documentation and/or other materials provided with the
  20.  *     distribution.
  21.  *
  22.  *   * Neither the name of Sebastian Bergmann nor the names of his
  23.  *     contributors may be used to endorse or promote products derived
  24.  *     from this software without specific prior written permission.
  25.  *
  26.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  27.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  28.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  29.  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  30.  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  31.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  32.  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  33.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  34.  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRIC
  35.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  36.  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  37.  * POSSIBILITY OF SUCH DAMAGE.
  38.  *
  39.  * @category   Testing
  40.  * @package    PHPUnit2
  41.  * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
  42.  * @copyright  2002-2006 Sebastian Bergmann <sb@sebastian-bergmann.de>
  43.  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
  44.  * @version    CVS: $Id: Renderer.php,v 1.8.2.7 2006/02/25 17:02:23 sebastian Exp $
  45.  * @link       http://pear.php.net/package/PHPUnit2
  46.  * @since      File available since Release 2.3.0
  47.  */
  48.  
  49. /**
  50.  * Abstract base class for Code Coverage renderers.
  51.  *
  52.  * @category   Testing
  53.  * @package    PHPUnit2
  54.  * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
  55.  * @copyright  2002-2006 Sebastian Bergmann <sb@sebastian-bergmann.de>
  56.  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
  57.  * @version    Release: 2.3.6
  58.  * @link       http://pear.php.net/package/PHPUnit2
  59.  * @since      Class available since Release 2.1.0
  60.  * @abstract
  61.  */
  62. abstract class PHPUnit2_Util_CodeCoverage_Renderer {
  63.     /**
  64.      * @var    array
  65.      * @access protected
  66.      */
  67.     protected $codeCoverageInformation;
  68.  
  69.     /**
  70.      * Constructor.
  71.      *
  72.      * @param  array $codeCoverageInformation
  73.      * @access protected
  74.      */
  75.     protected function __construct($codeCoverageInformation) {
  76.         $this->codeCoverageInformation = $codeCoverageInformation;
  77.     }
  78.  
  79.     /**
  80.      * Abstract Factory.
  81.      *
  82.      * @param  string  $rendererName
  83.      * @param  array   $codeCoverageInformation
  84.      * @throws Exception
  85.      * @access public
  86.      */
  87.     public function factory($rendererName, $codeCoverageInformation) {
  88.         require_once 'PHPUnit2/Util/CodeCoverage/Renderer/' . $rendererName . '.php';
  89.  
  90.         $class = 'PHPUnit2_Util_CodeCoverage_Renderer_' . $rendererName;
  91.         return new $class($codeCoverageInformation);
  92.     }
  93.  
  94.     /**
  95.      * Visualizes the result array of
  96.      * PHPUnit2_Framework_TestResult::getCodeCoverageInformation().
  97.      *
  98.      * @return string
  99.      * @access public
  100.      * @final
  101.      */
  102.     public final function render() {
  103.         $buffer = $this->header();
  104.  
  105.         foreach ($this->getSummary() as $sourceFile => $executedLines) {
  106.             if (file_exists($sourceFile)) {
  107.                 $buffer .= $this->startSourceFile($sourceFile);
  108.                 $buffer .= $this->renderSourceFile(file($sourceFile), $executedLines);
  109.                 $buffer .= $this->endSourceFile($sourceFile);
  110.             }
  111.         }
  112.  
  113.         return $buffer . $this->footer();
  114.     }
  115.  
  116.     /**
  117.      * Visualizes the result array of
  118.      * PHPUnit2_Framework_TestResult::getCodeCoverageInformation()
  119.      * and writes it to a file.
  120.      *
  121.      * @param  string $filename
  122.      * @access public
  123.      * @since  Method available since Release 2.2.0
  124.      */
  125.     public function renderToFile($filename) {
  126.         if ($fp = fopen($filename, 'w')) {
  127.             fputs(
  128.               $fp,
  129.               $this->render()
  130.             );
  131.  
  132.             fclose($fp);
  133.         }
  134.     }
  135.  
  136.     /**
  137.      * Returns summarized Code Coverage data.
  138.      *
  139.      * Format of the result array:
  140.      *
  141.      * <code>
  142.      * array(
  143.      *   "/tested/code.php" => array(
  144.      *     linenumber => flag
  145.      *   )
  146.      * )
  147.      * </code>
  148.      *
  149.      * flag > 0: line was executed.
  150.      * flag < 0: line is executable but was not executed.
  151.      *
  152.      * @return array
  153.      * @access protected
  154.      * @since  Method available since Release 2.2.0
  155.      */
  156.     protected function getSummary() {
  157.         $summary = array();
  158.  
  159.         foreach ($this->codeCoverageInformation as $testCaseName => $sourceFiles) {
  160.             foreach ($sourceFiles as $sourceFile => $executedLines) {
  161.                 foreach ($executedLines as $lineNumber => $flag) {
  162.                     if (!isset($summary[$sourceFile][$lineNumber])) {
  163.                         $summary[$sourceFile][$lineNumber] = $flag;
  164.                     }
  165.  
  166.                     else if ($flag > 0) {
  167.                         $summary[$sourceFile][$lineNumber] = $flag;
  168.                     }
  169.                 }
  170.             }
  171.         }
  172.  
  173.         return $summary;
  174.     }
  175.  
  176.     /**
  177.      * @return string
  178.      * @access protected
  179.      * @since  Method available since Release 2.1.1
  180.      */
  181.     protected function header() {
  182.     }
  183.  
  184.     /**
  185.      * @return string
  186.      * @access protected
  187.      * @since  Method available since Release 2.1.1
  188.      */
  189.     protected function footer() {
  190.     }
  191.  
  192.     /**
  193.      * @param  string $sourceFile
  194.      * @return string
  195.      * @access protected
  196.      */
  197.     protected function startSourceFile($sourceFile) {
  198.     }
  199.  
  200.     /**
  201.      * @param  string $sourceFile
  202.      * @return string
  203.      * @access protected
  204.      */
  205.     protected function endSourceFile($sourceFile) {
  206.     }
  207.  
  208.     /**
  209.      * @param  array $codeLines
  210.      * @param  array $executedLines
  211.      * @return string
  212.      * @access protected
  213.      * @abstract
  214.      */
  215.     abstract protected function renderSourceFile($codeLines, $executedLines);
  216. }
  217.  
  218. /*
  219.  * Local variables:
  220.  * tab-width: 4
  221.  * c-basic-offset: 4
  222.  * c-hanging-comment-ender-p: nil
  223.  * End:
  224.  */
  225. ?>
  226.