1--TEST--
2Testing unlink() function : error conditions
3--SKIPIF--
4<?php
5if (substr(PHP_OS, 0, 3) == 'WIN') {
6    die('skip.. only on Linux');
7}
8?>
9--FILE--
10<?php
11/* Prototype : bool unlink ( string $filename [, resource $context] );
12   Description : Deletes filename
13*/
14
15$file_path = dirname(__FILE__);
16
17$filename = "$file_path/unlink_error.tmp";  // temp file name used here
18$fp = fopen($filename, "w");  // create file
19fclose($fp);
20
21// creating a context
22$context = stream_context_create();
23
24echo "*** Testing unlink() : error conditions ***\n";
25
26echo "-- Testing unlink() on unexpected no. of arguments --\n";
27// arg < expected
28var_dump( unlink() );
29// args > expected
30var_dump( unlink($filename, $context, true) );
31var_dump( file_exists($filename) ); // expected true
32
33echo "\n-- Testing unlink() on invalid arguments --\n";
34// invalid arguments
35var_dump( unlink('') );  // $filename as empty string
36var_dump( file_exists('') );  // confirm file doesnt exist
37
38var_dump( unlink(NULL) );  // $filename as NULL
39var_dump( file_exists(NULL) );  // confirm file doesnt exist
40
41var_dump( unlink(false) );  // $filename as boolean false
42var_dump( file_exists(false) );  // confirm file doesnt exist
43
44var_dump( unlink($filename, '') );  // $context as empty string
45var_dump( unlink($filename, false) );  // $context as boolean false
46var_dump( unlink($filename, NULL) );  // $context as NULL
47
48
49echo "\n-- Testing unlink() on non-existent file --\n";
50var_dump( unlink(dirname(__FILE__)."/non_existent_file.tmp") );
51
52echo "\n-- Testing unlink() on directory --\n";
53// temp directory used here
54$dirname = "$file_path/unlink_error";
55// create temp dir
56mkdir($dirname);
57// unlinking directory
58var_dump( unlink($dirname) );  // expected: false as unlink() does not work on dir
59
60echo "Done\n";
61?>
62--CLEAN--
63<?php
64unlink(dirname(__FILE__)."/unlink_error.tmp");
65rmdir(dirname(__FILE__)."/unlink_error");
66?>
67--EXPECTF--
68*** Testing unlink() : error conditions ***
69-- Testing unlink() on unexpected no. of arguments --
70
71Warning: unlink() expects at least 1 parameter, 0 given in %s on line %d
72bool(false)
73
74Warning: unlink() expects at most 2 parameters, 3 given in %s on line %d
75bool(false)
76bool(true)
77
78-- Testing unlink() on invalid arguments --
79
80Warning: unlink(): %s in %s on line %d
81bool(false)
82bool(false)
83
84Warning: unlink(): %s in %s on line %d
85bool(false)
86bool(false)
87
88Warning: unlink(): %s in %s on line %d
89bool(false)
90bool(false)
91
92Warning: unlink() expects parameter 2 to be resource, string given in %s on line %d
93bool(false)
94
95Warning: unlink() expects parameter 2 to be resource, boolean given in %s on line %d
96bool(false)
97
98Warning: unlink() expects parameter 2 to be resource, null given in %s on line %d
99bool(false)
100
101-- Testing unlink() on non-existent file --
102
103Warning: unlink(%s/non_existent_file.tmp): No such file or directory in %s on line %d
104bool(false)
105
106-- Testing unlink() on directory --
107
108Warning: unlink(%s/unlink_error): %s in %s on line %d
109bool(false)
110Done
111