1--TEST--
2Test file_get_contents() function : usage variation
3--CREDITS--
4Dave Kelsey <d_kelsey@uk.ibm.com>
5--SKIPIF--
6<?php if (PHP_INT_SIZE != 4) die("skip this test is for 32-bit only");
7--FILE--
8<?php
9/* Prototype  : string file_get_contents(string filename [, bool use_include_path [, resource context [, long offset [, long maxlen]]]])
10 * Description: Read the entire file into a string
11 * Source code: ext/standard/file.c
12 * Alias to functions:
13 */
14
15echo "*** Testing file_get_contents() : usage variation ***\n";
16
17// Define error handler
18function test_error_handler($err_no, $err_msg, $filename, $linenum, $vars) {
19	if (error_reporting() != 0) {
20		// report non-silenced errors
21		echo "Error: $err_no - $err_msg, $filename($linenum)\n";
22	}
23}
24set_error_handler('test_error_handler');
25
26// Initialise function arguments not being substituted (if any)
27$filename = 'FileGetContentsVar5.tmp';
28$absFile = dirname(__FILE__).'/'.$filename;
29$h = fopen($absFile,"w");
30fwrite($h, b"contents read");
31fclose($h);
32
33
34//get an unset variable
35$unset_var = 10;
36unset ($unset_var);
37
38// define some classes
39class classWithToString
40{
41	public function __toString() {
42		return "Class A object";
43	}
44}
45
46class classWithoutToString
47{
48}
49
50// heredoc string
51$heredoc = <<<EOT
52hello world
53EOT;
54
55// add arrays
56$index_array = array (1, 2, 3);
57$assoc_array = array ('one' => 1, 'two' => 2);
58
59//array of values to iterate over
60$inputs = array(
61
62      // int data
63      'int 0' => 0,
64      'int 1' => 1,
65      'int 12345' => 12345,
66      'int -12345' => -12345,
67	  'int -10' => -10,
68
69      // float data
70      'float 10.5' => 10.5,
71      'float -10.5' => -10.5,
72      'float -22.5' => -22.5,
73      'float 12.3456789000e10' => 12.3456789000e10,
74      'float -12.3456789000e10' => -12.3456789000e10,
75      'float .5' => .5,
76
77      // array data
78      'empty array' => array(),
79      'int indexed array' => $index_array,
80      'associative array' => $assoc_array,
81      'nested arrays' => array('foo', $index_array, $assoc_array),
82
83      // null data
84      'uppercase NULL' => NULL,
85      'lowercase null' => null,
86
87      // boolean data
88      'lowercase true' => true,
89      'lowercase false' =>false,
90      'uppercase TRUE' =>TRUE,
91      'uppercase FALSE' =>FALSE,
92
93      // empty data
94      'empty string DQ' => "",
95      'empty string SQ' => '',
96
97      // string data
98      'string DQ' => "string",
99      'string SQ' => 'string',
100      'mixed case string' => "sTrInG",
101      'heredoc' => $heredoc,
102
103      // object data
104      'instance of classWithToString' => new classWithToString(),
105      'instance of classWithoutToString' => new classWithoutToString(),
106
107      // undefined data
108      'undefined var' => @$undefined_var,
109
110      // unset data
111      'unset var' => @$unset_var,
112);
113
114// loop through each element of the array for offset
115
116foreach($inputs as $key =>$value) {
117      echo "\n--$key--\n";
118      var_dump( file_get_contents($absFile, false, null, $value) );
119};
120
121unlink($absFile);
122
123?>
124===DONE===
125--EXPECTF--
126*** Testing file_get_contents() : usage variation ***
127
128--int 0--
129string(%d) "contents read"
130
131--int 1--
132string(%d) "ontents read"
133
134--int 12345--
135string(%d) ""
136
137--int -12345--
138Error: 2 - file_get_contents(): Failed to seek to position -12345 in the stream, %s(%d)
139bool(false)
140
141--int -10--
142string(10) "tents read"
143
144--float 10.5--
145string(3) "ead"
146
147--float -10.5--
148string(10) "tents read"
149
150--float -22.5--
151Error: 2 - file_get_contents(): Failed to seek to position -22 in the stream, %s(%d)
152bool(false)
153
154--float 12.3456789000e10--
155Error: 2 - file_get_contents() expects parameter 4 to be integer, float given, %s(%d)
156NULL
157
158--float -12.3456789000e10--
159Error: 2 - file_get_contents() expects parameter 4 to be integer, float given, %s(%d)
160NULL
161
162--float .5--
163string(%d) "contents read"
164
165--empty array--
166Error: 2 - file_get_contents() expects parameter 4 to be integer, array given, %s(%d)
167NULL
168
169--int indexed array--
170Error: 2 - file_get_contents() expects parameter 4 to be integer, array given, %s(%d)
171NULL
172
173--associative array--
174Error: 2 - file_get_contents() expects parameter 4 to be integer, array given, %s(%d)
175NULL
176
177--nested arrays--
178Error: 2 - file_get_contents() expects parameter 4 to be integer, array given, %s(%d)
179NULL
180
181--uppercase NULL--
182string(%d) "contents read"
183
184--lowercase null--
185string(%d) "contents read"
186
187--lowercase true--
188string(12) "ontents read"
189
190--lowercase false--
191string(%d) "contents read"
192
193--uppercase TRUE--
194string(12) "ontents read"
195
196--uppercase FALSE--
197string(%d) "contents read"
198
199--empty string DQ--
200Error: 2 - file_get_contents() expects parameter 4 to be integer, %unicode_string_optional% given, %s(%d)
201NULL
202
203--empty string SQ--
204Error: 2 - file_get_contents() expects parameter 4 to be integer, %unicode_string_optional% given, %s(%d)
205NULL
206
207--string DQ--
208Error: 2 - file_get_contents() expects parameter 4 to be integer, %unicode_string_optional% given, %s(%d)
209NULL
210
211--string SQ--
212Error: 2 - file_get_contents() expects parameter 4 to be integer, %unicode_string_optional% given, %s(%d)
213NULL
214
215--mixed case string--
216Error: 2 - file_get_contents() expects parameter 4 to be integer, %unicode_string_optional% given, %s(%d)
217NULL
218
219--heredoc--
220Error: 2 - file_get_contents() expects parameter 4 to be integer, %unicode_string_optional% given, %s(%d)
221NULL
222
223--instance of classWithToString--
224Error: 2 - file_get_contents() expects parameter 4 to be integer, object given, %s(%d)
225NULL
226
227--instance of classWithoutToString--
228Error: 2 - file_get_contents() expects parameter 4 to be integer, object given, %s(%d)
229NULL
230
231--undefined var--
232string(%d) "contents read"
233
234--unset var--
235string(%d) "contents read"
236===DONE===
237