1--TEST--
2Test basename() function : usage variations
3--FILE--
4<?php
5/* Prototype: string basename ( string $path [, string $suffix] );
6   Description: Given a string containing a path to a file,
7                this function will return the base name of the file.
8                If the filename ends in suffix this will also be cut off.
9*/
10$file_path_variations = array (
11  /* paths with shortcut home dir char, with suffix variation */
12  array("~/home/user/bar"),
13  array("~/home/user/bar", ""),
14  array("~/home/user/bar", NULL),
15  array("~/home/user/bar", ' '),
16  array("~/home/user/bar.tar", ".tar"),
17  array("~/home/user/bar.tar", "~"),
18  array("~/home/user/bar.tar/", "~"),
19  array("~/home/user/bar.tar/", ""),
20  array("~/home/user/bar.tar", NULL),
21  array("~/home/user/bar.tar", ''),
22  array("~/home/user/bar.tar", " "),
23
24  /* paths with hostname:dir notation, with suffix variation */
25  array("hostname:/home/usr/bar.tar"),
26  array("hostname:/home/user/bar.tar", "home"),
27  array("hostname:/home/user/tbar.gz", "bar.gz"),
28  array("hostname:/home/user/tbar.gz", "/bar.gz"),
29  array("hostname:/home/user/tbar.gz", "/bar.gz/"),
30  array("hostname:/home/user/tbar.gz/", "/bar.gz/"),
31  array("hostname:/home/user/tbar.gz/", "/bar.gz/"),
32  array("hostname:/home/user/My Pics.gz/", "/bar.gz/"),
33  array("hostname:/home/user/My Pics.gz/", "Pics.gz/"),
34  array("hostname:/home/user/My Pics.gz/", "Pics.gz"),
35  array("hostname:/home/user/My Pics.gz/", ".gz"),
36  array("hostname:/home/user/My Pics.gz/"),
37  array("hostname:/home/user/My Pics.gz/", NULL),
38  array("hostname:/home/user/My Pics.gz/", ' '),
39  array("hostname:/home/user/My Pics.gz/", ''),
40  array("hostname:/home/user/My Pics.gz/", "My Pics.gz"),
41
42  /* paths with numeirc strings */
43  array("10.5"),
44  array("10.5", ".5"),
45  array("10.5", "10.5"),
46  array("10"),
47  array("105", "5"),
48  array("/10.5"),
49  array("10.5/"),
50  array("10/10.gz"),
51  array("0"),
52  array('0'),
53
54  /* paths and suffix given as same */
55  array("bar.gz", "bar.gz"),
56  array("/bar.gz", "/bar.gz"),
57  array("/bar.gz/", "/bar.gz/"),
58  array(" ", " "),
59  array(' ', ' '),
60  array(NULL, NULL),
61
62  /* path with spaces */
63  array(" "),
64  array(' '),
65
66  /* empty paths */
67  array(""),
68  array(''),
69  array(NULL),
70);
71
72function check_basename( $path_arrays ) {
73   $loop_counter = 1;
74   foreach ($path_arrays as $path) {
75     echo "\n--Iteration $loop_counter--\n"; $loop_counter++;
76     if( 1 == count($path) ) { // no suffix provided
77       var_dump( basename($path[0]) );
78     } else { // path as well as suffix provided,
79       var_dump( basename($path[0], $path[1]) );
80     }
81   }
82}
83
84echo "*** Testing possible variations in path and suffix ***\n";
85check_basename( $file_path_variations );
86
87echo "Done\n";
88--EXPECTF--
89*** Testing possible variations in path and suffix ***
90
91--Iteration 1--
92string(3) "bar"
93
94--Iteration 2--
95string(3) "bar"
96
97--Iteration 3--
98string(3) "bar"
99
100--Iteration 4--
101string(3) "bar"
102
103--Iteration 5--
104string(3) "bar"
105
106--Iteration 6--
107string(7) "bar.tar"
108
109--Iteration 7--
110string(7) "bar.tar"
111
112--Iteration 8--
113string(7) "bar.tar"
114
115--Iteration 9--
116string(7) "bar.tar"
117
118--Iteration 10--
119string(7) "bar.tar"
120
121--Iteration 11--
122string(7) "bar.tar"
123
124--Iteration 12--
125string(7) "bar.tar"
126
127--Iteration 13--
128string(7) "bar.tar"
129
130--Iteration 14--
131string(1) "t"
132
133--Iteration 15--
134string(7) "tbar.gz"
135
136--Iteration 16--
137string(7) "tbar.gz"
138
139--Iteration 17--
140string(7) "tbar.gz"
141
142--Iteration 18--
143string(7) "tbar.gz"
144
145--Iteration 19--
146string(10) "My Pics.gz"
147
148--Iteration 20--
149string(10) "My Pics.gz"
150
151--Iteration 21--
152string(3) "My "
153
154--Iteration 22--
155string(7) "My Pics"
156
157--Iteration 23--
158string(10) "My Pics.gz"
159
160--Iteration 24--
161string(10) "My Pics.gz"
162
163--Iteration 25--
164string(10) "My Pics.gz"
165
166--Iteration 26--
167string(10) "My Pics.gz"
168
169--Iteration 27--
170string(10) "My Pics.gz"
171
172--Iteration 28--
173string(4) "10.5"
174
175--Iteration 29--
176string(2) "10"
177
178--Iteration 30--
179string(4) "10.5"
180
181--Iteration 31--
182string(2) "10"
183
184--Iteration 32--
185string(2) "10"
186
187--Iteration 33--
188string(4) "10.5"
189
190--Iteration 34--
191string(4) "10.5"
192
193--Iteration 35--
194string(5) "10.gz"
195
196--Iteration 36--
197string(1) "0"
198
199--Iteration 37--
200string(1) "0"
201
202--Iteration 38--
203string(6) "bar.gz"
204
205--Iteration 39--
206string(6) "bar.gz"
207
208--Iteration 40--
209string(6) "bar.gz"
210
211--Iteration 41--
212string(1) " "
213
214--Iteration 42--
215string(1) " "
216
217--Iteration 43--
218string(0) ""
219
220--Iteration 44--
221string(1) " "
222
223--Iteration 45--
224string(1) " "
225
226--Iteration 46--
227string(0) ""
228
229--Iteration 47--
230string(0) ""
231
232--Iteration 48--
233string(0) ""
234Done
235