file_variation3.phpt 5.2 KB

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