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, integer given, %s(%d)
134bool(false)
135
136--int 1--
137Error: 2 - fopen() expects parameter 4 to be resource, integer given, %s(%d)
138bool(false)
139
140--int 12345--
141Error: 2 - fopen() expects parameter 4 to be resource, integer given, %s(%d)
142bool(false)
143
144--int -12345--
145Error: 2 - fopen() expects parameter 4 to be resource, integer given, %s(%d)
146bool(false)
147
148--float 10.5--
149Error: 2 - fopen() expects parameter 4 to be resource, double given, %s(%d)
150bool(false)
151
152--float -10.5--
153Error: 2 - fopen() expects parameter 4 to be resource, double given, %s(%d)
154bool(false)
155
156--float 12.3456789000e10--
157Error: 2 - fopen() expects parameter 4 to be resource, double given, %s(%d)
158bool(false)
159
160--float -12.3456789000e10--
161Error: 2 - fopen() expects parameter 4 to be resource, double given, %s(%d)
162bool(false)
163
164--float .5--
165Error: 2 - fopen() expects parameter 4 to be resource, double 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--
185Error: 2 - fopen() expects parameter 4 to be resource, null given, %s(%d)
186bool(false)
187
188--lowercase null--
189Error: 2 - fopen() expects parameter 4 to be resource, null given, %s(%d)
190bool(false)
191
192--lowercase true--
193Error: 2 - fopen() expects parameter 4 to be resource, boolean given, %s(%d)
194bool(false)
195
196--lowercase false--
197Error: 2 - fopen() expects parameter 4 to be resource, boolean given, %s(%d)
198bool(false)
199
200--uppercase TRUE--
201Error: 2 - fopen() expects parameter 4 to be resource, boolean given, %s(%d)
202bool(false)
203
204--uppercase FALSE--
205Error: 2 - fopen() expects parameter 4 to be resource, boolean given, %s(%d)
206bool(false)
207
208--empty string DQ--
209Error: 2 - fopen() expects parameter 4 to be resource, string given, %s(%d)
210bool(false)
211
212--empty string SQ--
213Error: 2 - fopen() expects parameter 4 to be resource, string given, %s(%d)
214bool(false)
215
216--string DQ--
217Error: 2 - fopen() expects parameter 4 to be resource, string given, %s(%d)
218bool(false)
219
220--string SQ--
221Error: 2 - fopen() expects parameter 4 to be resource, string given, %s(%d)
222bool(false)
223
224--mixed case string--
225Error: 2 - fopen() expects parameter 4 to be resource, string given, %s(%d)
226bool(false)
227
228--heredoc--
229Error: 2 - fopen() expects parameter 4 to be resource, string given, %s(%d)
230bool(false)
231
232--instance of classWithToString--
233Error: 2 - fopen() expects parameter 4 to be resource, object given, %s(%d)
234bool(false)
235
236--instance of classWithoutToString--
237Error: 2 - fopen() expects parameter 4 to be resource, object given, %s(%d)
238bool(false)
239
240--undefined var--
241Error: 2 - fopen() expects parameter 4 to be resource, null given, %s(%d)
242bool(false)
243
244--unset var--
245Error: 2 - fopen() expects parameter 4 to be resource, null given, %s(%d)
246bool(false)
247
248--file resource--
249Error: 2 - fopen(): supplied resource is not a valid Stream-Context resource, %s(%d)
250ok
251===DONE===
252