1--TEST--
2Test fread() function : error conditions
3--FILE--
4<?php
5/*
6 Prototype: string fread ( resource $handle [, int $length] );
7 Description: reads up to length bytes from the file pointer referenced by handle.
8   Reading stops when up to length bytes have been read, EOF (end of file) is
9   reached, (for network streams) when a packet becomes available, or (after
10   opening userspace stream) when 8192 bytes have been read whichever comes first.
11*/
12
13echo "*** Testing error conditions ***\n";
14$filename = __FILE__;
15$file_handle = fopen($filename, "r");
16
17// zero argument
18echo "-- Testing fread() with zero argument --\n";
19var_dump( fread() );
20
21// more than expected no. of args
22echo "-- Testing fread() with more than expected number of arguments --\n";
23var_dump( fread($file_handle, 10, $file_handle) );
24
25// invalid length argument
26echo "-- Testing fread() with invalid length arguments --\n";
27$len = 0;
28var_dump( fread($file_handle, $len) );
29$len = -10;
30var_dump( fread($file_handle, $len) );
31
32// test invalid arguments : non-resources
33echo "-- Testing fread() with invalid arguments --\n";
34$invalid_args = array (
35  "string",
36  10,
37  10.5,
38  true,
39  array(1,2,3),
40  new stdclass,
41);
42/* loop to test fread() with different invalid type of args */
43for($loop_counter = 1; $loop_counter <= count($invalid_args); $loop_counter++) {
44  echo "-- Iteration $loop_counter --\n";
45  var_dump( fread($invalid_args[$loop_counter - 1], 10) );
46}
47
48// fwrite() on a file handle which is already closed
49echo "-- Testing fwrite() with closed/unset file handle --\n";
50fclose($file_handle);
51var_dump( fread($file_handle,$file_content_type) );
52
53// fwrite on a file handle which is unset
54$fp = fopen($filename, "r");
55unset($fp); //unset file handle
56var_dump( fread(@$fp,10) );
57var_dump( fclose(@$fp) );
58
59echo "Done\n";
60--EXPECTF--
61*** Testing error conditions ***
62-- Testing fread() with zero argument --
63
64Warning: fread() expects exactly 2 parameters, 0 given in %s on line %d
65bool(false)
66-- Testing fread() with more than expected number of arguments --
67
68Warning: fread() expects exactly 2 parameters, 3 given in %s on line %d
69bool(false)
70-- Testing fread() with invalid length arguments --
71
72Warning: fread(): Length parameter must be greater than 0 in %s on line %d
73bool(false)
74
75Warning: fread(): Length parameter must be greater than 0 in %s on line %d
76bool(false)
77-- Testing fread() with invalid arguments --
78-- Iteration 1 --
79
80Warning: fread() expects parameter 1 to be resource, string given in %s on line %d
81bool(false)
82-- Iteration 2 --
83
84Warning: fread() expects parameter 1 to be resource, int given in %s on line %d
85bool(false)
86-- Iteration 3 --
87
88Warning: fread() expects parameter 1 to be resource, float given in %s on line %d
89bool(false)
90-- Iteration 4 --
91
92Warning: fread() expects parameter 1 to be resource, bool given in %s on line %d
93bool(false)
94-- Iteration 5 --
95
96Warning: fread() expects parameter 1 to be resource, array given in %s on line %d
97bool(false)
98-- Iteration 6 --
99
100Warning: fread() expects parameter 1 to be resource, object given in %s on line %d
101bool(false)
102-- Testing fwrite() with closed/unset file handle --
103
104Notice: Undefined variable: file_content_type in %s on line %d
105
106Warning: fread(): supplied resource is not a valid stream resource in %s on line %d
107bool(false)
108
109Warning: fread() expects parameter 1 to be resource, null given in %s on line %d
110bool(false)
111
112Warning: fclose() expects parameter 1 to be resource, null given in %s on line %d
113bool(false)
114Done
115