Skip to content

Commit

Permalink
OXDEV-7573 Get float value via ShopConfigurationSettingDao
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Fedurtsya <[email protected]>
  • Loading branch information
Sieg committed Nov 16, 2023
1 parent 353e175 commit b9ca792
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 48 deletions.
44 changes: 25 additions & 19 deletions src/Setting/Infrastructure/ShopSettingRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use Doctrine\DBAL\Result;
use OxidEsales\EshopCommunity\Internal\Framework\Config\Dao\ShopConfigurationSettingDaoInterface;
use OxidEsales\EshopCommunity\Internal\Framework\Config\DataObject\ShopConfigurationSetting;
use OxidEsales\EshopCommunity\Internal\Framework\Config\Utility\ShopSettingEncoderInterface;
use OxidEsales\EshopCommunity\Internal\Framework\Database\QueryBuilderFactoryInterface;
use OxidEsales\EshopCommunity\Internal\Framework\Theme\Event\ThemeSettingChangedEvent;
Expand All @@ -35,31 +36,23 @@ public function __construct(

public function getInteger(string $name): int
{
$value = $this->configurationSettingDao->get(
$name,
$this->basicContext->getCurrentShopId()
);

if ($value->getType() !== FieldType::NUMBER) {
throw new WrongSettingTypeException();
}
$setting = $this->getShopSetting($name);
$this->checkSettingType($setting, FieldType::NUMBER);

return (int)$value->getValue();
return (int)$setting->getValue();
}

public function getFloat(ID $name): float
public function getFloat(string $name): float
{
try {
$value = $this->getSettingValue($name, FieldType::NUMBER);
} catch (NotFound $e) {
$this->throwGetterNotFoundException('float');
}
$setting = $this->getShopSetting($name);
$this->checkSettingType($setting, FieldType::NUMBER);

if (!$this->isFloatString($value)) {
throw new UnexpectedValueException('The queried configuration was found as an integer, not a float');
}
return (float)$setting->getValue();
}

return (float)$value;
protected function getShopSetting(string $name): ShopConfigurationSetting
{
return $this->configurationSettingDao->get($name, $this->basicContext->getCurrentShopId());
}

public function getBoolean(ID $name): bool
Expand Down Expand Up @@ -128,6 +121,19 @@ protected function throwGetterNotFoundException(string $typeString): void
throw new NotFound("The queried name couldn't be found as $aOrAn $typeString configuration");
}

/**
* @param ShopConfigurationSetting $value
* @param string $requiredType
* @return void
* @throws WrongSettingTypeException
*/
public function checkSettingType(ShopConfigurationSetting $value, string $requiredType): void
{
if ($value->getType() !== $requiredType) {
throw new WrongSettingTypeException();
}
}

protected function throwSetterNotFoundException(string $typeString, string $name): void
{
throw new NotFound('The ' . $typeString . ' setting "' . $name . '" doesn\'t exist');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ interface ShopSettingRepositoryInterface
{
public function getInteger(string $name): int;

public function getFloat(ID $name): float;
public function getFloat(string $name): float;

public function getBoolean(ID $name): bool;

Expand Down
4 changes: 2 additions & 2 deletions src/Setting/Service/ShopSettingService.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ public function getIntegerSetting(string $name): IntegerSetting
return new IntegerSetting(new ID($name), $integer);
}

public function getFloatSetting(ID $name): FloatSetting
public function getFloatSetting(string $name): FloatSetting
{
$float = $this->shopSettingRepository->getFloat($name);
return new FloatSetting($name, $float);
return new FloatSetting(new ID($name), $float);
}

public function getBooleanSetting(ID $name): BooleanSetting
Expand Down
2 changes: 1 addition & 1 deletion src/Setting/Service/ShopSettingServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ interface ShopSettingServiceInterface
{
public function getIntegerSetting(string $name): IntegerSetting;

public function getFloatSetting(ID $name): FloatSetting;
public function getFloatSetting(string $name): FloatSetting;

public function getBooleanSetting(ID $name): BooleanSetting;

Expand Down
57 changes: 33 additions & 24 deletions tests/Unit/Infrastructure/ShopSettingRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,37 +75,46 @@ public function testGetShopSettingIntegerWrongType(): void

public function testGetShopSettingFloat(): void
{
$nameID = new ID('floatSetting');


$repository = $this->getFetchOneShopSettingRepoInstance('1.23');

$float = $repository->getFloat($nameID);

$this->assertEquals(1.23, $float);
}
$settingName = 'settingName';
$shopId = 3;

public function testGetNoShopSettingFloat(): void
{
$nameID = new ID('NotExistingSetting');
$shopSettingValue = 1.23;
$shopSettingType = 'num';

$repository = $this->getFetchOneShopSettingRepoInstance(false);
$expectedValue = 1.23;

$this->expectException(NotFound::class);
$this->expectExceptionMessage('The queried name couldn\'t be found as a float configuration');
$repository->getFloat($nameID);
}
$shopSettingDaoStub = $this->createMock(ShopConfigurationSettingDaoInterface::class);
$shopSettingDaoStub->method('get')
->with($settingName, $shopId)
->willReturn(
$this->createConfiguredMock(ShopConfigurationSetting::class, [
'getName' => $settingName,
'getType' => $shopSettingType,
'getValue' => $shopSettingValue
])
);

public function testGetShopSettingInvalidFloat(): void
{
$nameID = new ID('intSetting');
$basicContext = $this->createStub(BasicContextInterface::class);
$basicContext->method('getCurrentShopId')->willReturn($shopId);

$repository = $this->getFetchOneShopSettingRepoInstance('123');
$sut = $this->getSut(
basicContext: $basicContext,
shopSettingDao: $shopSettingDaoStub
);

$this->expectException(UnexpectedValueException::class);
$this->expectExceptionMessage('The queried configuration was found as an integer, not a float');
$repository->getFloat($nameID);
$this->assertSame($expectedValue, $sut->getFloat($settingName));
}
//
// public function testGetShopSettingInvalidFloat(): void
// {
// $nameID = new ID('intSetting');
//
// $repository = $this->getFetchOneShopSettingRepoInstance('123');
//
// $this->expectException(UnexpectedValueException::class);
// $this->expectExceptionMessage('The queried configuration was found as an integer, not a float');
// $repository->getFloat($nameID);
// }

public function testGetShopSettingBooleanNegativ(): void
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Service/ShopSettingServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function testGetShopSettingFloat(): void

$this->assertEquals(
new FloatSetting($nameID, $repositoryResult),
$sut->getFloatSetting($nameID)
$sut->getFloatSetting((string)$nameID)
);
}

Expand Down

0 comments on commit b9ca792

Please sign in to comment.