Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a test case for UnsavedRelationList #77

Merged
merged 1 commit into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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