file_variation3.phpt 5.2 KB

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