1--TEST--
2Test fgets() function : basic functionality
3--FILE--
4<?php
5// include the file.inc for common test functions
6include ("file.inc");
7
8$file_modes = array("r", "rb", "rt", "r+", "r+b", "r+t");
9
10$file_content_types = array("numeric", "text", "text_with_new_line", "alphanumeric");
11
12echo "*** Testing fgets() : basic functionality ***\n";
13foreach($file_modes as $file_mode) {
14  echo "\n-- Testing fgets() with file opened using mode $file_mode --\n";
15  foreach($file_content_types as $file_content_type) {
16    echo "-- File content type : $file_content_type --\n";
17    /* create files with $file_content_type */
18    create_files ( __DIR__, 1, $file_content_type, 0755, 50, "w", "fgets_basic", 1, "bytes"); //create a file
19    $filename = __DIR__."/fgets_basic1.tmp"; // this is name of the file created by create_files()
20    $file_handle = fopen($filename, $file_mode);
21    if ( !$file_handle ) {
22      echo "Error: failed to open file $filename!";
23      exit();
24    }
25
26    echo "-- fgets() with default length, file pointer at 0 --\n";
27    var_dump( fgets($file_handle) ); // with default length
28    var_dump( ftell($file_handle) ); // ensure the file pointer position
29    var_dump( feof($file_handle) );  // ensure if eof set
30
31    echo "-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --\n";
32    var_dump( rewind($file_handle) );
33    var_dump( fgets($file_handle, 23) ); // expected: 22 chars
34    var_dump( ftell($file_handle) ); // ensure the file pointer position
35    var_dump( feof($file_handle) );  // ensure if eof set
36
37    //close file
38    fclose($file_handle);
39
40    // delete file
41    delete_file($filename);
42  } // file_content_type loop
43} // file_mode loop
44
45echo "Done\n";
46?>
47--EXPECT--
48*** Testing fgets() : basic functionality ***
49
50-- Testing fgets() with file opened using mode r --
51-- File content type : numeric --
52-- fgets() with default length, file pointer at 0 --
53string(50) "22222222222222222222222222222222222222222222222222"
54int(50)
55bool(true)
56-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
57bool(true)
58string(22) "2222222222222222222222"
59int(22)
60bool(false)
61-- File content type : text --
62-- fgets() with default length, file pointer at 0 --
63string(50) "text text text text text text text text text text "
64int(50)
65bool(true)
66-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
67bool(true)
68string(22) "text text text text te"
69int(22)
70bool(false)
71-- File content type : text_with_new_line --
72-- fgets() with default length, file pointer at 0 --
73string(5) "line
74"
75int(5)
76bool(false)
77-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
78bool(true)
79string(5) "line
80"
81int(5)
82bool(false)
83-- File content type : alphanumeric --
84-- fgets() with default length, file pointer at 0 --
85string(50) "ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 "
86int(50)
87bool(true)
88-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
89bool(true)
90string(22) "ab12 ab12 ab12 ab12 ab"
91int(22)
92bool(false)
93
94-- Testing fgets() with file opened using mode rb --
95-- File content type : numeric --
96-- fgets() with default length, file pointer at 0 --
97string(50) "22222222222222222222222222222222222222222222222222"
98int(50)
99bool(true)
100-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
101bool(true)
102string(22) "2222222222222222222222"
103int(22)
104bool(false)
105-- File content type : text --
106-- fgets() with default length, file pointer at 0 --
107string(50) "text text text text text text text text text text "
108int(50)
109bool(true)
110-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
111bool(true)
112string(22) "text text text text te"
113int(22)
114bool(false)
115-- File content type : text_with_new_line --
116-- fgets() with default length, file pointer at 0 --
117string(5) "line
118"
119int(5)
120bool(false)
121-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
122bool(true)
123string(5) "line
124"
125int(5)
126bool(false)
127-- File content type : alphanumeric --
128-- fgets() with default length, file pointer at 0 --
129string(50) "ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 "
130int(50)
131bool(true)
132-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
133bool(true)
134string(22) "ab12 ab12 ab12 ab12 ab"
135int(22)
136bool(false)
137
138-- Testing fgets() with file opened using mode rt --
139-- File content type : numeric --
140-- fgets() with default length, file pointer at 0 --
141string(50) "22222222222222222222222222222222222222222222222222"
142int(50)
143bool(true)
144-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
145bool(true)
146string(22) "2222222222222222222222"
147int(22)
148bool(false)
149-- File content type : text --
150-- fgets() with default length, file pointer at 0 --
151string(50) "text text text text text text text text text text "
152int(50)
153bool(true)
154-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
155bool(true)
156string(22) "text text text text te"
157int(22)
158bool(false)
159-- File content type : text_with_new_line --
160-- fgets() with default length, file pointer at 0 --
161string(5) "line
162"
163int(5)
164bool(false)
165-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
166bool(true)
167string(5) "line
168"
169int(5)
170bool(false)
171-- File content type : alphanumeric --
172-- fgets() with default length, file pointer at 0 --
173string(50) "ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 "
174int(50)
175bool(true)
176-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
177bool(true)
178string(22) "ab12 ab12 ab12 ab12 ab"
179int(22)
180bool(false)
181
182-- Testing fgets() with file opened using mode r+ --
183-- File content type : numeric --
184-- fgets() with default length, file pointer at 0 --
185string(50) "22222222222222222222222222222222222222222222222222"
186int(50)
187bool(true)
188-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
189bool(true)
190string(22) "2222222222222222222222"
191int(22)
192bool(false)
193-- File content type : text --
194-- fgets() with default length, file pointer at 0 --
195string(50) "text text text text text text text text text text "
196int(50)
197bool(true)
198-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
199bool(true)
200string(22) "text text text text te"
201int(22)
202bool(false)
203-- File content type : text_with_new_line --
204-- fgets() with default length, file pointer at 0 --
205string(5) "line
206"
207int(5)
208bool(false)
209-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
210bool(true)
211string(5) "line
212"
213int(5)
214bool(false)
215-- File content type : alphanumeric --
216-- fgets() with default length, file pointer at 0 --
217string(50) "ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 "
218int(50)
219bool(true)
220-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
221bool(true)
222string(22) "ab12 ab12 ab12 ab12 ab"
223int(22)
224bool(false)
225
226-- Testing fgets() with file opened using mode r+b --
227-- File content type : numeric --
228-- fgets() with default length, file pointer at 0 --
229string(50) "22222222222222222222222222222222222222222222222222"
230int(50)
231bool(true)
232-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
233bool(true)
234string(22) "2222222222222222222222"
235int(22)
236bool(false)
237-- File content type : text --
238-- fgets() with default length, file pointer at 0 --
239string(50) "text text text text text text text text text text "
240int(50)
241bool(true)
242-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
243bool(true)
244string(22) "text text text text te"
245int(22)
246bool(false)
247-- File content type : text_with_new_line --
248-- fgets() with default length, file pointer at 0 --
249string(5) "line
250"
251int(5)
252bool(false)
253-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
254bool(true)
255string(5) "line
256"
257int(5)
258bool(false)
259-- File content type : alphanumeric --
260-- fgets() with default length, file pointer at 0 --
261string(50) "ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 "
262int(50)
263bool(true)
264-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
265bool(true)
266string(22) "ab12 ab12 ab12 ab12 ab"
267int(22)
268bool(false)
269
270-- Testing fgets() with file opened using mode r+t --
271-- File content type : numeric --
272-- fgets() with default length, file pointer at 0 --
273string(50) "22222222222222222222222222222222222222222222222222"
274int(50)
275bool(true)
276-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
277bool(true)
278string(22) "2222222222222222222222"
279int(22)
280bool(false)
281-- File content type : text --
282-- fgets() with default length, file pointer at 0 --
283string(50) "text text text text text text text text text text "
284int(50)
285bool(true)
286-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
287bool(true)
288string(22) "text text text text te"
289int(22)
290bool(false)
291-- File content type : text_with_new_line --
292-- fgets() with default length, file pointer at 0 --
293string(5) "line
294"
295int(5)
296bool(false)
297-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
298bool(true)
299string(5) "line
300"
301int(5)
302bool(false)
303-- File content type : alphanumeric --
304-- fgets() with default length, file pointer at 0 --
305string(50) "ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 "
306int(50)
307bool(true)
308-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
309bool(true)
310string(22) "ab12 ab12 ab12 ab12 ab"
311int(22)
312bool(false)
313Done
314