DateTime_construct_variation2.phpt 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. --TEST--
  2. Test new DateTime() function : usage variation - Passing unexpected values to second argument $timezone.
  3. --FILE--
  4. <?php
  5. /* Prototype : DateTime::__construct ([ string $time="now" [, DateTimeZone $timezone=NULL ]] )
  6. * Description: Returns new DateTime object
  7. * Source code: ext/date/php_date.c
  8. * Alias to functions: date_create
  9. */
  10. echo "*** Testing new DateTime() : usage variation - unexpected values to second argument \$timezone***\n";
  11. //Set the default time zone
  12. date_default_timezone_set("Europe/London");
  13. //get an unset variable
  14. $unset_var = 10;
  15. unset ($unset_var);
  16. // define some classes
  17. class classWithToString
  18. {
  19. public function __toString() {
  20. return "Class A object";
  21. }
  22. }
  23. class classWithoutToString
  24. {
  25. }
  26. // heredoc string
  27. $heredoc = <<<EOT
  28. hello world
  29. EOT;
  30. // add arrays
  31. $index_array = array (1, 2, 3);
  32. $assoc_array = array ('one' => 1, 'two' => 2);
  33. // resource
  34. $file_handle = fopen(__FILE__, 'r');
  35. //array of values to iterate over
  36. $inputs = array(
  37. // int data
  38. 'int 0' => 0,
  39. 'int 1' => 1,
  40. 'int 12345' => 12345,
  41. 'int -12345' => -12345,
  42. // float data
  43. 'float 10.5' => 10.5,
  44. 'float -10.5' => -10.5,
  45. 'float .5' => .5,
  46. // array data
  47. 'empty array' => array(),
  48. 'int indexed array' => $index_array,
  49. 'associative array' => $assoc_array,
  50. 'nested arrays' => array('foo', $index_array, $assoc_array),
  51. // null data
  52. 'uppercase NULL' => NULL,
  53. 'lowercase null' => null,
  54. // boolean data
  55. 'lowercase true' => true,
  56. 'lowercase false' =>false,
  57. 'uppercase TRUE' =>TRUE,
  58. 'uppercase FALSE' =>FALSE,
  59. // empty data
  60. 'empty string DQ' => "",
  61. 'empty string SQ' => '',
  62. // string data
  63. 'string DQ' => "string",
  64. 'string SQ' => 'string',
  65. 'mixed case string' => "sTrInG",
  66. 'heredoc' => $heredoc,
  67. // object data
  68. 'instance of classWithToString' => new classWithToString(),
  69. 'instance of classWithoutToString' => new classWithoutToString(),
  70. // undefined data
  71. 'undefined var' => @$undefined_var,
  72. // unset data
  73. 'unset var' => @$unset_var,
  74. // resource
  75. 'resource' => $file_handle
  76. );
  77. $time = "2005-07-14 22:30:41";
  78. foreach($inputs as $variation =>$timezone) {
  79. echo "\n-- $variation --\n";
  80. try {
  81. var_dump( new DateTime($time, $timezone) );
  82. } catch(Exception $e) {
  83. $msg = $e->getMessage();
  84. echo "FAILED: " . $msg . "\n";
  85. }
  86. };
  87. // closing the resource
  88. fclose( $file_handle);
  89. ?>
  90. ===DONE===
  91. --EXPECTF--
  92. *** Testing new DateTime() : usage variation - unexpected values to second argument $timezone***
  93. -- int 0 --
  94. FAILED: DateTime::__construct() expects parameter 2 to be DateTimeZone, integer given
  95. -- int 1 --
  96. FAILED: DateTime::__construct() expects parameter 2 to be DateTimeZone, integer given
  97. -- int 12345 --
  98. FAILED: DateTime::__construct() expects parameter 2 to be DateTimeZone, integer given
  99. -- int -12345 --
  100. FAILED: DateTime::__construct() expects parameter 2 to be DateTimeZone, integer given
  101. -- float 10.5 --
  102. FAILED: DateTime::__construct() expects parameter 2 to be DateTimeZone, double given
  103. -- float -10.5 --
  104. FAILED: DateTime::__construct() expects parameter 2 to be DateTimeZone, double given
  105. -- float .5 --
  106. FAILED: DateTime::__construct() expects parameter 2 to be DateTimeZone, double given
  107. -- empty array --
  108. FAILED: DateTime::__construct() expects parameter 2 to be DateTimeZone, array given
  109. -- int indexed array --
  110. FAILED: DateTime::__construct() expects parameter 2 to be DateTimeZone, array given
  111. -- associative array --
  112. FAILED: DateTime::__construct() expects parameter 2 to be DateTimeZone, array given
  113. -- nested arrays --
  114. FAILED: DateTime::__construct() expects parameter 2 to be DateTimeZone, array given
  115. -- uppercase NULL --
  116. object(DateTime)#%d (3) {
  117. ["date"]=>
  118. string(26) "2005-07-14 22:30:41.000000"
  119. ["timezone_type"]=>
  120. int(3)
  121. ["timezone"]=>
  122. string(13) "Europe/London"
  123. }
  124. -- lowercase null --
  125. object(DateTime)#%d (3) {
  126. ["date"]=>
  127. string(26) "2005-07-14 22:30:41.000000"
  128. ["timezone_type"]=>
  129. int(3)
  130. ["timezone"]=>
  131. string(13) "Europe/London"
  132. }
  133. -- lowercase true --
  134. FAILED: DateTime::__construct() expects parameter 2 to be DateTimeZone, boolean given
  135. -- lowercase false --
  136. FAILED: DateTime::__construct() expects parameter 2 to be DateTimeZone, boolean given
  137. -- uppercase TRUE --
  138. FAILED: DateTime::__construct() expects parameter 2 to be DateTimeZone, boolean given
  139. -- uppercase FALSE --
  140. FAILED: DateTime::__construct() expects parameter 2 to be DateTimeZone, boolean given
  141. -- empty string DQ --
  142. FAILED: DateTime::__construct() expects parameter 2 to be DateTimeZone, string given
  143. -- empty string SQ --
  144. FAILED: DateTime::__construct() expects parameter 2 to be DateTimeZone, string given
  145. -- string DQ --
  146. FAILED: DateTime::__construct() expects parameter 2 to be DateTimeZone, string given
  147. -- string SQ --
  148. FAILED: DateTime::__construct() expects parameter 2 to be DateTimeZone, string given
  149. -- mixed case string --
  150. FAILED: DateTime::__construct() expects parameter 2 to be DateTimeZone, string given
  151. -- heredoc --
  152. FAILED: DateTime::__construct() expects parameter 2 to be DateTimeZone, string given
  153. -- instance of classWithToString --
  154. FAILED: DateTime::__construct() expects parameter 2 to be DateTimeZone, object given
  155. -- instance of classWithoutToString --
  156. FAILED: DateTime::__construct() expects parameter 2 to be DateTimeZone, object given
  157. -- undefined var --
  158. object(DateTime)#%d (3) {
  159. ["date"]=>
  160. string(26) "2005-07-14 22:30:41.000000"
  161. ["timezone_type"]=>
  162. int(3)
  163. ["timezone"]=>
  164. string(13) "Europe/London"
  165. }
  166. -- unset var --
  167. object(DateTime)#%d (3) {
  168. ["date"]=>
  169. string(26) "2005-07-14 22:30:41.000000"
  170. ["timezone_type"]=>
  171. int(3)
  172. ["timezone"]=>
  173. string(13) "Europe/London"
  174. }
  175. -- resource --
  176. FAILED: DateTime::__construct() expects parameter 2 to be DateTimeZone, resource given
  177. ===DONE===