Skip to content

Commit e49033a

Browse files
committed
Fix EntityMapperTest unit tests
1 parent 03b985a commit e49033a

2 files changed

Lines changed: 31 additions & 29 deletions

File tree

Datastore/src/EntityMapper.php

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -222,18 +222,26 @@ public function convertValue($type, $value, $className = Entity::class)
222222
break;
223223

224224
case 'timestampValue':
225-
// The Serializer converts timestamps to an array [seconds, nanos].
226-
// This code is taking that format to convert it into an Immutable date.
227-
$seconds = $value['seconds'];
228-
$nanos = $value['nanos'] ?? 0;
229-
$microseconds = (int)($nanos / 1000);
230-
$result = \DateTimeImmutable::createFromFormat(
231-
'U.u',
232-
sprintf('%d.%06d', $seconds, $microseconds)
233-
);
225+
if (is_array($value)) {
226+
// The Serializer converts timestamps to an array [seconds, nanos].
227+
// This code is taking that format to convert it into an Immutable date.
228+
$seconds = $value['seconds'];
229+
$nanos = $value['nanos'] ?? 0;
230+
$microseconds = (int)($nanos / 1000);
231+
$result = \DateTimeImmutable::createFromFormat(
232+
'U.u',
233+
sprintf('%d.%06d', $seconds, $microseconds)
234+
);
235+
} else {
236+
// This is to keep compatibility with the previous implementation
237+
$result = \DateTimeImmutable::createFromFormat(self::DATE_FORMAT, $value);
234238

235-
break;
239+
if (!$result) {
240+
$result = \DateTimeImmutable::createFromFormat(self::DATE_FORMAT_NO_MS, $value);
241+
}
242+
}
236243

244+
break;
237245
case 'keyValue':
238246
$namespaceId = (isset($value['partitionId']['namespaceId']))
239247
? $value['partitionId']['namespaceId']

Datastore/tests/Unit/EntityMapperTest.php

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,22 @@ public function testResponseToPropertiesDoubleValue($input, $expected)
109109

110110
public function testResponseToPropertiesTimestampValue()
111111
{
112-
$date = new \DateTimeImmutable;
112+
$seconds = 1678886400;
113+
$nanos = 123456789;
114+
$microseconds = floor($nanos / 1000);
115+
$date = \DateTimeImmutable::createFromFormat(
116+
'U.u',
117+
sprintf('%d.%06d', $seconds, $microseconds)
118+
);
113119

114120
$data = [
115121
'foo' => [
116-
'timestampValue' => $date->format(self::DATE_FORMAT)
122+
// This data is after a Proto has been transformed
123+
// by the serializer class
124+
'timestampValue' => [
125+
'seconds' => $seconds,
126+
'nanos' => $nanos
127+
]
117128
]
118129
];
119130

@@ -676,16 +687,6 @@ public function testValueObjectInt()
676687
$this->assertEquals(1, $int['integerValue']);
677688
}
678689

679-
/**
680-
* @dataProvider valueObjectDoubleCases
681-
*/
682-
public function testValueObjectDoubleForGrpcClient($input, $expected)
683-
{
684-
$double = $this->mapper->valueObject($input);
685-
686-
$this->compareResult($expected, $double['doubleValue']);
687-
}
688-
689690
/**
690691
* @dataProvider valueObjectDoubleForRestCases
691692
*/
@@ -922,16 +923,9 @@ public function datastoreToSimpleDoubleValueCases()
922923
{
923924
return [
924925
[1.1, 1.1],
925-
926-
// Happens when using rest client
927926
['Infinity', INF],
928927
['-Infinity', -INF],
929928
['NaN', NAN],
930-
931-
// Happens when using grpc client
932-
[INF, INF],
933-
[-INF, -INF],
934-
[NAN, NAN]
935929
];
936930
}
937931

0 commit comments

Comments
 (0)