Skip to content

Commit

Permalink
Merge pull request #178 from tomwalder/update-unit-test
Browse files Browse the repository at this point in the history
Update unit tests
  • Loading branch information
tomwalder authored Oct 27, 2020
2 parents 1cf7cc6 + 8583586 commit 05f8019
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ php:
- 5.6
- 7.0
- 7.1
- 7.2
- 7.3

before_script:
- composer self-update
Expand Down
5 changes: 4 additions & 1 deletion tests/GatewayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ public function testDelete()
{
$obj_entity = new GDS\Entity();
/** @var \GDS\Gateway $obj_gateway */
$obj_gateway = $this->getMockBuilder('\\GDS\\Gateway\\ProtoBuf')->setMethods(['deleteMulti'])->setConstructorArgs(['DatasetTest'])->getMock();
$obj_gateway = $this->getMockBuilder('\\GDS\\Gateway\\ProtoBuf')
->setMethods(['deleteMulti'])
->setConstructorArgs(['DatasetTest'])
->getMock();
$obj_gateway->expects($this->once())->method('deleteMulti')->with(
$this->equalTo([$obj_entity])
);
Expand Down
78 changes: 78 additions & 0 deletions tests/ProtoBufMapperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

/**
* Copyright 2020 Tom Walder
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* Tests for ProtoBuf Mapper
*
* @author Tom Walder <twalder@gmail.com>
*/
class ProtoBufMapperTest extends \PHPUnit_Framework_TestCase {

/**
* Additional test for timestamp microsecond handling
*/
public function testDateTimeMapToGoogle()
{
$obj_schema = (new \GDS\Schema('Person'))->addDatetime('retirement');

$obj_mapper = new \GDS\Mapper\ProtoBuf();
$obj_mapper->setSchema($obj_schema);

$obj_gds_entity = new \GDS\Entity();
$obj_gds_entity->setSchema($obj_schema);
$obj_gds_entity->setKind('Person');

$obj_gds_entity->dob = new DateTime('1979-02-05 08:30:00');
$obj_gds_entity->exact = new DateTime('1979-02-05T08:30:00.12345678Z');
$obj_gds_entity->retirement = '2050-01-01 09:00:00';

$obj_target_ent = new \google\appengine\datastore\v4\Entity();
$obj_mapper->mapToGoogle($obj_gds_entity, $obj_target_ent);

/** @var \google\appengine\datastore\v4\Property[] $arr_properties */
$arr_properties = $obj_target_ent->getPropertyList();
$this->assertTrue(is_array($arr_properties));
$this->assertCount(3, $arr_properties);

$arr_props_by_name = [];
foreach($arr_properties as $obj_prop) {
$arr_props_by_name[$obj_prop->getName()] = $obj_prop;
}

$this->assertArrayHasKey('dob', $arr_props_by_name);
$obj_dtm_value = $arr_props_by_name['dob']->getValue();
$this->assertTrue($obj_dtm_value->hasTimestampMicrosecondsValue());
// '1979-02-05T08:30:00.000000Z'
$this->assertEquals('287051400000000', $obj_dtm_value->getTimestampMicrosecondsValue());

$this->assertArrayHasKey('exact', $arr_props_by_name);
$obj_dtm_value = $arr_props_by_name['exact']->getValue();
$this->assertTrue($obj_dtm_value->hasTimestampMicrosecondsValue());
// '1979-02-05T08:30:00.123457Z' 6 OR 7, depending on PHP version (>= 7.2, cuts not rounds)
$this->assertTrue(in_array($obj_dtm_value->getTimestampMicrosecondsValue(), [
'287051400123456', // PHP >= 7.2
'287051400123457', // PHP up to 7.1
]));

$this->assertArrayHasKey('retirement', $arr_props_by_name);
$obj_dtm_value = $arr_props_by_name['retirement']->getValue();
$this->assertTrue($obj_dtm_value->hasTimestampMicrosecondsValue());
// '2050-01-01T09:00:00.000000Z'
$this->assertEquals('2524640400000000', $obj_dtm_value->getTimestampMicrosecondsValue());
}
}
7 changes: 6 additions & 1 deletion tests/RESTv1MapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,12 @@ public function testDateTimeMapToGoogle()

$this->assertObjectHasAttribute('exact', $obj_rest_entity->properties);
$this->assertObjectHasAttribute('timestampValue', $obj_rest_entity->properties->exact);
$this->assertEquals('1979-02-05T08:30:00.123457Z', $obj_rest_entity->properties->exact->timestampValue);

// '1979-02-05T08:30:00.123457Z' 6 OR 7, depending on PHP version (>= 7.2, cuts not rounds)
$this->assertTrue(in_array($obj_rest_entity->properties->exact->timestampValue, [
'1979-02-05T08:30:00.123456Z', // PHP >= 7.2
'1979-02-05T08:30:00.123457Z', // PHP up to 7.1
]));

$this->assertObjectHasAttribute('retirement', $obj_rest_entity->properties);
$this->assertObjectHasAttribute('timestampValue', $obj_rest_entity->properties->retirement);
Expand Down

0 comments on commit 05f8019

Please sign in to comment.