Skip to content

Commit

Permalink
Add a test case for UnsavedRelationList
Browse files Browse the repository at this point in the history
  • Loading branch information
chrispenny committed Oct 2, 2023
1 parent 7fa0639 commit 09cf941
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
32 changes: 32 additions & 0 deletions tests/Scenarios/CaresTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,38 @@ public function testManyManyThrough(string $readingMode, string $saveMethod, boo
$this->assertCacheKeyChanges($page, $model, $readingMode, $saveMethod, $expectKeyChange);
}

public function testUnsavedRelationList(): void
{
// Updates are processed as part of scaffold, so we need to flush before we kick off
ProcessedUpdatesService::singleton()->flush();

// A page that is not (yet) saved in the DB
$page = CaresPage::create();

// A model that is saved in the DB
$model = $this->objFromFixture(CaredHasMany::class, 'model2');
// Specifically fetching this way to make sure it's us fetching without any generation of KeyHash
$originalKey = CacheKey::findInStage($page);

// Our $page is not yet saved, so there should be no cache key
$this->assertNull($originalKey);

// Flush updates so that new changes generate new CacheKey hashes
ProcessedUpdatesService::singleton()->flush();

// Add $model to a relationship on Page. At this stage CaredHasMany() is an UnsavedRelationList
$page->CaredHasMany()->add($model);

// The UnsavedRelationList should now be converted to a DataList
$page->write();

// Specifically fetching this way to make sure it's us fetching without any generation of KeyHash
$newKey = CacheKey::findInStage($page);

$this->assertNotNull($newKey);
$this->assertNotEmpty($newKey->KeyHash);
}

protected function assertCacheKeyChanges(
CaresPage $page,
DataObject $model,
Expand Down
2 changes: 2 additions & 0 deletions tests/Scenarios/CaresTest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Terraformers\KeysForCache\Tests\Mocks\Models\CaredHasOneVersionedNonStaged:
Terraformers\KeysForCache\Tests\Mocks\Models\CaredHasMany:
model1:
Title: Has Many Model 1
model2:
Title: Has Many Model 2

Terraformers\KeysForCache\Tests\Mocks\Models\CaredManyMany:
model1:
Expand Down

0 comments on commit 09cf941

Please sign in to comment.