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