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