From 09cf9411a6759a35360ff80a6befdac2b9d1486d Mon Sep 17 00:00:00 2001 From: Chris Penny Date: Tue, 3 Oct 2023 08:21:59 +1300 Subject: [PATCH] Add a test case for UnsavedRelationList --- tests/Scenarios/CaresTest.php | 32 ++++++++++++++++++++++++++++++++ tests/Scenarios/CaresTest.yml | 2 ++ 2 files changed, 34 insertions(+) diff --git a/tests/Scenarios/CaresTest.php b/tests/Scenarios/CaresTest.php index c91a5b6..49410a9 100644 --- a/tests/Scenarios/CaresTest.php +++ b/tests/Scenarios/CaresTest.php @@ -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, diff --git a/tests/Scenarios/CaresTest.yml b/tests/Scenarios/CaresTest.yml index a6fe5ef..26e9dec 100644 --- a/tests/Scenarios/CaresTest.yml +++ b/tests/Scenarios/CaresTest.yml @@ -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: