Skip to content

Commit

Permalink
OXDEV-7458 Validate original setting type before save
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Fedurtsya <[email protected]>
  • Loading branch information
Sieg committed Nov 20, 2023
1 parent fad1e8c commit d6c4a2b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
10 changes: 10 additions & 0 deletions src/Setting/Infrastructure/ShopSettingRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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();
}
}
}
39 changes: 35 additions & 4 deletions tests/Unit/Infrastructure/ShopSettingRepositorySettersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,59 @@
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
*/
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);
}

Expand Down

0 comments on commit d6c4a2b

Please sign in to comment.