1--TEST--
2Test fnmatch() function: Variations
3--SKIPIF--
4<?php
5if (!function_exists('fnmatch'))
6    die("skip fnmatch() function is not available");
7?>
8--FILE--
9<?php
10
11echo "*** Testing fnmatch() with file and various patterns ***\n";
12$file_name = __DIR__."/match.tmp";
13
14/* avoid using \, it breaks the pattern */
15if (substr(PHP_OS, 0, 3) == 'WIN') {
16    $file_name = str_replace('\\','/', $file_name);
17}
18
19fopen($file_name, "w");
20
21$pattern_arr = array(
220 => "*.tmp",
231 => "match*",
242 => "mat*",
253 => "mat*tmp",
264 => "m*t",
275 => "ma[pt]ch*",
286 => "*.t*",
297 => "***.tmp",
308 => "match**",
319 => "*.t*p",
3210 => "",
3311 => "match",
3412 => ".tmp",
3513 => "?match",
3614 => "match?tmp",
3715 => "?tmp",
3816 => "match?",
3917 => "?match?",
4018 => "match.tmp",
4119 => "/match.tmp",
4220 => "/match.tmp/",
4321 => 'match.tmp',
4422 => 'match.tmp\0',
4523 => "match.tmp\0",
4624 => "match\0.tmp",
4725 => chr(109).chr(97)."tch.tmp",
4826 => chr(109).chr(97).chr(116).chr(99).chr(104).".tmp",
4927 => chr(109).chr(97).chr(116).chr(99).chr(104).chr(46).chr(116).chr(120).chr(116),
5028 => chr(109).chr(97).chr(116).chr(99).chr(104).".".chr(116).chr(120).chr(116),
5129 => "MATCH.TMP",
5230 => "MATCH*",
5331 => $file_name,
54
55/* binary inputs */
5632 => b"match*",
5733 => b"*.tmp",
5834 => b"mat*",
5935 => b"mat*tmp",
6036 => b"m*t",
61);
62
63for( $i = 0; $i<count($pattern_arr); $i++ ) {
64  echo "-- Iteration $i --\n";
65  try {
66    var_dump( fnmatch($pattern_arr[$i], $file_name) );
67  } catch (Error $e) {
68    echo $e->getMessage(), "\n";
69  }
70}
71unlink($file_name);
72
73
74echo "\n*** Testing fnmatch() with other types other than files ***";
75
76/* defining a common function */
77function match_( $pattern, $string ) {
78  for( $i = 0; $i<count($pattern); $i++ ) {
79    echo "-- Iteration $i --\n";
80    for( $j = 0; $j<count($string); $j++ ) {
81      try {
82        var_dump( fnmatch($pattern[$i], $string[$j]) );
83      } catch (Error $e) {
84        echo $e->getMessage(), "\n";
85      }
86    }
87  }
88}
89
90echo "\n--- With Integers ---\n";
91$int_arr = array(
92  16,
93  16.00,
94  020,
95  020.00,
96  0xF,
97  0xF0000
98);
99match_($int_arr, $int_arr);
100
101echo "\n--- With Strings ---\n";
102$str_arr = array(
103  "string",
104  "string\0",
105  'string',
106  "str\0ing",
107  "stringstring",
108
109  /* binary input */
110  b"string"
111);
112match_($str_arr, $str_arr);
113
114echo "\n--- With booleans ---\n";
115$bool_arr = array(
116  TRUE,
117  true,
118  1,
119  10,
120  FALSE,
121  false,
122  0,
123  "",
124  "string"
125);
126match_($bool_arr, $bool_arr);
127
128echo "\n--- With NULL ---\n";
129$null_arr = array(
130  "",
131  "\0",
132  "string",
133  0
134);
135match_($null_arr, $null_arr);
136
137echo "\n*** Done ***\n";
138?>
139--EXPECT--
140*** Testing fnmatch() with file and various patterns ***
141-- Iteration 0 --
142bool(true)
143-- Iteration 1 --
144bool(false)
145-- Iteration 2 --
146bool(false)
147-- Iteration 3 --
148bool(false)
149-- Iteration 4 --
150bool(false)
151-- Iteration 5 --
152bool(false)
153-- Iteration 6 --
154bool(true)
155-- Iteration 7 --
156bool(true)
157-- Iteration 8 --
158bool(false)
159-- Iteration 9 --
160bool(true)
161-- Iteration 10 --
162bool(false)
163-- Iteration 11 --
164bool(false)
165-- Iteration 12 --
166bool(false)
167-- Iteration 13 --
168bool(false)
169-- Iteration 14 --
170bool(false)
171-- Iteration 15 --
172bool(false)
173-- Iteration 16 --
174bool(false)
175-- Iteration 17 --
176bool(false)
177-- Iteration 18 --
178bool(false)
179-- Iteration 19 --
180bool(false)
181-- Iteration 20 --
182bool(false)
183-- Iteration 21 --
184bool(false)
185-- Iteration 22 --
186bool(false)
187-- Iteration 23 --
188fnmatch(): Argument #1 ($pattern) must not contain any null bytes
189-- Iteration 24 --
190fnmatch(): Argument #1 ($pattern) must not contain any null bytes
191-- Iteration 25 --
192bool(false)
193-- Iteration 26 --
194bool(false)
195-- Iteration 27 --
196bool(false)
197-- Iteration 28 --
198bool(false)
199-- Iteration 29 --
200bool(false)
201-- Iteration 30 --
202bool(false)
203-- Iteration 31 --
204bool(true)
205-- Iteration 32 --
206bool(false)
207-- Iteration 33 --
208bool(true)
209-- Iteration 34 --
210bool(false)
211-- Iteration 35 --
212bool(false)
213-- Iteration 36 --
214bool(false)
215
216*** Testing fnmatch() with other types other than files ***
217--- With Integers ---
218-- Iteration 0 --
219bool(true)
220bool(true)
221bool(true)
222bool(false)
223bool(false)
224bool(false)
225-- Iteration 1 --
226bool(true)
227bool(true)
228bool(true)
229bool(false)
230bool(false)
231bool(false)
232-- Iteration 2 --
233bool(true)
234bool(true)
235bool(true)
236bool(false)
237bool(false)
238bool(false)
239-- Iteration 3 --
240bool(false)
241bool(false)
242bool(false)
243bool(true)
244bool(false)
245bool(false)
246-- Iteration 4 --
247bool(false)
248bool(false)
249bool(false)
250bool(false)
251bool(true)
252bool(false)
253-- Iteration 5 --
254bool(false)
255bool(false)
256bool(false)
257bool(false)
258bool(false)
259bool(true)
260
261--- With Strings ---
262-- Iteration 0 --
263bool(true)
264fnmatch(): Argument #2 ($filename) must not contain any null bytes
265bool(true)
266fnmatch(): Argument #2 ($filename) must not contain any null bytes
267bool(false)
268bool(true)
269-- Iteration 1 --
270fnmatch(): Argument #1 ($pattern) must not contain any null bytes
271fnmatch(): Argument #1 ($pattern) must not contain any null bytes
272fnmatch(): Argument #1 ($pattern) must not contain any null bytes
273fnmatch(): Argument #1 ($pattern) must not contain any null bytes
274fnmatch(): Argument #1 ($pattern) must not contain any null bytes
275fnmatch(): Argument #1 ($pattern) must not contain any null bytes
276-- Iteration 2 --
277bool(true)
278fnmatch(): Argument #2 ($filename) must not contain any null bytes
279bool(true)
280fnmatch(): Argument #2 ($filename) must not contain any null bytes
281bool(false)
282bool(true)
283-- Iteration 3 --
284fnmatch(): Argument #1 ($pattern) must not contain any null bytes
285fnmatch(): Argument #1 ($pattern) must not contain any null bytes
286fnmatch(): Argument #1 ($pattern) must not contain any null bytes
287fnmatch(): Argument #1 ($pattern) must not contain any null bytes
288fnmatch(): Argument #1 ($pattern) must not contain any null bytes
289fnmatch(): Argument #1 ($pattern) must not contain any null bytes
290-- Iteration 4 --
291bool(false)
292fnmatch(): Argument #2 ($filename) must not contain any null bytes
293bool(false)
294fnmatch(): Argument #2 ($filename) must not contain any null bytes
295bool(true)
296bool(false)
297-- Iteration 5 --
298bool(true)
299fnmatch(): Argument #2 ($filename) must not contain any null bytes
300bool(true)
301fnmatch(): Argument #2 ($filename) must not contain any null bytes
302bool(false)
303bool(true)
304
305--- With booleans ---
306-- Iteration 0 --
307bool(true)
308bool(true)
309bool(true)
310bool(false)
311bool(false)
312bool(false)
313bool(false)
314bool(false)
315bool(false)
316-- Iteration 1 --
317bool(true)
318bool(true)
319bool(true)
320bool(false)
321bool(false)
322bool(false)
323bool(false)
324bool(false)
325bool(false)
326-- Iteration 2 --
327bool(true)
328bool(true)
329bool(true)
330bool(false)
331bool(false)
332bool(false)
333bool(false)
334bool(false)
335bool(false)
336-- Iteration 3 --
337bool(false)
338bool(false)
339bool(false)
340bool(true)
341bool(false)
342bool(false)
343bool(false)
344bool(false)
345bool(false)
346-- Iteration 4 --
347bool(false)
348bool(false)
349bool(false)
350bool(false)
351bool(true)
352bool(true)
353bool(false)
354bool(true)
355bool(false)
356-- Iteration 5 --
357bool(false)
358bool(false)
359bool(false)
360bool(false)
361bool(true)
362bool(true)
363bool(false)
364bool(true)
365bool(false)
366-- Iteration 6 --
367bool(false)
368bool(false)
369bool(false)
370bool(false)
371bool(false)
372bool(false)
373bool(true)
374bool(false)
375bool(false)
376-- Iteration 7 --
377bool(false)
378bool(false)
379bool(false)
380bool(false)
381bool(true)
382bool(true)
383bool(false)
384bool(true)
385bool(false)
386-- Iteration 8 --
387bool(false)
388bool(false)
389bool(false)
390bool(false)
391bool(false)
392bool(false)
393bool(false)
394bool(false)
395bool(true)
396
397--- With NULL ---
398-- Iteration 0 --
399bool(true)
400fnmatch(): Argument #2 ($filename) must not contain any null bytes
401bool(false)
402bool(false)
403-- Iteration 1 --
404fnmatch(): Argument #1 ($pattern) must not contain any null bytes
405fnmatch(): Argument #1 ($pattern) must not contain any null bytes
406fnmatch(): Argument #1 ($pattern) must not contain any null bytes
407fnmatch(): Argument #1 ($pattern) must not contain any null bytes
408-- Iteration 2 --
409bool(false)
410fnmatch(): Argument #2 ($filename) must not contain any null bytes
411bool(true)
412bool(false)
413-- Iteration 3 --
414bool(false)
415fnmatch(): Argument #2 ($filename) must not contain any null bytes
416bool(false)
417bool(true)
418
419*** Done ***
420