PHPUnit
[ class tree: PHPUnit ] [ index: PHPUnit ] [ all elements ]

Source for file SetupDecorator.php

Documentation is available at SetupDecorator.php

  1. <?php
  2. //
  3. // +------------------------------------------------------------------------+
  4. // | PEAR :: PHPUnit                                                        |
  5. // +------------------------------------------------------------------------+
  6. // | Copyright (c) 2002-2005 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.11 2005/01/28 05:57:49 sebastian 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.  * <code>
  24.  * <?php
  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.  * ?>
  29.  * </code>
  30.  *
  31.  * The example calls this class and tells it to:
  32.  *
  33.  *   - find all file under the directory /path/to/dir/tests
  34.  *   - for files, which end with '.php' (this is a piece of a regexp, that's why the . is escaped)
  35.  *   - and to exclude the files 'index.php' and 'sql.php'
  36.  *   - and include all the files that are left in the tests.
  37.  *
  38.  * Given that the path (the first parameter) ends with 'tests' it will be assumed
  39.  * that the classes are named tests_* where * is the directory plus the filename,
  40.  * according to PEAR standards.
  41.  *
  42.  * So that:
  43.  *
  44.  *   - 'testMe.php' in the dir 'tests' bill be assumed to contain a class tests_testMe
  45.  *   - '/moretests/aTest.php' should contain a class 'tests_moretests_aTest'
  46.  *
  47.  * @author      Wolfram Kriesing <wolfram@kriesing.de>
  48.  * @license     http://www.php.net/license/3_0.txt The PHP License, Version 3.0
  49.  * @category    Testing
  50.  * @package     PHPUnit
  51.  * @subpackage  GUI
  52.  */
  53. {
  54.     /**
  55.     *
  56.     *
  57.     */
  58.     function PHPUnit_GUI_SetupDecorator(&$gui)
  59.     {
  60.         $this->_gui $gui;
  61.     }
  62.  
  63.     /**
  64.     *   just forwarding the action to the decorated class.
  65.     *
  66.     */
  67.     function show($showPassed=TRUE)
  68.     {
  69.         $this->_gui->show($showPassed);
  70.     }
  71.  
  72.     /**
  73.     * Setup test suites that can be found in the given directory
  74.     * Using the second parameter you can also choose a subsets of the files found
  75.     * in the given directory. I.e. only all the files that contain '_UnitTest_',
  76.     * in order to do this simply call it like this:
  77.     * <code>getSuitesFromDir($dir,'.*_UnitTest_.*')</code>.
  78.     * There you can already see that the pattern is built for the use within a regular expression.
  79.     *
  80.     * @param  string  the directory where to search for test-suite files
  81.     * @param  string  the pattern (a regexp) by which to find the files
  82.     * @param  array   an array of file names that shall be excluded
  83.     */
  84.     function getSuitesFromDir($dir$filenamePattern ''$exclude = array())
  85.     {
  86.         if ($dir{strlen($dir)-1== DIRECTORY_SEPARATOR{
  87.             $dir substr($dir0-1);
  88.         }
  89.  
  90.         $files $this->_getFiles($dir$filenamePattern$excluderealpath($dir '/..'));
  91.         asort($files);
  92.  
  93.         foreach ($files as $className => $aFile{
  94.             include_once($aFile);
  95.  
  96.             if (class_exists($className)) {
  97.                 $suites[=new PHPUnit_TestSuite($className);
  98.             else {
  99.                 trigger_error("$className could not be found in $dir$aFile!");
  100.             }
  101.         }
  102.  
  103.         $this->_gui->addSuites($suites);
  104.     }
  105.  
  106.     /**
  107.     * This method searches recursively through the directories
  108.     * to find all the files that shall be added to the be visible.
  109.     *
  110.     * @param  string  the path where find the files
  111.     * @param  srting  the string pattern by which to find the files
  112.     * @param  string  the file names to be excluded
  113.     * @param  string  the root directory, which serves as the prefix to the fully qualified filename
  114.     * @access private
  115.     */
  116.     function _getFiles($dir$filenamePattern$exclude$rootDir)
  117.     {
  118.         $files = array();
  119.  
  120.         if ($dp opendir($dir)) {
  121.             while (FALSE !== ($file readdir($dp))) {
  122.                 $filename $dir . DIRECTORY_SEPARATOR . $file;
  123.                 $match    = TRUE;
  124.  
  125.                 if ($filenamePattern && !preg_match("~$filenamePattern~"$file)) {
  126.                     $match = FALSE;
  127.                 }
  128.  
  129.                 if (sizeof($exclude)) {
  130.                     foreach ($exclude as $aExclude{
  131.                         if (strpos($file$aExclude!== FALSE{
  132.                             $match = FALSE;
  133.                             break;
  134.                         }
  135.                     }
  136.                 }
  137.  
  138.                 if (is_file($filename&& $match{
  139.                     $tmp str_replace($rootDir''$filename);
  140.  
  141.                     if (strpos($tmpDIRECTORY_SEPARATOR=== 0{
  142.                         $tmp substr($tmp1);
  143.                     }
  144.  
  145.                     if (strpos($tmp'/'=== 0{
  146.                         $tmp substr($tmp1);
  147.                     }
  148.  
  149.                     $className str_replace(DIRECTORY_SEPARATOR'_'$tmp);
  150.                     $className basename($className'.php');
  151.  
  152.                     $files[$className$filename;
  153.                 }
  154.  
  155.                 if ($file != '.' && $file != '..' && is_dir($filename)) {
  156.                     $files array_merge($files$this->_getFiles($filename$filenamePattern$exclude$rootDir));
  157.                 }
  158.             }
  159.  
  160.             closedir($dp);
  161.         }
  162.  
  163.         return $files;
  164.     }
  165. }
  166. ?>

Documentation generated on Mon, 11 Mar 2019 14:22:35 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.