1--TEST--
2Test fwrite() function : error conditions
3--FILE--
4<?php
5/*
6 Prototype: int fwrite ( resource $handle,string string, [, int $length] );
7 Description: fwrite() writes the contents of string to the file stream pointed to by handle.
8              If the length arquement is given,writing will stop after length bytes have been
9              written or the end of string reached, whichever comes first.
10              fwrite() returns the number of bytes written or FALSE on error
11*/
12
13// include the file.inc for Function: function delete_file($filename)
14include ("file.inc");
15
16echo "*** Testing fwrite() : error conditions ***\n";
17
18$filename = __DIR__."/fwrite_error.tmp";
19
20echo "-- Testing fwrite() with less than expected number of arguments --\n";
21// zero argument
22var_dump( fwrite() );
23// less than expected, 1 arg
24$file_handle  = fopen ( $filename, "w");
25var_dump( fwrite($file_handle) );
26
27// more than expected no. of args
28echo "-- Testing fwrite() with more than expected number of arguments --\n";
29$data = "data";
30var_dump( fwrite($file_handle, $data, strlen($data), 10) );
31
32// invalid length argument
33echo "-- Testing fwrite() with invalid length arguments --\n";
34$len = 0;
35var_dump( fwrite($file_handle, $data, $len) );
36$len = -10;
37var_dump( fwrite($file_handle, $data, $len) );
38
39// test invalid arguments : non-resources
40echo "-- Testing fwrite() with invalid arguments --\n";
41$invalid_args = array (
42  "string",
43  10,
44  10.5,
45  true,
46  array(1,2,3),
47  new stdclass,
48);
49/* loop to test fwrite() with different invalid type of args */
50for($loop_counter = 1; $loop_counter <= count($invalid_args); $loop_counter++) {
51  echo "-- Iteration $loop_counter --\n";
52  var_dump( fwrite($invalid_args[$loop_counter - 1], 10) );
53}
54
55// fwrite() on a file handle which is already closed
56echo "-- Testing fwrite() with closed/unset file handle --\n";
57fclose($file_handle);
58var_dump(fwrite($file_handle,"data"));
59
60// fwrite on a file handle which is unset
61$fp = fopen($filename, "w");
62unset($fp); //unset file handle
63var_dump( fwrite(@$fp,"data"));
64
65echo "Done\n";
66?>
67--CLEAN--
68<?php
69$filename = __DIR__."/fwrite_error.tmp";
70unlink( $filename );
71?>
72--EXPECTF--
73*** Testing fwrite() : error conditions ***
74-- Testing fwrite() with less than expected number of arguments --
75
76Warning: fwrite() expects at least 2 parameters, 0 given in %s on line %d
77bool(false)
78
79Warning: fwrite() expects at least 2 parameters, 1 given in %s on line %d
80bool(false)
81-- Testing fwrite() with more than expected number of arguments --
82
83Warning: fwrite() expects at most 3 parameters, 4 given in %s on line %d
84bool(false)
85-- Testing fwrite() with invalid length arguments --
86int(0)
87int(0)
88-- Testing fwrite() with invalid arguments --
89-- Iteration 1 --
90
91Warning: fwrite() expects parameter 1 to be resource, string given in %s on line %d
92bool(false)
93-- Iteration 2 --
94
95Warning: fwrite() expects parameter 1 to be resource, int given in %s on line %d
96bool(false)
97-- Iteration 3 --
98
99Warning: fwrite() expects parameter 1 to be resource, float given in %s on line %d
100bool(false)
101-- Iteration 4 --
102
103Warning: fwrite() expects parameter 1 to be resource, bool given in %s on line %d
104bool(false)
105-- Iteration 5 --
106
107Warning: fwrite() expects parameter 1 to be resource, array given in %s on line %d
108bool(false)
109-- Iteration 6 --
110
111Warning: fwrite() expects parameter 1 to be resource, object given in %s on line %d
112bool(false)
113-- Testing fwrite() with closed/unset file handle --
114
115Warning: fwrite(): supplied resource is not a valid stream resource in %s on line %d
116bool(false)
117
118Warning: fwrite() expects parameter 1 to be resource, null given in %s on line %d
119bool(false)
120Done
121