diff --git a/src/Setting/Infrastructure/ShopSettingRepository.php b/src/Setting/Infrastructure/ShopSettingRepository.php index 4335f07..cbfc84d 100644 --- a/src/Setting/Infrastructure/ShopSettingRepository.php +++ b/src/Setting/Infrastructure/ShopSettingRepository.php @@ -214,6 +214,8 @@ public function saveAssocCollectionSetting(string $name, array $value): void */ private function saveAsType(string $type, string $name, $value): void { + $this->validateOriginalSettingType($name, $type); + $setting = (new ShopConfigurationSetting()) ->setShopId($this->basicContext->getCurrentShopId()) ->setName($name) @@ -222,4 +224,12 @@ private function saveAsType(string $type, string $name, $value): void $this->configurationSettingDao->save($setting); } + + private function validateOriginalSettingType(string $originalSettingName, string $type): void + { + $originalSetting = $this->getShopSetting($originalSettingName); + if ($originalSetting->getType() !== $type) { + throw new WrongSettingTypeException(); + } + } } diff --git a/tests/Unit/Infrastructure/ShopSettingRepositorySettersTest.php b/tests/Unit/Infrastructure/ShopSettingRepositorySettersTest.php index 6f1d966..b1faf68 100644 --- a/tests/Unit/Infrastructure/ShopSettingRepositorySettersTest.php +++ b/tests/Unit/Infrastructure/ShopSettingRepositorySettersTest.php @@ -12,6 +12,7 @@ use OxidEsales\EshopCommunity\Internal\Framework\Config\Dao\ShopConfigurationSettingDaoInterface; use OxidEsales\EshopCommunity\Internal\Framework\Config\DataObject\ShopConfigurationSetting; use OxidEsales\GraphQL\ConfigurationAccess\Setting\Enum\FieldType; +use OxidEsales\GraphQL\ConfigurationAccess\Setting\Exception\WrongSettingTypeException; /** * @covers \OxidEsales\GraphQL\ConfigurationAccess\Setting\Infrastructure\ShopSettingRepository @@ -19,21 +20,51 @@ class ShopSettingRepositorySettersTest extends AbstractShopSettingRepositoryTest { /** @dataProvider shopSettingsSaveMethodsDataProvider */ - public function testSetShopSetting(string $method, $settingValue, string $settingType): void - { + public function testSetShopSetting( + string $method, + $settingValue, + string $settingType + ): void { $shopId = 3; $settingName = 'settingName'; - $setting = $this->buildShopSettingStub($shopId, $settingType, $settingName, $settingValue); + $settingToSave = $this->buildShopSettingStub($shopId, $settingType, $settingName, $settingValue); + + $shopSettingDaoSpy = $this->createMock(ShopConfigurationSettingDaoInterface::class); + $shopSettingDaoSpy->method('get')->with($settingName, $shopId)->willReturn($settingToSave); + $shopSettingDaoSpy->expects($this->once())->method('save')->with($settingToSave); + + $sut = $this->getSut( + basicContext: $this->getBasicContextMock($shopId), + shopSettingDao: $shopSettingDaoSpy + ); + + $sut->$method($settingName, $settingValue); + } + + /** @dataProvider shopSettingsSaveMethodsDataProvider */ + public function testSetShopIsNotCalledOnOriginalSettingTypeMissmatch( + string $method, + $settingValue, + string $settingType + ): void { + $shopId = 3; + $settingName = 'settingName'; + + $originalSetting = $this->buildShopSettingStub($shopId, 'differentType', $settingName, $settingValue); + + $settingToSave = $this->buildShopSettingStub($shopId, $settingType, $settingName, $settingValue); $shopSettingDaoSpy = $this->createMock(ShopConfigurationSettingDaoInterface::class); - $shopSettingDaoSpy->expects($this->once())->method('save')->with($setting); + $shopSettingDaoSpy->method('get')->with($settingName, $shopId)->willReturn($originalSetting); + $shopSettingDaoSpy->expects($this->never())->method('save')->with($settingToSave); $sut = $this->getSut( basicContext: $this->getBasicContextMock($shopId), shopSettingDao: $shopSettingDaoSpy ); + $this->expectException(WrongSettingTypeException::class); $sut->$method($settingName, $settingValue); }