Skip to content

Commit 92aeca6

Browse files
committed
Update unit tests for microtime support in PHP 7.2, 7.3 - also add unit test for ProtoBuf microtime support
1 parent 1cf7cc6 commit 92aeca6

File tree

4 files changed

+87
-2
lines changed

4 files changed

+87
-2
lines changed

.travis.yml

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ php:
77
- 5.6
88
- 7.0
99
- 7.1
10+
- 7.2
11+
- 7.3
1012

1113
before_script:
1214
- composer self-update

tests/GatewayTest.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ public function testDelete()
2929
{
3030
$obj_entity = new GDS\Entity();
3131
/** @var \GDS\Gateway $obj_gateway */
32-
$obj_gateway = $this->getMockBuilder('\\GDS\\Gateway\\ProtoBuf')->setMethods(['deleteMulti'])->setConstructorArgs(['DatasetTest'])->getMock();
32+
$obj_gateway = $this->getMockBuilder('\\GDS\\Gateway\\ProtoBuf')
33+
->setMethods(['deleteMulti'])
34+
->setConstructorArgs(['DatasetTest'])
35+
->getMock();
3336
$obj_gateway->expects($this->once())->method('deleteMulti')->with(
3437
$this->equalTo([$obj_entity])
3538
);

tests/ProtoBufMapperTest.php

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
public function testDateTimeMapToGoogle()
27+
{
28+
$obj_schema = (new \GDS\Schema('Person'))->addDatetime('retirement');
29+
30+
$obj_mapper = new \GDS\Mapper\ProtoBuf();
31+
$obj_mapper->setSchema($obj_schema);
32+
33+
$obj_gds_entity = new \GDS\Entity();
34+
$obj_gds_entity->setSchema($obj_schema);
35+
$obj_gds_entity->setKind('Person');
36+
37+
$obj_gds_entity->dob = new DateTime('1979-02-05 08:30:00');
38+
$obj_gds_entity->exact = new DateTime('1979-02-05T08:30:00.12345678Z');
39+
$obj_gds_entity->retirement = '2050-01-01 09:00:00';
40+
41+
$obj_target_ent = new \google\appengine\datastore\v4\Entity();
42+
$obj_mapper->mapToGoogle($obj_gds_entity, $obj_target_ent);
43+
44+
/** @var \google\appengine\datastore\v4\Property[] $arr_properties */
45+
$arr_properties = $obj_target_ent->getPropertyList();
46+
$this->assertTrue(is_array($arr_properties));
47+
$this->assertCount(3, $arr_properties);
48+
49+
$arr_props_by_name = [];
50+
foreach($arr_properties as $obj_prop) {
51+
$arr_props_by_name[$obj_prop->getName()] = $obj_prop;
52+
}
53+
54+
$this->assertArrayHasKey('dob', $arr_props_by_name);
55+
$obj_dtm_value = $arr_props_by_name['dob']->getValue();
56+
$this->assertTrue($obj_dtm_value->hasTimestampMicrosecondsValue());
57+
// '1979-02-05T08:30:00.000000Z'
58+
$this->assertEquals('287051400000000', $obj_dtm_value->getTimestampMicrosecondsValue());
59+
60+
$this->assertArrayHasKey('exact', $arr_props_by_name);
61+
$obj_dtm_value = $arr_props_by_name['exact']->getValue();
62+
$this->assertTrue($obj_dtm_value->hasTimestampMicrosecondsValue());
63+
// '1979-02-05T08:30:00.123457Z' 6 OR 7, depending on PHP version (>= 7.2, cuts not rounds)
64+
$this->assertTrue(in_array($obj_dtm_value->getTimestampMicrosecondsValue(), [
65+
'287051400123456', // PHP >= 7.2
66+
'287051400123457', // PHP up to 7.1
67+
]));
68+
69+
$this->assertArrayHasKey('retirement', $arr_props_by_name);
70+
$obj_dtm_value = $arr_props_by_name['retirement']->getValue();
71+
$this->assertTrue($obj_dtm_value->hasTimestampMicrosecondsValue());
72+
// '2050-01-01T09:00:00.000000Z'
73+
$this->assertEquals('2524640400000000', $obj_dtm_value->getTimestampMicrosecondsValue());
74+
}
75+
}

tests/RESTv1MapperTest.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,12 @@ public function testDateTimeMapToGoogle()
109109

110110
$this->assertObjectHasAttribute('exact', $obj_rest_entity->properties);
111111
$this->assertObjectHasAttribute('timestampValue', $obj_rest_entity->properties->exact);
112-
$this->assertEquals('1979-02-05T08:30:00.123457Z', $obj_rest_entity->properties->exact->timestampValue);
112+
113+
// '1979-02-05T08:30:00.123457Z' 6 OR 7, depending on PHP version (>= 7.2, cuts not rounds)
114+
$this->assertTrue(in_array($obj_rest_entity->properties->exact->timestampValue, [
115+
'1979-02-05T08:30:00.123456Z', // PHP >= 7.2
116+
'1979-02-05T08:30:00.123457Z', // PHP up to 7.1
117+
]));
113118

114119
$this->assertObjectHasAttribute('retirement', $obj_rest_entity->properties);
115120
$this->assertObjectHasAttribute('timestampValue', $obj_rest_entity->properties->retirement);

0 commit comments

Comments
 (0)