Skip to content

Commit 05f8019

Browse files
authored
Merge pull request #178 from tomwalder/update-unit-test
Update unit tests
2 parents 1cf7cc6 + 8583586 commit 05f8019

File tree

4 files changed

+90
-2
lines changed

4 files changed

+90
-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

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
}

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)