xref: /PHP-7.4/ext/standard/tests/file/basename.phpt (revision d7a3edd4)
1--TEST--
2basename
3--CREDITS--
4Dave Kelsey <d_kelsey@uk.ibm.com>
5--FILE--
6<?php
7/*
8 * proto string basename(string path [, string suffix])
9 * Function is implemented in ext/standard/string.c
10 */
11$file_paths = array (
12  /* simple paths */
13  array("bar"),
14  array("/foo/bar"),
15  array("foo/bar"),
16  array("/bar"),
17
18  /* simple paths with trailing slashes */
19  array("bar/"),
20  array("/bar/"),
21  array("/foo/bar/"),
22  array("foo/bar/"),
23  array("/bar/"),
24
25  /* paths with suffix removal */
26  array("bar.gz", ".gz"),
27  array("bar.gz", "bar.gz"),
28  array("/foo/bar.gz", ".gz"),
29  array("foo/bar.gz", ".gz"),
30  array("/bar.gz", ".gz"),
31
32  /* paths with suffix and trailing slashes with suffix removal*/
33  array("bar.gz/", ".gz"),
34  array("/bar.gz/", ".gz"),
35  array("/foo/bar.gz/", ".gz"),
36  array("foo/bar.gz/", ".gz"),
37  array("/bar.gz/", ".gz"),
38
39  /* paths with basename only suffix, with suffix removal*/
40  array("/.gz", ".gz"),
41  array(".gz", ".gz"),
42  array("/foo/.gz", ".gz"),
43
44  /* paths with basename only suffix & trailing slashes, with suffix removal*/
45  array(".gz/", ".gz"),
46  array("/foo/.gz/", ".gz"),
47  array("foo/.gz/", ".gz"),
48
49  /* paths with binary value to check if the function is binary safe*/
50  array("foo".chr(0)."bar"),
51  array("/foo".chr(0)."bar"),
52  array("/foo".chr(0)."bar/"),
53  array("foo".chr(0)."bar/"),
54  array("foo".chr(0)."bar/test"),
55  array("/foo".chr(0)."bar/bar.gz", ".gz"),
56  array("/foo".chr(0)."bar/bar.gz")
57);
58
59$file_path_variations = array (
60  /* paths with shortcut home dir char, with suffix variation */
61  array("~/home/user/bar"),
62  array("~/home/user/bar", ""),
63  array("~/home/user/bar", NULL),
64  array("~/home/user/bar", ' '),
65  array("~/home/user/bar.tar", ".tar"),
66  array("~/home/user/bar.tar", "~"),
67  array("~/home/user/bar.tar/", "~"),
68  array("~/home/user/bar.tar/", ""),
69  array("~/home/user/bar.tar", NULL),
70  array("~/home/user/bar.tar", ''),
71  array("~/home/user/bar.tar", " "),
72
73  /* paths with hostname:dir notation, with suffix variation */
74  array("hostname:/home/usr/bar.tar"),
75  array("hostname:/home/user/bar.tar", "home"),
76  array("hostname:/home/user/tbar.gz", "bar.gz"),
77  array("hostname:/home/user/tbar.gz", "/bar.gz"),
78  array("hostname:/home/user/tbar.gz", "/bar.gz/"),
79  array("hostname:/home/user/tbar.gz/", "/bar.gz/"),
80  array("hostname:/home/user/tbar.gz/", "/bar.gz/"),
81  array("hostname:/home/user/My Pics.gz/", "/bar.gz/"),
82  array("hostname:/home/user/My Pics.gz/", "Pics.gz/"),
83  array("hostname:/home/user/My Pics.gz/", "Pics.gz"),
84  array("hostname:/home/user/My Pics.gz/", ".gz"),
85  array("hostname:/home/user/My Pics.gz/"),
86  array("hostname:/home/user/My Pics.gz/", NULL),
87  array("hostname:/home/user/My Pics.gz/", ' '),
88  array("hostname:/home/user/My Pics.gz/", ''),
89  array("hostname:/home/user/My Pics.gz/", "My Pics.gz"),
90
91  /* paths with numeirc strings */
92  array("10.5"),
93  array("10.5", ".5"),
94  array("10.5", "10.5"),
95  array("10"),
96  array("105", "5"),
97  array("/10.5"),
98  array("10.5/"),
99  array("10/10.gz"),
100  array("0"),
101  array('0'),
102
103  /* paths and suffix given as same */
104  array("bar.gz", "bar.gz"),
105  array("/bar.gz", "/bar.gz"),
106  array("/bar.gz/", "/bar.gz/"),
107  array(" ", " "),
108  array(' ', ' '),
109  array(NULL, NULL),
110
111  /* path with spaces */
112  array(" "),
113  array(' '),
114
115  /* empty paths */
116  array(""),
117  array(''),
118  array(NULL)
119);
120
121function check_basename( $path_arrays ) {
122   $loop_counter = 1;
123   foreach ($path_arrays as $path) {
124     echo "\n--Iteration $loop_counter--\n"; $loop_counter++;
125     if( 1 == count($path) ) { // no suffix provided
126       var_dump( basename($path[0]) );
127     } else { // path as well as suffix provided,
128       var_dump( basename($path[0], $path[1]) );
129     }
130   }
131}
132
133echo "*** Testing basic operations ***\n";
134check_basename( $file_paths );
135
136echo "\n*** Testing possible variations in path and suffix ***\n";
137check_basename( $file_path_variations );
138
139echo "\n*** Testing error conditions ***\n";
140// zero arguments
141var_dump( basename() );
142
143// more than expected no. of arguments
144var_dump( basename("/var/tmp/bar.gz", ".gz", ".gz") );
145
146// passing invalid type arguments
147$object = new stdclass;
148var_dump( basename( array("string/bar") ) );
149var_dump( basename( array("string/bar"), "bar" ) );
150var_dump( basename( "bar", array("string/bar") ) );
151var_dump( basename( $object, "bar" ) );
152var_dump( basename( $object ) );
153var_dump( basename( $object, $object ) );
154var_dump( basename( "bar", $object ) );
155
156echo "Done\n";
157?>
158--EXPECTF--
159*** Testing basic operations ***
160
161--Iteration 1--
162string(3) "bar"
163
164--Iteration 2--
165string(3) "bar"
166
167--Iteration 3--
168string(3) "bar"
169
170--Iteration 4--
171string(3) "bar"
172
173--Iteration 5--
174string(3) "bar"
175
176--Iteration 6--
177string(3) "bar"
178
179--Iteration 7--
180string(3) "bar"
181
182--Iteration 8--
183string(3) "bar"
184
185--Iteration 9--
186string(3) "bar"
187
188--Iteration 10--
189string(3) "bar"
190
191--Iteration 11--
192string(6) "bar.gz"
193
194--Iteration 12--
195string(3) "bar"
196
197--Iteration 13--
198string(3) "bar"
199
200--Iteration 14--
201string(3) "bar"
202
203--Iteration 15--
204string(3) "bar"
205
206--Iteration 16--
207string(3) "bar"
208
209--Iteration 17--
210string(3) "bar"
211
212--Iteration 18--
213string(3) "bar"
214
215--Iteration 19--
216string(3) "bar"
217
218--Iteration 20--
219string(3) ".gz"
220
221--Iteration 21--
222string(3) ".gz"
223
224--Iteration 22--
225string(3) ".gz"
226
227--Iteration 23--
228string(3) ".gz"
229
230--Iteration 24--
231string(3) ".gz"
232
233--Iteration 25--
234string(3) ".gz"
235
236--Iteration 26--
237string(7) "foo�bar"
238
239--Iteration 27--
240string(7) "foo�bar"
241
242--Iteration 28--
243string(7) "foo�bar"
244
245--Iteration 29--
246string(7) "foo�bar"
247
248--Iteration 30--
249string(4) "test"
250
251--Iteration 31--
252string(3) "bar"
253
254--Iteration 32--
255string(6) "bar.gz"
256
257*** Testing possible variations in path and suffix ***
258
259--Iteration 1--
260string(3) "bar"
261
262--Iteration 2--
263string(3) "bar"
264
265--Iteration 3--
266string(3) "bar"
267
268--Iteration 4--
269string(3) "bar"
270
271--Iteration 5--
272string(3) "bar"
273
274--Iteration 6--
275string(7) "bar.tar"
276
277--Iteration 7--
278string(7) "bar.tar"
279
280--Iteration 8--
281string(7) "bar.tar"
282
283--Iteration 9--
284string(7) "bar.tar"
285
286--Iteration 10--
287string(7) "bar.tar"
288
289--Iteration 11--
290string(7) "bar.tar"
291
292--Iteration 12--
293string(7) "bar.tar"
294
295--Iteration 13--
296string(7) "bar.tar"
297
298--Iteration 14--
299string(1) "t"
300
301--Iteration 15--
302string(7) "tbar.gz"
303
304--Iteration 16--
305string(7) "tbar.gz"
306
307--Iteration 17--
308string(7) "tbar.gz"
309
310--Iteration 18--
311string(7) "tbar.gz"
312
313--Iteration 19--
314string(10) "My Pics.gz"
315
316--Iteration 20--
317string(10) "My Pics.gz"
318
319--Iteration 21--
320string(3) "My "
321
322--Iteration 22--
323string(7) "My Pics"
324
325--Iteration 23--
326string(10) "My Pics.gz"
327
328--Iteration 24--
329string(10) "My Pics.gz"
330
331--Iteration 25--
332string(10) "My Pics.gz"
333
334--Iteration 26--
335string(10) "My Pics.gz"
336
337--Iteration 27--
338string(10) "My Pics.gz"
339
340--Iteration 28--
341string(4) "10.5"
342
343--Iteration 29--
344string(2) "10"
345
346--Iteration 30--
347string(4) "10.5"
348
349--Iteration 31--
350string(2) "10"
351
352--Iteration 32--
353string(2) "10"
354
355--Iteration 33--
356string(4) "10.5"
357
358--Iteration 34--
359string(4) "10.5"
360
361--Iteration 35--
362string(5) "10.gz"
363
364--Iteration 36--
365string(1) "0"
366
367--Iteration 37--
368string(1) "0"
369
370--Iteration 38--
371string(6) "bar.gz"
372
373--Iteration 39--
374string(6) "bar.gz"
375
376--Iteration 40--
377string(6) "bar.gz"
378
379--Iteration 41--
380string(1) " "
381
382--Iteration 42--
383string(1) " "
384
385--Iteration 43--
386string(0) ""
387
388--Iteration 44--
389string(1) " "
390
391--Iteration 45--
392string(1) " "
393
394--Iteration 46--
395string(0) ""
396
397--Iteration 47--
398string(0) ""
399
400--Iteration 48--
401string(0) ""
402
403*** Testing error conditions ***
404
405Warning: basename() expects at least 1 parameter, 0 given in %s on line %d
406NULL
407
408Warning: basename() expects at most 2 parameters, 3 given in %s on line %d
409NULL
410
411Warning: basename() expects parameter 1 to be string, array given in %s on line %d
412NULL
413
414Warning: basename() expects parameter 1 to be string, array given in %s on line %d
415NULL
416
417Warning: basename() expects parameter 2 to be string, array given in %s on line %d
418NULL
419
420Warning: basename() expects parameter 1 to be string, object given in %s on line %d
421NULL
422
423Warning: basename() expects parameter 1 to be string, object given in %s on line %d
424NULL
425
426Warning: basename() expects parameter 1 to be string, object given in %s on line %d
427NULL
428
429Warning: basename() expects parameter 2 to be string, object given in %s on line %d
430NULL
431Done
432