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