From fb5066a2d71cb8b57151582e9f8a833b99f7b382 Mon Sep 17 00:00:00 2001 From: Oliver Klee Date: Tue, 4 Feb 2025 19:06:44 +0100 Subject: [PATCH] [BUGFIX] Improve type safety for lazy-loaded associations (#1592) `_loadRealInstance` will return `null` if the target of the association does not exist (anymore). We need to handle that case in a clean way. Also use a dedicated folder for the fixture to make the fixture easier to find, and to discourage reuse of fixtures. --- Classes/Domain/Model/Tea.php | 3 +-- .../propertyMapping/TeaWithDeletedImage.csv | 3 +++ .../Domain/Repository/TeaRepositoryTest.php | 13 +++++++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 Tests/Functional/Domain/Repository/Fixtures/propertyMapping/TeaWithDeletedImage.csv diff --git a/Classes/Domain/Model/Tea.php b/Classes/Domain/Model/Tea.php index eb7d3fc4..21304b4c 100644 --- a/Classes/Domain/Model/Tea.php +++ b/Classes/Domain/Model/Tea.php @@ -52,9 +52,8 @@ public function setDescription(string $description): void public function getImage(): ?FileReference { if ($this->image instanceof LazyLoadingProxy) { - /** @var FileReference $image */ $image = $this->image->_loadRealInstance(); - $this->image = $image; + $this->image = ($image instanceof FileReference) ? $image : null; } return $this->image; diff --git a/Tests/Functional/Domain/Repository/Fixtures/propertyMapping/TeaWithDeletedImage.csv b/Tests/Functional/Domain/Repository/Fixtures/propertyMapping/TeaWithDeletedImage.csv new file mode 100644 index 00000000..65f39870 --- /dev/null +++ b/Tests/Functional/Domain/Repository/Fixtures/propertyMapping/TeaWithDeletedImage.csv @@ -0,0 +1,3 @@ +"tx_tea_domain_model_tea" +,"uid","title","image", +,1,"Gunpowder",1 diff --git a/Tests/Functional/Domain/Repository/TeaRepositoryTest.php b/Tests/Functional/Domain/Repository/TeaRepositoryTest.php index 08f6558a..9b45a196 100644 --- a/Tests/Functional/Domain/Repository/TeaRepositoryTest.php +++ b/Tests/Functional/Domain/Repository/TeaRepositoryTest.php @@ -115,6 +115,19 @@ public function fillsImageRelation(): void self::assertSame(1, $image->getUid()); } + /** + * @test + */ + public function MapsDeletedImageRelationToNull(): void + { + $this->importCSVDataSet(__DIR__ . '/Fixtures/propertyMapping/TeaWithDeletedImage.csv'); + + $model = $this->subject->findByUid(1); + self::assertInstanceOf(Tea::class, $model); + + self::assertNull($model->getImage()); + } + /** * @test */