Skip to content

Commit

Permalink
OXDEV-7458 Add save methods for all types in repository
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Fedurtsya <[email protected]>
  • Loading branch information
Sieg committed Nov 17, 2023
1 parent ed8af24 commit 9d36b31
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 3 deletions.
40 changes: 39 additions & 1 deletion src/Setting/Infrastructure/ShopSettingRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,49 @@ public function getArrayFromSettingValue(ShopConfigurationSetting $setting): arr
}

public function saveIntegerSetting(string $name, int $value): void
{
$this->saveAsType(FieldType::NUMBER, $name, $value);
}

public function saveFloatSetting(string $name, float $value): void
{
$this->saveAsType(FieldType::NUMBER, $name, $value);
}

public function saveBooleanSetting(string $name, bool $value): void
{
$this->saveAsType(FieldType::BOOLEAN, $name, $value);
}

public function saveStringSetting(string $name, string $value): void
{
$this->saveAsType(FieldType::STRING, $name, $value);
}

public function saveSelectSetting(string $name, string $value): void
{
$this->saveAsType(FieldType::SELECT, $name, $value);
}

public function saveCollectionSetting(string $name, array $value): void
{
$this->saveAsType(FieldType::ARRAY, $name, $value);
}

public function saveAssocCollectionSetting(string $name, array $value): void
{
$this->saveAsType(FieldType::ASSOCIATIVE_ARRAY, $name, $value);
}

/**
* @param mixed $value
*/
private function saveAsType(string $type, string $name, $value): void
{
$setting = (new ShopConfigurationSetting())
->setShopId($this->basicContext->getCurrentShopId())
->setName($name)
->setType(FieldType::NUMBER)
->setType($type)
->setValue($value);

$this->configurationSettingDao->save($setting);
Expand Down
12 changes: 12 additions & 0 deletions src/Setting/Infrastructure/ShopSettingRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,16 @@ public function getAssocCollection(string $name): array;
public function getSettingsList(): array;

public function saveIntegerSetting(string $name, int $value): void;

public function saveFloatSetting(string $name, float $value): void;

public function saveBooleanSetting(string $name, bool $value): void;

public function saveStringSetting(string $name, string $value): void;

public function saveSelectSetting(string $name, string $value): void;

public function saveCollectionSetting(string $name, array $value): void;

public function saveAssocCollectionSetting(string $name, array $value): void;
}
40 changes: 38 additions & 2 deletions tests/Unit/Infrastructure/ShopSettingRepositorySettersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function testSetShopSetting(string $method, $settingValue, string $settin
$basicContextStub->method('getCurrentShopId')->willReturn($shopId);

$shopSettingDaoSpy = $this->createMock(ShopConfigurationSettingDaoInterface::class);
$shopSettingDaoSpy->expects($this->once())->method('save')->with($this->equalTo($setting));
$shopSettingDaoSpy->expects($this->once())->method('save')->with($setting);

$sut = $this->getSut(
basicContext: $basicContextStub,
Expand All @@ -41,13 +41,49 @@ public function shopSettingsSaveMethodsDataProvider(): \Generator
'settingValue' => 123,
'settingType' => FieldType::NUMBER
];

yield 'float' => [
'method' => 'saveFloatSetting',
'settingValue' => 1.23,
'settingType' => FieldType::NUMBER
];

yield 'boolean' => [
'method' => 'saveBooleanSetting',
'settingValue' => true,
'settingType' => FieldType::BOOLEAN
];

yield 'string' => [
'method' => 'saveStringSetting',
'settingValue' => 'string value',
'settingType' => FieldType::STRING
];

yield 'select' => [
'method' => 'saveSelectSetting',
'settingValue' => 'string select value',
'settingType' => FieldType::SELECT
];

yield 'collection' => [
'method' => 'saveCollectionSetting',
'settingValue' => ['some', 'collection'],
'settingType' => FieldType::ARRAY
];

yield 'assoc collection' => [
'method' => 'saveAssocCollectionSetting',
'settingValue' => ['key1' => 'some', 'key2' => 'collection'],
'settingType' => FieldType::ASSOCIATIVE_ARRAY
];
}

public function buildShopSettingStub(
int $shopId,
string $settingType,
string $settingName,
int $settingValue
$settingValue
): ShopConfigurationSetting {
return (new ShopConfigurationSetting())
->setShopId($shopId)
Expand Down

0 comments on commit 9d36b31

Please sign in to comment.