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