Source for file collector.php

Documentation is available at collector.php

  1. <?php
  2. /**
  3.  * This file contains the following classes: {@link SimpleCollector},
  4.  * {@link SimplePatternCollector}.
  5.  *
  6.  * @author Travis Swicegood <development@domain51.com>
  7.  * @package SimpleTest
  8.  * @subpackage UnitTester
  9.  * @version $Id: collector.php 1723 2008-04-08 00:34:10Z lastcraft $
  10.  */
  11.  
  12. /**
  13.  * The basic collector for {@link GroupTest}
  14.  *
  15.  * @see collect(), GroupTest::collect()
  16.  * @package SimpleTest
  17.  * @subpackage UnitTester
  18.  */
  19. class SimpleCollector {
  20.  
  21.     /**
  22.      * Strips off any kind of slash at the end so as to normalise the path.
  23.      * @param string $path    Path to normalise.
  24.      * @return string         Path without trailing slash.
  25.      */
  26.     function _removeTrailingSlash($path{
  27.         if (substr($path-1== DIRECTORY_SEPARATOR{
  28.             return substr($path0-1);
  29.         elseif (substr($path-1== '/'{
  30.             return substr($path0-1);
  31.         else {
  32.             return $path;
  33.         }
  34.     }
  35.  
  36.     /**
  37.      * Scans the directory and adds what it can.
  38.      * @param object $test    Group test with {@link GroupTest::addTestFile()} method.
  39.      * @param string $path    Directory to scan.
  40.      * @see _attemptToAdd()
  41.      */
  42.     function collect(&$test$path{
  43.         $path $this->_removeTrailingSlash($path);
  44.         if ($handle opendir($path)) {
  45.             while (($entry readdir($handle)) !== false{
  46.                 if ($this->_isHidden($entry)) {
  47.                     continue;
  48.                 }
  49.                 $this->_handle($test$path DIRECTORY_SEPARATOR $entry);
  50.             }
  51.             closedir($handle);
  52.         }
  53.     }
  54.  
  55.     /**
  56.      * This method determines what should be done with a given file and adds
  57.      * it via {@link GroupTest::addTestFile()} if necessary.
  58.      *
  59.      * This method should be overriden to provide custom matching criteria,
  60.      * such as pattern matching, recursive matching, etc.  For an example, see
  61.      * {@link SimplePatternCollector::_handle()}.
  62.      *
  63.      * @param object $test      Group test with {@link GroupTest::addTestFile()} method.
  64.      * @param string $filename  A filename as generated by {@link collect()}
  65.      * @see collect()
  66.      * @access protected
  67.      */
  68.     function _handle(&$test$file{
  69.         if (is_dir($file)) {
  70.             return;
  71.         }
  72.         $test->addTestFile($file);
  73.     }
  74.     
  75.     /**
  76.      *  Tests for hidden files so as to skip them. Currently
  77.      *  only tests for Unix hidden files.
  78.      *  @param string $filename        Plain filename.
  79.      *  @return boolean                True if hidden file.
  80.      *  @access private
  81.      */
  82.     function _isHidden($filename{
  83.         return strncmp($filename'.'1== 0;
  84.     }
  85. }
  86.  
  87. /**
  88.  * An extension to {@link SimpleCollector} that only adds files matching a
  89.  * given pattern.
  90.  *
  91.  * @package SimpleTest
  92.  * @subpackage UnitTester
  93.  * @see SimpleCollector
  94.  */
  95.     var $_pattern;
  96.  
  97.     /**
  98.      *
  99.      * @param string $pattern   Perl compatible regex to test name against
  100.      *   See {@link http://us4.php.net/manual/en/reference.pcre.pattern.syntax.php PHP's PCRE}
  101.      *   for full documentation of valid pattern.s
  102.      */
  103.     function SimplePatternCollector($pattern '/php$/i'{
  104.         $this->_pattern = $pattern;
  105.     }
  106.  
  107.     /**
  108.      * Attempts to add files that match a given pattern.
  109.      *
  110.      * @see SimpleCollector::_handle()
  111.      * @param object $test    Group test with {@link GroupTest::addTestFile()} method.
  112.      * @param string $path    Directory to scan.
  113.      * @access protected
  114.      */
  115.     function _handle(&$test$filename{
  116.         if (preg_match($this->_pattern$filename)) {
  117.             parent::_handle($test$filename);
  118.         }
  119.     }
  120. }
  121. ?>

Documentation generated on Sun, 04 May 2008 09:21:21 -0500 by phpDocumentor 1.3.0