Skip to content

Commit 4920bb6

Browse files
authored
Merge pull request #831 from utopia-php/fix-preserve-dates-datetime-format
2 parents 438cc82 + 402225f commit 4920bb6

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

src/Database/Database.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7396,7 +7396,7 @@ public function increaseDocumentAttribute(
73967396

73977397
$time = DateTime::now();
73987398
$updatedAt = $document->getUpdatedAt();
7399-
$updatedAt = (empty($updatedAt) || !$this->preserveDates) ? $time : $updatedAt;
7399+
$updatedAt = (empty($updatedAt) || !$this->preserveDates) ? $time : DateTime::format(new \DateTime($updatedAt));
74007400
$max = $max ? $max - $value : null;
74017401

74027402
$this->adapter->increaseDocumentAttribute(
@@ -7496,7 +7496,7 @@ public function decreaseDocumentAttribute(
74967496

74977497
$time = DateTime::now();
74987498
$updatedAt = $document->getUpdatedAt();
7499-
$updatedAt = (empty($updatedAt) || !$this->preserveDates) ? $time : $updatedAt;
7499+
$updatedAt = (empty($updatedAt) || !$this->preserveDates) ? $time : DateTime::format(new \DateTime($updatedAt));
75007500
$min = $min ? $min + $value : null;
75017501

75027502
$this->adapter->increaseDocumentAttribute(

tests/e2e/Adapter/Scopes/DocumentTests.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1537,6 +1537,38 @@ public function testIncreaseArrayAttribute(Document $document): void
15371537
}
15381538
}
15391539

1540+
/**
1541+
* @depends testIncreaseDecrease
1542+
*/
1543+
public function testIncreaseDecreasePreserveDates(Document $document): void
1544+
{
1545+
/** @var Database $database */
1546+
$database = $this->getDatabase();
1547+
1548+
$database->setPreserveDates(true);
1549+
1550+
try {
1551+
$before = $database->getDocument('increase_decrease', $document->getId());
1552+
$updatedAt = $before->getUpdatedAt();
1553+
$increase = $before->getAttribute('increase');
1554+
$decrease = $before->getAttribute('decrease');
1555+
1556+
$database->increaseDocumentAttribute('increase_decrease', $document->getId(), 'increase', 1);
1557+
1558+
$after = $database->getDocument('increase_decrease', $document->getId());
1559+
$this->assertSame($increase + 1, $after->getAttribute('increase'));
1560+
$this->assertSame($updatedAt, $after->getUpdatedAt());
1561+
1562+
$database->decreaseDocumentAttribute('increase_decrease', $document->getId(), 'decrease', 1);
1563+
1564+
$after = $database->getDocument('increase_decrease', $document->getId());
1565+
$this->assertSame($decrease - 1, $after->getAttribute('decrease'));
1566+
$this->assertSame($updatedAt, $after->getUpdatedAt());
1567+
} finally {
1568+
$database->setPreserveDates(false);
1569+
}
1570+
}
1571+
15401572
/**
15411573
* @depends testCreateDocument
15421574
*/

0 commit comments

Comments
 (0)