Changeset 893

Show
Ignore:
Timestamp:
09/05/07 00:42:55 (1 year ago)
Author:
mikey
Message:

finished SimpleTestStreamDirectory?, added unit test

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/main/php/org/simpletest/extensions/streamwrapper/directory.php

    r892 r893  
    44 * 
    55 * @author      Frank Kleine <mikey@stubbles.net> 
    6  * @package        SimpleTest 
    7  * @subpackage StreamWrapper 
     6 * @package     SimpleTest 
     7 * @subpackage  StreamWrapper 
    88 */ 
    99require_once dirname(__FILE__) . '/abstractcontent.php'; 
     
    1212 * Directory container. 
    1313 * 
    14  * @package        SimpleTest 
    15  * @subpackage StreamWrapper 
     14 * @package     SimpleTest 
     15 * @subpackage  StreamWrapper 
    1616 */ 
    17 class SimpleTestStreamDirectory extends SimpleTestStreamAbstractContent 
     17class SimpleTestStreamDirectory extends SimpleTestStreamAbstractContent implements Iterator 
    1818{ 
    1919    /** 
     
    2323     */ 
    2424    protected $children = array(); 
    25     /** 
    26      * pointer for iterating 
    27      * 
    28      * @var  int 
    29      */ 
    30     protected $pointer  = 0; 
    3125 
    3226    /** 
     
    3731    public function __construct($name) 
    3832    { 
     33        if (strstr($name, '/') !== false) { 
     34            throw new SimpleTestStreamException('Directory name can not contain /.'); 
     35        } 
     36         
    3937        $this->type = SimpleTestStreamContent::TYPE_DIR; 
    4038        parent::__construct($name); 
     39    } 
     40 
     41    /** 
     42     * factory method to create a directory structure for a given string 
     43     * 
     44     * @param   string  $dir 
     45     * @return  SimpleTestStreamDirectory 
     46     */ 
     47    public static function create($dir) 
     48    { 
     49        if ('/' === $dir{0}) { 
     50            $dir = substr($dir, 1); 
     51        } 
     52         
     53        $dirParts = explode('/', $dir); 
     54        $content = new SimpleTestStreamDirectory($dirParts[0]); 
     55        if (count($dirParts) > 1) { 
     56            $content->addChild(self::create(self::getChildName($dir, $dirParts[0]))); 
     57        } 
     58         
     59        return $content; 
     60    } 
     61 
     62    /** 
     63     * renames the content 
     64     * 
     65     * @param  string  $newName 
     66     */ 
     67    public function rename($newName) 
     68    { 
     69        if (strstr($newName, '/') !== false) { 
     70            throw new SimpleTestStreamException('Directory name can not contain /.'); 
     71        } 
     72         
     73        parent::rename($newName); 
    4174    } 
    4275 
     
    79112    public function hasChild($name) 
    80113    { 
    81         if ($this->appliesTo($childName) === false) { 
    82             return false; 
    83         } 
    84          
    85         if ($this->name === $name) { 
    86             return true; 
    87         } 
    88          
    89         $childName = $this->getChildName($name); 
    90         foreach ($this->children as $child) { 
    91             if ($child->appliesTo($childName) == false) { 
    92                 continue; 
    93             } 
    94              
    95             if ($child->getName() === $childName || $child->hasChild($childName) === true) { 
    96                 return true; 
    97             } 
    98         } 
    99          
    100         return false; 
    101     } 
    102  
    103     /** 
    104      * returns the child with the given name 
    105      * 
    106      * @param   string  $name 
    107      * @return  SimpleTestStreamContent 
    108      */ 
    109     public function getChild($name) 
    110     { 
    111         if ($this->name === $name) { 
    112             return $this; 
    113         } 
    114          
    115         $childName = $this->getChildName($name); 
     114        if ($this->appliesTo($name) === true) { 
     115            $childName = self::getChildName($name, $this->name); 
     116        } else { 
     117            $childName = $name; 
     118        } 
     119         
    116120        foreach ($this->children as $child) { 
    117121            if ($child->appliesTo($childName) === false) { 
     
    119123            } 
    120124             
     125            if ($child->getName() === $childName || $child->hasChild($childName) === true) { 
     126                return true; 
     127            } 
     128        } 
     129         
     130        return false; 
     131    } 
     132 
     133    /** 
     134     * returns the child with the given name 
     135     * 
     136     * @param   string  $name 
     137     * @return  SimpleTestStreamContent 
     138     */ 
     139    public function getChild($name) 
     140    { 
     141        if ($this->appliesTo($name) === true) { 
     142            $childName = self::getChildName($name, $this->name); 
     143        } else { 
     144            $childName = $name; 
     145        } 
     146         
     147        foreach ($this->children as $child) { 
     148            if ($child->appliesTo($childName) === false) { 
     149                continue; 
     150            } 
     151             
    121152            if ($child->getName() === $childName) { 
    122                 return $content
    123             } 
    124              
    125             if ($child->hasContent($childName) === true) { 
    126                 return $child->getContent($childName); 
    127             } 
    128         } 
    129          
    130         throw new SimpleTestStreamException('Content ' . $name . ' is not available.')
     153                return $child
     154            } 
     155             
     156            if ($child->hasChild($childName) === true) { 
     157                return $child->getChild($childName); 
     158            } 
     159        } 
     160         
     161        return null
    131162    } 
    132163 
     
    137168     * @return  string 
    138169     */ 
    139     protected function getChildName($name) 
    140     { 
    141         return substr($name, strlen($this->name)); 
    142     } 
    143  
    144     /** 
    145      * returns a list of childs for this directory 
     170    protected static function getChildName($name, $ownName) 
     171    { 
     172        return substr($name, strlen($ownName) + 1); 
     173    } 
     174 
     175    /** 
     176     * returns a list of children for this directory 
    146177     * 
    147178     * @return  array<SimpleTestStreamContent> 
    148179     */ 
    149     public function getChilds() 
     180    public function getChildren() 
    150181    { 
    151182        return $this->children; 
     
    161192 
    162193    /** 
    163      * returns the next child 
     194     * returns the current child 
    164195     * 
    165196     * @return  SimpleTestStreamContent 
    166197     */ 
     198    public function current() 
     199    { 
     200        $child = current($this->children); 
     201        if (false === $child) { 
     202            return null; 
     203        } 
     204         
     205        return $child; 
     206    } 
     207 
     208    /** 
     209     * returns the name of the current child 
     210     * 
     211     * @return  string 
     212     */ 
     213    public function key() 
     214    { 
     215        $child = current($this->children); 
     216        if (false === $child) { 
     217            return null; 
     218        } 
     219         
     220        return $child->getName(); 
     221    } 
     222 
     223    /** 
     224     * iterates to next child 
     225     */ 
    167226    public function next() 
    168227    { 
    169         $next = next($this->children); 
    170         if (false === $next) { 
    171             return null; 
    172         } 
    173          
    174         return $next; 
     228        next($this->children); 
     229    } 
     230 
     231    /** 
     232     * checks if the current value is valid 
     233     * 
     234     * @return  bool 
     235     */ 
     236    public function valid() 
     237    { 
     238        return (false !== current($this->children)); 
    175239    } 
    176240}