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 / phpunit / BatchTest.php next >
Encoding:
PHP Script  |  2007-08-15  |  4.4 KB  |  177 lines

  1. <?php
  2. /**
  3.  * $Id: BatchTest.php 219 2007-08-15 18:58:31Z 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/types/FileSet.php';
  23.  
  24. /**
  25.  * Scans a list of files given by the fileset attribute, extracts
  26.  * all subclasses of PHPUnit(2)_Framework_TestCase / PHPUnit(2)_Framework_TestSuite.
  27.  *
  28.  * @author Michiel Rook <michiel.rook@gmail.com>
  29.  * @version $Id: BatchTest.php 219 2007-08-15 18:58:31Z mrook $
  30.  * @package phing.tasks.ext.phpunit
  31.  * @since 2.1.0
  32.  */
  33. class BatchTest
  34. {
  35.     /** the list of filesets containing the testcase filename rules */
  36.     private $filesets = array();
  37.  
  38.     /** the reference to the project */
  39.     private $project = NULL;
  40.  
  41.     /** the classpath to use with Phing::__import() calls */
  42.     private $classpath = NULL;
  43.     
  44.     /** names of classes to exclude */
  45.     private $excludeClasses = array();
  46.     
  47.     /**
  48.      * Create a new batchtest instance
  49.      *
  50.      * @param Project the project it depends on.
  51.      */
  52.     function __construct(Project $project)
  53.     {
  54.         $this->project = $project;
  55.     }
  56.     
  57.     /**
  58.      * Sets the classes to exclude
  59.      */
  60.     function setExclude($exclude)
  61.     {
  62.         $this->excludeClasses = explode(" ", $exclude);
  63.     }
  64.  
  65.     /**
  66.      * Sets the classpath
  67.      */
  68.     function setClasspath(Path $classpath)
  69.     {
  70.         if ($this->classpath === null)
  71.         {
  72.             $this->classpath = $classpath;
  73.         }
  74.         else
  75.         {
  76.             $this->classpath->append($classpath);
  77.         }
  78.     }
  79.  
  80.     /**
  81.      * Creates a new Path object
  82.      */
  83.     function createClasspath()
  84.     {
  85.         $this->classpath = new Path();
  86.         return $this->classpath;
  87.     }
  88.  
  89.     /**
  90.      * Returns the classpath
  91.      */
  92.     function getClasspath()
  93.     {
  94.         return $this->classpath;
  95.     }
  96.  
  97.     /**
  98.      * Add a new fileset containing the XML results to aggregate
  99.      *
  100.      * @param FileSet the new fileset containing XML results.
  101.      */
  102.     function addFileSet(FileSet $fileset)
  103.     {
  104.         $this->filesets[] = $fileset;
  105.     }
  106.  
  107.     /**
  108.      * Iterate over all filesets and return the filename of all files.
  109.      *
  110.      * @return array an array of filenames
  111.      */
  112.     private function getFilenames()
  113.     {
  114.         $filenames = array();
  115.  
  116.         foreach ($this->filesets as $fileset)
  117.         {
  118.             $ds = $fileset->getDirectoryScanner($this->project);
  119.             $ds->scan();
  120.  
  121.             $files = $ds->getIncludedFiles();
  122.  
  123.             foreach ($files as $file)
  124.             {
  125.                 $filenames[] = $ds->getBaseDir() . "/" . $file;
  126.             }
  127.         }
  128.  
  129.         return $filenames;
  130.     }
  131.     
  132.     /**
  133.      * Checks wheter $input is a subclass of PHPUnit(2)_Framework_TestCasse
  134.      * or PHPUnit(2)_Framework_TestSuite
  135.      */
  136.     private function isTestCase($input)
  137.     {
  138.         return is_subclass_of($input, 'PHPUnit2_Framework_TestCase') || is_subclass_of($input, 'PHPUnit_Framework_TestCase')
  139.             || is_subclass_of($input, 'PHPUnit2_Framework_TestSuite') || is_subclass_of($input, 'PHPUnit_Framework_TestSuite');
  140.     }
  141.     
  142.     /**
  143.      * Filters an array of classes, removes all classes that are not test cases or test suites,
  144.      * or classes that are declared abstract
  145.      */
  146.     private function filterTests($input)
  147.     {
  148.         $reflect = new ReflectionClass($input);
  149.         
  150.         return $this->isTestCase($input) && (!$reflect->isAbstract());
  151.     }
  152.  
  153.     /**
  154.      * Returns an array of test cases and test suites that are declared
  155.      * by the files included by the filesets
  156.      *
  157.      * @return array an array of PHPUnit(2)_Framework_TestCase or PHPUnit(2)_Framework_TestSuite classes.
  158.      */
  159.     function elements()
  160.     {
  161.         $filenames = $this->getFilenames();
  162.         
  163.         $declaredClasses = array();        
  164.  
  165.         foreach ($filenames as $filename)
  166.         {
  167.             $definedClasses = PHPUnitUtil::getDefinedClasses($filename, $this->classpath);
  168.             
  169.             $declaredClasses = array_merge($declaredClasses, $definedClasses);
  170.         }
  171.         
  172.         $elements = array_filter($declaredClasses, array($this, "filterTests"));
  173.  
  174.         return $elements;
  175.     }
  176. }
  177. ?>