fwrite_variation5.phpt 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. --TEST--
  2. Test fwrite() function : usage variation
  3. --CREDITS--
  4. Dave Kelsey <d_kelsey@uk.ibm.com>
  5. --FILE--
  6. <?php
  7. /* Prototype : int fwrite(resource fp, string str [, int length])
  8. * Description: Binary-safe file write
  9. * Source code: ext/standard/file.c
  10. * Alias to functions: bzwrite fputs gzwrite
  11. */
  12. echo "*** Testing fwrite() : 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 (if any)
  22. $filename = __DIR__ . '/fwriteVar5.tmp';
  23. //get an unset variable
  24. $unset_var = 10;
  25. unset ($unset_var);
  26. // define some classes
  27. class classWithToString
  28. {
  29. public function __toString() {
  30. return "Class A object";
  31. }
  32. }
  33. class classWithoutToString
  34. {
  35. }
  36. // heredoc string
  37. $heredoc = <<<EOT
  38. hello world
  39. EOT;
  40. // add arrays
  41. $index_array = array (1, 2, 3);
  42. $assoc_array = array ('one' => 1, 'two' => 2);
  43. //array of values to iterate over
  44. $inputs = array(
  45. // int data
  46. 'int 0' => 0,
  47. 'int 1' => 1,
  48. 'int 12345' => 12345,
  49. 'int -12345' => -2345,
  50. // float data
  51. 'float 10.5' => 10.5,
  52. 'float -10.5' => -10.5,
  53. 'float 12.3456789000e10' => 12.3456789000e10,
  54. 'float -12.3456789000e10' => -12.3456789000e10,
  55. 'float .5' => .5,
  56. // array data
  57. 'empty array' => array(),
  58. 'int indexed array' => $index_array,
  59. 'associative array' => $assoc_array,
  60. 'nested arrays' => array('foo', $index_array, $assoc_array),
  61. // null data
  62. 'uppercase NULL' => NULL,
  63. 'lowercase null' => null,
  64. // boolean data
  65. 'lowercase true' => true,
  66. 'lowercase false' =>false,
  67. 'uppercase TRUE' =>TRUE,
  68. 'uppercase FALSE' =>FALSE,
  69. // empty data
  70. 'empty string DQ' => "",
  71. 'empty string SQ' => '',
  72. // object data
  73. 'instance of classWithToString' => new classWithToString(),
  74. 'instance of classWithoutToString' => new classWithoutToString(),
  75. // undefined data
  76. 'undefined var' => @$undefined_var,
  77. // unset data
  78. 'unset var' => @$unset_var,
  79. );
  80. // loop through each element of the array for str
  81. foreach($inputs as $key =>$value) {
  82. echo "\n--$key--\n";
  83. $fp = fopen($filename,'w');
  84. fwrite($fp, $value);
  85. fclose($fp);
  86. readfile($filename);
  87. };
  88. unlink($filename);
  89. ?>
  90. ===DONE===
  91. --EXPECTF--
  92. *** Testing fwrite() : usage variation ***
  93. --int 0--
  94. 0
  95. --int 1--
  96. 1
  97. --int 12345--
  98. 12345
  99. --int -12345--
  100. -2345
  101. --float 10.5--
  102. 10.5
  103. --float -10.5--
  104. -10.5
  105. --float 12.3456789000e10--
  106. 123456789000
  107. --float -12.3456789000e10--
  108. -123456789000
  109. --float .5--
  110. 0.5
  111. --empty array--
  112. Error: 2 - fwrite() expects parameter 2 to be string, array given, %s(%d)
  113. --int indexed array--
  114. Error: 2 - fwrite() expects parameter 2 to be string, array given, %s(%d)
  115. --associative array--
  116. Error: 2 - fwrite() expects parameter 2 to be string, array given, %s(%d)
  117. --nested arrays--
  118. Error: 2 - fwrite() expects parameter 2 to be string, array given, %s(%d)
  119. --uppercase NULL--
  120. --lowercase null--
  121. --lowercase true--
  122. 1
  123. --lowercase false--
  124. --uppercase TRUE--
  125. 1
  126. --uppercase FALSE--
  127. --empty string DQ--
  128. --empty string SQ--
  129. --instance of classWithToString--
  130. Class A object
  131. --instance of classWithoutToString--
  132. Error: 2 - fwrite() expects parameter 2 to be string, object given, %s(%d)
  133. --undefined var--
  134. --unset var--
  135. ===DONE===