@@ -122,6 +122,63 @@ public function testDateTimeMapToGoogle()
122
122
123
123
}
124
124
125
+ /**
126
+ * Test data going into Datastore has been correctly converted to UTC when operating in another TZ
127
+ */
128
+ public function testDateTimeMapToGoogleWithTimezone ()
129
+ {
130
+ // Let's use a timezone with no Daylight savings
131
+ // This is -03:00 hours
132
+ $ str_existing_tz = date_default_timezone_get ();
133
+ date_default_timezone_set ('America/Cayenne ' );
134
+
135
+ $ obj_schema = (new \GDS \Schema ('Person ' ))->addDatetime ('retirement ' );
136
+
137
+ $ obj_mapper = new \GDS \Mapper \RESTv1 ();
138
+ $ obj_mapper ->setSchema ($ obj_schema );
139
+
140
+ $ obj_gds_entity = new \GDS \Entity ();
141
+ $ obj_gds_entity ->setSchema ($ obj_schema );
142
+ $ obj_gds_entity ->setKind ('Person ' );
143
+
144
+ $ obj_gds_entity ->zoned = new DateTime ('2021-02-04 08:30:00 ' ); // takes on default timezone
145
+ $ obj_gds_entity ->dob = new DateTime ('1979-02-05T08:30:00+09:00 ' ); // timezone specified
146
+ $ obj_gds_entity ->exact = new DateTime ('1979-02-05T08:30:00.12345678Z ' ); // UTC assumed
147
+ $ obj_gds_entity ->ts = new DateTime ('@946684800 ' ); // UTC assumed
148
+ $ obj_gds_entity ->retirement = '2050-01-01 09:00:00 ' ; // takes on default timezone
149
+
150
+ $ obj_rest_entity = $ obj_mapper ->mapToGoogle ($ obj_gds_entity );
151
+
152
+ $ this ->assertEquals (
153
+ '2021-02-04T11:30:00.000000Z ' ,
154
+ $ obj_rest_entity ->properties ->zoned ->timestampValue ,
155
+ '08:30 (-3) => 11:30 UTC '
156
+ );
157
+
158
+ // 1979-02-05T08:30:00+09:00 => previous day, 23:30
159
+ $ this ->assertEquals (
160
+ '1979-02-04T23:30:00.000000Z ' ,
161
+ $ obj_rest_entity ->properties ->dob ->timestampValue ,
162
+ 'Previous day, 23:30 '
163
+ );
164
+
165
+ // '1979-02-05T08:30:00.123457Z' 6 OR 7, depending on PHP version (>= 7.2, cuts not rounds)
166
+ $ this ->assertTrue (in_array ($ obj_rest_entity ->properties ->exact ->timestampValue , [
167
+ '1979-02-05T08:30:00.123456Z ' , // PHP >= 7.2
168
+ '1979-02-05T08:30:00.123457Z ' , // PHP up to 7.1
169
+ ]));
170
+
171
+ //
172
+ $ this ->assertEquals (
173
+ '2050-01-01T12:00:00.000000Z ' ,
174
+ $ obj_rest_entity ->properties ->retirement ->timestampValue ,
175
+ '-3 hours from Y-m-d H:i:s '
176
+ );
177
+
178
+ // Reset the timezone
179
+ date_default_timezone_set ($ str_existing_tz );
180
+ }
181
+
125
182
/**
126
183
* Ensure arrays of lat/lon pairs are supported for geopoints
127
184
*/
@@ -432,14 +489,82 @@ public function testAncestryFromArray()
432
489
$ this ->assertEquals ('Child ' , $ obj_path_last ->kind );
433
490
}
434
491
492
+ /**
493
+ * Confirm we correctly extract DateTime objects from REST responses
494
+ *
495
+ * @throws Exception
496
+ */
497
+ public function testMapDatetimeFromGoogle ()
498
+ {
499
+ $ obj_schema = (new \GDS \Schema ('Event ' ))->addDatetime ('when ' );
500
+ $ obj_mapper = new \GDS \Mapper \RESTv1 ();
501
+ $ obj_mapper ->setSchema ($ obj_schema );
502
+ $ obj_entity = $ obj_mapper ->mapOneFromResult ($ this ->buildFakeResponse ());
503
+ $ this ->assertInstanceOf ('\\DateTime ' , $ obj_entity ->when );
504
+ $ this ->assertInstanceOf ('\\DateTime ' , $ obj_entity ->then );
505
+ $ str_php_micros = '1412262083.045123 ' ;
506
+ $ this ->assertEquals ($ str_php_micros , $ obj_entity ->when ->format (\GDS \Mapper::DATETIME_FORMAT_UDOTU ));
507
+ $ this ->assertEquals ('2014-10-02 15:01:23 ' , $ obj_entity ->when ->format ('Y-m-d H:i:s ' ));
508
+ $ this ->assertEquals ('2015-11-03 16:02:24 ' , $ obj_entity ->then ->format ('Y-m-d H:i:s ' ));
509
+ }
510
+
511
+ /**
512
+ * Confirm we correctly extract DateTime objects from REST responses
513
+ *
514
+ * @throws Exception
515
+ */
516
+ public function testMapDatetimeFromGoogleInTimezone ()
517
+ {
518
+ $ str_existing_tz = date_default_timezone_get ();
519
+ date_default_timezone_set ('America/Cayenne ' );
435
520
436
- // public function testMapToGoogle()
437
- // {
438
- // $obj_mapper = new \GDS\Mapper\RESTv1();
439
- // $obj_gds_entity = new \GDS\Entity();
440
- // $obj_gds_entity->setKind('Person');
441
- // $obj_rest_entity = $obj_mapper->mapToGoogle($obj_gds_entity);
442
- // $this->assertEquals('expected', $obj_rest_entity->actual);
443
- // }
521
+ $ obj_schema = (new \GDS \Schema ('Event ' ))->addDatetime ('when ' );
522
+ $ obj_mapper = new \GDS \Mapper \RESTv1 ();
523
+ $ obj_mapper ->setSchema ($ obj_schema );
524
+ $ obj_entity = $ obj_mapper ->mapOneFromResult ($ this ->buildFakeResponse ());
525
+ $ this ->assertInstanceOf ('\\DateTime ' , $ obj_entity ->when );
526
+ $ this ->assertInstanceOf ('\\DateTime ' , $ obj_entity ->then );
527
+ $ str_php_micros = '1412262083.045123 ' ;
528
+ $ this ->assertEquals ($ str_php_micros , $ obj_entity ->when ->format (\GDS \Mapper::DATETIME_FORMAT_UDOTU ));
529
+ $ this ->assertEquals ('2014-10-02 12:01:23 ' , $ obj_entity ->when ->format ('Y-m-d H:i:s ' ));
530
+ $ this ->assertEquals ('2015-11-03 13:02:24 ' , $ obj_entity ->then ->format ('Y-m-d H:i:s ' ));
531
+ $ this ->assertEquals ('America/Cayenne ' , $ obj_entity ->when ->getTimezone ()->getName ());
532
+ $ this ->assertEquals ('America/Cayenne ' , $ obj_entity ->then ->getTimezone ()->getName ());
533
+
534
+ // Reset the timezone
535
+ date_default_timezone_set ($ str_existing_tz );
536
+ }
444
537
445
- }
538
+ /**
539
+ * Build a fake REST response payload
540
+ *
541
+ * @return stdClass
542
+ */
543
+ private function buildFakeResponse (): \stdClass
544
+ {
545
+ return (object )[
546
+ 'entity ' => (object ) [
547
+ 'key ' => (object )[
548
+ "partitionId " => (object )[
549
+ "projectId " => 'test-project ' ,
550
+ "namespaceId " => 'test-namespace ' ,
551
+ ],
552
+ 'path ' => [
553
+ (object )[
554
+ "kind " => 'Event ' ,
555
+ "id " => '123456789 ' ,
556
+ ]
557
+ ]
558
+ ],
559
+ 'properties ' => (object )[
560
+ 'when ' => (object )[
561
+ "timestampValue " => '2014-10-02T15:01:23.045123456Z ' ,
562
+ ],
563
+ 'then ' => (object )[
564
+ "timestampValue " => '2015-11-03T16:02:24.055123456Z ' ,
565
+ ],
566
+ ],
567
+ ]
568
+ ];
569
+ }
570
+ }
0 commit comments