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