123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377 |
- --TEST--
- Test new DateTime() function : usage variation - Passing unexpected values to first argument $time.
- --FILE--
- <?php
- /* Prototype : DateTime::__construct ([ string $time="now" [, DateTimeZone $timezone=NULL ]] )
- * Description: Returns new DateTime object
- * Source code: ext/date/php_date.c
- * Alias to functions: date_create
- */
- echo "*** Testing new DateTime(): usage variation - unexpected values to first argument \$time***\n";
- //Set the default time zone
- date_default_timezone_set("Europe/London");
- //get an unset variable
- $unset_var = 10;
- unset ($unset_var);
- // define some classes
- class classWithToString
- {
- public function __toString() {
- return "Class A object";
- }
- }
- class classWithoutToString
- {
- }
- // heredoc string
- $heredoc = <<<EOT
- hello world
- EOT;
- // add arrays
- $index_array = array (1, 2, 3);
- $assoc_array = array ('one' => 1, 'two' => 2);
- // resource
- $file_handle = fopen(__FILE__, 'r');
- //array of values to iterate over
- $inputs = array(
- // int data
- 'int 0' => 0,
- 'int 1' => 1,
- 'int 12345' => 12345,
- 'int -12345' => -12345,
- // float data
- 'float 10.5' => 10.5,
- 'float -10.5' => -10.5,
- 'float .5' => .5,
- // array data
- 'empty array' => array(),
- 'int indexed array' => $index_array,
- 'associative array' => $assoc_array,
- 'nested arrays' => array('foo', $index_array, $assoc_array),
- // null data
- 'uppercase NULL' => NULL,
- 'lowercase null' => null,
- // boolean data
- 'lowercase true' => true,
- 'lowercase false' =>false,
- 'uppercase TRUE' =>TRUE,
- 'uppercase FALSE' =>FALSE,
- // empty data
- 'empty string DQ' => "",
- 'empty string SQ' => '',
- // string data
- 'string DQ' => "string",
- 'string SQ' => 'string',
- 'mixed case string' => "sTrInG",
- 'heredoc' => $heredoc,
- // object data
- 'instance of classWithToString' => new classWithToString(),
- 'instance of classWithoutToString' => new classWithoutToString(),
- // undefined data
- 'undefined var' => @$undefined_var,
- // unset data
- 'unset var' => @$unset_var,
-
- // resource
- 'resource' => $file_handle
- );
- $timezone = new DateTimeZone("Europe/London");
- foreach($inputs as $variation =>$time) {
- echo "\n-- $variation --\n";
-
- try {
- var_dump( new DateTime($time) );
- } catch(Exception $e) {
- $msg = $e->getMessage();
- echo "FAILED: " . $msg . "\n";
- }
-
- try {
- var_dump( new DateTime($time, $timezone) );
- } catch(Exception$e) {
- $msg = $e->getMessage();
- echo "FAILED: " . $msg . "\n";
- }
- };
- // closing the resource
- fclose( $file_handle);
- ?>
- ===DONE===
- --EXPECTF--
- *** Testing new DateTime(): usage variation - unexpected values to first argument $time***
- -- int 0 --
- FAILED: DateTime::__construct(): Failed to parse time string (0) at position 0 (0): Unexpected character
- FAILED: DateTime::__construct(): Failed to parse time string (0) at position 0 (0): Unexpected character
- -- int 1 --
- FAILED: DateTime::__construct(): Failed to parse time string (1) at position 0 (1): Unexpected character
- FAILED: DateTime::__construct(): Failed to parse time string (1) at position 0 (1): Unexpected character
- -- int 12345 --
- FAILED: DateTime::__construct(): Failed to parse time string (12345) at position 4 (5): Unexpected character
- FAILED: DateTime::__construct(): Failed to parse time string (12345) at position 4 (5): Unexpected character
- -- int -12345 --
- FAILED: DateTime::__construct(): Failed to parse time string (-12345) at position 5 (5): Unexpected character
- FAILED: DateTime::__construct(): Failed to parse time string (-12345) at position 5 (5): Unexpected character
- -- float 10.5 --
- object(DateTime)#%d (3) {
- ["date"]=>
- string(26) "%s"
- ["timezone_type"]=>
- int(3)
- ["timezone"]=>
- string(13) "Europe/London"
- }
- object(DateTime)#%d (3) {
- ["date"]=>
- string(26) "%s"
- ["timezone_type"]=>
- int(3)
- ["timezone"]=>
- string(13) "Europe/London"
- }
- -- float -10.5 --
- FAILED: DateTime::__construct(): Failed to parse time string (-10.5) at position 4 (5): Unexpected character
- FAILED: DateTime::__construct(): Failed to parse time string (-10.5) at position 4 (5): Unexpected character
- -- float .5 --
- object(DateTime)#%d (3) {
- ["date"]=>
- string(26) "%s"
- ["timezone_type"]=>
- int(3)
- ["timezone"]=>
- string(13) "Europe/London"
- }
- object(DateTime)#%d (3) {
- ["date"]=>
- string(26) "%s"
- ["timezone_type"]=>
- int(3)
- ["timezone"]=>
- string(13) "Europe/London"
- }
- -- empty array --
- FAILED: DateTime::__construct() expects parameter 1 to be string, array given
- FAILED: DateTime::__construct() expects parameter 1 to be string, array given
- -- int indexed array --
- FAILED: DateTime::__construct() expects parameter 1 to be string, array given
- FAILED: DateTime::__construct() expects parameter 1 to be string, array given
- -- associative array --
- FAILED: DateTime::__construct() expects parameter 1 to be string, array given
- FAILED: DateTime::__construct() expects parameter 1 to be string, array given
- -- nested arrays --
- FAILED: DateTime::__construct() expects parameter 1 to be string, array given
- FAILED: DateTime::__construct() expects parameter 1 to be string, array given
- -- uppercase NULL --
- object(DateTime)#%d (3) {
- ["date"]=>
- string(26) "%s"
- ["timezone_type"]=>
- int(3)
- ["timezone"]=>
- string(13) "Europe/London"
- }
- object(DateTime)#%d (3) {
- ["date"]=>
- string(26) "%s"
- ["timezone_type"]=>
- int(3)
- ["timezone"]=>
- string(13) "Europe/London"
- }
- -- lowercase null --
- object(DateTime)#%d (3) {
- ["date"]=>
- string(26) "%s"
- ["timezone_type"]=>
- int(3)
- ["timezone"]=>
- string(13) "Europe/London"
- }
- object(DateTime)#%d (3) {
- ["date"]=>
- string(26) "%s"
- ["timezone_type"]=>
- int(3)
- ["timezone"]=>
- string(13) "Europe/London"
- }
- -- lowercase true --
- FAILED: DateTime::__construct(): Failed to parse time string (1) at position 0 (1): Unexpected character
- FAILED: DateTime::__construct(): Failed to parse time string (1) at position 0 (1): Unexpected character
- -- lowercase false --
- object(DateTime)#%d (3) {
- ["date"]=>
- string(26) "%s"
- ["timezone_type"]=>
- int(3)
- ["timezone"]=>
- string(13) "Europe/London"
- }
- object(DateTime)#%d (3) {
- ["date"]=>
- string(26) "%s"
- ["timezone_type"]=>
- int(3)
- ["timezone"]=>
- string(13) "Europe/London"
- }
- -- uppercase TRUE --
- FAILED: DateTime::__construct(): Failed to parse time string (1) at position 0 (1): Unexpected character
- FAILED: DateTime::__construct(): Failed to parse time string (1) at position 0 (1): Unexpected character
- -- uppercase FALSE --
- object(DateTime)#%d (3) {
- ["date"]=>
- string(26) "%s"
- ["timezone_type"]=>
- int(3)
- ["timezone"]=>
- string(13) "Europe/London"
- }
- object(DateTime)#%d (3) {
- ["date"]=>
- string(26) "%s"
- ["timezone_type"]=>
- int(3)
- ["timezone"]=>
- string(13) "Europe/London"
- }
- -- empty string DQ --
- object(DateTime)#%d (3) {
- ["date"]=>
- string(26) "%s"
- ["timezone_type"]=>
- int(3)
- ["timezone"]=>
- string(13) "Europe/London"
- }
- object(DateTime)#%d (3) {
- ["date"]=>
- string(26) "%s"
- ["timezone_type"]=>
- int(3)
- ["timezone"]=>
- string(13) "Europe/London"
- }
- -- empty string SQ --
- object(DateTime)#%d (3) {
- ["date"]=>
- string(26) "%s"
- ["timezone_type"]=>
- int(3)
- ["timezone"]=>
- string(13) "Europe/London"
- }
- object(DateTime)#%d (3) {
- ["date"]=>
- string(26) "%s"
- ["timezone_type"]=>
- int(3)
- ["timezone"]=>
- string(13) "Europe/London"
- }
- -- string DQ --
- FAILED: DateTime::__construct(): Failed to parse time string (string) at position 0 (s): The timezone could not be found in the database
- FAILED: DateTime::__construct(): Failed to parse time string (string) at position 0 (s): The timezone could not be found in the database
- -- string SQ --
- FAILED: DateTime::__construct(): Failed to parse time string (string) at position 0 (s): The timezone could not be found in the database
- FAILED: DateTime::__construct(): Failed to parse time string (string) at position 0 (s): The timezone could not be found in the database
- -- mixed case string --
- FAILED: DateTime::__construct(): Failed to parse time string (sTrInG) at position 0 (s): The timezone could not be found in the database
- FAILED: DateTime::__construct(): Failed to parse time string (sTrInG) at position 0 (s): The timezone could not be found in the database
- -- heredoc --
- FAILED: DateTime::__construct(): Failed to parse time string (hello world) at position 0 (h): The timezone could not be found in the database
- FAILED: DateTime::__construct(): Failed to parse time string (hello world) at position 0 (h): The timezone could not be found in the database
- -- instance of classWithToString --
- FAILED: DateTime::__construct(): Failed to parse time string (Class A object) at position 0 (C): The timezone could not be found in the database
- FAILED: DateTime::__construct(): Failed to parse time string (Class A object) at position 0 (C): The timezone could not be found in the database
- -- instance of classWithoutToString --
- FAILED: DateTime::__construct() expects parameter 1 to be string, object given
- FAILED: DateTime::__construct() expects parameter 1 to be string, object given
- -- undefined var --
- object(DateTime)#%d (3) {
- ["date"]=>
- string(26) "%s"
- ["timezone_type"]=>
- int(3)
- ["timezone"]=>
- string(13) "Europe/London"
- }
- object(DateTime)#%d (3) {
- ["date"]=>
- string(26) "%s"
- ["timezone_type"]=>
- int(3)
- ["timezone"]=>
- string(13) "Europe/London"
- }
- -- unset var --
- object(DateTime)#%d (3) {
- ["date"]=>
- string(26) "%s"
- ["timezone_type"]=>
- int(3)
- ["timezone"]=>
- string(13) "Europe/London"
- }
- object(DateTime)#%d (3) {
- ["date"]=>
- string(26) "%s"
- ["timezone_type"]=>
- int(3)
- ["timezone"]=>
- string(13) "Europe/London"
- }
- -- resource --
- FAILED: DateTime::__construct() expects parameter 1 to be string, resource given
- FAILED: DateTime::__construct() expects parameter 1 to be string, resource given
- ===DONE===
|