Skip to content

Commit 451d523

Browse files
abnegateclaude
andcommitted
fix: Cast both sides of comparison and fix flaky timing test
Cast both $value and $oldValue by attribute type before comparison to handle adapters like MongoDB that don't cast floats on read. Add usleep before doc4 update to prevent same-millisecond timestamps. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 142b8dd commit 451d523

2 files changed

Lines changed: 18 additions & 8 deletions

File tree

src/Database/Database.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5704,16 +5704,25 @@ public function updateDocument(string $collection, string $id, Document $documen
57045704

57055705
$oldValue = $old->getAttribute($key);
57065706

5707-
// Cast value to attribute type for consistent comparison
5708-
// (e.g. cache JSON round-trip turns float 1.0 into int 1)
5707+
// Cast both values to attribute type for consistent comparison
5708+
// (e.g. cache JSON round-trip turns float 1.0 into int 1,
5709+
// and some adapters may not cast floats on read)
57095710
$attrType = $attributeTypes[$key] ?? null;
5710-
if ($attrType !== null && !\is_null($value) && !($value instanceof Operator)) {
5711-
$value = match ($attrType) {
5712-
self::VAR_FLOAT => (float)$value,
5713-
self::VAR_INTEGER => (int)$value,
5714-
self::VAR_BOOLEAN => (bool)$value,
5715-
default => $value,
5711+
if ($attrType !== null && !($value instanceof Operator)) {
5712+
$castFn = match ($attrType) {
5713+
self::VAR_FLOAT => fn($v) => (float)$v,
5714+
self::VAR_INTEGER => fn($v) => (int)$v,
5715+
self::VAR_BOOLEAN => fn($v) => (bool)$v,
5716+
default => null,
57165717
};
5718+
if ($castFn !== null) {
5719+
if (!\is_null($value)) {
5720+
$value = $castFn($value);
5721+
}
5722+
if (!\is_null($oldValue)) {
5723+
$oldValue = $castFn($oldValue);
5724+
}
5725+
}
57175726
}
57185727

57195728
// If values are not equal we need to update document.

tests/e2e/Adapter/Scopes/DocumentTests.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5834,6 +5834,7 @@ public function testSingleDocumentDateOperations(): void
58345834

58355835
$doc4->setAttribute('$updatedAt', null);
58365836
$doc4->setAttribute('$createdAt', null);
5837+
\usleep(2000); // Ensure updatedAt timestamp differs from creation time
58375838
$updatedDoc4 = $database->updateDocument($collection, 'doc4', document: $doc4);
58385839

58395840
$this->assertEquals($originalCreatedAt4, $updatedDoc4->getAttribute('$createdAt'));

0 commit comments

Comments
 (0)