xref: /PHP-7.2/ext/standard/tests/file/007_error.phpt (revision 17ccbeec)
1--TEST--
2Test fopen, fclose() & feof() functions: error conditions
3--CREDITS--
4Dave Kelsey <d_kelsey@uk.ibm.com>
5--FILE--
6<?php
7/*
8 Prototype: resource fopen(string $filename, string $mode
9                            [, bool $use_include_path [, resource $context]] );
10 Description: Opens file or URL.
11
12 Prototype: bool fclose ( resource $handle );
13 Description: Closes an open file pointer
14
15 Prototype: bool feof ( resource $handle )
16 Description: Returns TRUE if the file pointer is at EOF or an error occurs
17   (including socket timeout); otherwise returns FALSE.
18*/
19
20echo "*** Testing error conditions for fopen(), fclsoe() & feof() ***\n";
21/* Arguments less than minimum no.of args */
22
23// fopen ()
24var_dump(fopen(__FILE__)); // one valid argument
25var_dump(fopen());  // zero argument
26
27// fclose()
28$fp = fopen(__FILE__, "r");
29fclose($fp);
30var_dump( fclose($fp) ); // closed handle
31var_dump( fclose(__FILE__) ); // invalid handle
32var_dump( fclose() ); // zero argument
33
34//feof()
35var_dump( feof($fp) );  // closed handle
36var_dump( feof(__FILE__) );  // invalid handle
37var_dump( feof() );  //zero argument
38
39/* Arguments greater than maximum no.of ags */
40var_dump(fopen(__FILE__, "r", TRUE, "www.example.com", 100));
41
42$fp = fopen(__FILE__, "r");
43var_dump( fclose($fp, "handle") );
44
45var_dump( feof($fp, "handle"));
46fclose($fp);
47
48/* test invalid arguments : non-resources */
49echo "-- Testing fopen(), fclose() & feof() with invalid arguments --\n";
50$invalid_args = array (
51  "string",
52  10,
53  10.5,
54  true,
55  array(1,2,3),
56  new stdclass,
57  NULL,
58  ""
59);
60
61/* loop to test fclose with different invalid type of args */
62for($loop_counter = 1; $loop_counter <= count($invalid_args); $loop_counter++) {
63  echo "-- Iteration $loop_counter --\n";
64  var_dump( fopen($invalid_args[$loop_counter - 1], "r") );
65  var_dump( fclose($invalid_args[$loop_counter - 1]) );
66  var_dump( feof($invalid_args[$loop_counter - 1]) );
67}
68
69?>
70--EXPECTF--
71*** Testing error conditions for fopen(), fclsoe() & feof() ***
72
73Warning: fopen() expects at least 2 parameters, 1 given in %s on line %d
74bool(false)
75
76Warning: fopen() expects at least 2 parameters, 0 given in %s on line %d
77bool(false)
78
79Warning: fclose(): supplied resource is not a valid stream resource in %s on line %d
80bool(false)
81
82Warning: fclose() expects parameter 1 to be resource, string given in %s on line %d
83bool(false)
84
85Warning: fclose() expects exactly 1 parameter, 0 given in %s on line %d
86bool(false)
87
88Warning: feof(): supplied resource is not a valid stream resource in %s on line %d
89bool(false)
90
91Warning: feof() expects parameter 1 to be resource, string given in %s on line %d
92bool(false)
93
94Warning: feof() expects exactly 1 parameter, 0 given in %s on line %d
95bool(false)
96
97Warning: fopen() expects at most 4 parameters, 5 given in %s on line %d
98bool(false)
99
100Warning: fclose() expects exactly 1 parameter, 2 given in %s on line %d
101bool(false)
102
103Warning: feof() expects exactly 1 parameter, 2 given in %s on line %d
104bool(false)
105-- Testing fopen(), fclose() & feof() with invalid arguments --
106-- Iteration 1 --
107
108Warning: fopen(string): failed to open stream: No such file or directory in %s on line %d
109bool(false)
110
111Warning: fclose() expects parameter 1 to be resource, string given in %s on line %d
112bool(false)
113
114Warning: feof() expects parameter 1 to be resource, string given in %s on line %d
115bool(false)
116-- Iteration 2 --
117
118Warning: fopen(10): failed to open stream: No such file or directory in %s on line %d
119bool(false)
120
121Warning: fclose() expects parameter 1 to be resource, integer given in %s on line %d
122bool(false)
123
124Warning: feof() expects parameter 1 to be resource, integer given in %s on line %d
125bool(false)
126-- Iteration 3 --
127
128Warning: fopen(10.5): failed to open stream: No such file or directory in %s on line %d
129bool(false)
130
131Warning: fclose() expects parameter 1 to be resource, float given in %s on line %d
132bool(false)
133
134Warning: feof() expects parameter 1 to be resource, float given in %s on line %d
135bool(false)
136-- Iteration 4 --
137
138Warning: fopen(1): failed to open stream: No such file or directory in %s on line %d
139bool(false)
140
141Warning: fclose() expects parameter 1 to be resource, boolean given in %s on line %d
142bool(false)
143
144Warning: feof() expects parameter 1 to be resource, boolean given in %s on line %d
145bool(false)
146-- Iteration 5 --
147
148Warning: fopen() expects parameter 1 to be a valid path, array given in %s on line %d
149bool(false)
150
151Warning: fclose() expects parameter 1 to be resource, array given in %s on line %d
152bool(false)
153
154Warning: feof() expects parameter 1 to be resource, array given in %s on line %d
155bool(false)
156-- Iteration 6 --
157
158Warning: fopen() expects parameter 1 to be a valid path, object given in %s on line %d
159bool(false)
160
161Warning: fclose() expects parameter 1 to be resource, object given in %s on line %d
162bool(false)
163
164Warning: feof() expects parameter 1 to be resource, object given in %s on line %d
165bool(false)
166-- Iteration 7 --
167
168Warning: fopen(): Filename cannot be empty in %s on line %d
169bool(false)
170
171Warning: fclose() expects parameter 1 to be resource, null given in %s on line %d
172bool(false)
173
174Warning: feof() expects parameter 1 to be resource, null given in %s on line %d
175bool(false)
176-- Iteration 8 --
177
178Warning: fopen(): Filename cannot be empty in %s on line %d
179bool(false)
180
181Warning: fclose() expects parameter 1 to be resource, string given in %s on line %d
182bool(false)
183
184Warning: feof() expects parameter 1 to be resource, string given in %s on line %d
185bool(false)
186