1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
|
--TEST--
basename
--CREDITS--
Dave Kelsey <d_kelsey@uk.ibm.com>
--SKIPIF--
<?php
if (substr(PHP_OS, 0, 3) != 'WIN') {
die('skip Windows only basename tests');
}
?>
--FILE--
<?php
/*
* proto string basename(string path [, string suffix])
* Function is implemented in ext/standard/string.c
*/
$file_paths = array (
/* simple paths */
array("bar"),
array("\\foo\\bar"),
array("foo\\bar"),
array("\\bar"),
/* simple paths with trailing slashes */
array("bar\\"),
array("\\bar\\"),
array("\\foo\\bar\\"),
array("foo\\bar\\"),
array("\\bar\\"),
/* paths with suffix removal */
array("bar.zip", ".zip"),
array("bar.zip", "bar.zip"),
array("\\foo\\bar.zip", ".zip"),
array("foo\\bar.zip", ".zip"),
array("\\bar.zip", ".zip"),
/* paths with suffix and trailing slashes with suffix removal*/
array("bar.zip\\", ".zip"),
array("\\bar.zip\\", ".zip"),
array("\\foo\\bar.zip\\", ".zip"),
array("foo\\bar.zip\\", ".zip"),
array("\\bar.zip\\", ".zip"),
/* paths with basename only suffix, with suffix removal*/
array("\\.zip", ".zip"),
array(".zip", ".zip"),
array("\\foo\\.zip", ".zip"),
/* paths with basename only suffix & trailing slashes, with suffix removal*/
array(".zip\\", ".zip"),
array("\\foo\\.zip\\", ".zip"),
array("foo\\.zip\\", ".zip"),
);
$file_path_variations = array (
/* paths with shortcut home dir char, with suffix variation */
array("C:\\temp\\bar"),
array("C:\\temp\\bar", ""),
array("C:\\temp\\bar", NULL),
array("C:\\temp\\bar", ' '),
array("C:\\temp\\bar.tar", ".tar"),
array("C:\\temp\\bar.tar", "~"),
array("C:\\temp\\bar.tar\\", "~"),
array("C:\\temp\\bar.tar\\", ""),
array("C:\\temp\\bar.tar", NULL),
array("C:\\temp\\bar.tar", ''),
array("C:\\temp\\bar.tar", " "),
/* paths with numeric strings */
array("10.5"),
array("10.5", ".5"),
array("10.5", "10.5"),
array("10"),
array("105", "5"),
array("/10.5"),
array("10.5\\"),
array("10/10.zip"),
array("0"),
array('0'),
/* paths and suffix given as same */
array("bar.zip", "bar.zip"),
array("\\bar.zip", "\\bar.zip"),
array("\\bar.zip\\", "\\bar.zip\\"),
array(" ", " "),
array(' ', ' '),
array(NULL, NULL),
/* path with spaces */
array(" "),
array(' '),
/* empty paths */
array(""),
array(''),
array(NULL)
);
function check_basename( $path_arrays ) {
$loop_counter = 1;
foreach ($path_arrays as $path) {
echo "\n--Iteration $loop_counter--\n"; $loop_counter++;
if( 1 == count($path) ) { // no suffix provided
var_dump( basename($path[0]) );
} else { // path as well as suffix provided,
var_dump( basename($path[0], $path[1]) );
}
}
}
echo "*** Testing basic operations ***\n";
check_basename( $file_paths );
echo "\n*** Testing possible variations in path and suffix ***\n";
check_basename( $file_path_variations );
echo "\n*** Testing error conditions ***\n";
// zero arguments
var_dump( basename() );
// more than expected no. of arguments
var_dump( basename("\\blah\\tmp\\bar.zip", ".zip", ".zip") );
// passing invalid type arguments
$object = new stdclass;
var_dump( basename( array("string\\bar") ) );
var_dump( basename( array("string\\bar"), "bar" ) );
var_dump( basename( "bar", array("string\\bar") ) );
var_dump( basename( $object, "bar" ) );
var_dump( basename( $object ) );
var_dump( basename( $object, $object ) );
var_dump( basename( "bar", $object ) );
echo "Done\n";
?>
--EXPECTF--
*** Testing basic operations ***
--Iteration 1--
string(3) "bar"
--Iteration 2--
string(3) "bar"
--Iteration 3--
string(3) "bar"
--Iteration 4--
string(3) "bar"
--Iteration 5--
string(3) "bar"
--Iteration 6--
string(3) "bar"
--Iteration 7--
string(3) "bar"
--Iteration 8--
string(3) "bar"
--Iteration 9--
string(3) "bar"
--Iteration 10--
string(3) "bar"
--Iteration 11--
string(7) "bar.zip"
--Iteration 12--
string(3) "bar"
--Iteration 13--
string(3) "bar"
--Iteration 14--
string(3) "bar"
--Iteration 15--
string(3) "bar"
--Iteration 16--
string(3) "bar"
--Iteration 17--
string(3) "bar"
--Iteration 18--
string(3) "bar"
--Iteration 19--
string(3) "bar"
--Iteration 20--
string(4) ".zip"
--Iteration 21--
string(4) ".zip"
--Iteration 22--
string(4) ".zip"
--Iteration 23--
string(4) ".zip"
--Iteration 24--
string(4) ".zip"
--Iteration 25--
string(4) ".zip"
*** Testing possible variations in path and suffix ***
--Iteration 1--
string(3) "bar"
--Iteration 2--
string(3) "bar"
--Iteration 3--
string(3) "bar"
--Iteration 4--
string(3) "bar"
--Iteration 5--
string(3) "bar"
--Iteration 6--
string(7) "bar.tar"
--Iteration 7--
string(7) "bar.tar"
--Iteration 8--
string(7) "bar.tar"
--Iteration 9--
string(7) "bar.tar"
--Iteration 10--
string(7) "bar.tar"
--Iteration 11--
string(7) "bar.tar"
--Iteration 12--
string(4) "10.5"
--Iteration 13--
string(2) "10"
--Iteration 14--
string(4) "10.5"
--Iteration 15--
string(2) "10"
--Iteration 16--
string(2) "10"
--Iteration 17--
string(4) "10.5"
--Iteration 18--
string(4) "10.5"
--Iteration 19--
string(6) "10.zip"
--Iteration 20--
string(1) "0"
--Iteration 21--
string(1) "0"
--Iteration 22--
string(7) "bar.zip"
--Iteration 23--
string(7) "bar.zip"
--Iteration 24--
string(7) "bar.zip"
--Iteration 25--
string(1) " "
--Iteration 26--
string(1) " "
--Iteration 27--
string(0) ""
--Iteration 28--
string(1) " "
--Iteration 29--
string(1) " "
--Iteration 30--
string(0) ""
--Iteration 31--
string(0) ""
--Iteration 32--
string(0) ""
*** Testing error conditions ***
Warning: basename() expects at least 1 parameter, 0 given in %s on line %d
NULL
Warning: basename() expects at most 2 parameters, 3 given in %s on line %d
NULL
Warning: basename() expects parameter 1 to be string, array given in %s on line %d
NULL
Warning: basename() expects parameter 1 to be string, array given in %s on line %d
NULL
Warning: basename() expects parameter 2 to be string, array given in %s on line %d
NULL
Warning: basename() expects parameter 1 to be string, object given in %s on line %d
NULL
Warning: basename() expects parameter 1 to be string, object given in %s on line %d
NULL
Warning: basename() expects parameter 1 to be string, object given in %s on line %d
NULL
Warning: basename() expects parameter 2 to be string, object given in %s on line %d
NULL
Done
|