home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / SetupDecorator.php < prev    next >
Encoding:
PHP Script  |  2004-03-24  |  5.5 KB  |  139 lines

  1. <?php
  2. //
  3. // +------------------------------------------------------------------------+
  4. // | PEAR :: PHPUnit                                                        |
  5. // +------------------------------------------------------------------------+
  6. // | Copyright (c) 2002-2003 Sebastian Bergmann <sb@sebastian-bergmann.de>. |
  7. // +------------------------------------------------------------------------+
  8. // | This source file is subject to version 3.00 of the PHP License,        |
  9. // | that is available at http://www.php.net/license/3_0.txt.               |
  10. // | If you did not receive a copy of the PHP license and are unable to     |
  11. // | obtain it through the world-wide-web, please send a note to            |
  12. // | license@php.net so we can mail you a copy immediately.                 |
  13. // +------------------------------------------------------------------------+
  14. //
  15. // $Id: SetupDecorator.php,v 1.3 2003/06/09 17:22:33 quipo Exp $
  16. //
  17.  
  18. /**
  19. *   This decorator actually just adds the functionality to read the
  20. *   test-suite classes from a given directory and instanciate them
  21. *   automatically, use it as given in the example below.
  22. *
  23. *   usage example
  24. *   <code>
  25. *   $gui = new PHPUnit_GUI_SetupDecorator(new PHPUnit_GUI_HTML());
  26. *   $gui->getSuitesFromDir('/path/to/dir/tests','.*\.php$',array('index.php','sql.php'));
  27. *   $gui->show();
  28. *   </code>
  29. *   The example calls this class and tells it to:
  30. *       find all file under the directory /path/to/dir/tests
  31. *       for files, which end with '.php' (this is a piece of a regexp, that's why the . is escaped)
  32. *       and to exclude the files 'index.php' and 'sql.php'
  33. *   and include all the files that are left in the tests.
  34. *   Given that the path (the first parameter) ends with 'tests' it will be assumed
  35. *   that the classes are named tests_* where * is the directory plus the filename,
  36. *   according to PEAR standards.
  37. *   So that:
  38. *       'testMe.php' in the dir 'tests' bill be assumed to contain a class tests_testMe
  39. *       '/moretests/aTest.php' should contain a class 'tests_moretests_aTest'
  40. */
  41. class PHPUnit_GUI_SetupDecorator
  42. {
  43.     /**
  44.     *
  45.     *
  46.     */
  47.     function PHPUnit_GUI_SetupDecorator(&$gui)
  48.     {
  49.         $this->_gui = $gui;
  50.     }
  51.  
  52.     /**
  53.     *   just forwarding the action to the decorated class.
  54.     *
  55.     */
  56.     function show($showPassed=true)
  57.     {
  58.         $this->_gui->show($showPassed);
  59.     }
  60.  
  61.     /**
  62.     *   Setup test suites that can be found in the given directory
  63.     *   Using the second parameter you can also choose a subsets of the files found
  64.     *   in the given directory. I.e. only all the files that contain '_UnitTest_',
  65.     *   in order to do this simply call it like this:
  66.     *   <code>getSuitesFromDir($dir,'.*_UnitTest_.*')</code>.
  67.     *   There you can already see that the pattern is built for the use within a regular expression.
  68.     *
  69.     *   @param  string  the directory where to search for test-suite files
  70.     *   @param  string  the pattern (a regexp) by which to find the files
  71.     *   @param  array   an array of file names that shall be excluded
  72.     *
  73.     */
  74.     function getSuitesFromDir($dir,$filenamePattern='',$exclude=array())
  75.     {
  76.         // remove trailing DIRECTORY_SEPERATOR if missing
  77.         if ($dir{strlen($dir)-1} == DIRECTORY_SEPARATOR) {
  78.             $dir = substr($dir,0,-1);
  79.         }
  80.  
  81.         $files = $this->_getFiles($dir,$filenamePattern,$exclude,realpath($dir.'/..'));
  82.         asort($files);
  83.         foreach ($files as $className=>$aFile) {
  84.             include_once($aFile);
  85.             if (class_exists($className)) {
  86.                 $suites[] =& new PHPUnit_TestSuite($className);
  87.             } else {
  88.                 trigger_error("$className could not be found in $dir$aFile!");
  89.             }
  90.         }
  91.  
  92.         $this->_gui->addSuites($suites);
  93.     }
  94.  
  95.     /**
  96.     *   This method searches recursively through the directories
  97.     *   to find all the files that shall be added to the be visible.
  98.     *
  99.     *   @access private
  100.     *   @param  string  the path where find the files
  101.     *   @param  srting  the string pattern by which to find the files
  102.     *   @param  string  the file names to be excluded
  103.     *   @param  string  the root directory, which serves as the prefix to the fully qualified filename
  104.     */
  105.     function _getFiles($dir,$filenamePattern,$exclude,$rootDir)
  106.     {
  107.         $files = array();
  108.         if ($dp=opendir($dir)) {
  109.             while (false!==($file=readdir($dp))) {
  110.                 $filename = $dir.DIRECTORY_SEPARATOR.$file;
  111.                 $match = true;
  112.                 if ($filenamePattern && !preg_match("~$filenamePattern~",$file)) {
  113.                     $match = false;
  114.                 }
  115.                 if (sizeof($exclude)) {
  116.                     foreach ($exclude as $aExclude) {
  117.                         if (strpos($file,$aExclude)!==false) {
  118.                             $match = false;
  119.                             break;
  120.                         }
  121.                     }
  122.                 }
  123.                 if (is_file($filename) && $match) {
  124.                     $className = str_replace(DIRECTORY_SEPARATOR, '_', substr(str_replace($rootDir, '', $filename), 1));
  125.                     $className = basename($className,'.php');   // remove php-extension
  126.                     $files[$className] = $filename;
  127.                 }
  128.                 if ($file!='.' && $file!='..' && is_dir($filename)) {
  129.                     $files = array_merge($files,$this->_getFiles($filename,$filenamePattern,$exclude,$rootDir));
  130.                 }
  131.             }
  132.             closedir($dp);
  133.         }
  134.         return $files;
  135.     }
  136. }
  137.  
  138. ?>
  139.