1--TEST-- 2Test sprintf() function : usage variations - unexpected values for format argument 3--FILE-- 4<?php 5/* 6* Testing sprintf() : with different unexpected values for format argument other than the strings 7*/ 8 9echo "*** Testing sprintf() : with unexpected values for format argument ***\n"; 10 11// initialing required variables 12$arg1 = "second arg"; 13$arg2 = "third arg"; 14 15//get an unset variable 16$unset_var = 10; 17unset ($unset_var); 18 19// declaring class 20class sample 21{ 22 public function __toString() { 23 return "Object"; 24 } 25} 26 27// creating a file resource 28$file_handle = fopen(__FILE__, 'r'); 29 30//array of values to iterate over 31$values = array( 32 33 // int data 34 0, 35 1, 36 12345, 37 -2345, 38 39 // float data 40 10.5, 41 -10.5, 42 10.1234567e10, 43 10.7654321E-10, 44 .5, 45 46 // array data 47 array(), 48 array(0), 49 array(1), 50 array(1, 2), 51 array('color' => 'red', 'item' => 'pen'), 52 53 // null data 54 NULL, 55 null, 56 57 // boolean data 58 true, 59 false, 60 TRUE, 61 FALSE, 62 63 // empty data 64 "", 65 '', 66 67 // object data 68 new sample(), 69 70 // undefined data 71 @$undefined_var, 72 73 // unset data 74 @$unset_var, 75 76 // resource data 77 $file_handle 78); 79 80// loop through each element of the array for format 81 82$count = 1; 83foreach($values as $value) { 84 echo "\n-- Iteration $count --\n"; 85 86 // with default argument 87 try { 88 var_dump(sprintf($value)); 89 } catch (TypeError $exception) { 90 echo $exception->getMessage() . "\n"; 91 } 92 93 // with two arguments 94 try { 95 var_dump(sprintf($value, $arg1)); 96 } catch (TypeError $exception) { 97 echo $exception->getMessage() . "\n"; 98 } 99 100 // with three arguments 101 try { 102 var_dump(sprintf($value, $arg1, $arg2)); 103 } catch (TypeError $exception) { 104 echo $exception->getMessage() . "\n"; 105 } 106 107 $count++; 108} 109 110// close the resource 111fclose($file_handle); 112 113echo "Done"; 114?> 115--EXPECT-- 116*** Testing sprintf() : with unexpected values for format argument *** 117 118-- Iteration 1 -- 119string(1) "0" 120string(1) "0" 121string(1) "0" 122 123-- Iteration 2 -- 124string(1) "1" 125string(1) "1" 126string(1) "1" 127 128-- Iteration 3 -- 129string(5) "12345" 130string(5) "12345" 131string(5) "12345" 132 133-- Iteration 4 -- 134string(5) "-2345" 135string(5) "-2345" 136string(5) "-2345" 137 138-- Iteration 5 -- 139string(4) "10.5" 140string(4) "10.5" 141string(4) "10.5" 142 143-- Iteration 6 -- 144string(5) "-10.5" 145string(5) "-10.5" 146string(5) "-10.5" 147 148-- Iteration 7 -- 149string(12) "101234567000" 150string(12) "101234567000" 151string(12) "101234567000" 152 153-- Iteration 8 -- 154string(13) "1.07654321E-9" 155string(13) "1.07654321E-9" 156string(13) "1.07654321E-9" 157 158-- Iteration 9 -- 159string(3) "0.5" 160string(3) "0.5" 161string(3) "0.5" 162 163-- Iteration 10 -- 164sprintf(): Argument #1 ($format) must be of type string, array given 165sprintf(): Argument #1 ($format) must be of type string, array given 166sprintf(): Argument #1 ($format) must be of type string, array given 167 168-- Iteration 11 -- 169sprintf(): Argument #1 ($format) must be of type string, array given 170sprintf(): Argument #1 ($format) must be of type string, array given 171sprintf(): Argument #1 ($format) must be of type string, array given 172 173-- Iteration 12 -- 174sprintf(): Argument #1 ($format) must be of type string, array given 175sprintf(): Argument #1 ($format) must be of type string, array given 176sprintf(): Argument #1 ($format) must be of type string, array given 177 178-- Iteration 13 -- 179sprintf(): Argument #1 ($format) must be of type string, array given 180sprintf(): Argument #1 ($format) must be of type string, array given 181sprintf(): Argument #1 ($format) must be of type string, array given 182 183-- Iteration 14 -- 184sprintf(): Argument #1 ($format) must be of type string, array given 185sprintf(): Argument #1 ($format) must be of type string, array given 186sprintf(): Argument #1 ($format) must be of type string, array given 187 188-- Iteration 15 -- 189string(0) "" 190string(0) "" 191string(0) "" 192 193-- Iteration 16 -- 194string(0) "" 195string(0) "" 196string(0) "" 197 198-- Iteration 17 -- 199string(1) "1" 200string(1) "1" 201string(1) "1" 202 203-- Iteration 18 -- 204string(0) "" 205string(0) "" 206string(0) "" 207 208-- Iteration 19 -- 209string(1) "1" 210string(1) "1" 211string(1) "1" 212 213-- Iteration 20 -- 214string(0) "" 215string(0) "" 216string(0) "" 217 218-- Iteration 21 -- 219string(0) "" 220string(0) "" 221string(0) "" 222 223-- Iteration 22 -- 224string(0) "" 225string(0) "" 226string(0) "" 227 228-- Iteration 23 -- 229string(6) "Object" 230string(6) "Object" 231string(6) "Object" 232 233-- Iteration 24 -- 234string(0) "" 235string(0) "" 236string(0) "" 237 238-- Iteration 25 -- 239string(0) "" 240string(0) "" 241string(0) "" 242 243-- Iteration 26 -- 244sprintf(): Argument #1 ($format) must be of type string, resource given 245sprintf(): Argument #1 ($format) must be of type string, resource given 246sprintf(): Argument #1 ($format) must be of type string, resource given 247Done 248