From 720d9068bd3a7ef1c05d2c6bd99132910ceadc59 Mon Sep 17 00:00:00 2001 From: Jeremy Wadhams Date: Thu, 21 Nov 2024 12:20:42 -0600 Subject: [PATCH 1/2] fix: JsonModels in CollectionOfJsonModel should not be ->isDirty after ->fresh --- app/CollectionOfJsonModels.php | 5 ++++- tests/Unit/Traits/HasJsonModelAttributesTest.php | 2 ++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/app/CollectionOfJsonModels.php b/app/CollectionOfJsonModels.php index 9b3a05f..25b2599 100644 --- a/app/CollectionOfJsonModels.php +++ b/app/CollectionOfJsonModels.php @@ -73,7 +73,10 @@ public function setPrimaryKey(string $key = null): self */ public function fresh(): self { - return $this->fill($this->getLinkedData() ?: []); + return $this + ->fill($this->getLinkedData() ?: []) + // We loaded them from linked data, they are not dirty + ->each(fn ($model) => $model->syncOriginal()); } /** diff --git a/tests/Unit/Traits/HasJsonModelAttributesTest.php b/tests/Unit/Traits/HasJsonModelAttributesTest.php index 7706ebe..b84171b 100644 --- a/tests/Unit/Traits/HasJsonModelAttributesTest.php +++ b/tests/Unit/Traits/HasJsonModelAttributesTest.php @@ -33,6 +33,7 @@ public function testWholeAttributeExists() }; self::assertInstanceOf(ConcreteJsonModel::class, $model->jsonModel); self::assertSame(1, $model->jsonModel->a); + self::assertFalse($model->jsonModel->isDirty(), "Loaded attributes are not dirty"); } public function testWholeAttributeMissingIsEmptyObject() @@ -406,6 +407,7 @@ public function testJsonModelCollectionHydratesChildrenToClass() self::assertCount(1, $model->jsons); self::assertInstanceOf(ConcreteJsonModel::class, $model->jsons->first()); self::assertSame(1, $model->jsons->first()->id); + self::assertFalse($model->jsons->first()->isDirty(), "Loaded models in a collection-attribute are not dirty"); } public function testAttributeCanBeTestedWithIsset() From b230d34a18d3b13d6df4633ce745bc30983d7931 Mon Sep 17 00:00:00 2001 From: Jeremy Wadhams Date: Thu, 21 Nov 2024 12:24:04 -0600 Subject: [PATCH 2/2] fix: additional coverage --- tests/Unit/CollectionOfJsonModelsTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/Unit/CollectionOfJsonModelsTest.php b/tests/Unit/CollectionOfJsonModelsTest.php index b3bfa99..a88caf9 100644 --- a/tests/Unit/CollectionOfJsonModelsTest.php +++ b/tests/Unit/CollectionOfJsonModelsTest.php @@ -378,6 +378,7 @@ public function testElementsThatExistWhenCollectionIsCreatedExist() ->fresh(); self::assertCount(1, $collection); self::assertTrue($collection[0]->exists); + self::assertFalse($collection[0]->isDirty()); } public function testSaveCollectionSkipsCreatingListenerForExistingChildren()