DateTime_construct_variation1.phpt 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. --TEST--
  2. Test new DateTime() function : usage variation - Passing unexpected values to first argument $time.
  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 first argument \$time***\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. $timezone = new DateTimeZone("Europe/London");
  78. foreach($inputs as $variation =>$time) {
  79. echo "\n-- $variation --\n";
  80. try {
  81. var_dump( new DateTime($time) );
  82. } catch(Exception $e) {
  83. $msg = $e->getMessage();
  84. echo "FAILED: " . $msg . "\n";
  85. }
  86. try {
  87. var_dump( new DateTime($time, $timezone) );
  88. } catch(Exception$e) {
  89. $msg = $e->getMessage();
  90. echo "FAILED: " . $msg . "\n";
  91. }
  92. };
  93. // closing the resource
  94. fclose( $file_handle);
  95. ?>
  96. ===DONE===
  97. --EXPECTF--
  98. *** Testing new DateTime(): usage variation - unexpected values to first argument $time***
  99. -- int 0 --
  100. FAILED: DateTime::__construct(): Failed to parse time string (0) at position 0 (0): Unexpected character
  101. FAILED: DateTime::__construct(): Failed to parse time string (0) at position 0 (0): Unexpected character
  102. -- int 1 --
  103. FAILED: DateTime::__construct(): Failed to parse time string (1) at position 0 (1): Unexpected character
  104. FAILED: DateTime::__construct(): Failed to parse time string (1) at position 0 (1): Unexpected character
  105. -- int 12345 --
  106. FAILED: DateTime::__construct(): Failed to parse time string (12345) at position 4 (5): Unexpected character
  107. FAILED: DateTime::__construct(): Failed to parse time string (12345) at position 4 (5): Unexpected character
  108. -- int -12345 --
  109. FAILED: DateTime::__construct(): Failed to parse time string (-12345) at position 5 (5): Unexpected character
  110. FAILED: DateTime::__construct(): Failed to parse time string (-12345) at position 5 (5): Unexpected character
  111. -- float 10.5 --
  112. object(DateTime)#%d (3) {
  113. ["date"]=>
  114. string(26) "%s"
  115. ["timezone_type"]=>
  116. int(3)
  117. ["timezone"]=>
  118. string(13) "Europe/London"
  119. }
  120. object(DateTime)#%d (3) {
  121. ["date"]=>
  122. string(26) "%s"
  123. ["timezone_type"]=>
  124. int(3)
  125. ["timezone"]=>
  126. string(13) "Europe/London"
  127. }
  128. -- float -10.5 --
  129. FAILED: DateTime::__construct(): Failed to parse time string (-10.5) at position 4 (5): Unexpected character
  130. FAILED: DateTime::__construct(): Failed to parse time string (-10.5) at position 4 (5): Unexpected character
  131. -- float .5 --
  132. object(DateTime)#%d (3) {
  133. ["date"]=>
  134. string(26) "%s"
  135. ["timezone_type"]=>
  136. int(3)
  137. ["timezone"]=>
  138. string(13) "Europe/London"
  139. }
  140. object(DateTime)#%d (3) {
  141. ["date"]=>
  142. string(26) "%s"
  143. ["timezone_type"]=>
  144. int(3)
  145. ["timezone"]=>
  146. string(13) "Europe/London"
  147. }
  148. -- empty array --
  149. FAILED: DateTime::__construct() expects parameter 1 to be string, array given
  150. FAILED: DateTime::__construct() expects parameter 1 to be string, array given
  151. -- int indexed array --
  152. FAILED: DateTime::__construct() expects parameter 1 to be string, array given
  153. FAILED: DateTime::__construct() expects parameter 1 to be string, array given
  154. -- associative array --
  155. FAILED: DateTime::__construct() expects parameter 1 to be string, array given
  156. FAILED: DateTime::__construct() expects parameter 1 to be string, array given
  157. -- nested arrays --
  158. FAILED: DateTime::__construct() expects parameter 1 to be string, array given
  159. FAILED: DateTime::__construct() expects parameter 1 to be string, array given
  160. -- uppercase NULL --
  161. object(DateTime)#%d (3) {
  162. ["date"]=>
  163. string(26) "%s"
  164. ["timezone_type"]=>
  165. int(3)
  166. ["timezone"]=>
  167. string(13) "Europe/London"
  168. }
  169. object(DateTime)#%d (3) {
  170. ["date"]=>
  171. string(26) "%s"
  172. ["timezone_type"]=>
  173. int(3)
  174. ["timezone"]=>
  175. string(13) "Europe/London"
  176. }
  177. -- lowercase null --
  178. object(DateTime)#%d (3) {
  179. ["date"]=>
  180. string(26) "%s"
  181. ["timezone_type"]=>
  182. int(3)
  183. ["timezone"]=>
  184. string(13) "Europe/London"
  185. }
  186. object(DateTime)#%d (3) {
  187. ["date"]=>
  188. string(26) "%s"
  189. ["timezone_type"]=>
  190. int(3)
  191. ["timezone"]=>
  192. string(13) "Europe/London"
  193. }
  194. -- lowercase true --
  195. FAILED: DateTime::__construct(): Failed to parse time string (1) at position 0 (1): Unexpected character
  196. FAILED: DateTime::__construct(): Failed to parse time string (1) at position 0 (1): Unexpected character
  197. -- lowercase false --
  198. object(DateTime)#%d (3) {
  199. ["date"]=>
  200. string(26) "%s"
  201. ["timezone_type"]=>
  202. int(3)
  203. ["timezone"]=>
  204. string(13) "Europe/London"
  205. }
  206. object(DateTime)#%d (3) {
  207. ["date"]=>
  208. string(26) "%s"
  209. ["timezone_type"]=>
  210. int(3)
  211. ["timezone"]=>
  212. string(13) "Europe/London"
  213. }
  214. -- uppercase TRUE --
  215. FAILED: DateTime::__construct(): Failed to parse time string (1) at position 0 (1): Unexpected character
  216. FAILED: DateTime::__construct(): Failed to parse time string (1) at position 0 (1): Unexpected character
  217. -- uppercase FALSE --
  218. object(DateTime)#%d (3) {
  219. ["date"]=>
  220. string(26) "%s"
  221. ["timezone_type"]=>
  222. int(3)
  223. ["timezone"]=>
  224. string(13) "Europe/London"
  225. }
  226. object(DateTime)#%d (3) {
  227. ["date"]=>
  228. string(26) "%s"
  229. ["timezone_type"]=>
  230. int(3)
  231. ["timezone"]=>
  232. string(13) "Europe/London"
  233. }
  234. -- empty string DQ --
  235. object(DateTime)#%d (3) {
  236. ["date"]=>
  237. string(26) "%s"
  238. ["timezone_type"]=>
  239. int(3)
  240. ["timezone"]=>
  241. string(13) "Europe/London"
  242. }
  243. object(DateTime)#%d (3) {
  244. ["date"]=>
  245. string(26) "%s"
  246. ["timezone_type"]=>
  247. int(3)
  248. ["timezone"]=>
  249. string(13) "Europe/London"
  250. }
  251. -- empty string SQ --
  252. object(DateTime)#%d (3) {
  253. ["date"]=>
  254. string(26) "%s"
  255. ["timezone_type"]=>
  256. int(3)
  257. ["timezone"]=>
  258. string(13) "Europe/London"
  259. }
  260. object(DateTime)#%d (3) {
  261. ["date"]=>
  262. string(26) "%s"
  263. ["timezone_type"]=>
  264. int(3)
  265. ["timezone"]=>
  266. string(13) "Europe/London"
  267. }
  268. -- string DQ --
  269. FAILED: DateTime::__construct(): Failed to parse time string (string) at position 0 (s): The timezone could not be found in the database
  270. FAILED: DateTime::__construct(): Failed to parse time string (string) at position 0 (s): The timezone could not be found in the database
  271. -- string SQ --
  272. FAILED: DateTime::__construct(): Failed to parse time string (string) at position 0 (s): The timezone could not be found in the database
  273. FAILED: DateTime::__construct(): Failed to parse time string (string) at position 0 (s): The timezone could not be found in the database
  274. -- mixed case string --
  275. FAILED: DateTime::__construct(): Failed to parse time string (sTrInG) at position 0 (s): The timezone could not be found in the database
  276. FAILED: DateTime::__construct(): Failed to parse time string (sTrInG) at position 0 (s): The timezone could not be found in the database
  277. -- heredoc --
  278. FAILED: DateTime::__construct(): Failed to parse time string (hello world) at position 0 (h): The timezone could not be found in the database
  279. FAILED: DateTime::__construct(): Failed to parse time string (hello world) at position 0 (h): The timezone could not be found in the database
  280. -- instance of classWithToString --
  281. FAILED: DateTime::__construct(): Failed to parse time string (Class A object) at position 0 (C): The timezone could not be found in the database
  282. FAILED: DateTime::__construct(): Failed to parse time string (Class A object) at position 0 (C): The timezone could not be found in the database
  283. -- instance of classWithoutToString --
  284. FAILED: DateTime::__construct() expects parameter 1 to be string, object given
  285. FAILED: DateTime::__construct() expects parameter 1 to be string, object given
  286. -- undefined var --
  287. object(DateTime)#%d (3) {
  288. ["date"]=>
  289. string(26) "%s"
  290. ["timezone_type"]=>
  291. int(3)
  292. ["timezone"]=>
  293. string(13) "Europe/London"
  294. }
  295. object(DateTime)#%d (3) {
  296. ["date"]=>
  297. string(26) "%s"
  298. ["timezone_type"]=>
  299. int(3)
  300. ["timezone"]=>
  301. string(13) "Europe/London"
  302. }
  303. -- unset var --
  304. object(DateTime)#%d (3) {
  305. ["date"]=>
  306. string(26) "%s"
  307. ["timezone_type"]=>
  308. int(3)
  309. ["timezone"]=>
  310. string(13) "Europe/London"
  311. }
  312. object(DateTime)#%d (3) {
  313. ["date"]=>
  314. string(26) "%s"
  315. ["timezone_type"]=>
  316. int(3)
  317. ["timezone"]=>
  318. string(13) "Europe/London"
  319. }
  320. -- resource --
  321. FAILED: DateTime::__construct() expects parameter 1 to be string, resource given
  322. FAILED: DateTime::__construct() expects parameter 1 to be string, resource given
  323. ===DONE===