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 / phing / tasks / ext / phpunit2 / PHPUnit2ReportTask.php < prev    next >
Encoding:
PHP Script  |  2007-12-21  |  4.6 KB  |  187 lines

  1. <?php
  2. /**
  3.  * $Id: PHPUnit2ReportTask.php 82 2006-07-07 18:15:35Z mrook $
  4.  *
  5.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8.  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9.  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11.  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15.  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16.  *
  17.  * This software consists of voluntary contributions made by many individuals
  18.  * and is licensed under the LGPL. For more information please see
  19.  * <http://phing.info>.
  20.  */
  21.  
  22. require_once 'phing/Task.php';
  23. require_once 'phing/system/io/PhingFile.php';
  24. require_once 'phing/system/io/FileWriter.php';
  25. require_once 'phing/util/ExtendedFileStream.php';
  26.  
  27. /**
  28.  * Transform a PHPUnit2 xml report using XSLT.
  29.  * This transformation generates an html report in either framed or non-framed
  30.  * style. The non-framed style is convenient to have a concise report via mail, 
  31.  * the framed report is much more convenient if you want to browse into 
  32.  * different packages or testcases since it is a Javadoc like report.
  33.  *
  34.  * @author Michiel Rook <michiel.rook@gmail.com>
  35.  * @version $Id: PHPUnit2ReportTask.php 82 2006-07-07 18:15:35Z mrook $
  36.  * @package phing.tasks.ext.phpunit2
  37.  * @since 2.1.0
  38.  */
  39. class PHPUnit2ReportTask extends Task
  40. {
  41.     private $format = "noframes";
  42.     private $styleDir = "";
  43.     private $toDir = "";
  44.  
  45.     /** the directory where the results XML can be found */
  46.     private $inFile = "testsuites.xml";
  47.  
  48.     /**
  49.      * Set the filename of the XML results file to use.
  50.      */
  51.     function setInFile($inFile)
  52.     {
  53.         $this->inFile = $inFile;
  54.     }
  55.  
  56.     /**
  57.      * Set the format of the generated report. Must be noframes or frames.
  58.      */
  59.     function setFormat($format)
  60.     {
  61.         $this->format = $format;
  62.     }
  63.  
  64.     /**
  65.      * Set the directory where the stylesheets are located.
  66.      */
  67.     function setStyleDir($styleDir)
  68.     {
  69.         $this->styleDir = $styleDir;
  70.     }
  71.  
  72.     /**
  73.      * Set the directory where the files resulting from the 
  74.      * transformation should be written to.
  75.      */
  76.     function setToDir($toDir)
  77.     {
  78.         $this->toDir = $toDir;
  79.     }
  80.     
  81.     /**
  82.      * Returns the path to the XSL stylesheet
  83.      */
  84.     private function getStyleSheet()
  85.     {
  86.         $xslname = "phpunit2-" . $this->format . ".xsl";
  87.  
  88.         if ($this->styleDir)
  89.         {
  90.             $file = new PhingFile($this->styleDir, $xslname);
  91.         }
  92.         else
  93.         {
  94.             $path = Phing::getResourcePath("phing/etc/$xslname");
  95.             
  96.             if ($path === NULL)
  97.             {
  98.                 $path = Phing::getResourcePath("etc/$xslname");
  99.  
  100.                 if ($path === NULL)
  101.                 {
  102.                     throw new BuildException("Could not find $xslname in resource path");
  103.                 }
  104.             }
  105.             
  106.             $file = new PhingFile($path);
  107.         }
  108.  
  109.         if (!$file->exists())
  110.         {
  111.             throw new BuildException("Could not find file " . $file->getPath());
  112.         }
  113.  
  114.         return $file;
  115.     }
  116.     
  117.     /**
  118.      * Transforms the DOM document
  119.      */
  120.     private function transform(DOMDocument $document)
  121.     {
  122.         $dir = new PhingFile($this->toDir);
  123.         
  124.         if (!$dir->exists())
  125.         {
  126.             throw new BuildException("Directory '" . $this->toDir . "' does not exist");
  127.         }
  128.         
  129.         $xslfile = $this->getStyleSheet();
  130.  
  131.         $xsl = new DOMDocument();
  132.         $xsl->load($xslfile->getAbsolutePath());
  133.  
  134.         $proc = new XSLTProcessor();
  135.         $proc->importStyleSheet($xsl);
  136.  
  137.         if ($this->format == "noframes")
  138.         {
  139.             $writer = new FileWriter(new PhingFile($this->toDir, "phpunit2-noframes.html"));
  140.             $writer->write($proc->transformToXML($document));
  141.             $writer->close();
  142.         }
  143.         else
  144.         {
  145.             ExtendedFileStream::registerStream();
  146.  
  147.             // no output for the framed report
  148.             // it's all done by extension...
  149.             $dir = new PhingFile($this->toDir);
  150.             $proc->setParameter('', 'output.dir', $dir->getAbsolutePath());
  151.             $proc->transformToXML($document);
  152.         }
  153.     }
  154.     
  155.     /**
  156.      * Fixes 'testsuite' elements with no package attribute, adds
  157.      * package="default" to those elements.
  158.      */
  159.     private function fixPackages(DOMDocument $document)
  160.     {
  161.         $testsuites = $document->getElementsByTagName('testsuite');
  162.         
  163.         foreach ($testsuites as $testsuite)
  164.         {
  165.             if (!$testsuite->hasAttribute('package'))
  166.             {
  167.                 $testsuite->setAttribute('package', 'default');
  168.             }
  169.         }
  170.     }
  171.  
  172.     /**
  173.      * The main entry point
  174.      *
  175.      * @throws BuildException
  176.      */
  177.     public function main()
  178.     {
  179.         $testSuitesDoc = new DOMDocument();
  180.         $testSuitesDoc->load($this->inFile);
  181.         
  182.         $this->fixPackages($testSuitesDoc);
  183.         
  184.         $this->transform($testSuitesDoc);
  185.     }
  186. }
  187. ?>