Skip to content

Commit 7378b20

Browse files
author
john.brichevans
committed
Update null check and add to GRPC with tests
1 parent ff10304 commit 7378b20

File tree

3 files changed

+87
-3
lines changed

3 files changed

+87
-3
lines changed

src/GDS/Mapper/GRPCv1.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use GDS\Property\Geopoint;
2222
use GDS\Schema;
2323
use Google\Cloud\Datastore\V1\PartitionId;
24+
use Google\Protobuf\NullValue;
2425
use Google\Type\LatLng;
2526
use Google\Protobuf\Timestamp;
2627
use Google\Protobuf\Internal\RepeatedField;
@@ -276,8 +277,8 @@ private function configureGooglePropertyValue(array $arr_field_def, $mix_value)
276277
}
277278
$obj_val->setExcludeFromIndexes(!$bol_index);
278279

279-
// null checks
280-
if(null === $mix_value) {
280+
if (null === $mix_value) {
281+
$obj_val->setNullValue(NullValue::NULL_VALUE);
281282
return $obj_val;
282283
}
283284

src/GDS/Mapper/RESTv1.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ protected function createPropertyValue(array $arr_field_def, $mix_value)
401401
}
402402
$obj_property_value->excludeFromIndexes = !$bol_index;
403403

404-
if (is_null($mix_value)) {
404+
if (null === $mix_value) {
405405
$obj_property_value->nullValue = $mix_value;
406406
return $obj_property_value;
407407
}

tests/GRPCv1MapperTest.php

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
use Google\Cloud\Datastore\V1\Entity as GRPC_Entity;
4+
use Google\Cloud\Datastore\V1\PartitionId;
5+
6+
class GRPCv1MapperTest extends \PHPUnit\Framework\TestCase
7+
{
8+
public function testValidValuesMapToGoogleEntity()
9+
{
10+
$obj_schema = (new \GDS\Schema('Person'))
11+
->addString('name')
12+
->addInteger('age')
13+
->addFloat('weight')
14+
->addGeopoint('location')
15+
->addDatetime('dob');
16+
17+
$obj_mapper = new \GDS\Mapper\GRPCv1();
18+
$obj_mapper
19+
->setSchema($obj_schema);
20+
21+
22+
$obj_gds_entity = new \GDS\Entity();
23+
$obj_gds_entity->setSchema($obj_schema);
24+
$obj_gds_entity->setKind('Person');
25+
26+
$obj_gds_entity->name = 'Dave';
27+
$obj_gds_entity->age = 21;
28+
$obj_gds_entity->weight = 92.6;
29+
$obj_gds_entity->location = new \GDS\Property\Geopoint(1.2, 3.4);
30+
$obj_gds_entity->dob = new DateTime('1979-02-05 08:30:00');
31+
32+
$obj_grpc_entity = new GRPC_Entity();
33+
34+
$obj_mapper->mapToGoogle($obj_gds_entity, $obj_grpc_entity);
35+
36+
37+
$obj_properties = json_decode($obj_grpc_entity->serializeToJsonString())->properties;
38+
39+
$this->assertTrue(property_exists($obj_properties->name, 'stringValue'));
40+
$this->assertTrue(property_exists($obj_properties->age, 'integerValue'));
41+
$this->assertTrue(property_exists($obj_properties->weight, 'doubleValue'));
42+
$this->assertTrue(property_exists($obj_properties->location, 'geoPointValue'));
43+
$this->assertTrue(property_exists($obj_properties->dob, 'timestampValue'));
44+
}
45+
46+
public function testNullValuesMapToGoogleEntity()
47+
{
48+
$obj_schema = (new \GDS\Schema('Person'))
49+
->addString('name')
50+
->addInteger('age')
51+
->addFloat('weight')
52+
->addGeopoint('location')
53+
->addDatetime('dob');
54+
55+
$obj_mapper = new \GDS\Mapper\GRPCv1();
56+
$obj_mapper
57+
->setSchema($obj_schema);
58+
59+
60+
$obj_gds_entity = new \GDS\Entity();
61+
$obj_gds_entity->setSchema($obj_schema);
62+
$obj_gds_entity->setKind('Person');
63+
64+
$obj_gds_entity->name = null;
65+
$obj_gds_entity->age = null;
66+
$obj_gds_entity->weight = null;
67+
$obj_gds_entity->location = null;
68+
$obj_gds_entity->dob = null;
69+
70+
$obj_grpc_entity = new GRPC_Entity();
71+
72+
$obj_mapper->mapToGoogle($obj_gds_entity, $obj_grpc_entity);
73+
74+
75+
$obj_properties = json_decode($obj_grpc_entity->serializeToJsonString())->properties;
76+
77+
$this->assertTrue(property_exists($obj_properties->name, 'nullValue'));
78+
$this->assertTrue(property_exists($obj_properties->age, 'nullValue'));
79+
$this->assertTrue(property_exists($obj_properties->weight, 'nullValue'));
80+
$this->assertTrue(property_exists($obj_properties->location, 'nullValue'));
81+
$this->assertTrue(property_exists($obj_properties->dob, 'nullValue'));
82+
}
83+
}

0 commit comments

Comments
 (0)