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