Skip to content

Commit

Permalink
OXDEV-7573 Handle boolean case
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 adf8745 commit ed1dfdf
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 43 deletions.
17 changes: 7 additions & 10 deletions src/Setting/Infrastructure/ShopSettingRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,17 @@ private function matchesFloatDigits(string $value): bool
return (bool)preg_match("/^\d+(\.\d+)?$/", $value);
}

protected function getShopSetting(string $name): ShopConfigurationSetting
public function getBoolean(string $name): bool
{
return $this->configurationSettingDao->get($name, $this->basicContext->getCurrentShopId());
$setting = $this->getShopSetting($name);
$this->checkSettingType($setting, FieldType::BOOLEAN);

return (bool)$setting->getValue();
}

public function getBoolean(ID $name): bool
protected function getShopSetting(string $name): ShopConfigurationSetting
{
try {
$value = $this->getSettingValue($name, FieldType::BOOLEAN);
} catch (NotFound $e) {
$this->throwGetterNotFoundException('boolean');
}

return (bool)$value;
return $this->configurationSettingDao->get($name, $this->basicContext->getCurrentShopId());
}

public function getString(ID $name): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public function getInteger(string $name): int;

public function getFloat(string $name): float;

public function getBoolean(ID $name): bool;
public function getBoolean(string $name): bool;

public function getString(ID $name): string;

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 @@ -37,10 +37,10 @@ public function getFloatSetting(string $name): FloatSetting
return new FloatSetting(new ID($name), $float);
}

public function getBooleanSetting(ID $name): BooleanSetting
public function getBooleanSetting(string $name): BooleanSetting
{
$bool = $this->shopSettingRepository->getBoolean($name);
return new BooleanSetting($name, $bool);
return new BooleanSetting(new ID($name), $bool);
}

public function getStringSetting(ID $name): StringSetting
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 @@ -15,7 +15,7 @@ public function getIntegerSetting(string $name): IntegerSetting;

public function getFloatSetting(string $name): FloatSetting;

public function getBooleanSetting(ID $name): BooleanSetting;
public function getBooleanSetting(string $name): BooleanSetting;

public function getStringSetting(ID $name): StringSetting;

Expand Down
67 changes: 39 additions & 28 deletions tests/Unit/Infrastructure/ShopSettingRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use OxidEsales\EshopCommunity\Internal\Framework\Database\QueryBuilderFactoryInterface;
use OxidEsales\EshopCommunity\Internal\Transition\Utility\BasicContextInterface;
use OxidEsales\GraphQL\Base\Exception\NotFound;
use OxidEsales\GraphQL\ConfigurationAccess\Setting\Enum\FieldType;
use OxidEsales\GraphQL\ConfigurationAccess\Setting\Exception\WrongSettingTypeException;
use OxidEsales\GraphQL\ConfigurationAccess\Setting\Exception\WrongSettingValueException;
use OxidEsales\GraphQL\ConfigurationAccess\Setting\Infrastructure\ShopSettingRepository;
Expand All @@ -28,15 +29,13 @@ public function testGetShopSettingInteger($possibleValue, $expectedResult): void
$settingName = 'settingName';
$shopId = 3;

$shopSettingType = 'num';

$shopSettingDaoStub = $this->createMock(ShopConfigurationSettingDaoInterface::class);
$shopSettingDaoStub->method('get')
->with($settingName, $shopId)
->willReturn(
$this->createConfiguredMock(ShopConfigurationSetting::class, [
'getName' => $settingName,
'getType' => $shopSettingType,
'getType' => FieldType::NUMBER,
'getValue' => $possibleValue
])
);
Expand Down Expand Up @@ -70,7 +69,7 @@ public function testGetShopSettingFloat($possibleValue, $expectedResult): void
->willReturn(
$this->createConfiguredMock(ShopConfigurationSetting::class, [
'getName' => $settingName,
'getType' => 'num',
'getType' => FieldType::NUMBER,
'getValue' => $possibleValue
])
);
Expand Down Expand Up @@ -166,41 +165,53 @@ public function wrongSettingsDataProvider(): \Generator
'value' => 'any',
'expectedException' => WrongSettingValueException::class
];
}

public function testGetShopSettingBooleanNegativ(): void
{
$nameID = new ID('booleanSetting');


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

$boolean = $repository->getBoolean($nameID);

$this->assertEquals(false, $boolean);
yield [
'method' => 'getBoolean',
'type' => 'wrong',
'value' => 'any',
'expectedException' => WrongSettingTypeException::class
];
}

public function testGetShopSettingBooleanPositiv(): void
/** @dataProvider possibleBoolValuesDataProvider */
public function testGetShopSettingBoolean($possibleValue, $expectedResult): void
{
$nameID = new ID('booleanSetting');
$settingName = 'settingName';
$shopId = 3;

$shopSettingDaoStub = $this->createMock(ShopConfigurationSettingDaoInterface::class);
$shopSettingDaoStub->method('get')
->with($settingName, $shopId)
->willReturn(
$this->createConfiguredMock(ShopConfigurationSetting::class, [
'getName' => $settingName,
'getType' => FieldType::BOOLEAN,
'getValue' => $possibleValue
])
);

$repository = $this->getFetchOneShopSettingRepoInstance('1');
$basicContext = $this->createStub(BasicContextInterface::class);
$basicContext->method('getCurrentShopId')->willReturn($shopId);

$boolean = $repository->getBoolean($nameID);
$sut = $this->getSut(
basicContext: $basicContext,
shopSettingDao: $shopSettingDaoStub
);

$this->assertEquals(true, $boolean);
$this->assertSame($expectedResult, $sut->getBoolean($settingName));
}

public function testGetNoShopSettingBoolean(): void
public function possibleBoolValuesDataProvider(): \Generator
{
$nameID = new ID('NotExistingSetting');

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

$this->expectException(NotFound::class);
$this->expectExceptionMessage('The queried name couldn\'t be found as a boolean configuration');
$repository->getBoolean($nameID);
yield ['possibleValue' => 1, 'expectedResult' => true];
yield ['possibleValue' => '1', 'expectedResult' => true];
yield ['possibleValue' => true, 'expectedResult' => true];
yield ['possibleValue' => 'anything', 'expectedResult' => true];
yield ['possibleValue' => null, 'expectedResult' => false];
yield ['possibleValue' => 0, 'expectedResult' => false];
yield ['possibleValue' => '0', 'expectedResult' => false];
yield ['possibleValue' => false, 'expectedResult' => false];
}

public function testGetShopSettingString(): 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 @@ -68,7 +68,7 @@ public function testGetShopSettingBoolean(): void

$this->assertEquals(
new BooleanSetting($nameID, $repositoryResult),
$settingService->getBooleanSetting($nameID)
$settingService->getBooleanSetting((string)$nameID)
);
}

Expand Down

0 comments on commit ed1dfdf

Please sign in to comment.