Changeset 42343 for trunk/tests/phpunit/includes/mock-fs.php
- Timestamp:
- 11/30/2017 11:09:33 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/includes/mock-fs.php
r41289 r42343 11 11 12 12 public $verbose = false; // Enable to debug WP_Filesystem_Base::find_folder() / etc. 13 public $errors = array();14 public $method = 'MockFS';13 public $errors = array(); 14 public $method = 'MockFS'; 15 15 16 16 function __construct() {} … … 22 22 // Copy of core's function, but accepts a path. 23 23 function abspath( $path = false ) { 24 if ( ! $path ) 24 if ( ! $path ) { 25 25 $path = ABSPATH; 26 } 26 27 $folder = $this->find_folder( $path ); 27 28 28 29 // Perhaps the FTP folder is rooted at the WordPress installation, Check for wp-includes folder in root, Could have some false positives, but rare. 29 if ( ! $folder && $this->is_dir( '/wp-includes') )30 if ( ! $folder && $this->is_dir( '/wp-includes' ) ) { 30 31 $folder = '/'; 32 } 31 33 return $folder; 32 34 } … … 39 41 */ 40 42 function init( $paths = '', $home_dir = '/' ) { 41 $this->fs = new MockFS_Directory_Node( '/' );43 $this->fs = new MockFS_Directory_Node( '/' ); 42 44 $this->fs_map = array( 43 45 '/' => $this->fs, 44 46 ); 45 $this->cache = array(); // Used by find_folder() and friends46 $this->cwd = isset( $this->fs_map[ $home_dir ] ) ? $this->fs_map[ $home_dir ] : '/';47 $this->cache = array(); // Used by find_folder() and friends 48 $this->cwd = isset( $this->fs_map[ $home_dir ] ) ? $this->fs_map[ $home_dir ] : '/'; 47 49 $this->setfs( $paths ); 48 50 } … … 52 54 */ 53 55 function setfs( $paths ) { 54 if ( ! is_array( $paths) )56 if ( ! is_array( $paths ) ) { 55 57 $paths = explode( "\n", $paths ); 58 } 56 59 57 60 $paths = array_filter( array_map( 'trim', $paths ) ); … … 59 62 foreach ( $paths as $path ) { 60 63 // Allow for comments 61 if ( '#' == $path[0] ) 62 continue; 64 if ( '#' == $path[0] ) { 65 continue; 66 } 63 67 64 68 // Directories 65 if ( '/' == $path[ strlen( $path) -1 ] )69 if ( '/' == $path[ strlen( $path ) - 1 ] ) { 66 70 $this->mkdir( $path ); 67 else// Files (with dummy content for now)71 } else { // Files (with dummy content for now) 68 72 $this->put_contents( $path, 'This is a test file' ); 73 } 69 74 } 70 75 … … 96 101 $this->mkdir( $dirname ); 97 102 $parent_node = $this->locate_parent_node( $path ); 98 if ( ! $parent_node ) 103 if ( ! $parent_node ) { 99 104 return false; 105 } 100 106 } 101 107 … … 103 109 104 110 $parent_node->children[ $node->name ] = $node; 105 $this->fs_map[ $path ] = $node;111 $this->fs_map[ $path ] = $node; 106 112 107 113 return true; … … 109 115 110 116 function put_contents( $path, $contents = '', $mode = null ) { 111 if ( ! $this->is_dir( dirname( $path ) ) ) 117 if ( ! $this->is_dir( dirname( $path ) ) ) { 112 118 $this->mkdir( dirname( $path ) ); 113 114 $parent = $this->locate_parent_node( $path ); 119 } 120 121 $parent = $this->locate_parent_node( $path ); 115 122 $new_file = new MockFS_File_Node( $path, $contents ); 116 123 117 124 $parent->children[ $new_file->name ] = $new_file; 118 $this->fs_map[ $path ] = $new_file;125 $this->fs_map[ $path ] = $new_file; 119 126 } 120 127 121 128 function get_contents( $file ) { 122 if ( ! $this->is_file( $file ) ) 129 if ( ! $this->is_file( $file ) ) { 123 130 return false; 131 } 124 132 return $this->fs_map[ $file ]->contents; 125 133 } … … 130 138 131 139 function chdir( $path ) { 132 if ( ! isset( $this->fs_map[ $path ] ) ) 140 if ( ! isset( $this->fs_map[ $path ] ) ) { 133 141 return false; 142 } 134 143 135 144 $this->cwd = $this->fs_map[ $path ]; … … 153 162 function dirlist( $path = '.', $include_hidden = true, $recursive = false ) { 154 163 155 if ( empty( $path ) || '.' == $path ) 164 if ( empty( $path ) || '.' == $path ) { 156 165 $path = $this->cwd(); 157 158 if ( ! $this->exists( $path ) ) 166 } 167 168 if ( ! $this->exists( $path ) ) { 159 169 return false; 170 } 160 171 161 172 $limit_file = false; 162 173 if ( $this->is_file( $path ) ) { 163 174 $limit_file = $this->locate_node( $path )->name; 164 $path = dirname( $path ) . '/';175 $path = dirname( $path ) . '/'; 165 176 } 166 177 167 178 $ret = array(); 168 179 foreach ( $this->fs_map[ $path ]->children as $entry ) { 169 if ( '.' == $entry->name || '..' == $entry->name ) 170 continue; 171 172 if ( ! $include_hidden && '.' == $entry->name ) 173 continue; 174 175 if ( $limit_file && $entry->name != $limit_file ) 176 continue; 177 178 $struc = array(); 180 if ( '.' == $entry->name || '..' == $entry->name ) { 181 continue; 182 } 183 184 if ( ! $include_hidden && '.' == $entry->name ) { 185 continue; 186 } 187 188 if ( $limit_file && $entry->name != $limit_file ) { 189 continue; 190 } 191 192 $struc = array(); 179 193 $struc['name'] = $entry->name; 180 194 $struc['type'] = $entry->type; 181 195 182 196 if ( 'd' == $struc['type'] ) { 183 if ( $recursive ) 197 if ( $recursive ) { 184 198 $struc['files'] = $this->dirlist( trailingslashit( $path ) . trailingslashit( $struc['name'] ), $include_hidden, $recursive ); 185 else199 } else { 186 200 $struc['files'] = array(); 201 } 187 202 } 188 203 … … 214 229 215 230 class MockFS_Directory_Node extends MockFS_Node { 216 public $type = 'd';231 public $type = 'd'; 217 232 public $children = array(); // The child nodes of this directory 218 233 } 219 234 220 235 class MockFS_File_Node extends MockFS_Node { 221 public $type = 'f';236 public $type = 'f'; 222 237 public $contents = ''; // The contents of the file 223 238
Note: See TracChangeset
for help on using the changeset viewer.