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 / PHPUnit2Task.php < prev    next >
Encoding:
PHP Script  |  2007-12-21  |  6.1 KB  |  239 lines

  1. <?php
  2. /**
  3.  * $Id: PHPUnit2Task.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/Writer.php';
  25. require_once 'phing/util/LogWriter.php';
  26.  
  27. /**
  28.  * Runs PHPUnit2 tests.
  29.  *
  30.  * @author Michiel Rook <michiel.rook@gmail.com>
  31.  * @version $Id: PHPUnit2Task.php 82 2006-07-07 18:15:35Z mrook $
  32.  * @package phing.tasks.ext.phpunit2
  33.  * @see BatchTest
  34.  * @since 2.1.0
  35.  */
  36. class PHPUnit2Task extends Task
  37. {
  38.     private $batchtests = array();
  39.     private $formatters = array();
  40.     private $haltonerror = false;
  41.     private $haltonfailure = false;
  42.     private $failureproperty;
  43.     private $errorproperty;
  44.     private $printsummary = false;
  45.     private $testfailed = false;
  46.     private $codecoverage = false;
  47.  
  48.     /**
  49.      * Initialize Task.
  50.       * This method includes any necessary PHPUnit2 libraries and triggers
  51.      * appropriate error if they cannot be found.  This is not done in header
  52.      * because we may want this class to be loaded w/o triggering an error.
  53.      */
  54.     function init() {
  55.         include_once 'PHPUnit2/Util/Filter.php';
  56.         if (!class_exists('PHPUnit2_Util_Filter')) {
  57.             throw new BuildException("PHPUnit2Task depends on PEAR PHPUnit2 package being installed.", $this->getLocation());
  58.         }
  59.         
  60.         if (version_compare(PHP_VERSION, '5.0.3') < 0) {
  61.             throw new BuildException("PHPUnit2Task requires PHP version >= 5.0.3.", $this->getLocation());
  62.         }
  63.  
  64.         // other dependencies that should only be loaded when class is actually used.
  65.         require_once 'phing/tasks/ext/phpunit2/PHPUnit2TestRunner.php';
  66.         require_once 'phing/tasks/ext/phpunit2/BatchTest.php';
  67.         require_once 'phing/tasks/ext/phpunit2/FormatterElement.php';
  68.         require_once 'phing/tasks/ext/phpunit2/SummaryPHPUnit2ResultFormatter.php';
  69.  
  70.         // add some defaults to the PHPUnit2 Filter
  71.         PHPUnit2_Util_Filter::addFileToFilter('PHPUnit2Task.php');
  72.         PHPUnit2_Util_Filter::addFileToFilter('PHPUnit2TestRunner.php');
  73.         PHPUnit2_Util_Filter::addFileToFilter('phing/Task.php');
  74.         PHPUnit2_Util_Filter::addFileToFilter('phing/Target.php');
  75.         PHPUnit2_Util_Filter::addFileToFilter('phing/Project.php');
  76.         PHPUnit2_Util_Filter::addFileToFilter('phing/Phing.php');
  77.         PHPUnit2_Util_Filter::addFileToFilter('phing.php');
  78.  
  79.     }
  80.     
  81.     function setFailureproperty($value)
  82.     {
  83.         $this->failureproperty = $value;
  84.     }
  85.     
  86.     function setErrorproperty($value)
  87.     {
  88.         $this->errorproperty = $value;
  89.     }
  90.     
  91.     function setHaltonerror($value)
  92.     {
  93.         $this->haltonerror = $value;
  94.     }
  95.  
  96.     function setHaltonfailure($value)
  97.     {
  98.         $this->haltonfailure = $value;
  99.     }
  100.  
  101.     function setPrintsummary($printsummary)
  102.     {
  103.         $this->printsummary = $printsummary;
  104.     }
  105.     
  106.     function setCodecoverage($codecoverage)
  107.     {
  108.         $this->codecoverage = $codecoverage;
  109.     }
  110.  
  111.     /**
  112.      * Add a new formatter to all tests of this task.
  113.      *
  114.      * @param FormatterElement formatter element
  115.      */
  116.     function addFormatter(FormatterElement $fe)
  117.     {
  118.         $this->formatters[] = $fe;
  119.     }
  120.  
  121.     /**
  122.      * The main entry point
  123.      *
  124.      * @throws BuildException
  125.      */
  126.     function main()
  127.     {
  128.         $tests = array();
  129.         
  130.         if ($this->printsummary)
  131.         {
  132.             $fe = new FormatterElement();
  133.             $fe->setClassName('SummaryPHPUnit2ResultFormatter');
  134.             $fe->setUseFile(false);
  135.             $this->formatters[] = $fe;
  136.         }
  137.         
  138.         foreach ($this->batchtests as $batchtest)
  139.         {
  140.             $tests = array_merge($tests, $batchtest->elements());
  141.         }            
  142.         
  143.         foreach ($this->formatters as $fe)
  144.         {
  145.             $formatter = $fe->getFormatter();            
  146.             $formatter->setProject($this->getProject());
  147.  
  148.             if ($fe->getUseFile())
  149.             {
  150.                 $destFile = new PhingFile($fe->getToDir(), $fe->getOutfile());
  151.                 
  152.                 $writer = new FileWriter($destFile->getAbsolutePath());
  153.  
  154.                 $formatter->setOutput($writer);
  155.             }
  156.             else
  157.             {
  158.                 $formatter->setOutput($this->getDefaultOutput());
  159.             }
  160.  
  161.             $formatter->startTestRun();
  162.         }
  163.         
  164.         foreach ($tests as $test)
  165.         {
  166.             $this->execute(new PHPUnit2_Framework_TestSuite(new ReflectionClass($test)));
  167.         }
  168.  
  169.         foreach ($this->formatters as $fe)
  170.         {
  171.             $formatter = $fe->getFormatter();
  172.             $formatter->endTestRun();
  173.         }
  174.         
  175.         if ($this->testfailed)
  176.         {
  177.             throw new BuildException("One or more tests failed");
  178.         }
  179.     }
  180.  
  181.     /**
  182.      * @throws BuildException
  183.      */
  184.     private function execute($suite)
  185.     {
  186.         $runner = new PHPUnit2TestRunner($suite, $this->project);
  187.         
  188.         $runner->setCodecoverage($this->codecoverage);
  189.  
  190.         foreach ($this->formatters as $fe)
  191.         {
  192.             $formatter = $fe->getFormatter();
  193.  
  194.             $runner->addFormatter($formatter);
  195.         }
  196.  
  197.         $runner->run();
  198.  
  199.         $retcode = $runner->getRetCode();
  200.         
  201.         if ($retcode == PHPUnit2TestRunner::ERRORS) {
  202.             if ($this->errorproperty) {
  203.                 $this->project->setNewProperty($this->errorproperty, true);
  204.             }
  205.             if ($this->haltonerror) {
  206.                 $this->testfailed = true;
  207.             }
  208.         } elseif ($retcode == PHPUnit2TestRunner::FAILURES) {
  209.             if ($this->failureproperty) {
  210.                 $this->project->setNewProperty($this->failureproperty, true);
  211.             }
  212.             
  213.             if ($this->haltonfailure) {
  214.                 $this->testfailed = true;
  215.             }
  216.         }
  217.         
  218.     }
  219.  
  220.     private function getDefaultOutput()
  221.     {
  222.         return new LogWriter($this);
  223.     }
  224.  
  225.     /**
  226.      * Adds a set of tests based on pattern matching.
  227.      *
  228.      * @return BatchTest a new instance of a batch test.
  229.      */
  230.     function createBatchTest()
  231.     {
  232.         $batchtest = new BatchTest($this->getProject());
  233.  
  234.         $this->batchtests[] = $batchtest;
  235.  
  236.         return $batchtest;
  237.     }
  238. }
  239. ?>