|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright 2020 Tom Walder |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +/** |
| 20 | + * Tests for ProtoBuf Mapper |
| 21 | + * |
| 22 | + * @author Tom Walder <[email protected]> |
| 23 | + */ |
| 24 | +class ProtoBufMapperTest extends \PHPUnit_Framework_TestCase { |
| 25 | + |
| 26 | + /** |
| 27 | + * Additional test for timestamp microsecond handling |
| 28 | + */ |
| 29 | + public function testDateTimeMapToGoogle() |
| 30 | + { |
| 31 | + $obj_schema = (new \GDS\Schema('Person'))->addDatetime('retirement'); |
| 32 | + |
| 33 | + $obj_mapper = new \GDS\Mapper\ProtoBuf(); |
| 34 | + $obj_mapper->setSchema($obj_schema); |
| 35 | + |
| 36 | + $obj_gds_entity = new \GDS\Entity(); |
| 37 | + $obj_gds_entity->setSchema($obj_schema); |
| 38 | + $obj_gds_entity->setKind('Person'); |
| 39 | + |
| 40 | + $obj_gds_entity->dob = new DateTime('1979-02-05 08:30:00'); |
| 41 | + $obj_gds_entity->exact = new DateTime('1979-02-05T08:30:00.12345678Z'); |
| 42 | + $obj_gds_entity->retirement = '2050-01-01 09:00:00'; |
| 43 | + |
| 44 | + $obj_target_ent = new \google\appengine\datastore\v4\Entity(); |
| 45 | + $obj_mapper->mapToGoogle($obj_gds_entity, $obj_target_ent); |
| 46 | + |
| 47 | + /** @var \google\appengine\datastore\v4\Property[] $arr_properties */ |
| 48 | + $arr_properties = $obj_target_ent->getPropertyList(); |
| 49 | + $this->assertTrue(is_array($arr_properties)); |
| 50 | + $this->assertCount(3, $arr_properties); |
| 51 | + |
| 52 | + $arr_props_by_name = []; |
| 53 | + foreach($arr_properties as $obj_prop) { |
| 54 | + $arr_props_by_name[$obj_prop->getName()] = $obj_prop; |
| 55 | + } |
| 56 | + |
| 57 | + $this->assertArrayHasKey('dob', $arr_props_by_name); |
| 58 | + $obj_dtm_value = $arr_props_by_name['dob']->getValue(); |
| 59 | + $this->assertTrue($obj_dtm_value->hasTimestampMicrosecondsValue()); |
| 60 | + // '1979-02-05T08:30:00.000000Z' |
| 61 | + $this->assertEquals('287051400000000', $obj_dtm_value->getTimestampMicrosecondsValue()); |
| 62 | + |
| 63 | + $this->assertArrayHasKey('exact', $arr_props_by_name); |
| 64 | + $obj_dtm_value = $arr_props_by_name['exact']->getValue(); |
| 65 | + $this->assertTrue($obj_dtm_value->hasTimestampMicrosecondsValue()); |
| 66 | + // '1979-02-05T08:30:00.123457Z' 6 OR 7, depending on PHP version (>= 7.2, cuts not rounds) |
| 67 | + $this->assertTrue(in_array($obj_dtm_value->getTimestampMicrosecondsValue(), [ |
| 68 | + '287051400123456', // PHP >= 7.2 |
| 69 | + '287051400123457', // PHP up to 7.1 |
| 70 | + ])); |
| 71 | + |
| 72 | + $this->assertArrayHasKey('retirement', $arr_props_by_name); |
| 73 | + $obj_dtm_value = $arr_props_by_name['retirement']->getValue(); |
| 74 | + $this->assertTrue($obj_dtm_value->hasTimestampMicrosecondsValue()); |
| 75 | + // '2050-01-01T09:00:00.000000Z' |
| 76 | + $this->assertEquals('2524640400000000', $obj_dtm_value->getTimestampMicrosecondsValue()); |
| 77 | + } |
| 78 | +} |
0 commit comments