Skip to content

Commit 45c00a4

Browse files
authored
fix(database): hydrate deferred writes before staging values (#162)
* fix(database): hydrate deferred writes before staging values * add similar tests for the file handler to prove it is working correctly
1 parent 7dfcace commit 45c00a4

3 files changed

Lines changed: 51 additions & 0 deletions

File tree

src/Handlers/DatabaseHandler.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ public function get(string $class, string $property, ?string $context = null)
8181
public function set(string $class, string $property, $value = null, ?string $context = null): void
8282
{
8383
if ($this->deferWrites) {
84+
$this->hydrate($context);
8485
$this->markPending($class, $property, $value, $context);
8586
} else {
8687
$this->persist($class, $property, $value, $context);
@@ -104,6 +105,8 @@ public function setMany(array $settings, ?string $context = null): void
104105
}
105106

106107
if ($this->deferWrites) {
108+
$this->hydrate($context);
109+
107110
foreach ($settings as $setting) {
108111
$this->markPending($setting['class'], $setting['property'], $setting['value'], $context);
109112
$this->setStored($setting['class'], $setting['property'], $setting['value'], $context);

tests/DatabaseHandlerTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,30 @@ public function testDeferredSetManyPersistsAfterPersist(): void
490490
]);
491491
}
492492

493+
public function testDeferredSetReturnsPendingValueBeforePersist(): void
494+
{
495+
$this->settings->set('Example.siteName', 'StoredSingle');
496+
497+
$deferredSettings = $this->createDeferredSettings();
498+
499+
$deferredSettings->set('Example.siteName', 'PendingSingle');
500+
501+
$this->assertSame('PendingSingle', $deferredSettings->get('Example.siteName'));
502+
}
503+
504+
public function testDeferredSetManyReturnsPendingValueBeforePersist(): void
505+
{
506+
$this->settings->set('Example.siteName', 'StoredBatch');
507+
508+
$deferredSettings = $this->createDeferredSettings();
509+
510+
$deferredSettings->setMany([
511+
'Example.siteName' => 'PendingBatch',
512+
]);
513+
514+
$this->assertSame('PendingBatch', $deferredSettings->get('Example.siteName'));
515+
}
516+
493517
public function testDeferredSetManyPersistsDifferentClassesAfterPersist(): void
494518
{
495519
$deferredSettings = $this->createDeferredSettings();

tests/FileHandlerTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,30 @@ public function testDeferredSetManyPersistsAfterPersist(): void
682682
$this->assertSame('deferred@example.com', $data['siteEmail']['value']);
683683
}
684684

685+
public function testDeferredSetReturnsPendingValueBeforePersist(): void
686+
{
687+
$this->settings->set('Example.siteName', 'FileStoredSingle');
688+
689+
$deferredSettings = $this->createDeferredSettings();
690+
691+
$deferredSettings->set('Example.siteName', 'FilePendingSingle');
692+
693+
$this->assertSame('FilePendingSingle', $deferredSettings->get('Example.siteName'));
694+
}
695+
696+
public function testDeferredSetManyReturnsPendingValueBeforePersist(): void
697+
{
698+
$this->settings->set('Example.siteName', 'FileStoredBatch');
699+
700+
$deferredSettings = $this->createDeferredSettings();
701+
702+
$deferredSettings->setMany([
703+
'Example.siteName' => 'FilePendingBatch',
704+
]);
705+
706+
$this->assertSame('FilePendingBatch', $deferredSettings->get('Example.siteName'));
707+
}
708+
685709
public function testDeferredSetManyPersistsDifferentClassesAfterPersist(): void
686710
{
687711
$deferredSettings = $this->createDeferredSettings();

0 commit comments

Comments
 (0)