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