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 / simpletest / SimpleTestTask.php < prev   
Encoding:
PHP Script  |  2006-07-06  |  5.5 KB  |  238 lines

  1. <?php
  2. /**
  3.  * $Id: SimpleTestTask.php 81 2006-07-06 13:07:29Z 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/Writer.php';
  25. require_once 'phing/util/LogWriter.php';
  26.  
  27. /**
  28.  * Runs SimpleTest tests.
  29.  *
  30.  * @author Michiel Rook <michiel.rook@gmail.com>
  31.  * @version $Id: SimpleTestTask.php 81 2006-07-06 13:07:29Z mrook $
  32.  * @package phing.tasks.ext.simpletest
  33.  * @since 2.2.0
  34.  */
  35. class SimpleTestTask extends Task
  36. {
  37.     private $formatters = array();
  38.     private $haltonerror = false;
  39.     private $haltonfailure = false;
  40.     private $failureproperty;
  41.     private $errorproperty;
  42.     private $printsummary = false;
  43.     private $testfailed = false;
  44.  
  45.     /**
  46.      * Initialize Task.
  47.       * This method includes any necessary SimpleTest libraries and triggers
  48.      * appropriate error if they cannot be found.  This is not done in header
  49.      * because we may want this class to be loaded w/o triggering an error.
  50.      */
  51.     function init() {
  52.         @include_once 'simpletest/scorer.php';
  53.         
  54.         if (!class_exists('SimpleReporter')) {
  55.             throw new BuildException("SimpleTestTask depends on SimpleTest package being installed.", $this->getLocation());
  56.         }
  57.         
  58.         require_once 'simpletest/reporter.php';
  59.         require_once 'simpletest/xml.php';
  60.         require_once 'simpletest/test_case.php';
  61.         require_once 'phing/tasks/ext/simpletest/SimpleTestCountResultFormatter.php';
  62.         require_once 'phing/tasks/ext/simpletest/SimpleTestFormatterElement.php';
  63.     }
  64.     
  65.     function setFailureproperty($value)
  66.     {
  67.         $this->failureproperty = $value;
  68.     }
  69.     
  70.     function setErrorproperty($value)
  71.     {
  72.         $this->errorproperty = $value;
  73.     }
  74.     
  75.     function setHaltonerror($value)
  76.     {
  77.         $this->haltonerror = $value;
  78.     }
  79.  
  80.     function setHaltonfailure($value)
  81.     {
  82.         $this->haltonfailure = $value;
  83.     }
  84.  
  85.     function setPrintsummary($printsummary)
  86.     {
  87.         $this->printsummary = $printsummary;
  88.     }
  89.     
  90.     /**
  91.      * Add a new formatter to all tests of this task.
  92.      *
  93.      * @param SimpleTestFormatterElement formatter element
  94.      */
  95.     function addFormatter(SimpleTestFormatterElement $fe)
  96.     {
  97.         $this->formatters[] = $fe;
  98.     }
  99.  
  100.     /**
  101.      * Add a new fileset containing the XML results to aggregate
  102.      *
  103.      * @param FileSet the new fileset containing XML results.
  104.      */
  105.     function addFileSet(FileSet $fileset)
  106.     {
  107.         $this->filesets[] = $fileset;
  108.     }
  109.  
  110.     /**
  111.      * Iterate over all filesets and return the filename of all files
  112.      * that end with .php.
  113.      *
  114.      * @return array an array of filenames
  115.      */
  116.     private function getFilenames()
  117.     {
  118.         $filenames = array();
  119.  
  120.         foreach ($this->filesets as $fileset)
  121.         {
  122.             $ds = $fileset->getDirectoryScanner($this->project);
  123.             $ds->scan();
  124.  
  125.             $files = $ds->getIncludedFiles();
  126.  
  127.             foreach ($files as $file)
  128.             {
  129.                 if (strstr($file, ".php"))
  130.                 {
  131.                     $filenames[] = $ds->getBaseDir() . "/" . $file;
  132.                 }
  133.             }
  134.         }
  135.  
  136.         return $filenames;
  137.     }
  138.  
  139.     /**
  140.      * The main entry point
  141.      *
  142.      * @throws BuildException
  143.      */
  144.     function main()
  145.     {
  146.         $group = new GroupTest();
  147.         
  148.         $filenames = $this->getFilenames();
  149.         
  150.         foreach ($filenames as $testfile)
  151.         {
  152.             $group->addTestFile($testfile);
  153.         }
  154.         
  155.         if ($this->printsummary)
  156.         {
  157.             $fe = new SimpleTestFormatterElement();
  158.             $fe->setType('summary');
  159.             $fe->setUseFile(false);
  160.             $this->formatters[] = $fe;
  161.         }
  162.         
  163.         foreach ($this->formatters as $fe)
  164.         {
  165.             $formatter = $fe->getFormatter();
  166.             $formatter->setProject($this->getProject());
  167.  
  168.             if ($fe->getUseFile())
  169.             {
  170.                 $destFile = new PhingFile($fe->getToDir(), $fe->getOutfile());
  171.                 
  172.                 $writer = new FileWriter($destFile->getAbsolutePath());
  173.  
  174.                 $formatter->setOutput($writer);
  175.             }
  176.             else
  177.             {
  178.                 $formatter->setOutput($this->getDefaultOutput());
  179.             }
  180.         }
  181.         
  182.         $this->execute($group);
  183.         
  184.         if ($this->testfailed)
  185.         {
  186.             throw new BuildException("One or more tests failed");
  187.         }
  188.     }
  189.     
  190.     private function execute($suite)
  191.     {
  192.         $counter = new SimpleTestCountResultFormatter();
  193.         $reporter = new MultipleReporter();
  194.         $reporter->attachReporter($counter);
  195.         
  196.         foreach ($this->formatters as $fe)
  197.         {
  198.             $formatter = $fe->getFormatter();
  199.  
  200.             $reporter->attachReporter($formatter);
  201.         }        
  202.         
  203.         $suite->run($reporter);
  204.         
  205.         $retcode = $counter->getRetCode();
  206.         
  207.         if ($retcode == SimpleTestCountResultFormatter::ERRORS)
  208.         {
  209.             if ($this->errorproperty)
  210.             {
  211.                 $this->project->setNewProperty($this->errorproperty, true);
  212.             }
  213.             
  214.             if ($this->haltonerror)
  215.             {
  216.                 $this->testfailed = true;
  217.             }
  218.         }
  219.         elseif ($retcode == SimpleTestCountResultFormatter::FAILURES)
  220.         {
  221.             if ($this->failureproperty)
  222.             {
  223.                 $this->project->setNewProperty($this->failureproperty, true);
  224.             }
  225.             
  226.             if ($this->haltonfailure)
  227.             {
  228.                 $this->testfailed = true;
  229.             }
  230.         }
  231.     }
  232.  
  233.     private function getDefaultOutput()
  234.     {
  235.         return new LogWriter($this);
  236.     }
  237. }
  238. ?>